EIPs Explained

EIP-2926 · Draft · Core · 2020-08-25

EIP-2926: Chunk-Based Code Merkleization

Splits contract bytecode into 31-byte chunks committed in a per-account trie, so stateless clients only need the chunks a transaction actually touches instead of whole contracts.

What problem does it solve?

A stateless client verifies a block using a witness: the pieces of state the block touches, plus proofs that they belong to the state root. Today a contract's code is committed as one hash, so proving even one executed instruction means shipping the whole contract (up to 24 KB, see EIP-170). The spec notes bytecode is the second-largest part of witnesses after proof hashes, and would become the largest once the trie itself gets more efficient.

EIP-2926 merkleizes code: it breaks bytecode into fixed-size chunks and commits to them in a small trie, so a witness can include just the chunks that were executed.

How it works

Chunking. Code is cut into 31-byte chunks. Each chunk is stored with a one-byte first instruction offset (FIO): where the first real instruction starts. It is non-zero only when a PUSHn from the previous chunk spills its data across the boundary (and equals 31 if the whole chunk is push data). With FIO, a client holding only some chunks can still do safe JUMPDEST analysis.

Code trie. For a non-empty contract, the account gains two fields:

  • codeSize = byte length of the code
  • codeRoot = root of a trie whose leaves are:
    • key BE(i, 4) → value BE(FIO_i, 1) || chunk_i
    • key max(u256) → version byte 0

codeHash is kept so EXTCODEHASH behaves the same. Accounts with no code omit both new fields.

Costs. Code deposit cost rises from 200 to 500 gas per byte (aligned with EIP-4762), and the rationale sketches a warming cost for reading a not-yet-accessed chunk.

Transition. After activation, new contracts use the new format immediately. Existing contracts are converted by a sweep: at the end of each block, clients convert a fixed number of accounts (ITERATOR_STRIDE, provisionally 50,000), finishing in roughly 10,000 blocks by the spec's estimate. Shared bytecode only needs chunking once.

Sample code

Chunking with first-instruction offsets (Python):

CHUNK_SIZE = 31

def chunkify(code: bytes) -> list[tuple[int, bytes]]:
    chunks, pc = [], 0
    for start in range(0, len(code), CHUNK_SIZE):
        end = start + CHUNK_SIZE
        fio = min(max(pc - start, 0), CHUNK_SIZE)   # where the first opcode begins
        chunks.append((fio, code[start:end]))
        # advance pc past every instruction that starts inside this chunk
        while pc < end and pc < len(code):
            op = code[pc]
            pc += 1 + (op - 0x5F if 0x60 <= op <= 0x7F else 0)  # PUSH1..PUSH32
    return chunks

def leaves(code: bytes):
    for i, (fio, chunk) in enumerate(chunkify(code)):
        yield i.to_bytes(4, "big"), bytes([fio]) + chunk

Things to know

  • Status: Draft. Constants such as the chunk size, ITERATOR_STRIDE and the activation time are still open, and test vectors are TBD.
  • The code trie uses the same format as the account trie. If Ethereum moves to a new state tree (EIP-6800 verkle, or EIP-7864 binary), the code trie would have to be converted along with it.
  • Contracts see no behavioural change apart from gas costs, but the state layout change requires a hard fork.
  • For EIP-7702 delegated accounts, the spec sets the code size to 23 and forwards CODESIZE / CODECOPY to the delegate.

#statelessness #state #bytecode #witness #core

Details

Authors
Sina Mahmoodi (@s1na), Alex Beregszaszi (@axic), Guillaume Ballet (@gballet), Jochem Brouwer (@jochem-brouwer), Ignacio Hagopian (@jsign)
Status
Draft
Type
Standards Track / Core
Created
2020-08-25
Requires
Required by

Machine-readable: Markdown · JSON