1

I'm pretty new to Solidity, and I was writing a contract to play around with dynamic arrays. My array length grows, but it seems to stop at length 8.

Here's the contract I wrote:

pragma solidity 0.4.19;


contract BigData { 
  bytes public doubleBag = "1";

  function doubleStorage() public { 
    uint256 index = 0;
    uint256 addThisMany = doubleBag.length;
    while (index < addThisMany) { 
      doubleBag.push("1");
      index += 1;
    } 
  } 

  function getLength() public view returns (uint256) { 
    return doubleBag.length;
  } 
}

And the truffle test with web3@^0.20.6:

var BigData = artifacts.require('./BigData.sol')
const BigNumber = require('bignumber.js')

contract('BigData', function(accounts) {
  it('Should grow storage exponentially', async function() {
    let contract = await BigData.deployed()
    for (let i = 1; i <= 3; i++) {
      let tx = await contract.doubleStorage()
      let size = await contract.getLength()
      let expected = i * 2 
      assert.equal(size.toNumber(), expected)
    }   
  })  
})
Breedly
  • 111
  • 4
  • When trying this code in Remix with the JavaScript VM, I successfully got to a length of 256 before I stopped. How are you deploying/calling this code? Perhaps you're not supplying enough gas when calling doubleStorage? – user19510 Mar 15 '18 at 21:26
  • No gas is specified, perhaps it is throwing an out of gas exception? – Ismael Mar 15 '18 at 23:06

1 Answers1

0

It's the Gas! Everyone was right.

I needed to update the gas limit in my web3js call:

  contract.methods.squareStorage().send({
    from: acct,
    gas: 6721975
  }) 

Looking at Ganache's panels I can see that the "Gas Limit" or maximum gas you can spend per "block" is 6721975 by default. Web3js sets a much lower limit, not sure exactly what it is, but when I up the limit I can count to higher numbers, but not very high.

I think if I wanted to make doubleBag really big to see how large I can make it I would have to adjust the size by a constant factor each transaction. If I increase the rate at which the bag grows each time I can very easily pass the gas limit per block.

I'm not sure why I did not see any errors thrown.

Breedly
  • 111
  • 4