EIPs Explained

EIP-2200 · Final · Core · 2019-07-18

EIP-2200: Structured Definitions for Net Gas Metering

The net gas metering rules for SSTORE that went live in Istanbul: repeated writes to the same slot in a transaction are cheap, with refunds for restoring values and a stipend guard against reentrancy.

What problem does it solve?

Writing a storage slot used to cost a full 5,000 or 20,000 gas every time, even if the same slot was written several times in one transaction. Reentrancy locks, counters and values passed between call frames all paid far more than the work clients actually did.

EIP-1283 fixed that with "net gas metering" but was pulled from Constantinople: a write to an already-changed slot cost only 200 gas, which fits inside the 2,300 gas stipend that Solidity's transfer/send gives the recipient. Contracts that relied on "the stipend is too small to change state" could become reentrant.

EIP-2200 combines EIP-1283 with EIP-1706's fix and rewrites the rules with named parameters so later repricings can plug in cleanly. It shipped in Istanbul (December 2019).

How it works

Parameters at Istanbul:

Name Value
SLOAD_GAS 800 (raised from 200, matching EIP-1884)
SSTORE_SET_GAS 20,000
SSTORE_RESET_GAS 5,000
SSTORE_CLEARS_SCHEDULE 15,000

For each SSTORE, using the slot's original value (start of transaction), current value and new value:

  1. If gas left ≤ 2,300 (the call stipend), fail with out-of-gas. This is the reentrancy guard.
  2. current == new (no-op): charge SLOAD_GAS.
  3. original == current (first change): charge SSTORE_SET_GAS if original is 0, else SSTORE_RESET_GAS (and refund SSTORE_CLEARS_SCHEDULE if new is 0).
  4. Otherwise the slot is dirty: charge SLOAD_GAS and adjust refunds: undo or grant the clear refund as the value moves to or from 0, and if new == original refund SET_GAS − SLOAD_GAS or RESET_GAS − SLOAD_GAS.

Sample code

The Istanbul rule as Python-style pseudocode:

SLOAD_GAS, SET, RESET, CLEARS = 800, 20000, 5000, 15000
STIPEND = 2300

def sstore(gasleft, original, current, new, refund):
    if gasleft <= STIPEND:
        raise OutOfGas()
    if current == new:
        return SLOAD_GAS, refund
    if original == current:
        if original == 0:
            return SET, refund
        if new == 0:
            refund += CLEARS
        return RESET, refund
    if original != 0:
        if current == 0:
            refund -= CLEARS
        if new == 0:
            refund += CLEARS
    if original == new:
        refund += (SET - SLOAD_GAS) if original == 0 else (RESET - SLOAD_GAS)
    return SLOAD_GAS, refund

Things to know

  • Status is Final and it is the basis of today's SSTORE pricing, but the numbers have moved on. EIP-2929 (Berlin) folded in cold/warm access costs, and EIP-3529 (London) cut SSTORE_CLEARS_SCHEDULE to 4,800 and capped total refunds at one fifth of gas used.
  • The structure (no-op / fresh / dirty, original vs current value) is unchanged, which is exactly why the spec switched to named parameters.
  • Gas estimation must account for the stipend check: any SSTORE needs more than 2,300 gas left, even if the write itself is cheap.
  • Refunds are paid at the end of the transaction, not when the SSTORE runs.

#gas #sstore #storage #core #istanbul

Details

Authors
Wei Tang (@sorpaas)
Status
Final
Type
Standards Track / Core
Created
2019-07-18
Required by

Machine-readable: Markdown · JSON