2

If I suppose I use a smart contract as a database, how much data can I store? speaking only of strings and integers, save them in contract data structures does it cost in addition to the gas used for the variable store?

The contract size must be a maximum of 24576 bytes, as also written in the yellow paper, but that refers to the weight of the contract code; the memory is represented as an array of 2 ^ 256 32 bytes slots, but can I really use all this memory?

If I have a contract with data structures that contain thousands of elements, when I update a variable and then change the status of the contract, how is the updating of this contract implemented on all nodes?

Making calls on a contract with lots of data saved in it has a higher cost?

Sorry for the many questions, I know that this type of operations are expensive, but I would like to understand how the smart contract memory is managed.

Thanks!

1

Gas is the only cost you have. There is no explicit "keep in storage" cost as such so if you pay X amount of gas when you save something in a smart contract then you never have to pay for that storage again. Except of course when you save something new or utilize the data.

As for the maximum amount of data a contract can store you can check Is there a (theoretical) limit for amount of data that a contract can store? . So in theory you can store 2^261 bytes but in practice you can never get anywhere near that limit. As Vitalik points on in his post in that link the blockchain will cease to function before you reach that hard limit ;)

Any updates to contract state are performed by regular transactions. Therefore all the data is propagated normally to all nodes. Whenever your transactions in included in a mined block it becomes part of the blockchain and information about this new block is propagated through the network. Of course it's not certain that the block will stay in existence due to consensus issues but mostly it will stay like that.

Accessing (only reading) data in any contract is free since you only read the information from your own node and the operation is not broadcasted anywhere. But if you want to access the data in a smart contract for processing purposes you have to pay for the access (and of course processing). This cost depends largely on the type of storage and the way of accessing the data so it's impossible to give any numbers here. But more data doesn't necessarily mean higher gas costs - for example reading a mapping is always the same amount of gas no matter how many entries there are.

  • Thanks for the answer, I ask you just another thing, after a variable is saved and then the contract status changes, how is the blockchain updated? Is the entire modified data structure recopied or just a pointer? Or is a new instance created on all nodes? – Chaos Mar 24 at 8:51
  • 1
    At least full nodes replay the transactions on their version of the contract and make sure the end result is what it's supposed to be. So they all perform exactly the same calculations and operations. Different non-full nodes have various "shortcuts" they take but those depend on the actual implementation. – Lauri Peltonen Mar 24 at 10:59
1

Just to add to Lauri's answer.

the memory is represented as an array of 2 ^ 256 32 bytes slots, but can I really use all this memory?

If you pay for it, yes. Note that this equates to 3.705 x 1066 TB...

It costs 20,000 gas to store a 32-byte word in a storage slot that hasn't been used before. (To update costs 5,000.)

At current gas prices of 2 Gwei:

=> 40,000 Gwei per 32-byte word

To fill a contract's entire storage allocation:

=> 2256 * 40,000 = 4.63 x 1081 GWei => 4.63 x 1072 ETH

At the current dollar-cost of ETH (~$138):

=> 4.63 x 1072 * 138 => $6.39 x 1074

Google tells me that the combined wealth of everyone in the world is ~$250 Trillion => $2.5 x 1014

That's a factor of 1060 difference. (Probably. If my maths isn't screwy.)

So:

If I suppose I use a smart contract as a database, how much data can I store?

...You'd probably be better off using a database, depending on what you're doing :-)

  • Thanks a lot for the answer, obviously I didn't think of using 3.705 x 10^66 TB, but it was an example. :) – Chaos Mar 24 at 8:44

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.