---
eip: 2935
label: EIP-2935
title: Serve historical block hashes from state
status: Final
type: Standards Track
category: Core
created: 2020-09-03
official: https://eips.ethereum.org/EIPS/eip-2935
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2935.md
canonical: https://eips.huatuo.xyz/zh/eip-2935/
lang: zh
tldr: 在系统合约的存储中保存最近 8191 个区块哈希，使合约和无状态客户端可以从状态中读取约一天的区块哈希历史，而不必依赖 BLOCKHASH 的 256 个区块窗口。
tags: [core, state, blockhash, system-contract, stateless]
---

# EIP-2935: Serve historical block hashes from state

> Store and serve last 8191 block hashes as storage slots of a system contract to allow for stateless execution

**TL;DR:** 在系统合约的存储中保存最近 8191 个区块哈希，使合约和无状态客户端可以从状态中读取约一天的区块哈希历史，而不必依赖 BLOCKHASH 的 256 个区块窗口。

## 它解决什么问题？

`BLOCKHASH` 操作码只返回最近 256 个区块的哈希，而 EVM 默默假定客户端手头有这些哈希。这个假设对**无状态客户端**不成立：它们不在本地保存链，只接收自己所需的状态（见证）。如果区块哈希存放在状态中，就可以像其他存储值一样被纳入见证。

把它们放进状态，还能让合约和 rollup 在证明最近区块相关事项时拥有长得多的窗口，而无需改变 `BLOCKHASH` 本身的行为。

## 它如何运作

| 参数 | 值 |
| --- | --- |
| `HISTORY_SERVE_WINDOW` | 8191 |
| `BLOCKHASH_SERVE_WINDOW` | 256（不变） |
| `SYSTEM_ADDRESS` | `0xfffffffffffffffffffffffffffffffffffffffe` |
| `HISTORY_STORAGE_ADDRESS` | `0x0000F90827F1C53a10cb7A02335B175320002935` |

- 在每个区块开始时、任何交易之前，客户端从 `SYSTEM_ADDRESS` 调用历史合约，以父区块哈希作为 calldata。这遵循 [EIP-4788](/eip-4788/) 的系统调用约定：它必须执行完成，不计入区块 gas 上限，也不转移任何价值。
- 合约的 `set` 路径把哈希存入一个**环形缓冲区**：槽位为 `(block.number - 1) % 8191`。
- 其他任何调用者都进入 `get` 路径：发送 32 字节的大端区块号，得到它的 32 字节哈希。如果输入不是恰好 32 字节，或者区块号不在 `[block.number - 8191, block.number - 1]` 范围内，调用会 revert。
- `BLOCKHASH` 的语义和成本保持不变。
- 该合约通过一个无密钥发送者的预签名交易部署，固定地址就是这样得到的。

激活之后，需要 8191 个区块（按当前的 slot 时间约一天）才能填满缓冲区；分叉区块之前的哈希永远不会被写入。

## 示例代码

在 Solidity 中读取较早的区块哈希（在启用了该 EIP 的链上）：

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

library BlockHashHistory {
    address constant HISTORY = 0x0000F90827F1C53a10cb7A02335B175320002935;

    function get(uint256 blockNumber) internal view returns (bytes32 h) {
        (bool ok, bytes memory out) = HISTORY.staticcall(abi.encode(blockNumber));
        require(ok && out.length == 32, "out of range");
        h = abi.decode(out, (bytes32));
    }
}
```

使用 ethers v6 在链下读取：

```ts
import { JsonRpcProvider, toBeHex, zeroPadValue } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const n = (await provider.getBlockNumber()) - 1000;
const hash = await provider.call({
  to: "0x0000F90827F1C53a10cb7A02335B175320002935",
  data: zeroPadValue(toBeHex(n), 32),
});
console.log(n, hash);
```

## 需要注意

- 该合约和其他合约一样被调用，遵循正常的 [EIP-2929](/eip-2929/) 热/冷 gas 规则；系统调用不会预热它。
- calldata 必须恰好是 32 字节；对单个 `uint256` 做 ABI 编码就能得到。
- 窗口是 8191 个区块，并非无限。对于更早的历史，预期的途径是针对信标区块根（通过 [EIP-4788](/eip-4788/)）进行证明。
- [EIP-7709](/eip-7709/)（Draft）提议让 `BLOCKHASH` 本身从该合约读取。


---

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