Repository navigation
Description
Background
crates/reference-index/src/reader.rs::query_at currently gates reads through a 7-variant ReferenceIndexPhase state machine (Opening/PreJade/Deferred/Backfill/Live/Repairing/Unavailable) plus a generation seqlock (set_phase bumps generation odd→even; query_at spins for an even value and re-checks it at the end).
Reviewing the read path, most of this machinery is not load-bearing for correctness. Read correctness already comes from three independent layers:
- MDBX MVCC read-transaction snapshot (
db.tx()):indexed_to,indexed_hash, and the iterated rows all come from one consistent snapshot; later commits cannot perturb an in-flight read. (indexed_to, indexed_hash) == tipcomparison (reader.rs:176): decides whether that snapshot is exactly the canonical tip.- RPC-layer before/after
chain_info()bracketing (crates/rpc/src/morph/handler.rs): decides whether the tip moved during the read.
Problems
generationseqlock is redundant. A seqlock protects multi-field snapshots; here it guards a singleAtomicU8(phase), whoseAcquireload is already consistent. The end-of-read generation re-check (reader.rs:208-212) is also over-conservative: a fully-correct result (single snapshot,== tip) is discarded asIndexUnavailablemerely because the runtime flippedphasemid-read. It adds a spin point and can cause spurious failures without adding any guarantee the tx snapshot + tip comparison don't already provide.phaseover-couples the runtime state machine into the read gate. From the reader's "can I serve?" perspective,Deferred/Backfillare identical (IndexBehind) andRepairing/Unavailableare identical (IndexUnavailable). Worse, inDeferred/Backfill/Repairingthe cursor already satisfiesindexed_to != tip, so the line-176 comparison would returnIndexBehindon its own — the phase gate there is only a micro-optimization (skip opening a tx) plus an error-code distinction, not a correctness requirement.
Non-goal / keep as-is
PreJade semantics must stay: pre-Jade must return Ok([]) (a complete, terminal empty answer — do NOT retry), which is the opposite instruction to IndexBehind (may have data — retry). Folding pre-Jade into IndexBehind would make every query retry forever until Jade activates. The Jade-boundary race handling (tip.timestamp < jade_timestamp fallback) must be preserved too.
Proposed simplification
- Delete the
generationseqlock (the spin-for-even loop and the end-of-read re-check). Readphase(if still needed) with a singleAtomicU8load. - Take
phaseout of the read path. Keep it as a pure metrics/log field (runtime keeps setting it; the gauge keeps exportingDeferred/Backfill/Repairingfor observability). If the "Repairing/Unavailable → tell client not to retry" hint is worth keeping, use a singleunavailable: AtomicBoolinstead of a 7-way match. - Move the pre-Jade check up to the RPC layer. handler.rs already has
header.timestamp(); doif tip.timestamp < jade_timestamp { return Ok(vec![]) }there so the query never enters the reader, and remove thePreJadephase.
Resulting query_at: open tx → read indexed_to/indexed_hash → compare (number, hash) == tip → iterate → return. Correctness carried by the MDBX snapshot + tip comparison + RPC bracketing, equivalent to (arguably better than) today, with much less complexity.
Scope
crates/reference-index/src/reader.rs(main),crates/reference-index/src/runtime.rs(phase set-sites stay for metrics),crates/rpc/src/morph/handler.rs(pre-Jade shortcut).- No storage-layer or write-path change.
- Independent of refactor(reference-index): replace ExEx with canonical cursor runtime #141/perf(reference-index): bound reorg rewind floor to the L1 finalized block #143/feat(reference-index): bounded server-side wait on IndexBehind to avoid client retry storms #144 — separate PR.
Activity
perf(reference-index): bound reorg to finalized, simplify read gate, …
refactor(reference-index): replace ExEx with canonical cursor runtime (…