Skip to content

Block changes and incremental state root - #28

Merged
sbillig merged 8 commits into
feat/chainfrom
feat/block-changes
Nov 16, 2026
Merged

sbillig merged 8 commits into
feat/chainfrom
feat/block-changes

Conversation

@sbillig

@sbillig sbillig commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

Chain::import cloned the world to undo an invalid block and computed the state root from the whole world, so each block cost time in proportion to the state. Both now cost in proportion to what the block changed.

  • BlockChanges: what a block changed, with what each value held before it: each account's existence, balance, nonce and code, each storage slot, and the BLOCKHASH entries begin wrote. It is derived when a transaction, a system call or the withdrawals commit, in the journal traversal that also feeds the access list; nothing is tracked per write. Block::finish gives it in BlockOutput, and undo(world) puts the world back. Block::abandon and a failing finish undo the block.
  • StateTrie: the state trie kept in memory with each node's hash. reread_account and reread_slot reread one key from the world, and root rehashes only the changed paths. The trie depends only on what it holds, so restoring the world and rereading the same keys undoes a block.
  • Chain holds a StateTrie, rereads a block's changes into it, and for a rejected block undoes the world and the trie. state_root(world) stays as the one-pass function.

Notes for review:

  • Changes are recorded only inside a Block.
  • The access list is now noted before settlement, where the change log has to run. For a self-destructed account it skips the nonce, code and storage notes, which settlement clears back to what they were.
  • An allocation failure leaves the trie failed, and Chain rebuilds it from the world before the next block.
  • The trie keeps its own copy of each leaf's values, so it adds memory in proportion to the state.

While a `Block` runs, each transaction, system call and the withdrawals
that commit add to `BlockChanges` what each account field and slot they
changed held before the block, the block's first change of a value
keeping it, and `begin` adds the BLOCKHASH entries it writes. One pass
over the journal before `settle` feeds both the changes and the access
list; for a touched account that settlement deletes, it keeps the account
and its slots. A run whose changes cannot grow is undone and fails as a
host failure.
`BlockChanges::undo` puts the world back as it was before `begin`:
slots first, then each account's existence, balance, nonce and code,
then the BLOCKHASH entries. `abandon` undoes the block, and so does a
`finish` that fails; a host can undo a finished block with its output's
changes.
`Chain::import` no longer clones the world before each block. A block
that faults is abandoned, `finish` undoes one that fails, and a finished
block whose output or roots do not match its header, or whose roots
cannot be computed, is undone from its changes.
The fixture fails the growth of a block's changes past 16 accounts. The
transfer that needed it fails as a host failure and is undone, and
abandoning the block undoes the transfers before it.
A Merkle-Patricia trie of the state held in arenas, with each node's
reference in its parent cached. Rereading a changed account or slot from
the world updates its leaf and marks its path stale, and the root rehashes
only stale nodes. Tested against `state_root` and fixture roots.
`Chain` builds the trie of its world once and, after `finish`, rereads
the accounts and slots of the block's changes before taking the root. A
block rejected after `finish` is undone and its keys reread, so the trie
is the head's again. A trie an allocation failed is rebuilt from the
world when the chain next needs it, before a block runs; the driver's
genesis check takes the chain's root.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-14T06:24:50.164439Z 9b5ea00 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@sbillig
sbillig added this pull request to stack #29 November 14, 2026 06:23
@sbillig
sbillig merged commit 3df19a3 into main Nov 16, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant