EIPs Explained

EIP-1283 · Final · Core · 2018-08-01

EIP-1283: Net gas metering for SSTORE without dirty maps

Net gas metering for SSTORE: writing the same storage slot several times in one transaction costs only 200 gas after the first write, with refunds when a slot is restored. Pulled from Constantinople over a reentrancy risk and later reshipped as EIP-2200.

What problem does it solve?

Before this proposal, every SSTORE that changed a slot cost 5,000 gas (or 20,000 when setting a zero slot), no matter how many times the slot had already been written in the same transaction. Common patterns paid heavily for that:

  • a reentrancy lock set to 1 and back to 0 paid for two full writes,
  • a contract updating a counter several times in one call paid for each write,
  • contracts passing values between call frames via storage paid as if each value were persisted.

Clients do not actually write to disk on every SSTORE; they commit at the end. EIP-1283 makes gas reflect that, using only information most clients already track: the slot's original value (at the start of the transaction) and its current value.

How it works

For each SSTORE compare original, current and new values:

  • No-op (current == new): 200 gas.
  • Fresh slot (original == current, first change in this transaction):
    • original is 0: 20,000 gas;
    • otherwise 5,000 gas, plus a 15,000 refund if new is 0.
  • Dirty slot (original != current, already changed): 200 gas, and the refund counter is adjusted:
    • if original ≠ 0 and current is 0, take back the 15,000 clear refund; if new is 0, grant it;
    • if new == original (slot reset), refund 19,800 (original 0) or 4,800 (original ≠ 0).

Examples from the spec:

  • Empty slot set to 1 then back to 0: 20000 + 200 - 19800 = 400 gas.
  • Empty slot incremented 5 times: 20000 + 5 × 200 = 21000 gas.

It never charges more than the old scheme, and it gives roughly the effect of transient storage without a separate "dirty map".

Sample code

The cost rule as Python-style pseudocode:

def sstore_cost(original, current, new, refund):
    if current == new:
        return 200, refund
    if original == current:
        if original == 0:
            return 20000, refund
        if new == 0:
            refund += 15000
        return 5000, refund
    # dirty slot
    if original != 0:
        if current == 0:
            refund -= 15000
        if new == 0:
            refund += 15000
    if original == new:
        refund += 19800 if original == 0 else 4800
    return 200, refund

Things to know

  • EIP-1283 was scheduled for Constantinople but was pulled just before activation after a reentrancy issue was found: a dirty-slot SSTORE costing 200 gas fits inside the 2,300 gas stipend of Solidity's transfer/send, which existing contracts assumed was too little to change state. The Petersburg upgrade removed it from the fork.
  • Its status reads Final, but it is not active on mainnet in this form. The same logic shipped in Istanbul as EIP-2200, combined with EIP-1706's rule that SSTORE fails if gas left is at or below the stipend.
  • Refund numbers changed again later; see EIP-2200 for the current lineage. For truly temporary values, EIP-1153 transient storage is now the purpose-built tool.

#gas #sstore #storage #core #constantinople

Details

Authors
Wei Tang (@sorpaas)
Status
Final
Type
Standards Track / Core
Created
2018-08-01
Required by

Machine-readable: Markdown · JSON