EIP-1087 Improving net-gas accounting for SSTORE (Net gas metering for SSTORE operations)
No call decision · Fees & gas · EIP status Stagnant · created 2018-05-17
A proposal to recompute storage-write (SSTORE) gas based on the final state at the end of the transaction, so that if a value is changed multiple times but ends with no net change, almost no gas is charged.
Why it is needed
Currently, SSTORE charges 20000 gas for changing a slot from 0 to a nonzero value, and 5000 gas for any other change, and refunds 10000 gas at the end of the transaction if the slot is reset back to 0. This model is appropriate when a slot is changed only once, but for sequences that modify the same slot multiple times, it charges excessive cost regardless of the actual amount of disk work performed. Changing a slot from 0 to 1 and back to 0 still costs 15000 gas, and incrementing the same slot 5 times costs 45000 gas, even though in practice this is no different from a single disk write. Because of this, useful patterns that end with no net change in storage, such as reentrancy-guard mutexes or approveAndCall, which bundles approval and the call into one transaction, are impractical to use due to cost.
What changes
Today
- Changing a slot 0 to 1 and back to 0 costs 15000 gas even though there is no disk write
- Incrementing the same slot 5 times costs 45000 gas
- Moving a balance from account A to B to C costs 20000 gas
- Patterns with no net change, such as mutexes or approveAndCall, are impractical due to cost
After EIP-1087
- The same 0→1→0 sequence drops to 400 gas
- The 5-increment sequence drops to 21000 gas
- The A→B→C balance transfer drops to 10400 gas
- Storage patterns with no net change within a transaction can be used cheaply
Who is affected
- Smart contract developers: Patterns such as mutexes, passing context between calls, and approveAndCall can be designed without worrying about gas cost.
- Client and core developers: New transaction-level dirty-map tracking logic must be implemented in the EVM, and a hard fork is required.
- Wallet and dApp developers: Gas estimation logic depends on write order and refund timing, so the estimation approach must be redesigned.
- Users: Fees go down for transactions that touch storage multiple times but end with a small net change.
In depth
Current SSTORE charging structure
Currently SSTORE (0x55) charges 20000 gas for changing a slot from 0 to a nonzero value, and 5000 gas for any other change (nonzero to a different nonzero value, or nonzero to 0). Changing a slot from nonzero to 0 refunds 10000 gas, applied once at the end of the transaction. This model matches actual resource use when a slot is written only once, but charges excessive cost for sequences that modify the same slot repeatedly.
Introducing a dirty map
This proposal maintains a 'dirty map' per transaction that records every storage slot (across all contracts) whose value changed during that transaction. The dirty map follows the same scoping rules as storage updates, so if a call frame reverts, the dirty-map changes made in that frame are also undone.
New gas rules at write time
Writing the same value a slot already holds costs only 200 gas. If a slot's value changes for the first time in this transaction, the slot is registered in the dirty map, and 20000 gas is charged if the original value was 0, otherwise 5000 gas. Writing again to a slot already in the dirty map costs only 200 gas (even if the value changes).
Refund rules at the end of the transaction
At the end of the transaction, each slot in the dirty map is refunded under one of three cases. If it was 0 before the transaction and is still 0, 19800 gas is refunded. If it was nonzero before the transaction and its value has not changed, 4800 gas is refunded. If it was nonzero before the transaction and is now 0, 15000 gas is refunded.
Gas savings seen in examples
Recomputing the original text's three examples: for a slot changed 0→1→0, the cost is 20000+200-19800=400 gas, down from the previous 15000 gas. For a slot incremented 5 times, the cost is 20000+5×200=21000 gas, down from the previous 45000 gas. For moving a balance through A→B→C (all start and end values nonzero), the cost is 5000×3+200-4800=10400 gas, down from the previous 20000 gas. Transactions that change a slot only once keep the same cost as before.
Alternatives considered and reasons for rejection
One alternative was to charge a flat 200 gas for every SSTORE and apply the additional 19800/4800 charges for new/changed values at the end of the transaction, but this pushes gas consumption outside the EVM's execution flow (to the end of the transaction), making debugging harder and making it difficult for a contract to limit the gas consumption of a call target. Using a separate counter for storage refunds avoids the problem of refunds being capped at half of gas consumed, but increases tracking complexity. Refunding immediately whenever a slot returns to its original value requires a new mechanism, complicates gas accounting for nested calls, and would allow calls with negative execution cost. All three alternatives were rejected.
Backward compatibility and current status
This change requires a hard fork. The original text states that no existing contract will see increased gas cost from this change, and many contracts will see reduced cost, so no backward-compatibility issues are expected on the contract side. The Implementation section remains TBD, and the status is Stagnant; the original text contains no record of this being included in a later fork.