Skip to main content
This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.

Overview

Opcode: 0xfd Introduced: Byzantium (EIP-140) REVERT halts execution, reverts all state changes in the current execution context, and returns error data to the caller. Unlike pre-Byzantium failures, REVERT refunds remaining gas and provides error information. This enables graceful failure handling with gas efficiency and informative error messages.

Specification

Stack Input:
Stack Output: None Gas Cost: Memory expansion cost (dynamic) Operation:

Behavior

REVERT terminates execution with error:
  1. Pops offset from stack (top)
  2. Pops length from stack (second)
  3. Validates offset and length fit in u32
  4. Charges gas for memory expansion to offset+length
  5. Copies length bytes from memory[offset] to output buffer
  6. Reverts all state changes (storage, logs, balance transfers)
  7. Sets execution state to reverted
  8. Returns control to caller with failure status
  9. Refunds remaining gas to transaction
State Effects:
  • All state changes reverted in current call context
  • Error data available to caller
  • Remaining gas refunded (not consumed like INVALID)
  • Execution marked as failed
Hardfork Requirement:
  • Byzantium or later: REVERT available
  • Pre-Byzantium: REVERT triggers InvalidOpcode error

Examples

Basic Revert

Require Statement

Compiled to (simplified):

Custom Error (Solidity 0.8.4+)

Compiled to:

Empty Revert

Pre-Byzantium Error

Gas Cost

Cost: Memory expansion cost (dynamic) + remaining gas refunded Memory Expansion Formula:
Key Difference from INVALID:
  • REVERT: Refunds remaining gas
  • INVALID (0xfe): Consumes all gas
Example:

Edge Cases

Zero Length Revert

Large Error Data

Out of Bounds

Stack Underflow

State Reversion

Common Usage

Input Validation

Each require compiles to conditional REVERT.

Access Control

Business Logic Checks

Custom Errors (Gas Efficient)

Custom errors are more gas efficient than string messages:
  • String: ~50 gas per character
  • Custom error: ~4 gas (function selector) + parameter encoding

Implementation

Testing

Test Coverage

Security

REVERT vs INVALID

REVERT (0xfd):
  • Refunds remaining gas
  • Returns error data
  • Graceful failure
  • Use for: validation, business logic, access control
INVALID (0xfe):
  • Consumes all gas
  • No error data
  • Hard failure
  • Use for: should-never-happen, invariant violations
Example:

Gas Refund Implications

REVERT refunds gas - important for: Nested calls:
Gas griefing prevention:

Error Data Validation

Caller must validate error data:
Safe pattern:

State Reversion Scope

REVERT only reverts current call context:

Reentrancy

REVERT doesn’t prevent reentrancy:
Safe: Update state before external calls (Checks-Effects-Interactions).

Compiler Behavior

Require Statements

Compiles to:

Custom Errors

Compiles to:

Try-Catch

Caller receives REVERT data and decodes based on error type.

Hardfork History

Pre-Byzantium (Frontier, Homestead, Tangerine Whistle, Spurious Dragon)

No REVERT opcode:
  • Only INVALID (0xfe) for reverting
  • Consumed all gas
  • No error data
  • Poor UX

Byzantium (EIP-140)

REVERT introduced:
  • Opcode 0xfd
  • Refunds remaining gas
  • Returns error data
  • Graceful failure handling
Impact:
  • Better error messages
  • Gas efficiency
  • Improved debugging
  • Custom errors possible

Post-Byzantium (Constantinople → Cancun)

No changes to REVERT:
  • Behavior stable since Byzantium
  • Foundation for Solidity 0.8.4+ custom errors
  • Essential for modern error handling

References