---
eip: 2976
label: EIP-2976
title: Typed Transactions over Gossip
status: Final
type: Standards Track
category: Networking
created: 2020-09-13
requires: 2718
official: https://eips.ethereum.org/EIPS/eip-2976
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2976.md
canonical: https://eips.huatuo.xyz/zh/eip-2976/
lang: zh
tldr: 定义类型化交易和收据（EIP-2718）如何通过 devp2p 的 eth 协议传输，使新的交易类型无需提升协议版本即可被广播。
tags: [networking, devp2p, transactions, typed-transactions]
---

# EIP-2976: Typed Transactions over Gossip

> Adds support for transmission of typed transactions over devp2p.

**TL;DR:** 定义类型化交易和收据（EIP-2718）如何通过 devp2p 的 eth 协议传输，使新的交易类型无需提升协议版本即可被广播。

## 它解决什么问题？

[EIP-2718](/eip-2718/) 引入了类型化交易信封：一笔交易可以是 `TransactionType || TransactionPayload`，而不是普通的 RLP 列表。这改变了区块对交易和收据的承诺方式，但它本身对点对点网络只字未提。如果节点之间无法互相传递类型化交易，就没有人能把它们放进区块。

EIP-2976 更新了 `eth` devp2p 线路协议，使类型化交易和收据可以在对等节点之间传输。它以通用的方式一次性完成这件事，这样后续的类型（访问列表、1559 费用交易、blob 等）就不必每次都需要新的协议版本。

## 它如何运作

- 凡是协议原来承载交易的地方，现在承载的要么是**旧式交易**（RLP 列表 `[nonce, gasPrice, gasLimit, to, value, data, v, r, s]`），要么是**类型化交易**（不透明字节 `type || payload`，类型为 `0x00`–`0x7f`）。
- 收据遵循同样的模式：旧式 RLP 列表，或 `type || receiptPayload`。
- 受影响的消息：`Transactions`、`BlockBodies`、`NewBlock`、`NewPooledTransactionIds`、`GetPooledTransactions`、`PooledTransactions` 和 `Receipts`。
- 交易标识符是类型化字节的 `keccak256`，对旧式交易则是其 RLP 的 `keccak256`。
- 协议层**并不理解**负载。每个新类型的 EIP 定义自己的负载；线路格式保持不变。

对等节点规则：

- 未知的 `TransactionType`，或对其类型无效的负载：接收方**应该**断开与发送方的连接。
- 节点**不得**在新类型的激活分叉之前发送它，并且**可以**在分叉之前很早就断开这样做的对等节点。

这些改动追溯适用于所有 `eth` 协议版本，而不是引入新版本。

## 示例代码

在线路上区分旧式交易与类型化交易（TypeScript）：

```ts
import { decodeRlp, getBytes, keccak256 } from "ethers";

function classify(raw: string) {
  const bytes = getBytes(raw);
  const first = bytes[0];
  if (first >= 0xc0) {
    // RLP list prefix: legacy transaction
    return { kind: "legacy", fields: decodeRlp(raw), hash: keccak256(bytes) };
  }
  if (first <= 0x7f) {
    // EIP-2718 envelope: type byte + opaque payload
    return { kind: "typed", type: first, hash: keccak256(bytes) };
  }
  throw new Error("invalid transaction encoding");
}
```

消息结构（伪 RLP）：

```
Transactions (0x02):        [tx_0, tx_1, ...]            ; tx = legacy list | typed bytes
BlockBodies  (0x06):        [[txList, uncleList], ...]
Receipts     (0x10):        [[receipt_0, receipt_1, ...], ...]
```

## 需要注意

- **状态：Final。** 如今使用的每一种交易类型，包括 [EIP-2930](/eip-2930/) 访问列表交易和 [EIP-1559](/eip-1559/) 费用市场交易，都按这些规则在网络上传输。
- 遇到未知类型就断开连接，可以防范伪装成"也许是未来类型"的垃圾信息。规范建议只对可信的对等节点放宽这一点。
- 后来的协议版本建立在此基础之上。例如，eth/68（[EIP-5793](/eip-5793/)）在交易公告中加入了交易类型。
- 旧式交易仍然完全受支持。


---

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