---
eip: 7609
label: EIP-7609
title: Decrease base cost of TLOAD/TSTORE
status: Stagnant
type: Standards Track
category: Core
created: 2024-02-01
requires: 1153
official: https://eips.ethereum.org/EIPS/eip-7609
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7609.md
canonical: https://eips.huatuo.xyz/zh/eip-7609/
lang: zh
tldr: 一项陷入停滞的提案，旨在大幅降低瞬态存储的成本（TLOAD 5 gas，TSTORE 8 gas 外加随新 slot 增长的费用），让重入锁之类的功能几乎免费，同时让 DoS 攻击依然昂贵。
tags: [gas, evm, transient-storage, repricing, stagnant]
---

# EIP-7609: Decrease base cost of TLOAD/TSTORE

> Improve the efficiency of TLOAD/TSTORE by decreasing the base cost and introducing a superlinear pricing model.

**TL;DR:** 一项陷入停滞的提案，旨在大幅降低瞬态存储的成本（TLOAD 5 gas，TSTORE 8 gas 外加随新 slot 增长的费用），让重入锁之类的功能几乎免费，同时让 DoS 攻击依然昂贵。

## 它解决什么问题？

[EIP-1153](/eip-1153/) 引入了瞬态存储：`TSTORE` / `TLOAD` 给合约提供一个只在当前交易内存在的键值存储。它非常适合用于重入锁和在调用之间传递数据。它的定价是每次操作 100 gas，与热存储访问相同，部分是为了简单，部分是为了防范两种 DoS 担忧：分配过多的瞬态内存，以及在 revert 时回滚它的成本。

作者认为这个定价偏高。瞬态存储从不接触数据库，没有退款，每个合约在每笔交易中也只需要分配一次。如果最初的几个 slot 非常便宜，编译器甚至可以默认启用重入保护。

## 它如何运作

该提案用下面的方案取代固定价格：

- `TLOAD`：**5 gas**（在基准测试中 CPU 成本与 `MUL` 相近）。
- `TSTORE`：**8 gas**，如果该键在该合约的瞬态存储中是新的，还要加上扩展费用。扩展费用是 1 gas 乘以已经分配的 slot 数量。

因此，写入第 n 个新 slot 的成本大约是 `8 + (n - 1)` gas。在常见情形下这比现在便宜（规范估计每个合约写入的 slot 少于约 95 个），而对于试图分配海量空间的人，成本会呈二次增长。

规范的安全分析假设区块为 3000 万 gas：单个合约大约可以分配 7,739 个 slot（约 248 KB）。把分配分散到多个合约，每笔交易可达约 23,000 个 slot（约 736 KB），作者指出这比当前定价所允许的内存还要少。

## 示例代码

规范中的定价：

```python
G_LOW = 5
G_MID = 8
SLOPE = 1

def gas_tload(_key):
    return G_LOW

def gas_tstore(key, transient_mapping):
    cost = G_MID
    if key not in transient_mapping:
        cost += SLOPE * transient_mapping.size()
    return cost
```

它所针对的使用场景：瞬态重入保护（Solidity 0.8.24+）：

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

abstract contract TransientReentrancyGuard {
    bytes32 private constant SLOT = keccak256("reentrancy.guard");

    modifier nonReentrant() {
        bytes32 slot = SLOT;
        assembly {
            if tload(slot) { revert(0, 0) }
            tstore(slot, 1)
        }
        _;
        assembly { tstore(slot, 0) }
    }
}
```

现在每次 `tload`/`tstore` 花费 100 gas；在这个提案下，同样的保护大约只需要 5 + 8 + 8 gas。

## 需要注意

- 状态为 **Stagnant**：它没有被安排进任何升级，主网的瞬态存储仍然使用 EIP-1153 的定价。
- 额外费用取决于当前合约在本笔交易中已经写入了多少个 slot，所以 `TSTORE` 的 gas 会随先前的活动而变化。
- 规范中的 DoS 上限是按 3000 万 gas 限制计算的，并随之线性变化。


---

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