---
eip: 7971
label: EIP-7971
title: Hard Limits for Transient Storage
status: Draft
type: Standards Track
category: Core
created: 2025-06-12
requires: 1153
official: https://eips.ethereum.org/EIPS/eip-7971
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7971.md
canonical: https://eips.huatuo.xyz/zh/eip-7971/
lang: zh
tldr: 通过给 transient storage 设置每笔交易的硬性槽位上限，而不是按 warm storage 定价，大幅降低 TLOAD 和 TSTORE 的成本。
tags: [evm, gas, transient-storage, repricing, core]
---

# EIP-7971: Hard Limits for Transient Storage

> Decrease costs for TLOAD and TSTORE with a transaction-global limit

**TL;DR:** 通过给 transient storage 设置每笔交易的硬性槽位上限，而不是按 warm storage 定价，大幅降低 TLOAD 和 TSTORE 的成本。

## 它解决什么问题？

[EIP-1153](/eip-1153/) 引入了 transient storage：`TSTORE`/`TLOAD` 槽位只在一笔交易期间存在。它非常适合重入锁、临时授权以及在调用帧之间传递数据。但这两个操作码的成本都是 100 gas，与 warm `SLOAD` 相同，尽管 transient storage 从不接触磁盘或状态根。

在这个价格下，各语言不愿默认加入重入保护，开发者也会避免把 transient storage 用于其他正当用途。价格无法更低的主要原因是拒绝服务（DoS）风险：廉价的写入可能让一笔交易分配海量内存。

## 它如何运作

EIP-7971 用**硬性上限**取代基于价格的防护：

| 参数 | 值 | 含义 |
| --- | --- | --- |
| `GAS_TLOAD` | 5 | `TLOAD` 的成本（原为 100） |
| `GAS_TSTORE` | 12 | `TSTORE` 的基础成本（原为 100） |
| `GAS_TSTORE_ALLOCATE` | 24 | 槽位首次写入时的额外费用 |
| `MAX_TRANSIENT_SLOTS` | 131072 | 每笔交易的唯一 transient 槽位上限 |

一个计数器记录交易中**所有**合约写入过的唯一 `(contract_address, key)` 槽位。重复写入同一个槽位不会使计数增加。如果计数器超过上限，交易将异常终止。

这个上限把每笔交易的 transient 内存限制在大约 8 MB（131072 × 64 字节），因此客户端可以预先预留。规范指出，在目前 100 gas 的定价下，一笔 6000 万 gas 的交易可以分配约 38.4 MB。

## 示例代码

在本 EIP 下会便宜得多的重入保护（Solidity 0.8.28+ 支持 `transient` 状态变量）：

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

abstract contract TransientGuard {
    bool transient locked;

    modifier nonReentrant() {
        require(!locked, "reentrant");
        locked = true;   // TSTORE: 100 gas today, 12 (+24 first write) proposed
        _;
        locked = false;
    }
}
```

参考实现中的客户端规则：

```python
def tstore(ctx, address, key, value):
    ctx.charge_gas(GAS_TSTORE)
    slot = (address, key)
    if slot not in ctx.unique_slots:
        ctx.charge_gas(GAS_TSTORE_ALLOCATE)
        ctx.unique_slots.add(slot)
        if len(ctx.unique_slots) > MAX_TRANSIENT_SLOTS:
            raise ExceptionalHalt("transient storage limit exceeded")
    ctx.transient_storage[slot] = value
```

## 需要注意

- **状态：Draft。** gas 数值明确尚未最终确定，有待基准测试；作者表示应与 [EIP-8038](/eip-8038/) 保持一致。
- Glamsterdam 重新定价目录 [EIP-8007](/eip-8007/) 把本 EIP 列为该分叉中被拒绝纳入的提案。
- 如果一笔交易确实需要写入超过 131072 个唯一 transient 槽位，在此规则下会失败；典型用途只需要少数几个。
- transient storage 的其他行为均不变：值仍会在交易结束时清除，并随其调用帧一起回滚。


---

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