EIPs Explained

EIP-5920 · Draft · Core · 2022-03-14

EIP-5920: PAY opcode

Proposes a PAY opcode that sends ether to an address without executing any of its code, removing reentrancy, griefing and wasted gas from plain ETH transfers.

What problem does it solve?

Today a contract sends ether with CALL, which hands execution to the recipient. That has side effects nobody asked for:

  • Reentrancy: the recipient can call back into the sender. Even the 2300-gas stipend is enough to write transient storage (EIP-1153).
  • Denial of service: the recipient can revert or burn gas, so payouts can be blocked.
  • Wasted gas: running recipient code costs gas even when you only want to move value. Since EIP-7702, even EOAs can have delegated code, so "just send to an EOA" no longer guarantees no code runs.

A dedicated opcode that only moves value avoids all of this.

How it works

PAY (0xfc) pops addr then val (same order as CALL) and:

  1. Fails exceptionally inside a static call frame.
  2. Fails exceptionally if addr has any of its high 12 bytes set (not a clean 20-byte address). This keeps room for future address space extension.
  3. Charges gas, marks addr warm, and transfers val wei if the caller's balance is sufficient.
  4. Pushes 1 on success, 0 on failure. Currently only insufficient balance yields 0.

No code at addr runs.

Gas is the sum of:

  • account access: warm read cost if addr is already accessed, otherwise cold access cost (EIP-2929);
  • GAS_NEW_ACCOUNT if addr does not exist and val is non-zero;
  • GAS_CALL_VALUE if val is non-zero.

PAY relies on networks having no empty accounts (EIP-7523).

Sample code

Pseudocode of the opcode, following the spec:

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)

Things to know

  • Status is Draft. It is a Core change that needs a hard fork, and it is not part of any scheduled upgrade, so it is not available on mainnet.
  • Contracts should not rely on the invalid-address halting behaviour; it may change.
  • It does not create a new class of risk: ether can already be forced into a contract via SELFDESTRUCT (restricted by EIP-6780) or as a block's coinbase. Contracts must never assume they control their own balance.
  • Until something like PAY ships, use pull-payment patterns or reentrancy guards when sending ether.

#evm #opcode #core #ether #security

Details

Authors
Gavin John (@Pandapip1), Zainan Victor Zhou (@xinbenlv), Sam Wilson (@SamWilsn), Jochem Brouwer (@jochem-brouwer), Charles Cooper (@charles-cooper)
Status
Draft
Type
Standards Track / Core
Created
2022-03-14
Requires

Machine-readable: Markdown · JSON