---
eip: 2584
label: EIP-2584
title: Trie format transition with overlay trees
status: Stagnant
type: Standards Track
category: Core
created: 2020-04-03
official: https://eips.ethereum.org/EIPS/eip-2584
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2584.md
canonical: https://eips.huatuo.xyz/zh/eip-2584/
lang: zh
tldr: 一项处于停滞（Stagnant）状态的计划：在不停止链的情况下，把以太坊状态从十六叉 trie 切换为二叉 trie，做法是在旧 trie 于后台转换期间，把新状态写入覆盖层（overlay）trie。
tags: [state, trie, binary, migration, core, stagnant]
---

# EIP-2584: Trie format transition with overlay trees

> This EIP proposes a method to convert the state trie format from hexary to binary: new values are directly stored in a binary trie “laid over” the hexary trie. Meanwhile, the hexary trie is converted to a binary trie in the background. When the process is finished, both layers are merged.

**TL;DR:** 一项处于停滞（Stagnant）状态的计划：在不停止链的情况下，把以太坊状态从十六叉 trie 切换为二叉 trie，做法是在旧 trie 于后台转换期间，把新状态写入覆盖层（overlay）trie。

## 它解决什么问题？

以太坊把状态存放在十六叉 Merkle Patricia Trie 中：每个分支节点有 16 个子节点。二叉 trie（每个分支 2 个子节点）能让 Merkle 证明小得多，这对无状态客户端和轻客户端很重要。

难点在于迁移。完整状态太大，不可能在一个出块时间内转换完，所以不能在分叉区块简单地一键切换。早先的想法是“stop the world”（转换期间暂停整条链）或“copy on write”（只在被触及时才转换分支，可能永远转换不完）。EIP-2584 提出了一种方法，既让链持续运行，又能在可预测的时间内完成。

## 它如何运作

分三个阶段（摘要里说是四个，但正文描述的是三个）：

1. **阶段 1：覆盖层。** 从区块 `h₁` 开始，所有状态*写入*都进入一个新的、最初为空的二叉**覆盖层 trie**，其根是区块头中的一个新字段。旧的十六叉**基础 trie** 被冻结；读取先查覆盖层，未命中再回退到基础 trie。被删除的账户在覆盖层中用空哈希标记。与此同时，每个节点在后台把冻结的十六叉 trie 转换为二叉。
2. **阶段 2：合并。** 在区块 `h₂`，状态根切换为转换好的二叉基础根。覆盖层变为只读/只能删除；写入进入基础 trie。每个区块之前，覆盖层中前 `N` 个账户（按键顺序）被移入基础 trie。
3. **阶段 3：完成。** 覆盖层清空之后，其根字段会从区块头中移除。

`h₂` 必须与 `h₁` 相隔足够远，让矿工能完成转换；作者报告称，原型在一台高速机器上大约 30 分钟即可完成转换。

## 示例代码

阶段 1 中状态访问的伪代码：

```python
EMPTY = keccak(b"")

def read_account(addr):
    v = overlay.get(addr)
    if v == EMPTY:            # deleted during the transition
        return None
    if v is not None:
        return v
    return base_hex.get(addr) # frozen hexary trie

def write_account(addr, value):
    overlay.put(addr, value)  # base trie is never touched in phase 1

def delete_account(addr):
    overlay.put(addr, EMPTY)
```

以及阶段 2 中每个区块的合并步骤：

```python
for addr, v in overlay.first_n_sorted(N):
    if v == EMPTY:
        base_bin.delete(addr)
    else:
        base_bin.put(addr, v)
    overlay.delete(addr)
```

## 需要注意

- 状态为 **Stagnant**。这是一次会改变区块格式和哈希的硬分叉，从未被排入计划。
- 规范中的安全说明：攻击者可能撑大覆盖层以拖延阶段 2（也许可以通过在阶段 1 提高 `SSTORE` 的价格来缓解）；如果 `h₂` 来得太早，矿工可能来不及算出转换后的根。
- 覆盖层的想法延续到了后来的状态迁移工作中：[EIP-7612](/eip-7612/)（向 Verkle 的覆盖层过渡）和 [EIP-7748](/eip-7748/)（状态转换）。目标树此后已从 Verkle（[EIP-6800](/eip-6800/)）转向统一二叉树（[EIP-7864](/eip-7864/)），两者仍然是草案或停滞状态。


---

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