---
eip: 658
label: EIP-658
title: Embedding transaction status code in receipts
status: Final
type: Standards Track
category: Core
created: 2017-06-30
requires: 140
official: https://eips.ethereum.org/EIPS/eip-658
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-658.md
canonical: https://eips.huatuo.xyz/zh/eip-658/
lang: zh
tldr: 用状态码（1 = 成功，0 = 失败）取代交易收据中的中间状态根，使每个客户端都能可靠地判断一笔交易是否成功。
tags: [receipts, transactions, core, byzantium]
---

# EIP-658: Embedding transaction status code in receipts

> This EIP replaces the intermediate state root field of the receipt with a status code indicating if the top-level call succeeded or failed.

**TL;DR:** 用状态码（1 = 成功，0 = 失败）取代交易收据中的中间状态根，使每个客户端都能可靠地判断一笔交易是否成功。

## 它解决什么问题？

在 Byzantium 之前，一种常见的经验判断是"如果交易用光了所有 gas，就说明失败了"。[EIP-140](/eip-140/) 增加了 `REVERT` 操作码，它会撤销状态更改，**并退还剩余的 gas**。此后，失败的交易可能看起来像一笔廉价的成功交易。

全节点可以重放交易来查明发生了什么。快速同步的节点只能对近期区块这样做，轻客户端则完全做不到。钱包和 dapp 需要把结果记录在共识数据中。

## 它如何运作

过去每份收据都包含一个**中间状态根**：该交易执行完毕后的状态树根。这个字段此前已被早先的提案 EIP-98 废弃。

从 Byzantium 分叉起，该字段改为存放**状态码**：

- `1` 表示顶层调用成功，其状态更改已被应用。
- `0` 表示因任何会导致交易回滚的原因而失败，例如 `REVERT`、gas 耗尽或无效操作码。

收据仍然包含累计 gas 使用量、日志布隆过滤器和日志，因此变更仅涉及一个字段。由于收据已提交到区块头的收据根中，轻客户端可以用默克尔证明来验证状态。

## 示例代码

RLP 形式的收据，变更前后对比：

```
pre-Byzantium:  [post_state_root (32 bytes), cumulative_gas_used, logs_bloom, logs]
post-Byzantium: [status (0x01 or empty for 0), cumulative_gas_used, logs_bloom, logs]
```

检查状态（ethers v6）：

```ts
import { JsonRpcProvider } from "ethers";

const provider = new JsonRpcProvider(process.env.RPC_URL);
const receipt = await provider.getTransactionReceipt(txHash);

if (receipt?.status === 1) {
  console.log("success");
} else if (receipt?.status === 0) {
  console.log("reverted");
}
```

在 JSON-RPC 中，`eth_getTransactionReceipt` 返回 `"status": "0x1"` 或 `"0x0"`。Byzantium 之前的收据则返回 `root` 字段。

## 需要注意

- 状态只描述**顶层**调用。即使内部调用失败，只要合约捕获了该失败，交易仍可能成功。
- 状态为 `0` 仍然意味着发送方为已用的 gas 付了费，nonce 也已被消耗。
- 状态码不包含回滚原因或返回数据。请通过 `eth_call` 或追踪来读取这些信息。[EIP-211](/eip-211/) 使返回数据在 EVM 内部可用。
- 类型化交易（[EIP-2718](/eip-2718/)）在其收据中保留状态字段。
- 随 Byzantium（[EIP-609](/eip-609/)）发布。


---

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