Skip to content

perf(reference-index): bound reorg rewind floor to the L1 finalized block #143

Closed

Description

@panos-xyz

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:

  1. Rewind floor. In reconcile_cursor, set the lower bound to finalized.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).
  2. Pruning. On backfill/commit, once a finalized block exists, delete IndexedBlocks breadcrumbs strictly below finalized — they can never be needed for reorg rewind again. The ReferenceIndex / BlockReferenceIndex query data is never touched, so pruned history stays fully queryable; only the reorg breadcrumbs shrink.

  3. Data source. Morph sets finalized via the engine API: newSafeL2Block → set_safe_and_finalized sets safe and finalized to the same block. BlockIdReader::finalized_block_number reads it. The CanonicalChain blanket impl over P: BlockReaderIdExt already carries the required bound (BlockReaderIdExt: ReceiptProviderIdExt: BlockIdReader), so no new trait bound is needed — just add a finalized_block_number() method to the CanonicalChain trait 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

Activity

  1. added
    enhancement
    on Jul 20, 2026
  2. self-assigned this
    on Jul 20, 2026
  3. changed the title [-]perf(reference-index): bound reorg rewind depth to cap IndexedBlocks growth[/-] [+]perf(reference-index): bound reorg rewind floor to the L1 finalized block[/+] on Jul 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions