Skip to main content

StateRoot

32-byte Merkle Patricia Trie root hash representing global Ethereum state.

Overview

StateRoot represents the root hash of the Ethereum state trie. It uniquely identifies the entire global state at a given block - all account balances, nonces, contract code, and storage. Every block header includes a state root.

Type Definition (Zig)

const primitives = @import("primitives");
const Hash = primitives.Hash.Hash; // 32-byte [32]u8

Usage

Create StateRoot

const root = try primitives.Hash.fromHex(
    "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
);

Convert to Hex

const hex = primitives.Hash.toHex(root);

Compare State Roots

const is_equal = primitives.Hash.equal(root1, root2);

API Reference

Constructors

  • Hash.fromHex(hex) !Hash – Create from hex string

Methods

FunctionDescription
  • Hash.toHex(root) – Convert to hex string
  • Hash.equal(a, b) – Equality check

Constants

ConstantValueDescription
SIZE32State root size in bytes

State Trie Structure

The Ethereum state is a Merkle Patricia Trie mapping addresses to account states:
State Root (32 bytes)
    |
    v
Merkle Patricia Trie
    |
    +-- Address A --> Account State A
    |                   - nonce
    |                   - balance
    |                   - codeHash
    |                   - storageRoot
    |
    +-- Address B --> Account State B
    ...

Block Header Context

Every block header contains a state root:
// Block header fields include stateRoot (Hash)

Empty State Root

The empty state root (no accounts) has a known value:
// Empty trie root = keccak256(RLP([]))
const EMPTY_STATE_ROOT = try primitives.Hash.fromHex(
    "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
);

Use Cases

Verify State Against Header

// {"method":"eth_getBlockByNumber","params":["latest",false]} returns stateRoot
// {"method":"eth_getProof","params":["0xADDRESS",[],"latest"]}

Track State Changes

// Compare state roots between two blocks by hex equality

Light Client Sync

// Light client verifies proofs against stateRoot from trusted headers.

Specification

Per the Yellow Paper, the state trie encodes mappings from addresses (160-bit identifiers) to account states. The state root is a 32-byte Keccak-256 hash of the root node of the state trie.

See Also