0

I was trying the code in https://gist.github.com/sogoiii/f0ced0a4e569b5f38d302e7072d78b43 to observe the difference between call and delegatecall.

pragma solidity ^0.4.18;
contract D {
  uint public n;
  address public sender;

  function callSetN(address _e, uint _n) public {
    _e.call(bytes4(keccak256("setN(uint256)")), _n); // E's storage is set, D is not modified 
  }

  function callcodeSetN(address _e, uint _n) public {
    _e.callcode(bytes4(keccak256("setN(uint256)")), _n); // D's storage is set, E is not modified 
  }

  function delegatecallSetN(address _e, uint _n) public {
    _e.delegatecall(bytes4(keccak256("setN(uint256)")), _n); // D's storage is set, E is not modified 
  }
}

contract E {
  uint public n;
  address public sender;

  function setN(uint _n) public {
    n = _n;
    sender = msg.sender;
    // msg.sender is D if invoked by D's callcodeSetN. None of E's storage is updated
    // msg.sender is C if invoked by C.foo(). None of E's storage is updated

    // the value of "this" is D, when invoked by either D's callcodeSetN or C.foo()
  }
}

I deployed both contracts on Remix and called D.delegatecallSetN with the address of the E and an arbitrary value. Based on the comments, D.n should be modified. However, that was not the case.

Am I understanding something wrongly here?

  • 1
    When I tested this in Remix, everything worked as expected. D's state variables n and sender were modified, and E's were not. – smarx Apr 4 at 5:43
  • @smarx have I passed in anything wrong here? snag.gy/khRWZV.jpg – daniellimws Apr 4 at 5:53
  • 1
    That looks pretty much identical to what I'm doing. I'm using compiler 0.4.25+commit.59dbf8f1.Emscripten.clang with optimizations turned off, and the JavaScript VM for the environment. – smarx Apr 4 at 5:56
  • It worked! This is so confusing... But thanks for the help! – daniellimws Apr 4 at 6:00

Your Answer

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

Browse other questions tagged or ask your own question.