EIPs Explained

EIP-3860 · Final · Core · 2021-07-16

EIP-3860: Limit and meter initcode

Caps contract initcode at 49,152 bytes and charges 2 gas per 32-byte word of it, so the work clients do analysing deployment code is bounded and paid for.

What problem does it solve?

When a contract is created, the EVM runs its initcode (constructor plus the code to be deployed). Before running it, a client must do jumpdest analysis over the whole initcode, work that scales with its length. Before this EIP that work was neither limited nor metered:

  • A create transaction paid calldata gas for initcode, but CREATE and CREATE2 called from a contract paid nothing proportional to initcode size (except CREATE2's hashing cost).
  • There was no protocol cap on initcode size, which made worst-case analysis hard and complicated several other EVM proposals.

EIP-3860 adds both a size limit and a per-word charge. It went live in the Shanghai upgrade (2023).

How it works

Two parameters:

Constant Value
MAX_INITCODE_SIZE 2 * MAX_CODE_SIZE = 49152 bytes (EIP-170 sets MAX_CODE_SIZE = 24576)
INITCODE_WORD_COST 2 gas per 32-byte word

initcode_cost(initcode) = 2 * ceil(len(initcode) / 32)

Rules:

  1. Create transactions with initcode larger than MAX_INITCODE_SIZE are invalid and cannot be included in a block.
  2. Create transactions add initcode_cost to their intrinsic gas, so a transaction that cannot cover it is invalid.
  3. CREATE / CREATE2 with initcode over the limit abort exceptionally, as if out of gas.
  4. CREATE / CREATE2 charge initcode_cost up front, before computing the new address and running the initcode. For CREATE2 this sits alongside its existing 6 gas/word hashing cost (EIP-1014), so it now pays 8 gas per word in total.

The limit also means code size, PC and jump offsets all fit in 16 bits, a simplification for EVM implementations.

Sample code

The extra cost and limit check, as a client would compute them:

MAX_CODE_SIZE = 24576
MAX_INITCODE_SIZE = 2 * MAX_CODE_SIZE   # 49152
INITCODE_WORD_COST = 2

def initcode_cost(initcode: bytes) -> int:
    return INITCODE_WORD_COST * ((len(initcode) + 31) // 32)

def check_create(initcode: bytes) -> int:
    if len(initcode) > MAX_INITCODE_SIZE:
        raise Exception("exceptional abort: initcode too large")
    return initcode_cost(initcode)

print(initcode_cost(b"\x00" * 49152))  # 3072 gas at the maximum size

A pre-deployment check in a script (ethers v6):

import { ContractFactory, getBytes } from "ethers";

const tx = await factory.getDeployTransaction(...args); // factory: ContractFactory
const size = getBytes(tx.data!).length;
if (size > 49152) throw new Error(`initcode is ${size} bytes; limit is 49152`);

Things to know

  • Status: Final, active on mainnet since Shanghai.
  • For a normal deployment the limit leaves room for a full 24 KB runtime contract plus an equally large constructor. Factories that embed several contracts' bytecode in one initcode are the most likely to hit it.
  • An oversized create transaction is rejected outright; an oversized CREATE/CREATE2 inside execution only fails that call (consuming its gas), and the outer transaction can still be included.
  • The cost for create transactions is tiny next to calldata pricing (4/16 gas per byte); it was included mainly for consistency and future extensibility.
  • Already deployed contracts are unaffected.

#evm #gas #contract-creation #initcode #core #shanghai

Details

Authors
Martin Holst Swende (@holiman), Paweł Bylica (@chfast), Alex Beregszaszi (@axic), Andrei Maiboroda (@gumb0)
Status
Final
Type
Standards Track / Core
Created
2021-07-16
Requires
Required by

Machine-readable: Markdown · JSON