2

I have 2 contracts

pragma solidity ^0.4.24;

contract C1 {

    event Sig(bytes4 sig);
    C2 c2;

    constructor(address c2Addr)public {
        c2 = C2(c2Addr);
    }

    function func1()public {
        bytes4 sigFromC2Func2 = c2.func2();
        emit Sig(sigFromC2Func2);
        bytes4 sigFromThisFunc2 = func2();
        emit Sig(sigFromThisFunc2);
    }

    function func2() pure private returns(bytes4) {
        return msg.sig;
    }
}

contract C2 {

    function func2() pure public returns(bytes4) {
        return msg.sig;
    }
}

Deployed C2 than C1 on remix and executed func1 of C1, got below result.

  • When calling from func1 --> C2.func2() the msg.sig is 0xb1ade4db
  • When calling from func1 --> this.func2() the msg.sig is 0x74135154

According to the docs:

msg.sig (bytes4): first four bytes of the calldata (i.e. function identifier)

It is the same called function (func2), Why the signatures are different?

[
    {
        "from": "0xd25ed029c093e56bc8911a07c46545000cbf37c6",
        "topic": "0xcb04e53cd65d1928a26cbb70d9f1dae5ab09a5610fbf7fe897129c102325099f",
        "event": "Sig",
        "args": {
            "0": "0xb1ade4db",
            "sig": "0xb1ade4db",
            "length": 1
        }
    },
    {
        "from": "0xd25ed029c093e56bc8911a07c46545000cbf37c6",
        "topic": "0xcb04e53cd65d1928a26cbb70d9f1dae5ab09a5610fbf7fe897129c102325099f",
        "event": "Sig",
        "args": {
            "0": "0x74135154",
            "sig": "0x74135154",
            "length": 1
        }
    }
]
4

This is because of the way Solidity does internal calls. Internal function calls (in your example, bytes4 sigFromThisFunc2 = func2();) don't create a message call, they just jump to the appropriate section in the bytecode within the current call frame. This means that the function signature you're getting for sigFromThisFunc2 is actually the function signature of func1()

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.