Skip to content

fix(derivation): clamp deriveForce skipNumber to batch tip#983

Merged
curryxbo merged 1 commit into
feat/sequencer-finalfrom
fix/derive-force-clamp-skipnumber
Jun 9, 2026
Merged

fix(derivation): clamp deriveForce skipNumber to batch tip#983
curryxbo merged 1 commit into
feat/sequencer-finalfrom
fix/derive-force-clamp-skipnumber

Conversation

@curryxbo

@curryxbo curryxbo commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Targets the CodeRabbit review comment on #966 (r3340287543).

In deriveForce, skipNumber >= rollupData.lastBlockNumber is reachable due to a small race between scenario-C dispatch and reactor quiesce; without a guard it returns a header past the batch tip and corrupts downstream verifyBatchRoots / advanceSafe.

Why the race exists

scenario-C entry in derivationBlock is decided before withReactorsQuiesced stops the blocksync / broadcast reactors:

T0: HeaderByNumber(lastBlockNumber) → nil   // dispatch decision, P2P alive
T0..T1: fetchRollupDataByTxHash(...)         // L1 RPC, P2P still alive
T1: withReactorsQuiesced.preWrite read
T1+: StopReactorsBeforeReorg()              // P2P stops here
T2: body() reads localLatest                 // skipNumber = this value

Between T0 and T1+, blocksync can deliver blocks up to and past lastBlockNumber (peers already hold them; we were just lagging). localLatest read at T2 reflects that catchup, so skipNumber >= lastBlockNumber is physically reachable even though it should not be by the dispatch logic.

l2Grew is a coarse cross-poll growth signal — single misjudgements are tolerated by design. The clamp here is what makes that tolerance actually safe at the deriveForce layer.

Symptom without the fix

When the race materialises:

  1. The block loop's if blockData.SafeL2Data.Number <= skipNumber { continue } skips every block.
  2. deriveForce returns header(skipNumber) — a block strictly past rollupData.lastBlockNumber.
  3. verifyBatchRoots(batchInfo, lastHeader) compares the batch's expected post-state / withdrawal roots against a later block → mismatch → SetBatchStatus(stateException) (spurious alarm).
  4. tagAdvancer.advanceSafe(batchIndex, lastHeader) pushes safe head past the batch tip → (batchIndex, safe) association is wrong.

Fix

Add an early return in deriveForce when skipNumber >= rollupData.lastBlockNumber:

  • Read header(rollupData.lastBlockNumber) from the local node (it must exist by definition of the race).
  • Return it directly. Upstream now sees the same header that scenario A would produce if the dispatcher had observed the now-present batch tip.

The early return runs inside withReactorsQuiesced's body, so the deferred StartReactorsAfterReorg still fires — reactors are restarted normally.

Test plan

  • Unit-test or manual trace verifying that a BatchInfo with lastBlockNumber <= skipNumber returns header(lastBlockNumber) and writes nothing.
  • Manual or integration scenario where blocksync catches up between dispatch and quiesce: confirm no stateException is raised and safeHead matches batchInfo.lastBlockNumber.
  • Existing scenario B (skipNumber==0) and scenario C with a true gap (skipNumber < lastBlockNumber) still write blocks as before.

🤖 Generated with Claude Code

@coderabbitai

coderabbitai Bot commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: b5891744-027d-449e-b150-94d5ffe89e82

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/derive-force-clamp-skipnumber

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@curryxbo curryxbo requested a review from a team as a code owner June 9, 2026 09:22
@curryxbo curryxbo requested review from panos-xyz and removed request for a team June 9, 2026 09:22
Scenario-C dispatch (lastBlockNumber missing locally + !l2Grew) is
decided in derivationBlock BEFORE reactors are quiesced; the
localLatest passed into deriveForce is read AFTER StopReactorsBeforeReorg.
In the window between the dispatch decision and the Stop, blocksync can
backfill past the batch tip. When that happens skipNumber >=
rollupData.lastBlockNumber, the existing loop short-circuits every
block via the `Number <= skipNumber` continue, and the function returns
header(skipNumber) — a block past the batch.

Upstream verifyBatchRoots and tagAdvancer.advanceSafe then run against
that wrong header: roots compared against post-batch state (false
stateException) and safe head pushed past the actual batch tip. With
the clamp, the race materialising degrades to the same outcome
scenario A would have produced once P2P caught up — verifyBatchRoots
sees header(lastBlockNumber), advanceSafe pins safe to the correct
batch tip.

Targets PR #966 review (CodeRabbit comment r3340287543).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@curryxbo curryxbo force-pushed the fix/derive-force-clamp-skipnumber branch from ac04c28 to 34bd70d Compare June 9, 2026 10:32
@curryxbo curryxbo merged commit 4a7b911 into feat/sequencer-final Jun 9, 2026
6 of 7 checks passed
@curryxbo curryxbo deleted the fix/derive-force-clamp-skipnumber branch June 9, 2026 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant