4

I'm trying to store a dataset into a mapped struct. After adding more than 3 variables, seems type unspecific, the dataset is not stored anymore. I don't get any errors. Does this has to do something with memory? I'm using embark in a local testchain, so gas seems not to be the problem. Here the sample code:

pragma solidity ^0.4.2;
contract test {

  struct Tree {
        int value1;
        int value2;
        int value3;
        int value4;
    }

  mapping(int => Tree) public dataItems;

  function addDataItem(int _value0,int _value1,int _value2,int _value3,int _value4) external {
        dataItems[_value0].value1= _value1;
        dataItems[_value0].value2= _value2;
        dataItems[_value0].value3= _value3;
        dataItems[_value0].value4= _value4;
    }

    function getvalue1(int _number) constant returns (int) {
        return dataItems[_number].value1;
    }
    }

    

When value4 is removed everything seems to work. Is there a right / other way to do this?


solc --version
solc, the solidity compiler commandline interface
Version: 0.4.4+commit.4633f3de.Darwin.appleclang
Geth
Version: 1.5.3-stable
Git Commit: 978737f5d51ee4e60c00afb0c4ac8784b2faa059
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.7.3
OS: darwin
GOPATH=
GOROOT=/usr/local/Cellar/go/1.7.3/libexec

    

Edit: This is a running out of gas problem as suggested by @BokkyPooBah. Adding additional gas in embark can be done like this:


test.addDataItem(1,1,1,1,1,{gas:150000})

3
  • I didn't understand your problem i've tried your code and it works perfect
    – Badr Bellaj
    Dec 18, 2016 at 23:51
  • That code looks OK - maybe try writing a test for it and if it still doesn't work, try posting the code along with the failing test? Dec 19, 2016 at 0:34
  • Works fine for me. ethereum.github.io/browser-solidity/…
    – Jamie Hale
    Dec 19, 2016 at 1:31

1 Answer 1

2

The reason why your contract execution fails after the fourth variable is added is because you have run out of gas. If you do not specify the gas amount when sending a transaction to a contract, the default gas is 90,000.

From my testing of your code in Browser Solidity, you will need to specify a gas amount greater (or equal) than 102,944 as you can see in the screenshot below:

enter image description here

And you can see that the getvalue1(...) method returned a value of 2.

1
  • 1
    This solved the problem, thanks! In Embark this seems to be possible with: test.addDataItem(1,1,1,1,1,{gas:103000}) Dec 19, 2016 at 10:18

Your Answer

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

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