dfinity / motoko

Simple high-level language for writing Internet Computer canisters
Apache License 2.0
517 stars 97 forks source link

Motoko interpreter determines caller incorrectly #4485

Open reginleif888 opened 8 months ago

reginleif888 commented 8 months ago

Problem statement: The interpreter determines the caller incorrectly if the call is made not from the actor directly, but inside the object that the actor uses. In a call made from an object, the caller is always ID:0

Command used: moc -r

Motoko version: 0.11.1

Code:

import Debug "mo:base/Debug";
import Principal "mo:base/Principal";
import Text "mo:base/Text";

actor exampleActor {
  public shared (msg) func printCaller() : async () {
    Debug.print("caller: " # debug_show Text.decodeUtf8(Principal.toBlob(msg.caller)));
  };
};

class ExampleClass() {
  public func exampleMethod() : async* () {
    await exampleActor.printCaller();
  };
};

actor agent = {
  public func test() : async () {
    let example : ExampleClass = ExampleClass();

    let agentPrincipal = Principal.fromActor(agent);

    Debug.print("agent: " # debug_show Text.decodeUtf8(Principal.toBlob(agentPrincipal)));

    await exampleActor.printCaller();

    await* example.exampleMethod();
  };
};

await agent.test();

Actual output:

agent: ?"ID:2"
caller: ?"ID:2"
caller: ?"ID:0"

Expected output:

agent: ?"ID:2"
caller: ?"ID:2"
caller: ?"ID:2"
timohanke commented 8 months ago

If we move the definition of ExampleClass inside the agent actor then it works (not surprising).

Would be nice to get this fixed in the interpreter.

crusso commented 7 months ago

Yeah, that looks like a bug. Thanks for reporting it! I would regard this as low priority, since the IC doesn't support multiple actors in the same canister.

How important is the fix to you?

reginleif888 commented 7 months ago

It's low priority. Thanks!