contract-development – How Much Data Can Be Stored in a Smart Contract and What Are the Costs?

contract-developmentcontract-invocationgasmemorypayments

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!

Best Answer

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.

Related Topic