Skip to content

fix(nonce): recompute evolving nonce on block count mismatch#102

Merged
wcatz merged 1 commit intomasterfrom
fix/resync-block-count-mismatch
Mar 13, 2026
Merged

fix(nonce): recompute evolving nonce on block count mismatch#102
wcatz merged 1 commit intomasterfrom
fix/resync-block-count-mismatch

Conversation

@wcatz
Copy link
Copy Markdown
Owner

@wcatz wcatz commented Mar 13, 2026

Summary

  • ResyncFromDB now cross-checks epoch_nonces.block_count against actual COUNT(*) FROM blocks
  • On mismatch (OOM between CopyFrom and ProcessBatch), recomputes evolving nonce inline from raw VRF outputs
  • Prevents stale nonce from corrupting the nonce chain for all subsequent epochs

Root cause

InsertBlockBatch (CopyFrom) writes blocks atomically, but ProcessBatch (nonce evolution) runs after as a separate step. An OOM between the two leaves blocks in the DB but epoch_nonces.block_count stale. On restart, ResyncFromDB read the stale count uncorrected.

Test plan

  • TestResyncFromDB_BlockCountMismatch — simulates OOM scenario, verifies recompute
  • All 70 tests pass

Summary by CodeRabbit

  • Bug Fixes

    • Fixed nonce synchronization to detect and correct block count mismatches, ensuring accurate nonce recomputation during data recovery scenarios.
  • Tests

    • Added test coverage for block count mismatch detection and recovery.

…cFromDB

InsertBlockBatch (CopyFrom) writes blocks atomically but ProcessBatch
runs separately. An OOM between the two leaves blocks in the DB with a
stale epoch_nonces.block_count. On restart, ResyncFromDB read the stale
count and if the reconnect overlap landed in a later epoch, the
incomplete epoch's nonce was persisted uncorrected.

ResyncFromDB now cross-checks block_count against the actual block count
and recomputes the evolving nonce inline from raw VRF outputs on mismatch.
@wcatz wcatz merged commit c1d90c0 into master Mar 13, 2026
3 of 4 checks passed
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 13, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 3860134b-77e3-4d93-b2a2-6be162997dcf

📥 Commits

Reviewing files that changed from the base of the PR and between 8710e09 and 3fef027.

📒 Files selected for processing (2)
  • comprehensive_test.go
  • nonce.go

📝 Walkthrough

Walkthrough

This pull request adds detection and recovery logic for block count mismatches in the nonce synchronization process. When a mismatch occurs between stored block count and actual blocks in the database, the evolving nonce is recomputed from raw VRF outputs and state is repaired.

Changes

Cohort / File(s) Summary
Nonce Recovery Logic
nonce.go
Adds cross-check in ResyncFromDB to detect block_count mismatches; when detected, recomputes evolving nonce from raw VRF outputs by seeding from previous epoch nonce, fetching VRF outputs, iterating evolution, and persisting corrected nonce and block_count.
Test Coverage
comprehensive_test.go
Adds TestResyncFromDB_BlockCountMismatch test that verifies mismatch detection, nonce recomputation, and block_count repair. Introduces bytes import for nonce value comparison.

Sequence Diagram

sequenceDiagram
    participant NT as NonceTracker
    participant DB as Database
    participant VO as VRF Outputs
    
    NT->>DB: ResyncFromDB (post-bulk insert)
    DB->>DB: Count blocks for epoch
    DB->>NT: Return actual block count
    NT->>NT: Compare stored vs actual block_count
    
    alt Block count mismatch detected
        NT->>DB: Fetch previous epoch evolving nonce
        DB->>NT: Return previous nonce (or initial)
        NT->>VO: GetVrfOutputsForEpoch(current epoch)
        VO->>NT: Return VRF outputs list
        NT->>NT: Iterate VRF outputs to evolve nonce
        NT->>DB: Upsert recomputed nonce + correct block_count
        DB->>NT: Persist complete
        NT->>NT: Update evolvingNonce & blockCount in-memory
        NT->>NT: Log recomputation event
    else Block count matches
        NT->>NT: No action (nonce is valid)
    end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Possibly related PRs

Poem

🐰 A mismatch in blocks? Oh, what a plight!
But our nonce recovery sets things right—
We fetch the VRF seeds from the store,
Evolve and repair, block counts restore!
Through epochs we hop with nonce held tight 🌱

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/resync-block-count-mismatch
📝 Coding Plan
  • Generate coding plan for human review comments

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.

@wcatz wcatz deleted the fix/resync-block-count-mismatch branch March 20, 2026 14:23
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