1

I've created a simple smart contract to store large amounts of data (13-20kB) on the Ethereum blockchain. But when I'm trying to make a transaction they fail sometimes (with little correlation to the amount of data) with a "Warning! Error encountered during contract execution [Out of gas]" error

The gas limit is 3000000 and there is no clear data threshold that causes the transaction to fail. Two transactions with the same content can behave differently (one gives an error while the other doesn't).

Here's the code that I'm using to store the data:

contract SimpleStorage { 
    mapping(address => string) public Usersdata; 
    function Storedata(string data) public { 
        Usersdata[msg.sender] = data; 
    } 

    function getData() public view returns (string) { 
        return Usersdata[msg.sender]; 
    } 
}
  • First of all, I sure hope you aren't going to run this on mainnet! Secondly, 3 million gas is not enough to put that much data in storage. How are you storing the data? Are you just including it as tx data? – flygoing Dec 4 '18 at 14:51
  • It runs on the testnet right now but I plan to run it in the mainnet in the future. – Ron Berreby Dec 4 '18 at 15:09
  • How are you storing the data on-chain? Are you just including it as tx data or are you putting it in contract storage? – flygoing Dec 4 '18 at 15:10
  • When transactions are successful they use considerwbly less gas. And yes, the data is stored as tx data. – Ron Berreby Dec 4 '18 at 15:12
  • I can't really answer any questions without seeing the contract you're interacting with. – flygoing Dec 4 '18 at 15:17
1

Based on the contract you commented (and I edited to add to the original question), it's not surprising you run into gas problems. Contract storage costs 20k for initialization of a slot, and each slot stores 32 bytes. Storing 10 KB of data in storage would cost approximately 10000/32*20000 gas, which is over 6 million gas. This is also ignoring the cost of the data as input data, which isn't cheap (although it is much cheaper than the storage costs) . Overall, storing large sets of data like this on-chain is considered bad practice.

  • I did a transaction with a payload of 10kB and it used ~7000000 gas. I set the gas price to be 1 gwei. Which only costs $0.78. Will the price be the same if I deploy it to the mainnet? Do you know of a reasonable option to on-chain storage? – Ron Berreby Dec 5 '18 at 7:31
  • There is no cheap way to store that much data on-chain. It's just unrealistic to want to add that much data to storage. If you don't need to operate on the data within your contract and it's just for storage, then you could store the data on IPFS and put the IPFS hash on-chain. – flygoing Dec 5 '18 at 13:44
  • @flygoing , say if "Usersdata" already has 1000 entries; EVM has to read all data in memory; add new entry and persist new version. In that case; does cost include only the size of new entry or size of existing entries as well? – Shamit Verma Dec 5 '18 at 15:05
  • Make sure you don't mix-up memory and storage, they're two very different things. The EVM doesn't have to read all data. In the contract you used as an example, you would only have to pay the price of storing the new data, nothing else, but of course that's extremely expensive. – flygoing Dec 5 '18 at 15:22

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.