EIPs Explained

EIP-140 · Final · Core · 2017-02-06

EIP-140: REVERT instruction

Adds the REVERT opcode, which aborts execution and rolls back state while refunding unused gas and returning an error message to the caller.

What problem does it solve?

Before the Byzantium hard fork (2017), a contract had only two ways to abort: run out of gas, or hit an invalid instruction. Both consumed all the remaining gas, even when the contract had simply checked a condition and found it false. Neither could explain why. The caller saw only "failed".

EIP-140 adds a clean way to fail: stop, undo every state change, keep the unused gas, and hand back a message.

How it works

  • New opcode REVERT at 0xfd, taking two stack items: memory_offset and memory_length.
  • Execution stops and all state changes from the current call (including logs) are rolled back.
  • The memory slice is returned to the caller as return data, the same way RETURN returns data. The caller can read it with RETURNDATASIZE / RETURNDATACOPY from EIP-211.
  • Gas cost is the same as RETURN: you pay only for memory. The remaining gas goes back to the caller.
  • The calling CALL (or CREATE / CREATE2) pushes 0, as it does for any failure. For a creation, no code is deployed.
  • If there is not enough gas for REVERT itself, or a stack underflow happens, it acts as a normal out-of-gas error and consumes all gas.

The EIP does not define what the returned bytes mean. Solidity encodes them as Error(string) for require(cond, "msg") (selector 0x08c379a0), Panic(uint256) for assertion and arithmetic failures, and ABI-encoded custom errors for revert MyError(...).

Sample code

High-level Solidity compiles to REVERT:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract Vault {
    error InsufficientBalance(uint256 have, uint256 want);
    mapping(address => uint256) public balance;

    function withdraw(uint256 amount) external {
        uint256 b = balance[msg.sender];
        if (b < amount) revert InsufficientBalance(b, amount); // REVERT with ABI-encoded error
        balance[msg.sender] = b - amount;
        payable(msg.sender).transfer(amount);
    }
}

The raw opcode in inline assembly, returning a 32-byte payload:

assembly {
    mstore(0, 0x2a)   // put 42 in memory
    revert(0, 32)     // REVERT(offset=0, length=32)
}

Decoding the reason off-chain (ethers v6):

try {
  await vault.withdraw(10n ** 18n);
} catch (e: any) {
  console.log(e.revert?.name, e.revert?.args); // "InsufficientBalance", [have, want]
}

Things to know

  • REVERT is not free. You still pay for the gas used up to that point. Only the remaining gas is refunded, unlike the old invalid-opcode approach.
  • assert failures in Solidity before 0.8.0 compiled to the invalid opcode 0xfe and used up all gas. Since 0.8.0 they use REVERT with Panic(uint256).
  • The return-data format for reasons was proposed separately in EIP-838, which remains a Draft. The Error(string) convention is defined by Solidity, not by an EIP.
  • Shipped in Byzantium together with EIP-211. See EIP-609 for the full fork list.

#evm #opcode #errors #byzantium #core

Details

Authors
Alex Beregszaszi (@axic), Nikolai Mushegian <[email protected]>
Status
Final
Type
Standards Track / Core
Created
2017-02-06
Required by

Machine-readable: Markdown · JSON