---
eip: 2031
label: EIP-2031
title: State Rent B - Net transaction counter
status: Stagnant
type: Standards Track
category: Core
created: 2019-05-15
requires: 2029
official: https://eips.ethereum.org/EIPS/eip-2031
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-2031.md
canonical: https://eips.huatuo.xyz/zh/eip-2031/
lang: zh
tldr: State Rent 计划的一部分：在系统合约中维护全局交易计数，使被租金机制驱逐、之后又被唤醒的账户能获得一个新的、可防重放的 nonce。
tags: [state-rent, nonce, replay-protection, core, state]
---

# EIP-2031: State Rent B - Net transaction counter

> Ethereum starts to track the number of transactions inside its state (for now, only number of transactions after this change is introduced, therefore it is called Net transaction count). It is done by incrementing a storage slot in the special contract, called State counter contract (EIP-2029).

**TL;DR:** State Rent 计划的一部分：在系统合约中维护全局交易计数，使被租金机制驱逐、之后又被唤醒的账户能获得一个新的、可防重放的 nonce。

## 它解决什么问题？

2019 年前后，以太坊研究者探索了**状态租金（state rent）**：账户需要为其占用的状态随时间付费，停止付费的账户可能被驱逐。这就带来了重放问题。如果被驱逐的账户因有人向它发送 ether 而复活，它会重新从 nonce 0 开始。它过去用 nonce 0、1、2…… 签名的每一笔交易都可能被重放。

EIP-2031（"State Rent B"）提供了一种方法，选出一个保证高于该账户以往用过的任何 nonce 的起始 nonce。

## 它如何运作

- [EIP-2029](/eip-2029/)（"State Rent A"）引入了一个特殊的**状态计数器合约**，其存储中保存全局计数器。
- EIP-2031 在该合约的**存储槽 0** 中加入 `txCount`。以 32 个零字节作为输入调用该合约即可读取。
- 从分叉区块 B（或计数器合约部署时，以较早者为准）起，`txCount` 在**每一笔**交易之后递增。即使交易本身失败，这些更新也永远不会被回滚。
- 由于它只统计激活之后的交易，因此被称为*净*交易计数器。
- 该计数器随后用于设置新创建的非合约账户（包括被重新创建的已驱逐账户）的 nonce。任何账户发送的交易数都不可能超过全局总数，因此取自 `txCount` 的 nonce 不会与旧 nonce 冲突。

规范还考虑了两个替代方案并否决了它们：

1. 在交易上加 `valid-until` 字段，这样 nonce 可保持为 0，但与用 nonce 推导 `CREATE` 合约地址的做法冲突。
2. 由区块号推导 nonce，这需要对每个区块的交易数设定一个任意上限。

## 示例代码

状态转换的伪代码：

```python
STATE_COUNTER_CONTRACT = ...   # defined by EIP-2029
TX_COUNT_SLOT = 0

def apply_transaction(state, tx):
    result = execute(state, tx)           # may succeed or fail
    n = state.get_storage(STATE_COUNTER_CONTRACT, TX_COUNT_SLOT)
    state.set_storage(STATE_COUNTER_CONTRACT, TX_COUNT_SLOT, n + 1)  # never reverted
    return result

def create_account(state, address):
    state.accounts[address] = Account(
        nonce=state.get_storage(STATE_COUNTER_CONTRACT, TX_COUNT_SLOT),
        balance=0,
    )
```

## 需要注意

- **状态：Stagnant**，与 State Rent 系列的其他提案（[EIP-2029](/eip-2029/)、[EIP-2027](/eip-2027/)、[EIP-2026](/eip-2026/)）一样。
- 它需要一次硬分叉。区块号 B 从未确定。
- 状态租金没有在以太坊上被采纳。对状态增长的研究转向了无状态（statelessness）和状态过期（state expiry）等其他方向。
- 即使重新启用，该规范也只是初步的：测试用例和参考实现仍被列为未来工作。


---

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