Skip to content

fix(nonce): use per-network genesis nonce seed instead of hardcoded mainnet#118

Merged
wcatz merged 1 commit intomasterfrom
fix/per-network-genesis-nonce
Apr 9, 2026
Merged

fix(nonce): use per-network genesis nonce seed instead of hardcoded mainnet#118
wcatz merged 1 commit intomasterfrom
fix/per-network-genesis-nonce

Conversation

@wcatz
Copy link
Copy Markdown
Owner

@wcatz wcatz commented Apr 9, 2026

Summary

Nonce evolution was seeded with mainnet's Shelley genesis hash for ALL networks. Preview and preprod have different genesis nonces, so every computed epoch nonce was wrong from epoch 0 onwards.

Changes

  • Per-network genesis nonce constants (mainnet, preprod, preview — verified against Koios)
  • genesisNonceForNetwork() and shelleyStartForNetwork() helpers
  • Replace all hardcoded ShelleyGenesisHash references with network-aware lookups
  • PreviewShelleyStartEpoch = 0 (preview has no Byron era)

Test plan

  • All tests pass
  • Deploy preview full mode, verify nonces match Koios for all epochs
  • Deploy mainnet, verify nonce computation

Summary by CodeRabbit

Release Notes

  • New Features

    • Added support for preview network alongside mainnet and preprod environments.
    • Enabled network-specific genesis nonce initialization for accurate epoch calculations across all supported networks.
  • Bug Fixes

    • Fixed epoch-to-slot calculations for preview network.

…ainnet

The nonce evolution was seeded with mainnet's Shelley genesis hash for
ALL networks. Preview (and preprod) have different genesis nonces, so
every computed epoch nonce was wrong from epoch 0 onwards.

Add per-network genesis nonce constants (verified against Koios):
  mainnet: 1a3be38b... (epoch 208)
  preprod: 162d29c4... (epoch 4)
  preview: 363498d1... (epoch 0)

Add shelleyStartForNetwork() and genesisNonceForNetwork() helpers.
Replace all hardcoded ShelleyGenesisHash references in ComputeEpochNonce,
BackfillNonces, and NonceIntegrityCheck with network-aware lookups.
Add PreviewShelleyStartEpoch = 0 (preview has no Byron era).
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 9, 2026

📝 Walkthrough

Walkthrough

The PR introduces network-specific nonce initialization by adding genesis nonce constants for mainnet, preprod, and preview networks, creating helper functions to select network-appropriate values, and updating the initialNonce function signature to accept a networkMagic parameter for per-network nonce seeding.

Changes

Cohort / File(s) Summary
Nonce Core Logic
nonce.go
Added network-specific genesis nonce constants (MainnetGenesisNonce, PreprodGenesisNonce, PreviewGenesisNonce). Introduced genesisNonceForNetwork(networkMagic int) and shelleyStartForNetwork(networkMagic int) helpers. Changed initialNonce signature to initialNonce(fullMode bool, networkMagic int). Updated ComputeEpochNonce, BackfillNonces, and NonceIntegrityCheck to use network-aware logic. Retained ShelleyGenesisHash as backward-compatible alias for MainnetGenesisNonce.
Epoch Constants
leaderlog.go
Added new exported constant PreviewShelleyStartEpoch = 0 to define the preview network's Shelley era epoch baseline.
Test Updates
comprehensive_test.go
Updated initialNonce call sites in TestInitialNonceModes and TestResyncFromDB_BlockCountMismatch to pass MainnetNetworkMagic as the second argument to match the new function signature.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 Across the networks, nonces bloom,
Mainnet, Preprod, Preview's room,
Each network its genesis seed knows,
As magic constants gently flows! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 71.43% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main change: replacing hardcoded mainnet genesis nonce with per-network seeds.
Description check ✅ Passed The description includes all required template sections: summary of the issue and fix, detailed list of changes, test plan with checkboxes, and necessary context for reviewers.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/per-network-genesis-nonce

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.

Copy link
Copy Markdown

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@comprehensive_test.go`:
- Around line 1642-1643: The prevNonce is created with MainnetNetworkMagic but
the test constructs the NonceTracker using PreprodNetworkMagic; update the call
to initialNonce to use PreprodNetworkMagic so the nonce network magic matches
the NonceTracker setup (change prevNonce := initialNonce(true,
MainnetNetworkMagic) to use PreprodNetworkMagic); ensure this change is applied
in the test where prevNonce is used alongside store.UpsertEvolvingNonce and the
NonceTracker instance so the recomputation/repair scenario remains identical.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f5c4c9f3-24a0-4138-8cbe-31fbdddaf3df

📥 Commits

Reviewing files that changed from the base of the PR and between 82ad6b3 and 5c46721.

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

Comment on lines +1642 to 1643
prevNonce := initialNonce(true, MainnetNetworkMagic)
if err := store.UpsertEvolvingNonce(ctx, epoch-1, prevNonce, 100); err != nil {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Network magic mismatch in test setup.

The test uses PreprodNetworkMagic for the NonceTracker (line 1648) but initializes prevNonce with MainnetNetworkMagic. For consistency with the test scenario, this should use PreprodNetworkMagic:

🔧 Suggested fix
-	prevNonce := initialNonce(true, MainnetNetworkMagic)
+	prevNonce := initialNonce(true, networkMagic)

The test still validates the recomputation mechanism correctly (block count mismatch triggers repair), but using the matching network magic makes the test setup more realistic and consistent.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
prevNonce := initialNonce(true, MainnetNetworkMagic)
if err := store.UpsertEvolvingNonce(ctx, epoch-1, prevNonce, 100); err != nil {
prevNonce := initialNonce(true, networkMagic)
if err := store.UpsertEvolvingNonce(ctx, epoch-1, prevNonce, 100); err != nil {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@comprehensive_test.go` around lines 1642 - 1643, The prevNonce is created
with MainnetNetworkMagic but the test constructs the NonceTracker using
PreprodNetworkMagic; update the call to initialNonce to use PreprodNetworkMagic
so the nonce network magic matches the NonceTracker setup (change prevNonce :=
initialNonce(true, MainnetNetworkMagic) to use PreprodNetworkMagic); ensure this
change is applied in the test where prevNonce is used alongside
store.UpsertEvolvingNonce and the NonceTracker instance so the
recomputation/repair scenario remains identical.

@wcatz wcatz merged commit 86b4959 into master Apr 9, 2026
5 checks passed
@wcatz wcatz deleted the fix/per-network-genesis-nonce branch April 9, 2026 23:26
@wcatz wcatz mentioned this pull request Apr 9, 2026
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