---
eip: 4881
label: EIP-4881
title: Deposit Contract Snapshot Interface
status: Final
type: Standards Track
category: Interface
created: 2021-01-29
official: https://eips.ethereum.org/EIPS/eip-4881
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-4881.md
canonical: https://eips.huatuo.xyz/zh/eip-4881/
lang: zh
tldr: 定义信标链存款 Merkle 树的紧凑快照，并通过标准 Beacon API 端点提供，使新的共识客户端无需重放每一笔历史存款即可重建该树。
tags: [consensus, deposits, beacon-api, sync, staking]
---

# EIP-4881: Deposit Contract Snapshot Interface

> Establishing the format and endpoint for transmitting a snapshot of the deposit Merkle tree

**TL;DR:** 定义信标链存款 Merkle 树的紧凑快照，并通过标准 Beacon API 端点提供，使新的共识客户端无需重放每一笔历史存款即可重建该树。

## 它解决什么问题？

每个验证者都是通过向执行层上的存款合约发送 ETH 来加入的。合约维护着所有存款的增量 Merkle 树，信标区块必须为每笔新存款附上针对该树的 Merkle 证明。

为了构建这些证明，大多数共识客户端会下载并存储自合约上线以来的*每一条*存款日志。这拖慢了新节点，尤其是使用弱主观性（检查点）同步的节点，也使执行节点永远无法修剪旧日志。然而，一旦某笔存款已被处理并最终确定，其路径上的大部分哈希就再也不需要了。

EIP-4881 对树中仍然重要的那部分内容的小型快照以及获取它的端点进行了标准化。

## 它如何运作

客户端内部可以随意存储该树，但在传输时必须使用：

```python
class DepositTreeSnapshot:
    finalized: List[Hash32, DEPOSIT_CONTRACT_DEPTH]  # roots of fully finalized subtrees
    deposit_root: Hash32
    deposit_count: uint64
    execution_block_hash: Hash32
    execution_block_height: uint64
```

- `finalized` 对于树中已最终确定的部分，只保存每棵完整左子树的根哈希。对于深度为 32 的树，无论存款数量多少，最多只有 32 个哈希。
- `deposit_root`、`deposit_count` 和 `execution_block_hash` 与信标链在该时间点的 `Eth1Data` 一致。同时还包含 `execution_block_height`，这样正在同步的客户端就不必向可能尚未同步的执行引擎查询。
- 接收方可以根据 `finalized` 和 `deposit_count` 重新计算根，并与 `deposit_root` 比较，以低成本验证快照。
- 快照之后的新存款会追加在其上，并可由此生成证明。

共识客户端通过 Beacon Node API 端点 `/eth/v1/beacon/deposit_snapshot` 提供快照。一旦存款满足最终确定条件，客户端就可以修剪其下方的哈希。

## 示例代码

从信标节点获取快照（TypeScript）：

```ts
const res = await fetch(`${process.env.BEACON_URL}/eth/v1/beacon/deposit_snapshot`);
const { data } = await res.json();

console.log(data.deposit_count);            // number of deposits covered
console.log(data.finalized.length);         // at most 32 hashes
console.log(data.execution_block_hash);
```

验证快照，改编自规范的参考实现：

```python
def calculate_root(snap) -> Hash32:
    size, index = snap.deposit_count, len(snap.finalized)
    root = zerohashes[0]
    for level in range(DEPOSIT_CONTRACT_DEPTH):
        if size & 1:
            index -= 1
            root = sha256(snap.finalized[index] + root)
        else:
            root = sha256(root + zerohashes[level])
        size >>= 1
    return sha256(root + to_le_bytes(snap.deposit_count))  # mix in the count

assert calculate_root(snap) == snap.deposit_root
```

## 需要注意

- 这是一项面向共识客户端和节点运营者的 **Interface** EIP；它不改变共识规则或存款合约，并且完全向后兼容。
- 快照是由提供者给出的受信数据，类似于检查点同步的状态。请从你信任的节点获取，并检查根。
- 规范包含测试向量和 Python 参考实现。
- [EIP-6110](/eip-6110/) 后来把存款的传递移到了执行层，随着时间推移，这降低了客户端对旧的 `Eth1Data` 存款流程的依赖。


---

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