Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines EIP-140, which introduces a mechanism for stopping contract execution and returning information about an error.

This explanation translates, summarizes, and expands on the following specification.

For other ERCs, see https://cardene.dev/proposals.

Abstract

The REVERT instruction stops execution, rolls back state changes, and returns error data without consuming all remaining gas.

Overview

REVERT is an instruction used in Ethereum smart contracts, which are programs executed on the blockchain. When an error occurs, REVERT rolls back state changes made within the relevant execution context and identifies a memory region containing error data.

When REVERT executes, contract execution stops and changes made in that execution context are rolled back. Importantly, it does not consume all remaining gas simply because execution failed. It provides a way to handle failure while preserving unused gas.

This is important for smart contract safety and avoiding unnecessary gas consumption. Contracts implementing standards such as ERC-20 and ERC-721 frequently use this mechanism.

For ERC-20, see the following article.

https://chaldene.net/erc20

For ERC-721, see the following article.

https://chaldene.net/erc721

Motivation

At the time of the proposal, Ethereum smart contracts lacked a practical mechanism for deliberately reverting execution without consuming all remaining gas. Two available approaches both consumed the remaining gas.

Running Out of Gas

Execution exhausts its available gas, causing the execution context to fail and its changes to be reverted.

Executing an Invalid Instruction

Executing an invalid instruction causes an exceptional halt and rolls back the execution context.

Reverting EVM execution also discards changes, including logs. Before the proposed instruction, there was no convenient way to return the reason for aborting execution.

This matters particularly for ERC-20 and ERC-721 contracts, where security and efficiency are important.

Specification

Starting at BYZANTIUM_FORK_BLKNUM, Ethereum introduced REVERT. It stops contract execution and takes two stack arguments, memory_offset and memory_length, identifying the memory region containing error data.

REVERT behaves as follows.

Halting Execution and Rolling Back Changes

Execution stops, and changes made within the reverted execution context are rolled back.

Returning Error Data

Data from the specified memory region is returned to the caller, similarly to the RETURN instruction.

Cost

REVERT has the same cost as RETURN. Rolling back state does not itself consume all remaining gas; the instruction incurs the applicable memory expansion cost.

Insufficient Gas

If there is insufficient gas to execute REVERT or a stack underflow occurs, it behaves like an ordinary out-of-gas exception, consuming the remaining gas available to that execution context.

Use with CREATE and CREATE2

When contract initialization executes REVERT in a CREATE or CREATE2 operation, no code is deployed, zero is pushed onto the stack, and the error data is available to the caller.

The contents of the returned memory region are not defined by this EIP and may be specified separately. This mechanism supports safer and more efficient contracts, including implementations of ERC-20 and ERC-721.

Backward Compatibility

The change does not affect previously deployed contracts unless they use 0xfd as an instruction.

Tests

text
6c726576657274656420646174616000557f726576657274206d657373616765000000000000000000000000000000000000600052600e6000fd

The example contains EVM bytecode.

Returning REVERT Data

The code executes REVERT, stops execution, and returns the expected error data, 0x726576657274206d657373616765, from the specified memory location.

Storage Remains Unchanged

After execution, storage at key 0x0 remains unset because REVERT rolls back the attempted state change.

Gas Consumption

The example requires 20024 gas under the historical gas schedule used in the test. Gas measures computational resources consumed by Ethereum execution.

This bytecode illustrates low-level contract behavior. It is not itself an implementation of a token standard such as ERC-20 or ERC-721.

Citation

Alex Beregszaszi (@axic), Nikolai Mushegian nikolai@nexusdev.us, "EIP-140: REVERT instruction," Ethereum Improvement Proposals, no. 140, February 2017. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-140.

Conclusion

EIP-140 introduces REVERT, enabling contracts to stop execution, roll back state changes, and return error data without necessarily consuming all remaining gas.

Further reading and author links: