---
eip: 161
label: EIP-161
title: State trie clearing (invariant-preserving alternative)
status: Final
type: Standards Track
category: Core
created: 2016-10-24
official: https://eips.ethereum.org/EIPS/eip-161
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-161.md
canonical: https://eips.huatuo.xyz/eip-161/
lang: en
tldr: 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.
tags: [state, core, spurious-dragon, accounts, dos]
---

# EIP-161: State trie clearing (invariant-preserving alternative)

**TL;DR:** 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](/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:

1. **Contracts start at nonce 1.** `CREATE` and 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.
2. **The new-account charge is targeted.** `CALL` and `SUICIDE` used 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.
3. **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.
4. **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:

```python
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:

```python
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](/eip-607/). It followed Tangerine Whistle, whose [EIP-150](/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 `CREATE` uses nonce 1, not 0.
- Address collisions during contract creation are handled separately by [EIP-684](/eip-684/).


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-161
