---
eip: 1884
label: EIP-1884
title: Repricing for trie-size-dependent opcodes
status: Final
type: Standards Track
category: Core
created: 2019-03-28
requires: 150, 1052
official: https://eips.ethereum.org/EIPS/eip-1884
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1884.md
canonical: https://eips.huatuo.xyz/zh/eip-1884/
lang: zh
tldr: 伊斯坦布尔时期的重新定价：提高 SLOAD、BALANCE 和 EXTCODEHASH 的 gas 成本，使其与读取不断增长的状态树的实际成本相符，并新增了廉价的 SELFBALANCE 操作码。
tags: [gas, opcodes, state, core, istanbul]
---

# EIP-1884: Repricing for trie-size-dependent opcodes

> This EIP proposes repricing certain opcodes, to obtain a good balance between gas expenditure and resource consumption.

**TL;DR:** 伊斯坦布尔时期的重新定价：提高 SLOAD、BALANCE 和 EXTCODEHASH 的 gas 成本，使其与读取不断增长的状态树的实际成本相符，并新增了廉价的 SELFBALANCE 操作码。

## 它解决什么问题？

随着以太坊状态的增长，读取状态树的操作码变得越来越慢，但它们的 gas 价格却没有变。来自 go-ethereum 全量同步的测量表明，存储和账户读取占据了区块处理时间的大头。`SLOAD` 每单位 gas 的耗时，已经是它在 [EIP-150](/eip-150/) 中上一次重新定价之后的两倍多。

定价过低的操作很危险：攻击者可以用它们填满区块，使区块处理变慢。它们还会让每单位 gas 的处理时间不均衡，从而限制区块 gas 上限能安全提高到多高。

## 它如何运作

在伊斯坦布尔硬分叉中激活：

| 操作码 | 之前 | 之后 |
| --- | --- | --- |
| `SLOAD`（`0x54`） | 200 | **800** |
| `BALANCE`（`0x31`） | 400 | **700** |
| `EXTCODEHASH`（`0x3f`） | 400 | **700** |
| `SELFBALANCE`（`0x47`，新增） | — | **5** |

- `BALANCE` 提高到 700，以与原本就是 700 的 `EXTCODESIZE` 一致。
- `EXTCODEHASH`（[EIP-1052](/eip-1052/)）与 `BALANCE` 执行相同的账户查找，所以定价相同，也随之上涨。
- `SELFBALANCE` 不接受参数，把当前合约的余额压入栈。检查自己的余额很便宜，因为该账户已经加载，所以不必再支付任意查找的价格。它位于 `0x47`，因为 `0x3X` 区段已满；它的成本是 5 而不是 2，因为余额可能在执行期间变化，仍然需要一次缓存查找。

## 示例代码

面向伊斯坦布尔及之后版本的 Solidity 编译器会自动把 `address(this).balance` 编译成 `SELFBALANCE`。在内联汇编中可以直接使用：

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

contract Vault {
    function myBalance() external view returns (uint256 bal) {
        assembly {
            bal := selfbalance() // 5 gas, vs. balance(address()) for an external lookup
        }
    }
}
```

这份 EIP 暴露出的回退函数陷阱：

```solidity
contract Fragile {
    uint256 public counter;
    // Called via transfer()/send(), which forward only a 2300 gas stipend.
    // An SLOAD (and SSTORE) here can exceed the stipend after a repricing -> the transfer reverts.
    receive() external payable { counter += 1; }
}
```

## 需要注意

- 状态为 **Final**。它与 [EIP-2200](/eip-2200/) 净 gas 计量等其他变更一起，随伊斯坦布尔上线。
- 对于假定 gas 成本固定的合约，这是一次破坏性变更。读取存储的回退函数，在 `transfer`/`send` 转发的 2300 补贴下可能会耗尽 gas。这是如今不再提倡这些方法、转而推荐 `call` 的主要原因。
- 规范核查了 [ERC-165](/eip-165/) 对 `supportsInterface` 的 30,000 gas 限制，认为典型实现会远低于该限制。
- 这些价格在 Berlin 中又被 [EIP-2929](/eip-2929/) 重新调整，它引入了冷/热访问定价（例如，冷 `SLOAD` 为 2100，热的为 100）。


---

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