Skip to content

chore: Cache proven chain tip in State#1861

Merged
sergerad merged 13 commits intonextfrom
sergerad-tip-cache
Apr 6, 2026
Merged

chore: Cache proven chain tip in State#1861
sergerad merged 13 commits intonextfrom
sergerad-tip-cache

Conversation

@sergerad
Copy link
Copy Markdown
Collaborator

@sergerad sergerad commented Mar 30, 2026

Closes #1841.

Cache proven-in-sequence tip to avoid repeated DB queries

Adds a watch::channel-based cache for the proven-in-sequence block number, so RPC endpoints requesting Finality::Proven no longer hit the database on every call.

Changes

  • Introduces a Finality enum (Committed / Proven) in the store's State
  • Replaces State::latest_block_num() with State::chain_tip(finality) which returns the committed tip (from the in-memory MMR) or the proven tip (from the cached watch channel) depending on the requested finality
  • State::load() initializes the proven tip from the DB and returns a watch::Sender that the proof scheduler uses to push updates after mark_proven_and_advance_sequence advances the sequence
  • Simplifies sync_chain_mmr from a match + DB query into a single chain_tip(finality) call
  • Splits up Store::serve() into helper functions spawn_proof_scheduler() and spawn_grpc_servers()

@sergerad sergerad added the no changelog This PR does not require an entry in the `CHANGELOG.md` file label Mar 30, 2026
Comment thread crates/store/src/server/mod.rs Outdated
Comment thread crates/store/src/server/proof_scheduler.rs Outdated
Comment thread crates/store/src/server/proof_scheduler.rs Outdated
Comment thread crates/store/src/server/rpc_api.rs Outdated
impl From<proto::rpc::Finality> for Finality {
fn from(finality: proto::rpc::Finality) -> Self {
match finality {
proto::rpc::Finality::Unspecified | proto::rpc::Finality::Committed => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm not sure how I feel about defaulting here for Unspecified, but its probably not a train smash.

Comment thread crates/store/src/state/mod.rs Outdated
Copy link
Copy Markdown
Collaborator

@SantiagoPittella SantiagoPittella left a comment

Choose a reason for hiding this comment

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

Could we benefit of using AtomicU32 inside State instead of the channel? Considering that we are only borrowing to get the latest value

@Mirko-von-Leipzig
Copy link
Copy Markdown
Collaborator

Could we benefit of using AtomicU32 inside State instead of the channel? Considering that we are only borrowing to get the latest value

Possibly yeah. Though I would insist we new type wrap it so it has a BlockNumber API, and that we get a single writer (non-clone), and a reader (clone/copy).

@sergerad
Copy link
Copy Markdown
Collaborator Author

sergerad commented Apr 6, 2026

Could we benefit of using AtomicU32 inside State instead of the channel? Considering that we are only borrowing to get the latest value

Possibly yeah. Though I would insist we new type wrap it so it has a BlockNumber API, and that we get a single writer (non-clone), and a reader (clone/copy).

Happy to do this in a followup if we think its worth while. Seems a bit more boilerplate for potentially low/no benefit.

EDIT: Updating this in #1857 as it does help with the logic (potentially even necessary)

@sergerad sergerad merged commit 6eb0af9 into next Apr 6, 2026
18 checks passed
@sergerad sergerad deleted the sergerad-tip-cache branch April 6, 2026 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

no changelog This PR does not require an entry in the `CHANGELOG.md` file

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Keep chain tip in memory for sync_chain_mmr endpoint

3 participants