Repository navigation
Description
Background
After PR #141 migrated the reference-index from an ExEx to a canonical-cursor runtime, precise incremental reorg rollback relies on the IndexedBlocks table (block_number → block_hash). When the runtime detects a reorg, it walks that table backwards to recover "which hash each height was indexed at", compares against the canonical chain, and — once the common ancestor is found — rolls back only the diverged suffix.
Problem
The rewind lower bound in reconcile_cursor is jade_first_block. To support a deep reorg that could theoretically rewind all the way to the Jade start, IndexedBlocks must retain every block hash across jade_first ..= indexed_to; a gap triggers CorruptMetadata.
Consequence: the table grows linearly and unbounded with the chain, roughly 40 bytes × (tip − jade_first). Jade is already live on mainnet, so long-term this is 100 MB+ and monotonically increasing — it has no natural upper bound.
Why a fixed depth cap is the wrong fix
An earlier draft proposed a constant MAX_REORG_DEPTH (e.g. 8192) as the rewind floor. This is incorrect and has been rejected:
- Any block that is not yet finalized on L1 can still reorg. There is no safe fixed constant below which reorg is impossible.
- Reorg depth is a function of L2 block-production speed and the L1 batch-submission interval — it is not a fixed number of blocks. A slow L1 submission cadence combined with fast block production can push a legitimate reorg arbitrarily deep.
- A hard cap would silently treat a real (if rare) deep reorg as unrecoverable (
ManualRebuildRequired), which is a correctness risk, not just an operational one.
Correct approach: bound the rewind floor to the L1 finalized block
The true reorg hard-boundary for an L2 is the L1 finalized block: by definition, a finalized block can never be reorged. So instead of an arbitrary constant, bound the rewind floor and pruning to finalized:
-
Rewind floor. In
reconcile_cursor, set the lower bound tofinalized.map(|f| f.max(jade_first_block)).unwrap_or(jade_first_block):- If a finalized block exists, rewind only needs to reach it — anything at or below finalized cannot have diverged.
- If no finalized block exists yet (fresh chain / L1 not yet producing finality), fall back to
jade_first_block(current behavior).
-
Pruning. On backfill/commit, once a finalized block exists, delete
IndexedBlocksbreadcrumbs strictly belowfinalized— they can never be needed for reorg rewind again. TheReferenceIndex/BlockReferenceIndexquery data is never touched, so pruned history stays fully queryable; only the reorg breadcrumbs shrink. -
Data source. Morph sets finalized via the engine API:
newSafeL2Block→set_safe_and_finalizedsets safe and finalized to the same block.BlockIdReader::finalized_block_numberreads it. TheCanonicalChainblanket impl overP: BlockReaderIdExtalready carries the required bound (BlockReaderIdExt: ReceiptProviderIdExt: BlockIdReader), so no new trait bound is needed — just add afinalized_block_number()method to theCanonicalChaintrait that forwards to it.
Accepted trade-off
When L1 batch submission stalls (no new finalized block for a long time), the retained window grows to tip − finalized. This is correct: the breadcrumbs that remain are exactly the ones that could still be needed for a reorg, and the growth is bounded by L1 liveness rather than an artificial constant. Under normal operation finalized tracks close to the tip, so the table stays small.
Scope
- Confined to
crates/reference-index/(runtime / backfill / reconcile / source / writer). - Decoupled from PR refactor(reference-index): replace ExEx with canonical cursor runtime #141 — separate PR.
- Sequencing: implement after feat(reference-index): bounded server-side wait on IndexBehind to avoid client retry storms #144 and refactor(reference-index): simplify reader read gate (drop generation seqlock, demote phase to metrics) #145; this is the last of the three.
- Upgrade compatibility: an existing DB with a full
IndexedBlocksremains correct; finalized-based pruning simply starts trimming on the next commit once a finalized block is observed.
Activity
[-]perf(reference-index): bound reorg rewind depth to cap IndexedBlocks growth[/-][+]perf(reference-index): bound reorg rewind floor to the L1 finalized block[/+]perf(reference-index): bound reorg to finalized, simplify read gate, …
refactor(reference-index): replace ExEx with canonical cursor runtime (…
fix(proofs): keep only revert retry and zeroed-slot pruning from the …