Skip to content

fix: provide parent header during EuclidV2 transition verification #1187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

Thegaram
Copy link

@Thegaram Thegaram commented May 14, 2025

1. Purpose or design rationale of this PR

This is a follow-up PR to #1186.

While that PR correctly handled a race condition, it did not address the real root cause, which is that engine.VerifyHeaders can be triggered from two different code paths:

  1. Inside BlockChain.insertChain, which would do header verification and insertion in parallel (pipelined).
  2. Inside BlockChain.InsertHeaderChain, which does blocking verification, and then inserts the header chain.

The upgradable engine fails in (2), because calling SystemConfig.VerifyHeaders assumes that the ancestor of this chain had already been inserted.

To address this edge case, we pass an optional parent hint to VerifyHeaders, which should work in both cases.

2. PR title

Your PR title must follow conventional commits (as we are doing squash merge for each PR), so it must start with one of the following types:

  • fix: A bug fix

3. Deployment tag versioning

Has the version in params/version.go been updated?

  • This PR doesn't involve a new deployment, git tag, docker image tag, and it doesn't affect traces
  • Yes

4. Breaking change label

Does this PR have the breaking-change label?

  • This PR is not a breaking change
  • Yes

Summary by CodeRabbit

  • New Features

    • Improved header verification by allowing additional context to be provided during batch verification.
    • Added support for seamless consensus engine transitions during block verification.
    • Introduced enhanced testing for consensus engine upgrades and hard fork transitions.
  • Bug Fixes

    • Enhanced reliability when verifying headers across consensus transitions.
  • Chores

    • Updated internal method signatures to support the new verification parameter.
    • Increased patch version to 47.

Copy link

coderabbitai bot commented May 14, 2025

Walkthrough

The signatures of the VerifyHeaders method in various consensus engine implementations and the Engine interface were updated to accept an additional parent *types.Header parameter. Corresponding call sites throughout the codebase were modified to provide this new argument, including passing the parent header where applicable. The patch version was incremented. Additionally, a new mock FakeEthClient was introduced for system contract consensus testing, and miner tests were extended to cover the upgradable consensus engine and EuclidV2 transition.

Changes

File(s) Change Summary
consensus/clique/clique.go
consensus/ethash/consensus.go
consensus/system_contract/consensus.go
consensus/wrapper/consensus.go
Updated VerifyHeaders method signatures to include a parent *types.Header parameter. System contract logic now uses the parent header when verifying the first header. Wrapper engine passes the parent header to underlying engines, removing previous polling logic.
consensus/consensus.go Modified the Engine interface's VerifyHeaders method to accept the new parent *types.Header parameter.
core/block_validator_test.go
core/blockchain.go
core/headerchain.go
Updated all calls to VerifyHeaders to include the new fourth argument (typically nil).
params/version.go Incremented the patch version from 46 to 47.
consensus/system_contract/system_contract.go Added new type FakeEthClient implementing a minimal subset of sync_service.EthClient interface for testing.
consensus/system_contract/system_contract_test.go Removed old mock fakeEthClient and replaced references with new FakeEthClient. Cleaned up mutex locking and imports.
miner/scroll_worker_test.go Extended miner tests to support the upgradable consensus engine and EuclidV2 hard fork transition. Added new test worker constructors and a test verifying the EuclidV2 transition.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant UpgradableEngine
    participant Clique
    participant SystemContract

    Caller->>UpgradableEngine: VerifyHeaders(chain, headers, seals, parent)
    alt Headers belong to Clique consensus
        UpgradableEngine->>Clique: VerifyHeaders(chain, headers, seals, parent)
    else Headers belong to SystemContract consensus
        UpgradableEngine->>SystemContract: VerifyHeaders(chain, headers, seals, parent)
    end
    Clique-->>UpgradableEngine: (abortCh, resultCh)
    SystemContract-->>UpgradableEngine: (abortCh, resultCh)
    UpgradableEngine-->>Caller: (abortCh, resultCh)
Loading

Possibly related PRs

  • scroll-tech/go-ethereum#1128: Modifies internal validation logic of verifyHeader in SystemContract consensus; related by context as both PRs alter the same consensus engine but focus on different aspects (signature vs. validation logic).

Suggested labels

bump-version

Suggested reviewers

  • jonastheis
  • georgehao
  • colinlyguo

Poem

A header here, a parent there,
Consensus engines now aware.
With signatures changed and calls aligned,
The patch ticks up, the code refined.
Hop, hop! The rabbit grins with glee—
More robust chains for all to see!
🐇✨

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@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

🧹 Nitpick comments (2)
consensus/wrapper/consensus.go (2)

61-66: Document the new parent parameter

VerifyHeaders’s godoc comment has not been updated to mention the new parent *types.Header hint. Future readers (and ‑ve tools that rely on comments) will have no clue what the extra argument is for.

Please add a short sentence such as “parent — (optional) previously-verified header that immediately precedes the first element of headers.


110-113: Consider re-using the outer parent for the Clique segment

Although Clique currently ignores the hint, passing it through keeps the API usage symmetrical and avoids ad-hoc nils sprinkled through the code:

- abortClique, cliqueResults := ue.clique.VerifyHeaders(chain, cliqueHeaders, cliqueSeals, nil)
+ abortClique, cliqueResults := ue.clique.VerifyHeaders(chain, cliqueHeaders, cliqueSeals, parent)

Purely cosmetic today, but future Clique upgrades (or test doubles) might appreciate the extra context.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2500647 and c570e94.

📒 Files selected for processing (9)
  • consensus/clique/clique.go (1 hunks)
  • consensus/consensus.go (1 hunks)
  • consensus/ethash/consensus.go (1 hunks)
  • consensus/system_contract/consensus.go (1 hunks)
  • consensus/wrapper/consensus.go (4 hunks)
  • core/block_validator_test.go (3 hunks)
  • core/blockchain.go (1 hunks)
  • core/headerchain.go (1 hunks)
  • params/version.go (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (4)
consensus/system_contract/consensus.go (2)
consensus/consensus.go (1)
  • ChainHeaderReader (32-47)
core/types/block.go (1)
  • Header (69-122)
consensus/clique/clique.go (2)
consensus/consensus.go (1)
  • ChainHeaderReader (32-47)
core/types/block.go (1)
  • Header (69-122)
consensus/wrapper/consensus.go (2)
consensus/consensus.go (1)
  • ChainHeaderReader (32-47)
core/types/block.go (1)
  • Header (69-122)
consensus/ethash/consensus.go (2)
consensus/consensus.go (1)
  • ChainHeaderReader (32-47)
core/types/block.go (1)
  • Header (69-122)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: test
  • GitHub Check: build-mock-ccc-geth
  • GitHub Check: check
  • GitHub Check: semgrep/ci
  • GitHub Check: zizmor
  • GitHub Check: goimports-lint
  • GitHub Check: Analyze (go)
🔇 Additional comments (10)
params/version.go (1)

27-27: Version bump for patch release.

The patch version has been incremented from 46 to 47, reflecting the non-breaking changes introduced in this PR for the EuclidV2 transition verification.

core/headerchain.go (1)

342-342: Added new parent header parameter to VerifyHeaders call.

The call to VerifyHeaders has been updated to include the new parent parameter (currently nil), aligning with the updated method signature in the consensus engine interface. This will enable parent header context during verification when needed.

core/blockchain.go (1)

1549-1549: Added new parent header parameter to VerifyHeaders call.

The call to VerifyHeaders has been updated to include the new parent parameter (currently nil), aligning with the modified consensus engine interface. This addresses the race condition described in the PR objectives, ensuring consistency across both code paths that invoke header verification.

core/block_validator_test.go (1)

54-54: Updated test calls to VerifyHeaders with new parent parameter.

All test calls to VerifyHeaders have been updated to include the new parent parameter (passed as nil), ensuring tests remain compatible with the modified consensus engine interface. No changes to the test logic or verification workflow were required.

Also applies to: 57-57, 110-110, 114-114, 179-179

consensus/consensus.go (1)

74-74: Interface correctly updated to support parent header context.

The VerifyHeaders method in the Engine interface has been appropriately updated to include a parent *types.Header parameter. This change enables consensus engines to access the parent header during batch verification, which is particularly useful for the first header in a batch when ancestor headers may not be available in the chain yet.

consensus/system_contract/consensus.go (3)

89-89: Method signature correctly updated to match interface changes.

The VerifyHeaders method signature now includes the new parent *types.Header parameter to match the updated Engine interface.


95-98: Appropriate utilization of parent header parameter.

This logic now properly handles the case where the first header in the batch needs verification but has no preceding headers in the batch itself. By using the provided parent parameter as a fallback, the method can now correctly verify the first header in scenarios where ancestor headers may not have been inserted into the chain yet.


100-100: Correctly passes parents to verifyHeader.

The updated line correctly uses the potentially modified parents slice that may now include the provided parent header parameter.

consensus/clique/clique.go (1)

229-229: Method signature updated but parameter not utilized.

The VerifyHeaders method signature has been properly updated to include the new parent *types.Header parameter to match the Engine interface. However, the implementation doesn't actually use this parameter in the verification logic. This is acceptable since the clique engine already has a working mechanism to handle parent headers through the verifyHeader method's existing functionality.

Consider reviewing whether the Clique implementation should also use the parent parameter in certain scenarios for consistency with the SystemContract implementation, particularly for the first header in a batch when no preceding headers exist.

consensus/ethash/consensus.go (1)

122-122: Method signature updated but parameter not utilized.

The VerifyHeaders method signature has been properly updated to include the new parent *types.Header parameter to match the Engine interface. However, the implementation doesn't actually use this parameter in the verification logic. This is acceptable since the ethash verification process already handles parent header retrieval through the verifyHeaderWorker method.

Consider reviewing whether the Ethash implementation should also use the parent parameter in certain scenarios for consistency with the SystemContract implementation, particularly when verifying the first header in a batch.

colinlyguo
colinlyguo previously approved these changes May 14, 2025
jonastheis
jonastheis previously approved these changes May 15, 2025
@Thegaram Thegaram dismissed stale reviews from jonastheis and colinlyguo via b86775c May 15, 2025 17:17
Copy link

@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: 3

♻️ Duplicate comments (1)
consensus/system_contract/system_contract_test.go (1)

130-133: Prefer the new setter for updating the mock client

After introducing FakeEthClient.SetValue, replace the manual lock manipulation:

-fakeClient.mu.Lock()
-fakeClient.Value = updatedSigner
-fakeClient.mu.Unlock()
+fakeClient.SetValue(updatedSigner)

This keeps tests concise and lock-error-free.

🧹 Nitpick comments (1)
miner/scroll_worker_test.go (1)

230-238: Avoid aliasing a global when selecting the worker config

testWorker assigns config := testConfig, meaning the worker receives the shared pointer.
If newWorker or the worker mutates it, every other test that reuses testConfig inherits those changes.

After introducing newTestConfig, this block simplifies to:

- config := testConfig
- if allowEmpty {
-     config = testConfigAllowEmpty
- }
+config := newTestConfig(allowEmpty)

This guarantees each worker works with its own copy.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between c570e94 and b86775c.

📒 Files selected for processing (3)
  • consensus/system_contract/system_contract.go (2 hunks)
  • consensus/system_contract/system_contract_test.go (4 hunks)
  • miner/scroll_worker_test.go (6 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
consensus/system_contract/system_contract.go (2)
interfaces.go (1)
  • FilterQuery (144-162)
core/types/block.go (2)
  • Header (69-122)
  • Block (223-241)
consensus/system_contract/system_contract_test.go (4)
rollup/sync_service/types.go (1)
  • EthClient (14-23)
consensus/system_contract/system_contract.go (1)
  • FakeEthClient (148-153)
log/handler.go (1)
  • DiscardHandler (333-337)
common/types.go (1)
  • HexToAddress (218-218)
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: test
  • GitHub Check: Analyze (go)
🔇 Additional comments (1)
miner/scroll_worker_test.go (1)

1411-1469: Re-use of the same consensus engine across two blockchains can leak state

engine (an UpgradableEngine instance) is passed to two independent core.NewBlockChain instances (chain and the second chain on lines 1460-1464).
If the engine caches chain-specific data (e.g., header snapshots, signer sets), the second blockchain may read stale data, giving false-negative results or masking bugs.

Creating a fresh engine for the second import isolates the test cases:

-chain, err := core.NewBlockChain(chainDb, nil, b.chain.Config(), engine, vm.Config{}, nil, nil)
+freshClique := clique.New(chainConfig.Clique, chainDb)
+freshSys    := system_contract.New(context.Background(), chainConfig.SystemContract,
+                                   &system_contract.FakeEthClient{Value: testBankAddress})
+freshEngine := wrapper.NewUpgradableEngine(chainConfig.IsEuclidV2, freshClique, freshSys)
+chain, err  := core.NewBlockChain(chainDb, nil, b.chain.Config(), freshEngine, vm.Config{}, nil, nil)

This keeps the test hermetic and closer to real-world usage where separate nodes hold independent engine instances.

@Thegaram Thegaram merged commit ad6cced into develop May 16, 2025
14 checks passed
@Thegaram Thegaram deleted the fix-provide-parent-header-during-euclid-transition-verification branch May 16, 2025 07:27
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.

3 participants