Why doesn't this work?

contract NameRegistry {

  function NameRegistry() {
    // names["users"]["stefreak"] = 0x123;

  function register(address record, string namespace, string name) returns (bool success) {
    if (names[namespace][name] > 0) {
      return false;

    names[namespace][name] = record;
    return true;

  function lookup(string namespace, string name) returns (address record) {
    return names[namespace][name];

  // namespace => name => address
  mapping(string => mapping(string => address)) names;

When uncommenting the line in the constructor, lookup works. When using register with the same parameters (0x123, "users", "stefreak") it does not work. I don't see why.

Here are my tests:

contract('NameRegistry', function(accounts) {
  it("can be used to register names.", function(done) {
    var registry = NameRegistry.deployed();

    registry.register.call(accounts[0], "users", "stefreak").then(function(registered) {
      assert.equal(registered.valueOf(), true, "register name was not successfull")

      return registry.lookup.call("users", "stefreak")
      assert.equal(address.valueOf(), accounts[0], "lookup did not give back the registered address")

The code inside the constructor is executed during contract creation, so the test passes because when you make a call later the values are already there.

If you don't set those parameters during creation phase, the test will fail because you are using a call method to register and not a sendTransaction. Transactions generated by call methods are never mint, so the blockchain state does not change. And the test will fail in any case because you need to wait for the block to be mint to run the test, you cannot do it just after the sendTransaction command.

I don't know what testing framework you are using, but I suggest to insert a "beforeTest" phase where you do initialization, and the test all functions.

Side note: use constant return for the lookup, like this:

function lookup(string namespace, string name) constant returns (address record) { return names[namespace][name]; }

It has not actually sense to use an online transaction for that.

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.