feat: Lock-free apply_block refactor - #2345
Open
sergerad wants to merge 61 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
…ckfree-store-state
sergerad
commented
Aug 2, 2026
| @@ -0,0 +1,419 @@ | |||
| use std::collections::HashSet; | |||
Collaborator
Author
There was a problem hiding this comment.
Much of the diffs here are a move of impl but partial / involving a split to other files. The only changes should be w.r.t BlockNumber -> ScopedBlockNumber and ScopedBlockRange.
sergerad
commented
Aug 2, 2026
| conn, | ||
| note_commitments.as_slice(), | ||
| up_to_block, | ||
| ) |
Collaborator
Author
There was a problem hiding this comment.
This up_to_block bound may technically not be required but I think it might still be a good idea to use it for any path that involves multiple DB calls - some of which are block bound sensitive. If anything, to catch / not be effected by desync-like bugs we would not expect.
…ckfree-store-state
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
Closes #1539.
Closes #1853.
Closes #2414.
Makes the store's block-write path lock-free for readers, and makes the resulting read-consistency rules type-enforced. Reads previously contended on
RwLocks over the in-memory trees (and blocked duringapply_block's DB-commit window); they now load an immutable snapshot viaArcSwapand are never blocked by writes. All reads flow through a request-scopedStateView, so a query combining tree and DB data at different chain heights is no longer expressible.Why:
apply_block(oneshot handshakes between the DB task and the in-memory update).StateViewmakes the scoping structural instead.How:
Lock-free write path (
state/writer/)WriteWorkertask owns the mutable nullifier tree, account tree, blockchain MMR, and account-state forest, processing blocks serially from an mpsc channel — no locks. In-flight writes always complete; shutdown is only observed between requests.StateSnapshot(trees backed by read-only RocksDB snapshot views) and publishes it atomically viaArcSwap, so readers keep a consistent frozen view while the next block commits.Db::apply_blockis now a plain transaction — the oneshotallow_acquire/acquire_donesynchronization is removed.Write capabilities (
state/lifecycle.rs)LoadedState::startspawns the worker and returns the read-onlyArc<State>plus non-cloneableBlockWriter/ProofWritercapabilities and aWriterTaskhandle, statically limiting each write path to one task. The capabilities expose no read access; tasks that read and write getArc<State>alongside their capability.BlockWriter::stopdrains and joins the worker so tree storage is released deterministically before the data directory is re-opened or deleted (used byrecoverand stress-test seeding).Type-enforced reads (
state/view/)StateView, pinned to one snapshot per request (State::view()). DB queries are scoped by the view's tip internally; callers cannot supply their own. RocksDB-backed trees are only reachable throughblock_in_placehelpers; snapshot fields are only visible inside the view module.Dbqueries require view-issued proof types (ScopedBlockNum/ScopedBlockRange), constructible only by aStateViewafter validating the bound against its tip — extending the enforcement to the DB boundary itself.range.end() <= tipthemselves via a newRangeBeyondTiperror (sameInvalidArgumentresponse as before); the RPC layer'srange_bounds_checkis deleted and pagination'schain_tipis now the tip the query actually ran against.get_account, the block producer'sget_tx_inputs);sync_chain_mmrclamps the proven tip to the view's tip.Live tips (
state/tip.rs)State::committed_tip()/proven_tip()read the watch channels their writers publish to (mirroringsubscribe_committed_tip/subscribe_proven_tip); theFinalityenum is removed. The committed tip is published after the snapshot, so it never reports a block a fresh view cannot serve.Observability:
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).Supporting changes: read-only
reader()views forAccountStateForest/AccountTreeWithHistory(relaxed toBackendReader/SmtStorageReaderbounds);statemodule restructured intoview/(read endpoints) andwriter/(worker + capabilities); new tracing field names allowlisted.Changelog