30

In many contracts, notably voting, there is a mapping of the kind mapping(address => bool) voted;

How many voters can be registered in that way? is there a size limit? will the contract need more and more gas for each query until it reaches the maximum gas limit?

what would happen if there are 100 million voters for example?

2 Answers 2

23

The cost of an addition to or a read from a mapping does not change with the number of keys mapped. The location of a value key is computed by sha3(key, p) where p is the storage slot that acts as the pointer of the mapping. As you can see the location of an individual value is random, and you may theoretically overwrite data from another key.

The total storage is 2^256 * 32 bytes, so there is, in practice, quite some time before you indeed overwrite old data.

If you have 100 million voters, that's 100 million storage slots used. You have, I believe, 1 chance in 10^69 of overwriting someone's vote on the next vote.

4

According to Is there a (theoretical) limit for amount of data that a contract can store? the maximum a single contract can store is around 1.46 GB of data.

So 100 million votes could be quite close to that limit.

3
  • 8
    A single contract has access to 2^256 slots of 32 bytes each. That's 3.6 * 10^75 TB of data. Sep 13, 2016 at 16:46
  • 1
    @XavierLeprêtreB9lab Can you explain more. I see this number everywhere as the storage size of a smart contract. Isn't this just simplified theoretical representation. This cannot physically be true. Don't we have to modulo the hash by the real size of the storage? If that is the case what is the size? Jun 13, 2020 at 2:21
  • 1
    The storage is stored in a Merkle tree. All leaves and branches that have 0 for data are truncated out of the tree. So the tree is indeed much smaller than the theoretical limit. Each storage slot has an index. This index is 32 bytes long, so 256 bits. Hence the storage "space". Jun 15, 2020 at 15:42

Your Answer

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

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