18

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!

Chaos
  • 377
  • 1
  • 2
  • 10

2 Answers2

11

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.

Lauri Peltonen
  • 29,391
  • 3
  • 20
  • 57
  • 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 '19 at 08: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 '19 at 10:59
  • @LauriPeltonen what's the meaning/diff. between "reading from our own node" and accessing the data for processing purposes? – sqlchild Jun 12 '22 at 00:43
  • Sorry, that's a bit badly worded. By the latter I meant if you call the read-only function as part of a real transaction, for example from another non-read-only function. – Lauri Peltonen Jun 12 '22 at 04:57
  • @LauriPeltonen - thanks a lot for your help, just a little doubt, reading data is free means if we make a todo list inside a smart contract database and only read the tasks ( using ethereumjs or web3.js ) saved in that smart contract, then it is free? means anyone can use this and manage their todo tasks for free, only executing the contract will be charged whever the contract is hit i.e. a new todo task being inserted – sqlchild Jun 12 '22 at 13:42
  • @LauriPeltonen Also why do you refer "our own node", when the contract can be run on any PC and by anyone in the world. – sqlchild Jun 12 '22 at 13:47
  • You always need a node to connect to, which has the blockchain data. So if the node is synchronized up-to-date, it will have the latest contract data and including your contract for tasks. Only writing to it will cost. Please post a new question or search among existing questions if you need further help. – Lauri Peltonen Jun 12 '22 at 14:30
7

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 :-)

Richard Horrocks
  • 37,835
  • 13
  • 87
  • 144
  • 1
    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 '19 at 08:44