EIP-161 · Final · Core · 2016-10-24
EIP-161: State trie clearing (invariant-preserving alternative)
Spurious Dragon rule that deletes empty accounts when a transaction touches them and stops new ones from being created, cleaning up the state bloat left by the 2016 DoS attacks.
What problem does it solve?
In autumn 2016, attackers exploited cheap opcodes to fill the state trie with millions of empty accounts: accounts with no code, a zero nonce and a zero balance. Each one was nearly free to create. For example, a zero-value CALL or SUICIDE to an unused address created it. Every node still had to store and process them, which slowed the whole network.
EIP-158 proposed clearing these accounts. EIP-161 is the version that was adopted. It reaches the same goal while preserving some important invariants. It went live in the Spurious Dragon hard fork at block 2,675,000.
How it works
The spec defines three terms:
- Empty: no code, zero nonce and zero balance.
- Dead: either non-existent or empty.
- Touched: involved in any potentially state-changing operation, even a zero-value transfer.
It then adds four rules:
- Contracts start at nonce 1.
CREATEand contract-creation transactions raise the new account's nonce by one before running init code. A contract that is still being created is therefore never "empty" and can never be swept away. - The new-account charge is targeted.
CALLandSUICIDEused to charge 25,000 gas whenever the destination did not exist. Now the charge applies only when the call sends non-zero value to a dead account. - Nothing new becomes empty. An account may not go from non-existent to existent-but-empty. If an operation would do that, the account stays non-existent.
- Touched empty accounts are deleted. At the end of each transaction, after self-destructs run, every touched account that is empty is removed from the state.
Rule 4 is how the network cleaned up after itself. Once Spurious Dragon was live, sending zero-value calls to the attack accounts touched them and deleted them.
Sample code
Pseudocode for the end-of-transaction sweep:
def is_empty(acct):
return acct.code == b"" and acct.nonce == 0 and acct.balance == 0
def finalize_transaction(state, touched):
run_selfdestructs(state)
for addr in touched:
acct = state.get(addr)
if acct is not None and is_empty(acct):
state.delete(addr) # account becomes non-existent
return state.root() # root used for the receipt
Gas rule for a value-transferring call:
if value > 0 and is_dead(state, to):
gas_cost += 25_000 # new-account charge
Things to know
- Spurious Dragon's other changes are listed in EIP-607. It followed Tangerine Whistle, whose EIP-150 repriced the IO-heavy opcodes behind the same attacks.
- An addendum clarifies that deletions are undone when the state is reverted. On 2016-11-24 Geth and Parity disagreed on this case, which caused a consensus bug. Out-of-gas calls to precompiles were part of the trouble.
- Starting contracts at nonce 1 is why the first address a contract derives with
CREATEuses nonce 1, not 0. - Address collisions during contract creation are handled separately by EIP-684.