0

The following code snippet does not work.

pragma solidity ^0.4.11;

contract Test1 {
    uint[] public a;
    uint[] public b;

    function putX(uint x) public {
        a.push(x);
        b.push(block.number);
    }

    function getA() public view returns (uint[]) {
        return a;
    }

    function getB() public view returns (uint[]) {
        return b;
    }
}

Output:

> test1.putX.sendTransaction(11, {from: eth.accounts[0]})
> test1.getA()
[]
> test1.getB()
[]

The following code snippet, in contrast, works.

pragma solidity ^0.4.11;

contract Test2 {
    uint[2][] public a;

    function putX(uint x) public {
        a.push([x, block.number]);
    }

    function getA() public view returns (uint[2][]) {
        return a;
    }
}

Output:

> test2.putX.sendTransaction(12, {from: eth.accounts[0]})
> test2.getA()
[[12, 1151]]

I'm wondering why... I'm using the latest stable geth (1.7)

I also tried to use a dynamically-sized array + a mapping, and it does not work, either.

I even doubt if the code in the official document can work, since it uses multiple dynamically-sized variables:

https://solidity.readthedocs.io/en/develop/solidity-by-example.html

Does anyone know why this happened?

  • It does work, you can return a dynamic sized array. I've tried your example against remix.ethereum.org and it work as expected. Are sure your putX transaction was mined before calling getA? Are you using web3 v1.0 or v0.20, or truffle? Some of them return a promiseand you have to await form them before calling the getter. – Ismael Nov 30 '17 at 3:36
  • It works in Remix, yes, I have also tried it. But it does not work with the latest stable version of geth (1.7) (I've built a private network on one machine with one miner, the first does not work, the second works.) – hebothu Nov 30 '17 at 13:05
0

I've tried your code with both remix, geth v1.7.2 and v1.7.3 and it both it work as expected.

Perhaps your problem you are making the query before the transaction is mined. That will cause the contract to return the previous state.

  • Really? I kept querying for more than 2 hours! And it still had nothing there! That's strange... why it works in your environment... emmm... – hebothu Dec 4 '17 at 2:17
  • It is a pretty basic functionality, that why I'm curious why it doesn't work. Are sure the transaction has been mined, and it run successfully? Perhaps it runs out of gas. Returning dynamic sized arrays should be fine, you can't return nested dynamic arrays, ie an array of strings will fail, but an array of int or uint should be fine. – Ismael Dec 4 '17 at 15:40
  • I see what happened. You are right. After I changed: "test1.putX.sendTransaction(11, {from: eth.accounts[0]})" to "test1.putX.sendTransaction(11, {from: eth.accounts[0], gas: 200000000})", it worked! The default amount of gas was not enough! I didn't realize that test1.putX() consumes so much gas! But test2.putX() works, so it does not consume less gas than test1.putX()... Hmm... – hebothu Dec 5 '17 at 3:13
  • Is there any log file or something similar, from which I can learn the status of the transactions I have submitted? – hebothu Dec 5 '17 at 3:17
  • Here is the artifact produced by truffle compiled with Solidity v0.4.18 (solc-js) gist.github.com/anonymous/fdc0ef75669ab57e40a84faf1bea9a03. You can examine the execution of a transaction from geth with debug.traceTransaction(txhash), from byzantinum eth.getTransactionReceipt(hash) has a 'status' field that indicates the transaction was executed correctly or reversed. – Ismael Dec 5 '17 at 5:04

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.