EIPs Explained

EIP-2936 · Stagnant · Core · 2020-09-03

EIP-2936: EXTCLEAR Opcode For SELFDESTRUCTed contracts

Proposed that SELFDESTRUCT stop wiping storage and that a new EXTCLEAR opcode let anyone clear a destroyed contract's storage slot by slot.

What problem does it solve?

When a contract called SELFDESTRUCT, the protocol had to delete all of its storage. That is an unbounded amount of work: a contract can hold any number of slots, and clients must track every one in case the contract self-destructs.

CREATE2 made it worse. A contract can be destroyed and redeployed ("reincarnated") at the same address, so clients had to be careful that a fresh contract does not see its predecessor's storage. Some clients avoided the deletion cost by not physically clearing storage and adding checks to SLOAD, which added complexity and a risk of consensus bugs.

EIP-2936 proposes to make SELFDESTRUCT cheap and simple by leaving storage in place, and to let cleanup happen later, one slot at a time.

How it works

After a fork block:

  1. SELFDESTRUCT no longer clears storage. The account's code and balance go away, but storage slots remain. This applies retroactively to contracts destroyed earlier.
  2. A new opcode EXTCLEAR (0x5C) pops two words, address and slot, and pushes nothing:
    • If a contract currently exists at address: nothing is cleared; charge the same as EXTCODEHASH.
    • Else, if the slot is already zero: charge EXTCODEHASH + SLOAD.
    • Else: set the slot to zero, charge EXTCODEHASH + the SSTORE reset cost, and grant the usual SSTORE clearing refund.

So only storage of destroyed contracts can be cleared, and anyone can do it.

A side benefit the author points out: the CREATE2 "reincarnation" upgrade pattern could keep storage across an upgrade and clear only selected slots while the contract is destroyed.

Sample code

Illustrative bytecode for clearing one slot of a destroyed contract (the opcode was never activated):

PUSH1  0x05               // storage slot to clear
PUSH20 <destroyed addr>   // address of the self-destructed contract
EXTCLEAR                  // 0x5C: pops address, slot; pushes nothing
STOP

Stack order follows the spec: the destroyed contract's address is popped first, then the storage slot.

Things to know

  • Status is Stagnant. It was never adopted.
  • Opcode 0x5C was later assigned to TLOAD by EIP-1153 (transient storage), so this proposal's opcode number is no longer available.
  • Ethereum solved the underlying problem differently: EIP-6780 restricts SELFDESTRUCT so it only deletes an account (and its storage) when called in the same transaction that created the contract. Otherwise it just sends the balance.
  • Gas refunds for storage clearing were also reduced by EIP-3529, which changes the economics of the refund mentioned here.
  • Under this proposal, a reincarnated contract would see leftover storage and anyone could clear it. The spec advises such contracts to reinitialize their state rather than assume either outcome.

#opcode #selfdestruct #storage #create2 #core

Details

Authors
William Morriss (@wjmelements)
Status
Stagnant
Type
Standards Track / Core
Created
2020-09-03

Machine-readable: Markdown · JSON