4

TL;DR: Second's msg.sender isn't being set when it should.

Consider these two contracts:

First.sol:

import "Second.sol";

contract First {
  address public second;

  function First() {

  }

  function createSecond() {
    second = address(new Second());
  }
}

Second.sol:

contract Second {
  address public sender;

  function Second() {
    sender = msg.sender;
  }
}

When I send a transaction to First's createSecond(), then get the Second's address and try to get the sender of Second, I get 0x0 returned instead of an address.

Perhaps this is best shown in a test. Here, I'm using Truffle to run the test, and the First and Second objects are provided by Truffle through ether-pudding.

contract('First -', function(accounts) {
  it("Second should have an sender on creation", function(done) {
    var first, second;
    console.log("Creating first...");
    First.new().then(function(instance) {
      first = instance;

      console.log("Creating second...");
      return first.createSecond();
    }).then(function(tx) {
      console.log("Getting Second's address...");
      return first.second.call();
    }).then(function(second_address) {
      console.log(second_address);
      second = Second.at(second_address);
      console.log("Getting Second's sender...");
      return second.sender.call();
    }).then(function(sender) {
      console.log("SENDER ADDRESS:", sender);
      assert.notEqual(sender, "0x0");
    }).then(done).catch(done);
  });
});

Which produces this output:

Using environment test.
Compiling contracts...


  Contract: First
    Creating first...
    Creating second...
    Getting Second's address...
    0xbf12bb70b9bb9e1b58984a4ba5331a118d622d86
    Getting Second's sender...
    SENDER ADDRESS: 0x0
    1) Second should have an admin on creation.
    > No events were emitted


  0 passing (2s)
  1 failing

  1) Contract: First - Second should have an sender on creation:
     AssertionError: expected '0x0' to not equal '0x0'

So as you can see, Second's msg.sender isn't being set when it should. Anyone know why?

My environment:

  • OS X El Capitan
  • solc 0.2.0-1 via npm
  • Confirmed with ethereumjs-testrpc and Geth/v1.3.2-54904379/darwin/go1.5.1
3

Alright. It looks like it was a very subtle bug in an edited version ether-pudding, causing this code Second.at(second_address); to be given the wrong address. In debugging I had mistakenly attributed the error the wrong cause (i.e., solc 0.2.0-1). Closing the question.

  • 2
    Don't forget to accept an answer if your satisfied with the results. Click the tick. – Waqar Lim Jan 29 '16 at 7:19
2

This might be related to a recent optimizer bug (https://github.com/ethereum/solidity/issues/333) that was fixed.

To get the latest solc in Truffle. Download: https://github.com/chriseth/browser-solidity/blob/gh-pages/bin/soljson-latest.js. And stick into /node_modules/solc/bin.

I ran it with solc at 20f7458 and it worked fine.

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.