feat: Lock-free apply_block refactor#2345
Open
sergerad wants to merge 25 commits into
Open
Conversation
…ckfree-store-state
…ckfree-store-state
…ckfree-store-state
sergerad
marked this pull request as ready for review
July 23, 2026 01:18
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Makes the store's block-write path lock-free for readers. Reads previously contended on
RwLocks over the in-memory trees (and were blocked during the DB-commit window ofapply_block); they now load an immutable snapshot viaArcSwapand are never blocked by writes.Why:
apply_block(oneshot handshakes between the DB task and the in-memory update), which was fragile and hard to reason about.How:
BlockWritertask (crates/store/src/state/writer.rs) owns the mutable nullifier tree, account tree, blockchain MMR, and account-state forest.State::apply_blockforwards blocks to it over an mpsc channel; requests are processed serially, so no locks are needed anywhere.InMemoryState(trees backed by read-only RocksDB snapshot views) and publishes it atomically viaArcSwap. Readersload_full()a snapshot wait-free and keep a consistent frozen view even while the next block commits.get_block_header,get_transaction_inputsnote lookups) are now scoped to the snapshot's block number, since mid-apply the DB may be ahead of the published snapshot.select_existing_note_commitmentsgains anup_to_blockbound.Db::apply_blockis now a plain transaction — the oneshotallow_acquire/acquire_donesynchronization is removed.State::shutdowndrains and joins the writer task so tree storage is released before the data directory is re-opened or deleted (used by stress-test seeding).SnapshotGuardtracks live snapshot generations and lifetimes; warns when a snapshot outlives 10s or more than 4 generations are pinned (a leaked/slow reader pins a RocksDB snapshot).reader()views forAccountStateForest/AccountTreeWithHistory(relaxed toBackendReader/SmtStorageReaderbounds),chain_tipis now sync, new tracing field names allowlisted.Followup:
Changelog
`