---
eip: 2200
label: EIP-2200
title: Structured Definitions for Net Gas Metering
status: Final
type: Standards Track
category: Core
created: 2019-07-18
official: https://eips.ethereum.org/EIPS/eip-2200
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2200.md
canonical: https://eips.huatuo.xyz/zh/eip-2200/
lang: zh
tldr: 在 Istanbul 中上线的 SSTORE 净 gas 计量规则：同一交易内对同一槽位的重复写入很便宜，恢复原值可获得退款，并设有补贴额度（stipend）检查以防范重入。
tags: [gas, sstore, storage, core, istanbul]
---

# EIP-2200: Structured Definitions for Net Gas Metering

> This is an EIP that implements net gas metering. It's a combined version of [EIP-1283] and [EIP-1706], with a structured definition so as to make it interoperable with other gas changes such as [EIP-1884].

**TL;DR:** 在 Istanbul 中上线的 SSTORE 净 gas 计量规则：同一交易内对同一槽位的重复写入很便宜，恢复原值可获得退款，并设有补贴额度（stipend）检查以防范重入。

## 它解决什么问题？

过去写入一个存储槽位每次都要花满 5,000 或 20,000 gas，即使同一笔交易中多次写同一个槽位也是如此。重入锁、计数器以及在调用帧之间传递的值，所付的费用远高于客户端实际做的工作。

[EIP-1283](/eip-1283/) 用"净 gas 计量"解决了这个问题，但在 Constantinople 中被撤下：对已经被修改过的槽位再次写入只需 200 gas，这个数额落在 Solidity 的 `transfer`/`send` 给接收方的 2,300 gas 补贴额度之内。依赖"补贴额度太小，无法改变状态"的合约可能因此变得可重入。

EIP-2200 把 EIP-1283 与 [EIP-1706](/eip-1706/) 的修复结合起来，并用命名参数重写规则，使后续的重新定价可以干净地接入。它随 Istanbul（2019 年 12 月）上线。

## 它如何运作

Istanbul 时的参数：

| 名称 | 值 |
| --- | --- |
| `SLOAD_GAS` | 800（从 200 提高，与 [EIP-1884](/eip-1884/) 一致） |
| `SSTORE_SET_GAS` | 20,000 |
| `SSTORE_RESET_GAS` | 5,000 |
| `SSTORE_CLEARS_SCHEDULE` | 15,000 |

对每次 `SSTORE`，使用槽位的*原始*值（交易开始时）、*当前*值和*新*值：

0. 如果剩余 gas ≤ 2,300（调用补贴额度），则以 out-of-gas 失败。这就是重入防护。
1. `current == new`（空操作）：收取 `SLOAD_GAS`。
2. `original == current`（首次修改）：如果 original 为 0，收取 `SSTORE_SET_GAS`，否则收取 `SSTORE_RESET_GAS`（如果 new 为 0，则退还 `SSTORE_CLEARS_SCHEDULE`）。
3. 否则槽位为 dirty 状态：收取 `SLOAD_GAS` 并调整退款：随着值变为 0 或从 0 变回，撤销或授予清除退款；如果 `new == original`，则退还 `SET_GAS − SLOAD_GAS` 或 `RESET_GAS − SLOAD_GAS`。

## 示例代码

Istanbul 的规则，用 Python 风格的伪代码表示：

```python
SLOAD_GAS, SET, RESET, CLEARS = 800, 20000, 5000, 15000
STIPEND = 2300

def sstore(gasleft, original, current, new, refund):
    if gasleft <= STIPEND:
        raise OutOfGas()
    if current == new:
        return SLOAD_GAS, refund
    if original == current:
        if original == 0:
            return SET, refund
        if new == 0:
            refund += CLEARS
        return RESET, refund
    if original != 0:
        if current == 0:
            refund -= CLEARS
        if new == 0:
            refund += CLEARS
    if original == new:
        refund += (SET - SLOAD_GAS) if original == 0 else (RESET - SLOAD_GAS)
    return SLOAD_GAS, refund
```

## 需要注意

- 状态为 **Final**，它是当今 `SSTORE` 定价的基础，但具体数值已经变化。[EIP-2929](/eip-2929/)（Berlin）加入了冷/热访问成本，[EIP-3529](/eip-3529/)（London）把 `SSTORE_CLEARS_SCHEDULE` 降到 4,800，并把退款总额上限设为已用 gas 的五分之一。
- 结构（空操作/首次/dirty，原始值与当前值）没有变化，这正是规范改用命名参数的原因。
- gas 估算必须考虑补贴额度检查：任何 `SSTORE` 都需要剩余超过 2,300 gas，即使写入本身很便宜。
- 退款在交易结束时发放，而不是在 `SSTORE` 执行时。


---

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