EIP-7971 · Draft(草案) · Core(核心) · 2025-06-12
EIP-7971: Hard Limits for Transient Storage
通过给 transient storage 设置每笔交易的硬性槽位上限,而不是按 warm storage 定价,大幅降低 TLOAD 和 TSTORE 的成本。
它解决什么问题?
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 状态变量):
// 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;
}
}
参考实现中的客户端规则:
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