EIP-2027 Recording the net change in a contract's storage slot count (State Rent C - Net contract size accounting)

No call decision · Scaling & data · EIP status Stagnant · created 2019-05-14

A proposal to add a new field called storagesize to each contract, recording only the net increase or decrease each time a storage slot is filled or emptied via SSTORE.

Why it is needed

Ethereum currently does not track at all how many storage slots each contract actually uses. There is no way to compute this number in constant time (O(1)) just by looking at the state data already received. Without this figure, two things are blocked. One is adjusting the gas cost of operations like SLOAD and SSTORE to match the bandwidth consumed by block proofs, and the other is snapshot sync protocols like fast sync verifying contract storage size via Merkle proofs when receiving it. This EIP is one stage of the State Rent roadmap, first recording only the net change in slot count to lay the groundwork for a later stage that counts the total number of slots precisely.

What changes

Today

After EIP-2027

Who is affected

In depth

Scope of application for the storagesize field

Every account whose codeHash is not equal to the hash of empty code (0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470), that is, contract accounts, gets a new uint64 field called storagesize. This field is updated after block C according to the changed behavior of the SSTORE operation.

Change in SSTORE behavior after block C

From block C onward, when SSTORE(location, value) is executed, if the previous value at location was 0 and the new value is non-zero, storagesize is incremented; if the previous value was non-zero and the new value is 0, storagesize is decremented. Cases where both values are 0 or both are non-zero have no effect on storagesize.

The meaning of increment

If the storagesize field does not yet exist, storagesize becomes HUGE_NUMBER + 1. If the field already exists, 1 is added to the existing value.

The meaning of decrement

If the storagesize field does not yet exist, storagesize becomes HUGE_NUMBER - 1. If the field already exists, 1 is subtracted from the existing value.

Why HUGE_NUMBER is set to 2^63

HUGE_NUMBER must be large enough that no real metric, such as the actual number of a contract's storage slots, the number of accounts, or the total amount of code and storage, could ever reach it, while still being small enough to fit in a 64-bit unsigned integer. The currently proposed value is 2^63; setting it this way makes it possible to later determine whether storagesize has ever been incremented or decremented at all (whether the field exists), and whether it has transitioned from the net method to the gross method (whether the value is less than HUGE_NUMBER/2). This assumes that at the time of block C, no contract can exceed 2^62.

How reverts are handled

Like other state changes, storagesize changes must also be canceled together when an execution frame reverts. The same techniques used for storage value changes, namely Geth's journalling or Parity's substates, must be applied here as well.

An estimation approach that was considered but not adopted

The author previously proposed an algorithm for estimating contract storage size, but it had the drawback of adding too much complexity to consensus logic due to the many edge cases that had to be handled depending on storage size. This EIP instead chose a simple increment/decrement counter approach.

Backward compatibility and current status

This change is a non-backward-compatible change that can only be activated via a hard fork, but since the storagesize value itself is not observable from a contract, it does not affect the behavior of existing smart contracts. It is stated that test cases will be created based on the reference implementation, but the status of this EIP is Stagnant, and since it is bundled with other sub-proposals of the State Rent roadmap, the specific timing or fork inclusion of Block C is not specified in the original text.

Original EIP · ← EIPs