3

I have the following method on a contract:

function addNode(address _address) constant returns (address) {
    nodes[_address] = Node(_address, balanceOf(_address), 0);
    return _address;
}

Here's the unit test calling it:

it("should add a node", function() {
    return LinkPool.deployed().then(function (instance) {
        meta = instance;
        return meta.addNode.call(nodeAddress, { from: account }).valueOf();
    }).then(function (address) {
        console.log(address);
        node = address;
        assert.equal(nodeAddress, address, "Address of node and the initial node address should be equal");
        return meta.balanceOf.call(node).valueOf();
    }).then(function (balance) {
        assert.equal(balance, 1, "Balance of node should be 0 ETH");
        return meta.getAmountStaked.call(node);
    }).then(function (amountStaked) {
        assert.equal(amountStaked, 0, "Amount staked on node should be 0 ETH");
        return true;
    });
});

Every time I use this method, with checking the parameter address is set, I get the following assertion error:

AssertionError: Address of node and the initial node address should be equal: expected '0x9a849eac304a334ef13682d51306fe3b715ea2ac' to equal '0x'

I've changed things around as many times as I can trying little nuances but cannot get any return value from the contract.

I feel like this is pretty basic, but I've tried as many different structures as possible and been Googling hard.

1 Answer 1

1

You are declaring addNode as constant, but you want it to modify the contract storage to add _address to nodes mapping. If you want it to be able to modify the storage you have to remove constant and execute the function within a transaction instead of a call.

function addNode(address _address) returns (address) {
    nodes[_address] = Node(_address, balanceOf(_address), 0);
    return _address;
}

Use meta.addNode() instead of meta.addNode.call() to create a transaction that can modify the storage. It will return a promise, so you cannot call .valueOf() immediately, but only after the promise has been resolved.

it("should add a node", function() {
    return LinkPool.deployed().then(function (instance) {
        meta = instance;
        return meta.addNode(nodeAddress, { from: account });
    }).then(function (address) {
        console.log(address.valueOf());
        // ... Rest of the test
    });
});
1
  • 1
    I've just set-up my work laptop this morning to amend on this to reply back to you, and the code I created the question worked first time. The only thing I can think of is that the differences between my work laptop and my own is that I was using Ubuntu sub-system in W10 with Ubuntu geth installed, whereas my work laptop I'm using cygwin with the Windows version of geth. I'm going to install the Windows version of Geth and see if that fixes it.
    – Jonny
    Commented Oct 30, 2017 at 9:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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