---
eip: 2464
label: EIP-2464
title: "eth/65: transaction announcements and retrievals"
status: Final
type: Standards Track
category: Networking
created: 2020-01-13
requires: 2364
official: https://eips.ethereum.org/EIPS/eip-2464
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2464.md
canonical: https://eips.huatuo.xyz/zh/eip-2464/
lang: zh
tldr: 引入 eth/65，让节点通过哈希来宣告新交易，只获取自己缺少的交易，减少向每个对等节点广播相同交易所浪费的带宽。
tags: [networking, devp2p, mempool, transactions, protocol]
---

# EIP-2464: eth/65: transaction announcements and retrievals

> Introduces `NewPooledTransactionHashes`, `GetPooledTransactions`, and `PooledTransactions`.

**TL;DR:** 引入 eth/65，让节点通过哈希来宣告新交易，只获取自己缺少的交易，减少向每个对等节点广播相同交易所浪费的带宽。

## 它解决什么问题？

对于区块，`eth` 线路协议已经有两种传播方式：把完整区块发给少量（平方根数量）的对等节点，只向其余节点*宣告*它的哈希。交易却没有这种区分。在 `eth/64` 及之前，节点只能通过 `Transactions (0x02)` 消息推送完整的交易，所以要让交易传遍网络的每个角落，就得把每笔交易发给所有对等节点。有 N 个对等节点时，同一笔交易会经过每一条连接，带宽被浪费了 N 倍。

连接新对等节点时更糟：两个节点无法知道对方已经有哪些待处理交易，只能交换整个交易池，规范估计其大小为数十到数百 MB。

## 它如何运作

EIP-2464 增加了三种消息，并把协议升级为 `eth/65`：

| 消息 | 代码 | 载荷 | 用途 |
| --- | --- | --- | --- |
| `NewPooledTransactionHashes` | `0x08` | 32 字节哈希的列表 | “我有这些待处理交易” |
| `GetPooledTransactions` | `0x09` | 32 字节哈希的列表 | “请把这些从你的池里发给我” |
| `PooledTransactions` | `0x0a` | 交易列表 | 对 `GetPooledTransactions` 请求的回复 |

流程是：向大多数对等节点宣告哈希，让每个对等节点只请求自己缺少的，然后用完整交易作答。完整广播可以限制在一小部分对等节点上，就像区块已经做的那样。与新对等节点同步交易池的开销降到大约 `len(pool) × 32 bytes`。

关键规则：

- 只有**交易池**中（尚未进入区块）的交易才会被宣告或提供。节点绝不会为了应答而读取数据库，这避免了一个廉价的拒绝服务攻击途径。
- 除了 10 MB 的 devp2p 数据包上限外，协议层面没有对列表长度设上限，但规范建议每次宣告约 4096 个哈希，每次请求 256 个。
- 回复保持请求的顺序，可以跳过不可用的项目，这样请求方就能分辨哪些哈希需要重试（最后一个返回项之后的全部），哪些视为不可用（它之前的空缺）。
- 使用单独的回复消息（而不是复用 `0x02`）便于把响应与请求对应起来。

## 示例代码

用类 RLP 伪代码表示的消息格式：

```text
NewPooledTransactionHashes (0x08): [txhash_0: B_32, txhash_1: B_32, ...]
GetPooledTransactions      (0x09): [txhash_0: B_32, txhash_1: B_32, ...]
PooledTransactions         (0x0a): [tx_0, tx_1, ...]   # same order as requested, gaps allowed
```

处理收到的宣告的简化处理函数：

```python
def on_new_pooled_hashes(peer, hashes):
    missing = [h for h in hashes if h not in txpool and h not in in_flight]
    for chunk in batches(missing, 256):           # suggested request size
        in_flight.update(chunk)
        peer.send(GetPooledTransactions(chunk))

def on_pooled_transactions(peer, requested, txs):
    for tx in txs:
        txpool.add(tx)
    # hashes after the last returned tx can be re-requested;
    # gaps before it are treated as unavailable
```

## 需要注意

- 这只是网络层的变更。它不涉及共识，也不需要硬分叉；devp2p 可以并行运行多个 `eth` 版本，所以升级后的客户端和旧客户端仍通过 `eth/64` 通信。
- 后续版本都建立在它之上：[eth/66](/eip-2481/) 增加了请求 ID，[eth/68](/eip-5793/) 在哈希宣告中加入了交易类型和大小。
- 对等节点可能宣告了某笔交易，之后却无法提供，因为它在此期间已被打包进区块；这不算违反协议。


---

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