EIP-1087 · Stagnant · Core · 2018-05-17
EIP-1087: Net gas metering for SSTORE operations
Proposed charging SSTORE by the net change to a storage slot over a whole transaction, so writing a slot several times or restoring its original value no longer costs full price.
What problem does it solve?
When this EIP was written in 2018, every SSTORE was priced as if it were the only write to that slot: 20,000 gas to go from zero to non-zero, 5,000 gas for any other change, and a 10,000 gas refund for clearing a slot. That is fair for a single write, but wildly overcharges sequences of writes that touch the same slot.
The spec gives three examples:
- Set a fresh slot to 1 and back to 0: charged 15,000 gas, though nothing ends up on disk.
- Increment a fresh slot five times: charged 45,000 gas, though only one final value is written.
- Transfer A → B, then B → C within one transaction: 20,000 gas for four balance updates.
This made patterns like reentrancy locks (set a flag, then clear it) and passing context between calls in the same transaction needlessly expensive.
How it works
The client keeps a per-transaction dirty map of storage slots that have been changed. It follows normal call scoping: if a call frame reverts, its dirty-map changes are discarded too.
- Writing the value a slot already holds: 200 gas.
- First change to a slot in the transaction: 20,000 gas if it was zero, otherwise 5,000 gas, and the slot is marked dirty.
- Any further write to a dirty slot: 200 gas.
- At the end of the transaction, for each dirty slot:
- was 0 and is 0 again → refund 19,800
- was non-zero and is back to its original value → refund 4,800
- was non-zero and is now 0 → refund 15,000
Under these rules the examples above cost 400, 21,000 and 10,400 gas respectively. A transaction that writes a slot only once pays exactly what it did before, so no contract gets more expensive.
Sample code
Pseudocode for the charging rule (original value = value at transaction start):
def sstore_cost(slot, new_value, current, dirty):
if new_value == current:
return 200
if slot in dirty:
return 200
dirty.add(slot)
return 20000 if current == 0 else 5000
def end_of_tx_refund(dirty, original, final):
refund = 0
for slot in dirty:
o, f = original[slot], final[slot]
if o == 0 and f == 0:
refund += 19800
elif o != 0 and f == o:
refund += 4800
elif o != 0 and f == 0:
refund += 15000
return refund
The kind of contract code that benefits, a simple reentrancy lock:
uint256 private locked = 1;
modifier nonReentrant() {
require(locked == 1, "reentrant");
locked = 2; // first change: 5,000 gas
_;
locked = 1; // back to original: cheap, plus a refund at tx end
}
Things to know
- Status: Stagnant. EIP-1087 itself was never activated. Its successor EIP-1283 achieved the same net-metering idea without a dirty map by comparing the original, current and new values of a slot.
- EIP-1283 was scheduled for Constantinople but pulled before activation because cheaper
SSTORElet code run within the 2,300-gas stipend given totransfer/send, opening a reentrancy risk. The idea finally shipped in Istanbul as EIP-2200, which adds a guard thatSSTOREfails when gas left is at or below the stipend (an idea from EIP-1706). - Storage pricing has since changed again with cold/warm access costs (EIP-2929) and reduced refunds (EIP-3529), so the exact numbers here are historical.