EIP-2936 Self-destructing contract storage cleanup, EXTCLEAR (EXTCLEAR Opcode For SELFDESTRUCTed contracts)

No call decision · EVM & execution · EIP status Stagnant · created 2020-09-03

A proposal that changes SELFDESTRUCT so it no longer clears all contract storage at once, and instead lets only the slots that need clearing be removed individually via a new opcode, EXTCLEAR.

Why it is needed

SELFDESTRUCT (0xFF) clears all storage used by a contract when that contract is destroyed. However, tracking how many slots a given contract has used places computational burden on nodes, and implementation bugs risk causing network splits where clients diverge in state. Furthermore, the pattern of recreating a contract at the same address with CREATE2 and reading previous storage with SLOAD has made the timing and method of clearing storage more complicated. In practice, some clients already work around this by not clearing storage at all, and instead checking at every SLOAD whether the contract has been reinitialized. This EIP removes the responsibility for clearing storage all at once from SELFDESTRUCT, and instead allows only the needed slots to be individually cleared later via EXTCLEAR.

What changes

Today

After EIP-2936

Who is affected

In depth

EXTCLEAR opcode specification

After FORK_BLOCK_NUM, a new opcode EXTCLEAR is activated at address 0x5C. It does not push a value onto the stack, and pops two words: the address of the destroyed contract and the storage address to clear. These two values together specify one particular slot of one particular contract to process.

Three-step gas calculation

If a contract still exists at the target address, only gas equal to EXTCODEHASH is charged. If the contract does not exist and the slot is already zero, gas equal to EXTCODEHASH plus SLOAD is charged. If the contract does not exist and the slot still has a remaining value, that slot is reset to zero, gas equal to EXTCODEHASH plus SSTORE (reset) is charged, and the amount specified by SSTORE is refunded.

Change in SELFDESTRUCT behavior and retroactive application

SELFDESTRUCT is modified so that it no longer clears contract storage. This change applies retroactively, so the remaining storage of contracts that were already SELFDESTRUCTed before this EIP can also be cleaned up using EXTCLEAR.

Motivation: state tracking burden and risk of network splits

For SELFDESTRUCT to clear all storage, nodes must track all storage used by every contract, which is computationally expensive. In addition, the pattern of recreating a contract at the same address with CREATE2 and then reading previous storage with SLOAD has complicated SLOAD logic. Because of this, several clients already work around the issue by not actually clearing storage, and instead checking at SLOAD time whether the contract has been reinitialized since the SSTORE. These subtle implementation differences carry the risk of bugs causing a network split.

Relationship to the reincarnation proxy pattern

The CREATE2 recreation (reincarnation) proxy pattern upgrades a contract by destroying it and recreating it at the same address. Under this EIP, since storage is no longer automatically cleared at the moment of destruction, it becomes possible to reincarnate while preserving storage during an upgrade. For an atomic upgrade, it is also possible to selectively clear only certain slots while the contract is destroyed, and then restore it.

Reason for choosing opcode number 0x5C

0x5C was an open slot in the same number range as SLOAD (0x54) and SSTORE (0x55). Placing it in the same range as the opcodes that read and write storage naturally signals that EXTCLEAR is also a storage manipulation opcode.

Backward compatibility and security considerations

The existing reincarnation upgrade mechanism assumes that all internal storage is cleared immediately upon destruction, and this change could break that assumption, but such mechanisms can simply be adjusted to match the new behavior. Also, since anyone can call EXTCLEAR to target a destroyed contract's slots, problems can arise if a recreated contract does not anticipate that its storage could be maliciously cleared. For this reason, recreated contracts should be designed to reinitialize themselves.

Current status

This proposal is in Stagnant status, and the original text itself still has test cases marked TODO, with the implementation incomplete. There is no separate decision on record from calls regarding fork inclusion.

Original EIP · ← EIPs