---
eip: 1681
label: EIP-1681
title: Temporal Replay Protection
status: Stagnant
type: Standards Track
category: Core
created: 2019-01-08
official: https://eips.ethereum.org/EIPS/eip-1681
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-1681.md
canonical: https://eips.huatuo.xyz/zh/eip-1681/
lang: zh
tldr: 提议为交易增加 `valid-until` 时间戳，使得在选定时间之前未被打包的交易，以后永远不会被挖出或被重放。
tags: [transactions, replay-protection, expiry, mempool, core]
---

# EIP-1681: Temporal Replay Protection

> This EIP proposes adding a 'temporal' replay protection to transactions, in the form of a valid-until timestamp. This EIP is very similar to https://github.

**TL;DR:** 提议为交易增加 `valid-until` 时间戳，使得在选定时间之前未被打包的交易，以后永远不会被挖出或被重放。

## 它解决什么问题？

以太坊交易一旦签名，在其 nonce 被使用之前一直有效。这带来了几个麻烦：

- 在繁忙时期（规范提到了 ICO），交易可能在内存池中停留数天，然后在用户早已不再需要它之后才执行，并且仍然要消耗 gas。
- 节点对于何时丢弃过时的待处理交易没有共同的规则。
- 一些状态清理的想法，例如清除空的"灰尘"账户，会重置 nonce，使旧交易可以被重放。通过修改 nonce 规则来防止这种情况，改动太大。

EIP-1681 增加了一个简单的过期机制：签名者声明"仅在时间 T 之前有效"。

## 它如何运作

推出分为两个阶段：

1. **硬分叉 X：** 交易获得一个可选的 `valid-until` 字段，与 `nonce` 一样是 `uint64`。如果存在，该交易只能被打包进 `timestamp < valid-until` 的区块。
2. **软分叉 Y：** `valid-until` 变为必填；没有它的交易无效。

为什么使用挂钟时间而不是区块号（早先的提案使用的是区块号）：

- 即使在离线的机器上，签名者也总是知道当前的 UTC 时间，
- 区块号与时间的对应关系会漂移（算力、难度炸弹），并且在测试网上毫无意义，
- "有效两小时"比"有效 514 个区块"更容易让人选择。

没有设置上限，所以用户仍然可以签署一笔在遥远的未来仍然有效的交易。

## 示例代码

打包检查和扩展后的旧版 RLP 的伪代码：

```python
# legacy transaction with the optional extra field
tx = rlp.encode([
    nonce, gas_price, gas_limit, to, value, data,
    valid_until,              # uint64 unix timestamp, new
    v, r, s,
])

def can_include(tx, block):
    if tx.valid_until is None:
        return block.number < FORK_Y   # mandatory after phase Y
    return block.timestamp < tx.valid_until
```

钱包可以这样设置它：

```ts
const validUntil = Math.floor(Date.now() / 1000) + 2 * 60 * 60; // two hours from now
```

## 需要注意

- 状态为 **Stagnant**。以太坊交易至今仍没有协议层面的过期机制；工具依靠替换 nonce 来取消卡住的交易。
- 上面 RLP 中的字段位置只是示意；规范没有固定编码方式。自 [EIP-2718](/eip-2718/) 类型化交易以来，这样的字段更自然地应放在新的交易类型中。[EIP-2711](/eip-2711/)（已撤回）就提出过一种带过期时间的交易类型。
- 阶段 Y 会使保存在纸质备份或礼品卡上的预签名交易失效，这是规范指出的主要安全权衡。
- 许多智能合约自行实现了截止时间参数（例如 DEX 兑换），以便在应用层获得同样的效果。


---

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