---
eip: 3102
label: EIP-3102
title: Binary trie structure
status: Stagnant
type: Standards Track
category: Core
created: 2020-09-01
official: https://eips.ethereum.org/EIPS/eip-3102
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-3102.md
canonical: https://eips.huatuo.xyz/zh/eip-3102/
lang: zh
tldr: 提议用单一的二叉树取代以太坊的十六叉 Merkle Patricia Trie，合并账户与存储数据，弃用 RLP 并改用 blake2b 哈希，以缩小无状态证明的体积。
tags: [state, trie, statelessness, merkle, core]
---

# EIP-3102: Binary trie structure

> Change the storage structure from hexary to binary, merge the account and storage tries, and use blake2b.

**TL;DR:** 提议用单一的二叉树取代以太坊的十六叉 Merkle Patricia Trie，合并账户与存储数据，弃用 RLP 并改用 blake2b 哈希，以缩小无状态证明的体积。

## 它解决什么问题？

以太坊的状态存放在**十六叉** Merkle Patricia Trie 中（每个节点 16 个子节点），每个合约还有单独的存储树。当瓶颈在数据库读取时这样做是合理的，但它会产生很大的证明：Merkle 证明的每一层都必须包含所有兄弟哈希，16 个子节点意味着每层 15 个哈希。

对于**无状态**以太坊，区块会附带其所触及状态的证明（witness），重要的是证明的大小。规范中对 2²⁴ 个元素的粗略估算表显示，二叉树每个分支约 768 字节，而十六叉树为 2,880 字节。客户端也已经转向扁平的键值数据库，所以十六叉布局在磁盘上不再带来多少好处。

## 它如何运作

- **只有一棵树。** 账户树和存储树合并为单一的二叉树；键长为 32 到 64 字节。
- **账户布局。** 对于地址 `a`，取 `hash(a)` 去掉最后两位，再附加一个 2 位标签：
  - `00` 余额，`01` nonce，`10` 代码，`11` 存储根
  - 存储槽 `k` 位于 `… ++ 0b11 ++ hash(k)`
  - 采用代码默克尔化（[EIP-2926](/eip-2926/)）时，代码块 `c` 位于 `10` 分支之下。
- **更简单的编码。** 没有 RLP，没有 hex-prefix 叶子标记（叶子就是没有子节点的节点），每个节点无论大小都会被哈希。
- **哈希函数。** 为了速度，用 blake2b 取代 keccak256，以抵消节点数量约 4 倍的增长。

默克尔化规则：

```
internal_hash = hash(left_child_hash || right_child_hash)
leaf_hash     = hash(hash(key) || hash(leaf_value))
# nodes with a prefix (extension) additionally commit to path length:
prefixed_hash = hash(u256(path_length - 1) || <hash above>)
```

承诺从根开始的完整路径长度（而不仅是前缀），意味着删除一个叶子不会迫使重新哈希它的兄弟节点。

## 示例代码

计算账户字段和存储槽的键（Python，使用 blake2b）：

```python
from hashlib import blake2b

def h(x: bytes) -> bytes:
    return blake2b(x, digest_size=32).digest()

def account_key(address: bytes, tag: int) -> int:
    """Return the 256-bit key: first 254 bits of h(address) ++ 2-bit tag."""
    stem = int.from_bytes(h(address), "big") >> 2
    return (stem << 2) | tag

BALANCE, NONCE, CODE, STORAGE = 0b00, 0b01, 0b10, 0b11

def storage_key(address: bytes, slot: int) -> bytes:
    prefix = account_key(address, STORAGE).to_bytes(32, "big")
    return prefix + h(slot.to_bytes(32, "big"))   # 64-byte key
```

## 需要注意

- **状态：Stagnant。** 它没有被采纳，而切换需要一次硬分叉外加完整的状态转换。规范指向 [EIP-2584](/eip-2584/) 作为过渡方案。
- 后来的无状态状态设计采取了不同的形态：[EIP-6800](/eip-6800/) 提议统一的 verkle 树，[EIP-7864](/eip-7864/) 则回到统一的二叉树，只是键布局不同。
- 选择 blake2b 而不是 BLAKE3，理由是当时没有官方的 Go 版 BLAKE3 发布。
- 规范提到了 EIP-2926 旧版本中的 32 字节代码块大小；该 EIP 现在使用 31 字节。


---

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