---
eip: 1153
label: EIP-1153
title: Transient storage opcodes
status: Final
type: Standards Track
category: Core
created: 2018-06-15
requires: 2200, 3529
official: https://eips.ethereum.org/EIPS/eip-1153
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1153.md
canonical: https://eips.huatuo.xyz/zh/eip-1153/
lang: zh
tldr: 新增 TLOAD 和 TSTORE，一个类似存储的键值空间，在每笔交易结束时清空，使重入锁和交易内回调变得低成本。
tags: [evm, opcodes, storage, gas, cancun]
---

# EIP-1153: Transient storage opcodes

> Add opcodes for manipulating state that behaves almost identically to storage but is discarded after every transaction

**TL;DR:** 新增 TLOAD 和 TSTORE，一个类似存储的键值空间，在每笔交易结束时清空，使重入锁和交易内回调变得低成本。

## 它解决什么问题？

一笔交易可以经过许多嵌套调用，同一个合约也可能被多次重入。这些调用帧常常需要共享一点状态：重入锁要知道“我是否已经在这个函数里了”，闪电兑换式的回调要知道“调用者到目前为止欠我多少”。

在这份 EIP 之前，唯一的共享位置是普通存储（`SSTORE`/`SLOAD`）。它很昂贵，而且自 [EIP-3529](/eip-3529/) 起，清除存储槽的退款被限制为交易 gas 的 20 %，所以“先设置再重置”的模式很难把成本收回来。当调用链中间有不受信任的合约时，通过 calldata 传值也行不通。

## 它如何运作

两个新操作码：

| 操作码 | 字节 | 栈 | Gas |
| --- | --- | --- | --- |
| `TLOAD` | `0x5c` | `key → value` | 100（与热的 `SLOAD` 相同） |
| `TSTORE` | `0x5d` | `key, value →` | 100（与对脏槽位的热 `SSTORE` 相同） |

- 寻址方式与存储相同：每个合约都有自己的从 32 字节键到 32 字节值的映射。
- **一切都会在交易结束时被丢弃。** 它从不接触磁盘，因此没有冷/热访问的意外开销，也没有退款。
- 与存储一样，它对合约是私有的。在 `DELEGATECALL`/`CALLCODE` 下使用调用者的瞬态存储；在 `CALL`/`STATICCALL` 下使用被调用者的。
- 如果某个调用帧回滚，它的瞬态写入（包括内部调用的写入）也会一并回滚。
- 在 `STATICCALL` 中执行 `TSTORE` 会抛出异常；`TLOAD` 是允许的。
- 与 `SSTORE` 不同，`TSTORE` 没有 [EIP-2200](/eip-2200/) 中“仅剩 2,300 gas 津贴时失败”的规则。

规范列出的用例包括重入锁、单笔交易内的 ERC-20 授权、在回调结束时检查余额是否已结清的“till”模式，以及无需 calldata 就向代理传递元数据。

## 示例代码

使用瞬态存储的重入保护（Solidity 内联汇编，0.8.24 或更高版本）：

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

abstract contract TransientReentrancyGuard {
    // Any fixed slot works; it is cleared automatically after the tx.
    bytes32 private constant LOCK = keccak256("example.reentrancy.lock");

    modifier nonReentrant() {
        bytes32 slot = LOCK;
        assembly {
            if tload(slot) { revert(0, 0) }
            tstore(slot, 1)
        }
        _;
        assembly {
            tstore(slot, 0) // reset so later calls in the same tx work
        }
    }
}

contract Vault is TransientReentrancyGuard {
    mapping(address => uint256) public balance;

    function withdraw(uint256 amount) external nonReentrant {
        balance[msg.sender] -= amount;
        (bool ok, ) = msg.sender.call{value: amount}("");
        require(ok);
    }
}
```

## 需要注意

- 状态为 **Final**，自 Cancun 升级（Dencun）起已在以太坊主网上线，同时上线的还有 `MCOPY`（[EIP-5656](/eip-5656/)）等其他 EVM 改动。
- “瞬态（transient）”指的是按**交易**，而不是按调用。值在同一笔交易内的多次调用之间会保留，所以对你依赖的槽位（例如锁）要主动重置，而不要假定下一次调用时它从零开始。
- 不要把它当作内存的替代品：内存在调用返回时被清除，而瞬态存储不会，这可能在重入时造成意外行为。
- 较新的 Solidity 版本也为状态变量提供了 `transient` 关键字；依赖它之前请检查编译器版本和 EVM 目标。
- [EIP-7609](/eip-7609/) 曾提议进一步降低这些操作码的成本，但状态为 Stagnant。


---

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