51

Is there a theoretical limit to the amount of data that one contract can store while running in a private net in which gas is not a concern?

Context: In a financial Dapp which is to replace global payment systems, tx/s throughput is certainly one limiting factor that is widely discussed. But that is not what I am getting at here. Can a contract store 1GB, 1TB, ... data in a contract? The data would not be written in one go, but rather accumulate over time.

Example: Let's assume a best case scenario in which we manage to squeeze a single transaction of tokens living on top of a smart contract into 100 bytes. At 1000 tps throughput this would yield 100*1000*3600*24*31*12 = 3.2TB/year. Feel free to guess if / when this will be possible.

2
  • 2
    Are you 100% sure you need to store all these records for all eternity? If this is about communicating things between parties using Whisper messages might be better. An other option would be to save a 32 byte hash which points to the real data in Swarm or IPFS etc.
    – Maran
    Feb 5, 2016 at 12:46
  • Good point, I am quite sure I do not need them for all eternity, just trying to understand the fundamentals. I am sure that I want all those records initially but it would be great to be able to move them to IPFS after N blocks - e.g. after a typical auditing period of one year. Seems tough to implement that in a contract without centralization right now (move out of blockchain onto IPFS and make sure the right stuff is now in IPFS).
    – SCBuergel
    Feb 8, 2016 at 9:26

2 Answers 2

60

Contract storage is a key of 32 bytes and a value of 32 bytes, so the maximum a single contract can store is around 1.46 GB (32^32).

False. 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....

6
  • 1
    Thanks, I updated and will fix my comment here ethereum.stackexchange.com/questions/986/…
    – eth
    Feb 5, 2016 at 13:11
  • 1
    What do you mean break, will the whole blockchain fail? Are there no checks for duplicate hashes? Will the transaction have to be rewinded/redone until no collision occurs? Jul 25, 2016 at 11:00
  • 2
    @BlockChange If duplicate hashes are discovered, there are much bigger problems because that would mean the hashing algorithm has been broken. For reference, for @2^261, assuming you can compute 1 hash per picosecond, you'd still need more time than we have until the heat death of the universe to find a conflict like that, unless the hashing algorithm is broken
    – Earlz
    Dec 13, 2016 at 15:37
  • 4
    2^261 = #bytes that can be stored with 2^256 addresses, each storing 2^5 (=32) bytes. It has nothing to do with the hashing algorithm. The hashing algorithm used is Keccak 256-bit hash, giving 2^256 possible hash values, but you would not have to wait until the last one, for a collision to happen. Recall the Birthday Problem (a.k.a Birthday Paradox). Probability of collision is > 99% after 2^130 values. With 2^160 of those values as addresses of 2^160 contracts, P(collision) > 99.999999.... % (not sure how many 9's after the decimal point) Jan 10, 2017 at 0:11
  • 2
    @Earlz - The point is that there are about 2^35 seconds in a millennium (1000 years). So at the rate of creating 1 contract per second, it would take 2^125 millennia to create 2^160 contracts. Getting collisions long before then does NOT imply that the hashing algorithm is "broken". Jan 10, 2017 at 0:18
7

Contract storage is a key of 32 bytes and a value of 32 bytes, so the maximum a single contract can store is around 2^261 bytes (2^256 * 32b).

In a private chain where gas is not a concern, since the address space is 160 bits, assuming it can all be used, 2^160 contracts can be created. So in theory around 2^421 bytes is the maximum that contracts can store.

4
  • 9
    You'd run into hash collisions long before you created 2^160 contracts; 2^80 is probably a more reasonable estimate. Apr 14, 2016 at 14:22
  • 1
    Why would there be hash collision after you created 2^80 contracts? @Nick Johnson
    – alper
    Dec 23, 2017 at 18:43
  • 1
    Can a key's value be a struct, which might be larger than 32 bytes? @eth♦
    – alper
    Dec 23, 2017 at 18:51
  • 2
    @Alper The keys of contract storage can only be 32 bytes.
    – eth
    Dec 30, 2017 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.