EIPs Explained

EIP-7637 · Stagnant · Core · 2024-02-26

EIP-7637: Optimize EOA EXTCODEHASH

Proposed making EXTCODEHASH return 0 for any account with no code, instead of keccak256("") for code-less accounts that hold a balance, so 'codehash == 0' becomes a safe, cheap 'is there no contract here?' check.

What problem does it solve?

EIP-1052 added EXTCODEHASH, which returns the hash of an account's code more cheaply than copying it. Its rules give two different answers for "no code":

  • 0 if the account does not exist or is empty (in the EIP-161 sense: no code, zero balance, zero nonce),
  • keccak256("") = 0xc5d2…a470 if the account exists but has no code, for example an address that has received ETH.

That distinction surprises developers. Contract addresses can be predicted in advance (especially with CREATE2), and anyone can send a little ETH to an address before a contract is deployed there. Code that checks codehash == 0 to mean "not deployed yet" then silently gets a different value. As a result, careful contracts fall back to extcodesize/code.length == 0, and the gas-saving purpose of EXTCODEHASH is partly lost.

How it works

The proposal changes one rule: if an account has no code, EXTCODEHASH returns 0, regardless of balance. In the spec's execution-specs style reference implementation, after computing the hash, a result equal to keccak256("") is replaced with 0.

Account Today Proposed
Non-existent / empty 0 0
Exists, no code (e.g. funded address) keccak256("") 0
Has code keccak256(code) keccak256(code)

Sample code

The pitfall the proposal targets:

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

contract Checker {
    // Fragile today: returns false if someone sent ETH to `a` before deployment,
    // because codehash is then keccak256("") instead of 0.
    function notDeployedFragile(address a) external view returns (bool) {
        return a.codehash == bytes32(0);
    }

    // Robust today: treats both "no account" and "account without code" as not deployed.
    bytes32 constant EMPTY_CODEHASH = keccak256("");
    function notDeployed(address a) external view returns (bool) {
        bytes32 h = a.codehash;
        return h == bytes32(0) || h == EMPTY_CODEHASH;
    }
}

Under EIP-7637 both functions would give the same answer.

Things to know

  • Status is Stagnant; current mainnet behaviour still follows EIP-1052, so write checks like notDeployed above.
  • It is a consensus change to an existing opcode. Contracts that use codehash to tell "never touched" from "funded but code-less" addresses would lose that distinction, as the spec notes.
  • The spec motivates the change only in terms of balance; accounts that are non-empty for other reasons (such as a non-zero nonce) are also code-less and would likewise return 0 under the reference implementation.
  • Since EIP-7702, EOAs can carry delegation code, which is another case to consider when reasoning about EXTCODEHASH on EOAs.

#evm #opcodes #extcodehash #core

Details

Authors
Jame (@ZWJKFLC)
Status
Stagnant
Type
Standards Track / Core
Created
2024-02-26
Requires

Machine-readable: Markdown · JSON