---
eip: 2930
label: EIP-2930
title: Optional access lists
status: Final
type: Standards Track
category: Core
created: 2020-08-29
requires: 2718, 2929
official: https://eips.ethereum.org/EIPS/eip-2930
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2930.md
canonical: https://eips.huatuo.xyz/zh/eip-2930/
lang: zh
tldr: 引入了带访问列表的 type-1 交易：交易提前声明将要访问的地址和存储槽，并以小幅折扣预先付费，之后的访问即为热访问。
tags: [transactions, access-list, gas, core, berlin]
---

# EIP-2930: Optional access lists

> Adds a transaction type which contains an access list, a list of addresses and storage keys that the transaction plans to access. Accesses outside the list are possible, but become more expensive.

**TL;DR:** 引入了带访问列表的 type-1 交易：交易提前声明将要访问的地址和存储槽，并以小幅折扣预先付费，之后的访问即为热访问。

## 它解决什么问题？

[EIP-2929](/eip-2929/) 与本 EIP 一同在 Berlin 升级（2021 年）中上线，它让交易中对账户或存储槽的*首次*访问贵了很多（“冷”），而重复访问变得便宜（“热”）。这修复了拒绝服务风险，但威胁到了那些使用硬编码 gas 津贴发起调用的合约：冷访问现在可能让它们耗尽 gas。

EIP-2930 让交易可以提前声明自己将触及哪些账户和槽，并预先付费。执行期间，这些已经是热的，只需 100 gas。它还引入了访问列表格式本身，后来的特性也复用了它。

## 它如何运作

一种新的 [EIP-2718](/eip-2718/) 交易类型 `0x01`：

```
0x01 || rlp([chainId, nonce, gasPrice, gasLimit, to, value, data,
             accessList, signatureYParity, signatureR, signatureS])
```

- `accessList` 是一组 `[address, [storageKey, ...]]` 对。
- 执行开始时，在正常的 intrinsic gas 之外，交易还要支付**每个地址 2,400 gas** 和**每个存储键 1,900 gas**。这些会被加入热集合，因此交易内的访问只需 100，而不是冷价格（账户 2,600，槽 2,100）。
- 不在列表中的访问仍然有效，只是按冷价格付费。
- 允许重复，但每次都会收费，所以没有理由包含重复项。
- 签名覆盖类型字节，因此 type-1 交易不会被重新解释为其他类型。`chainId` 是一等字段。

[EIP-1559](/eip-1559/) 的 type-2 交易也带有同样的 `accessList` 字段。

## 示例代码

访问列表的 JSON 形式，`eth_sendTransaction` 和 `eth_createAccessList` 使用它：

```json
[
  {
    "address": "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae",
    "storageKeys": [
      "0x0000000000000000000000000000000000000000000000000000000000000003",
      "0x0000000000000000000000000000000000000000000000000000000000000007"
    ]
  }
]
```

用 ethers v6 发送 type-1 交易：

```ts
import { JsonRpcProvider, Wallet } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const wallet = new Wallet(process.env.PRIVATE_KEY!, provider);

const tx = await wallet.sendTransaction({
  type: 1,
  to: contractAddress,
  data: calldata,
  accessList: [
    { address: contractAddress, storageKeys: ["0x" + "00".repeat(31) + "03"] },
  ],
});
await tx.wait();
```

## 需要注意

- 折扣很小（与冷访问相比约 10 %），所以只有在确定会触及这些槽时，访问列表才划算。列出一个永远不会被访问的项只会浪费 gas。
- 许多节点提供 `eth_createAccessList`，用来模拟交易并建议一个列表。
- 访问列表按项收费，而不是按字节，且按字节算比 calldata 更贵，所以不能用它低成本地撑大区块。
- 状态为 **Final**，在 Berlin 中激活。[EIP-7928](/eip-7928/) 中的区块级访问列表是另一个更晚出现的想法。


---

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