EIPs Explained

EIP-158 · Final · Core · 2016-10-16

EIP-158: State clearing

Proposed deleting empty accounts from Ethereum's state and treating 'empty' as 'nonexistent', to clean up millions of junk accounts created by the 2016 DoS attacks.

What problem does it solve?

In autumn 2016, attackers exploited underpriced opcodes to cheaply create huge numbers of accounts with zero balance, zero nonce and no code. Each one still had to be stored in the state trie. State grew, disks filled and fast sync slowed down.

EIP-158 proposes removing those "empty" accounts and making sure no new ones can be created. Once the junk is gone, "empty" and "does not exist" become the same thing, which simplifies the protocol for good.

How it works

From the fork block onward:

  1. Never save an empty account. If a state change would leave an account with nonce 0, balance 0, no code (and, in the base version, no storage), the account is deleted instead.
  2. Touching deletes. If an existing empty account is "touched", it is deleted. A touch is anything that would have created the account if it did not exist: a zero-value CALL, a zero-value SELFDESTRUCT (then SUICIDE), being a transaction recipient, being created with empty code, or receiving fees as the miner.
  3. Empty equals nonexistent. Whenever the EVM asks "does this account exist?", an empty account answers no.
  4. No surprise 25,000 gas. Zero-value calls and self-destructs no longer pay the new-account creation cost.

The spec lists two variants: 1b defines emptiness as balance, nonce and code only (storage ignored) to keep client caches simple, and 1c replaces "touching deletes" with a sweep that removes 1,000 empty accounts per block in hash order.

Sample code

The emptiness check from variant 1b, and the rule applied after each state change (pseudocode):

def is_empty(acct):
    return get_balance(acct) == 0 and get_code(acct) == b"" and get_nonce(acct) == 0

def after_state_change(addr):
    if account_exists(addr) and is_empty(addr):
        delete_account(addr)

def on_touch(addr):            # e.g. 0-value CALL to addr
    if account_exists(addr) and is_empty(addr):
        delete_account(addr)

Clearing an empty account is as simple as sending it a zero-value call:

(bool ok, ) = emptyAccount.call{value: 0}("");  // touches and deletes it

Things to know

  • EIP-158 is marked Final, but what actually shipped in the Spurious Dragon hard fork (November 2016, EIP-607) was the revised version, EIP-161 "State trie clearing". Read EIP-161 for the exact rules clients implement.
  • The spec admits a temporary side effect: touching an existing empty account costs about 700 gas instead of the usual 5,000 for a state write, so block processing could be heavier until the cleanup finished.
  • After this change, contracts cannot distinguish "never used" from "used but emptied". Code that relies on extcodesize or balance to detect prior activity should not assume otherwise.
  • It is the follow-up to the gas repricing of EIP-150 (Tangerine Whistle), which closed the cheap attack vectors. EIP-158/161 then dealt with the leftover junk.

#state #core #cleanup #spurious-dragon

Details

Authors
Vitalik Buterin (@vbuterin)
Status
Final
Type
Standards Track / Core
Created
2016-10-16

Machine-readable: Markdown · JSON