Skip to content

fix: use cumulative prefix sets for incremental trie state root#445

Open
avalonche wants to merge 3 commits intomainfrom
fix/incremental-trie-cumulative-prefix-sets
Open

fix: use cumulative prefix sets for incremental trie state root#445
avalonche wants to merge 3 commits intomainfrom
fix/incremental-trie-cumulative-prefix-sets

Conversation

@avalonche
Copy link
Collaborator

Fix: track cumulative TriePrefixSetsMut across all flashblocks. Before building TrieInput, extend the current prefix sets with all prior flashblocks' prefix sets. This forces the walker to re-visit every path modified in earlier flashblocks.

The fix is also ~30% faster than the unfixed incremental path because descending into cached in-memory nodes is faster than DB cursor seeks for skipped branches.

Benchmarks (10 flashblocks, 50k accounts):

  • Without cache: ~2,200ms (baseline)
  • Incremental (no fix): ~844ms (2.6x faster, incorrect on reverts)
  • Incremental (with fix): ~650ms (3.4x faster, correct)

@avalonche avalonche force-pushed the fix/incremental-trie-cumulative-prefix-sets branch 5 times, most recently from b74a166 to baa5064 Compare March 15, 2026 20:27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can keep the bench but maybe not the result?
If we choose to keep the result, atleast move it in crates/op-builder/benches/docs or results.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will move it to the benches folder, want a template for what the benchmark reports should look like

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a template then instead of an example?

Copy link
Contributor

@akundaz akundaz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The benchmark and tests run against replicated state root calculations instead of the actual production code path. Can you extract the code to run this calculation and and update the benchmark and tests to call that directly? Otherwise we can't really draw conclusions about real world performance from these results. And if the code drifts we should be able to catch with our tests and the benchmark.

avalonche and others added 2 commits March 17, 2026 11:15
The incremental trie cache produces wrong state roots when a storage slot
modified in flashblock N reverts in flashblock N+1. The reverted slot
disappears from the cumulative HashedPostState, so its nibble path is
missing from the prefix set. The trie walker skips the subtree and uses
the stale cached hash from the previous flashblock's branch node.

Fix: track cumulative TriePrefixSetsMut across all flashblocks. Before
building TrieInput, extend the current prefix sets with all prior
flashblocks' prefix sets. This forces the walker to re-visit every path
modified in earlier flashblocks.

The fix is also ~30% faster than the unfixed incremental path because
descending into cached in-memory nodes is faster than DB cursor seeks
for skipped branches.

Benchmarks (10 flashblocks, 50k accounts):
- Without cache:           ~2,200ms (baseline)
- Incremental (no fix):    ~844ms  (2.6x faster, incorrect on reverts)
- Incremental (with fix):  ~650ms  (3.4x faster, correct)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Extract the incremental state root calculation logic from
build_flashblock_payload_inner into builder::state_root::compute_state_root.

Tests and benchmarks now call the same function as production code,
ensuring they exercise the actual code path rather than replicated logic
that could drift.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@avalonche avalonche force-pushed the fix/incremental-trie-cumulative-prefix-sets branch from c866328 to 56da125 Compare March 17, 2026 22:10
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

3 participants