0

The following contract fails (I believe) because when Wordbase.set is called, it creates a new Node in storage, rather than passing the old Node. Thus, the value updates on the copy instead of the original.

How can I get a contract to pass a pointer instead of creating a copy?

contract Wordbase {

  struct Node {
    bytes32 value;
    mapping(bytes32 => Node) nodes;
  }

  mapping(address => Node) realms;

  function getNode(address addr , bytes32[] fields) internal returns (Node) {
    Node node = realms[addr];
    for (uint i = 0; i < fields.length; i ++) {
      node = node.nodes[fields[i]];
    }
    return node;
  }

  function get(address addr, bytes32[] fields) constant returns (bytes32) {
    return getNode(addr, fields).value;
  }

  function get(bytes32[] fields) constant returns (bytes32) {
    return get(msg.sender, fields);
  }

  function set(bytes32[] fields, bytes32 value) {
    getNode(msg.sender, fields).value = value;
  }

}
1

All you need to do is set the return type of getNode to storage.

i.e.

function getNode(address addr , bytes32[] fields) internal returns (Node storage) {
    Node node = realms[addr];
    for (uint i = 0; i < fields.length; i ++) {
      node = node.nodes[fields[i]];
    }
    return node;
}

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.