19

In other words, is there a limit on the amount of storage a contract can hold?

Suppose a field is an Array for instance, and the contract has a method for adding items. Can I keep calling the contract so it adds more and more data?

J-B
  • 8,941
  • 16
  • 46
  • 77
Fernando Tiberti
  • 2,299
  • 1
  • 15
  • 28

2 Answers2

19

Yes, a contract has infinite internal storage.

But there are 2 limits to practicality: gas costs and the block gas limit.

Gas costs are discussed in: Is there anything stopping me from using the ethereum blockchain as data storage?

Furthermore, the block gas limit will limit how much computation, search, iteration, etc can be performed on the data.

eth
  • 85,679
  • 53
  • 285
  • 406
3

No, contracts' internal storage cannot increment forever. However, it is fairly more than enough for all types of purposes.

The limits for all storage variables together is:

2^256 Keys of 32 bytes = 2^261 bytes .

This is: 115,792,089,237,316,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000 of Keys 32 bytes.

That is equal to: 3,705,346,855,594,120,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000 bytes.

Witch is in terabytes: 3,705,346,855,594,120,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000 terabytes

Reference: Is there a (theoretical) limit for amount of data that a contract can store?

There are 2^256 different keys, and each key can store 32 bytes, so that's a total of 2^261 bytes that could be stored. That said, by then the Ethereum blockchain will probably break due to a hash collision

However, as @eth♦ suggest, you should also consider the gas cost and the block gas limits.

Moreover, if you are using a mapping, a collision may happen because the location of a value key is computed using the hash function sha3(key, p). Check here for more details: Is there a maximum number of entries for a mapping?. But again this collision is a concern when your data size is about 2^160 or 2^80 bytes. And this is a very large storage to consider.

Muhammad Altabba
  • 2,157
  • 1
  • 15
  • 31
  • 1
    2^261 Bytes is ≈ 3,710^78 Bytes, but the estimation for the total data volume stored on earth in year 2025 according to [this article on forbes.com](https://www.forbes.com/sites/tomcoughlin/2018/11/27/175-zettabytes-by-2025/#3513adfb5459) is "just" 175 Zettabytes = 1,7510^23. – user1364368 Dec 08 '18 at 13:38