---
eip: 5920
label: EIP-5920
title: PAY opcode
status: Draft
type: Standards Track
category: Core
created: 2022-03-14
requires: 214, 2929, 7523
official: https://eips.ethereum.org/EIPS/eip-5920
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-5920.md
canonical: https://eips.huatuo.xyz/zh/eip-5920/
lang: zh
tldr: 提出一个 PAY opcode，向某个地址发送 ether 而不执行其任何代码，从而消除普通 ETH 转账中的重入、恶意捣乱（griefing）和浪费的 gas。
tags: [evm, opcode, core, ether, security]
---

# EIP-5920: PAY opcode

> Introduces a new opcode, PAY, to send ether to an address without calling any of its functions

**TL;DR:** 提出一个 PAY opcode，向某个地址发送 ether 而不执行其任何代码，从而消除普通 ETH 转账中的重入、恶意捣乱（griefing）和浪费的 gas。

## 它解决什么问题？

目前，合约通过 `CALL` 发送 ether，而这会把执行权交给接收方。这带来了没人想要的副作用：

- **重入**：接收方可以回调发送方。即便是 2300 gas 的津贴，也足以写入 transient storage（[EIP-1153](/eip-1153/)）。
- **拒绝服务**：接收方可以 revert 或耗尽 gas，因此付款可能被阻断。
- **浪费 gas**：即使你只想转移价值，运行接收方的代码也要花费 gas。自 [EIP-7702](/eip-7702/) 以来，即使是 EOA 也可以有被委托的代码，所以"只是发给 EOA"已不再能保证不会运行任何代码。

一个只转移价值的专用 opcode 可以避免这一切。

## 它如何运作

`PAY`（`0xfc`）依次弹出 `addr` 和 `val`（与 `CALL` 顺序相同），并且：

1. 在静态调用帧内会异常失败。
2. 如果 `addr` 的高 12 个字节中有任何一位被设置（不是干净的 20 字节地址），则异常失败。这为将来扩展地址空间留出了余地。
3. 扣除 gas，把 `addr` 标记为 warm，并在调用者余额足够时转移 `val` wei。
4. 成功时压入 `1`，失败时压入 `0`。目前只有余额不足才会得到 `0`。

`addr` 处的代码不会运行。

gas 是以下各项之和：

- 账户访问：如果 `addr` 已被访问过，则为 warm 读取成本，否则为 cold 访问成本（[EIP-2929](/eip-2929/)）；
- 如果 `addr` 不存在且 `val` 非零，则加上 `GAS_NEW_ACCOUNT`；
- 如果 `val` 非零，则加上 `GAS_CALL_VALUE`。

`PAY` 依赖于网络中不存在空账户（[EIP-7523](/eip-7523/)）。

## 示例代码

该 opcode 的伪代码，遵循规范：

```python
def op_pay(evm):
    if evm.message.is_static:
        raise ExceptionalHalt
    addr = evm.stack.pop()
    val = evm.stack.pop()
    if addr >> 160 != 0:
        raise ExceptionalHalt               # high 12 bytes must be zero
    addr = to_address(addr)

    cost = WARM_STORAGE_READ_COST if addr in evm.accessed_addresses else COLD_ACCOUNT_ACCESS_COST
    if val != 0 and not account_exists(addr):
        cost += GAS_NEW_ACCOUNT
    if val != 0:
        cost += GAS_CALL_VALUE
    charge_gas(evm, cost)
    evm.accessed_addresses.add(addr)

    if balance(evm.message.current_target) >= val:
        move_ether(evm.message.current_target, addr, val)   # no code executed
        evm.stack.push(1)
    else:
        evm.stack.push(0)
```

## 需要注意

- 状态为 **Draft**。这是一项需要硬分叉的 Core 变更，也不在任何已排期的升级之内，因此主网上不可用。
- 合约不应依赖对无效地址的中止行为；它可能会改变。
- 它不会带来新的一类风险：ether 本来就可以通过 `SELFDESTRUCT`（受 [EIP-6780](/eip-6780/) 限制）或作为区块的 coinbase 被强行塞入合约。合约绝不能假设自己能控制自己的余额。
- 在类似 `PAY` 的机制上线之前，发送 ether 时请使用拉取式付款模式或重入保护。


---

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