EIP-7642 · Final(最终) · Networking · 2024-02-29
EIP-7642: eth/69 - history expiry and simpler receipts
以太坊 eth 点对点协议的第 69 版:对等节点会公告自己仍提供的区块范围(丢弃合并前历史之后就需要这一点),握手去掉了已经过时的总难度,收据发送时不再附带 256 字节的 bloom 过滤器。
它解决什么问题?
eth 线路协议中有三处遗留问题,已经变得代价高昂或具有误导性:
- 历史过期(history expiry)。 历史过期工作组决定,客户端可以丢弃合并前的区块历史(自 2025 年 5 月 1 日起)。这样,通过 p2p 同步历史的节点就需要知道哪些对等节点仍然拥有它。否则,它只能通过询问并失败来发现。此前一个类似的提案 EIP-7542,在历史过期达成一致之前就被撤回了。
- 总难度(total difficulty)。
Status握手携带了td,而自合并以来它已毫无意义(合并后的区块难度为 0)。 - 收据 bloom。 没有客户端存储收据的 bloom 过滤器,因为可以根据日志重新计算。但协议仍然让提供数据的节点重新生成并发送它们。规范估计,每次全量同步因此多出约 530 GB(snappy 压缩后约 95 GiB)。
它如何运作
eth/69 修改了三条消息:
Status (0x00):去掉 td,加入所提供的范围。
eth/68: [version, networkid, td, blockhash, genesis, forkid]
eth/69: [version, networkid, genesis, forkid, earliestBlock, latestBlock, latestBlockHash]
Receipts (0x10):扁平列表,没有 bloom,对带类型的收据也不再做特殊包装。
eth/68: legacy = [status, cumulative-gas, bloom, logs]; typed = tx-type || rlp(legacy)
eth/69: receipt = [tx-type, post-state-or-status, cumulative-gas, logs]
接收方根据日志重新计算每个 bloom,以检查区块的收据根,这个代价很低。
BlockRangeUpdate (0x11),新增:[earliestBlock, latestBlock, latestBlockHash],在所提供的范围发生变化时发送,但最多大约每个 epoch(32 个区块)一次,以降低流量。
示例代码
根据 eth/69 的收据重建共识收据(含 bloom),TypeScript 伪代码:
import { encodeRlp, concat, toBeHex } from "ethers";
type Log = [address: string, topics: string[], data: string];
type Eth69Receipt = [txType: number, status: string, cumulativeGas: string, logs: Log[]];
function toConsensusEncoding([txType, status, gas, logs]: Eth69Receipt): string {
const bloom = logsBloom(logs); // 256-byte bloom over addresses + topics
const body = encodeRlp([status, gas, bloom, logs]);
return txType === 0 ? body : concat([toBeHex(txType, 1), body]); // typed: type || rlp(...)
}
logsBloom 是标准的以太坊日志 bloom(在 2048 位的过滤器中,每个地址和每个 topic 置位 3 个比特)。