-
Notifications
You must be signed in to change notification settings - Fork 221
finish consensus if header is received earlier #7583
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
finish consensus if header is received earlier #7583
Conversation
…ithub.com/multiversx/mx-chain-go into fix-round-canceled-when-header-received-early
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements an optimization to finish consensus early if a header for the current consensus round has already been received and committed. This prevents redundant consensus processing when the block has already been finalized through other means (e.g., sync).
Key Changes
- Added early detection in
initCurrentRound()to check if the current block header's round matches the ongoing consensus round - When detected, all consensus subrounds are marked as finished to skip unnecessary processing
- Added defensive checks in
receivedBlockBody()andreceivedBlockHeader()to prevent processing when the subround is already finished
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| consensus/spos/bls/v2/subroundStartRound.go | Adds import for bls constants and implements early consensus termination logic when header is already committed for current round |
| consensus/spos/bls/v2/subroundBlock.go | Adds early return guards in message reception handlers to prevent processing when subround is already finished |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| } | ||
|
|
||
| currentHeader := sr.Blockchain().GetCurrentBlockHeader() | ||
| if !check.IfNil(currentHeader) && int64(currentHeader.GetRound()) == sr.RoundHandler().Index() { |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comparison between currentHeader.GetRound() (which returns uint64) and sr.RoundHandler().Index() (which returns int64) involves casting uint64 to int64. This can cause issues if the round number exceeds the maximum value of int64, leading to incorrect comparisons due to overflow. Consider comparing both values as uint64 to avoid potential overflow issues.
| currentHeader := sr.Blockchain().GetCurrentBlockHeader() | ||
| if !check.IfNil(currentHeader) && int64(currentHeader.GetRound()) == sr.RoundHandler().Index() { | ||
| log.Debug("initCurrentRound: header for current consensus already committed, setting all subrounds as finished") | ||
|
|
||
| sr.SetStatus(sr.Current(), spos.SsFinished) | ||
| sr.SetStatus(bls.SrBlock, spos.SsFinished) | ||
| sr.SetStatus(bls.SrSignature, spos.SsFinished) | ||
| sr.SetStatus(bls.SrEndRound, spos.SsFinished) | ||
|
|
||
| return true | ||
| } |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This early exit logic for finishing consensus when a header is already committed lacks test coverage. The new behavior introduced in lines 135-145 should be tested to ensure it correctly handles the case where the current block header's round matches the consensus round, and that all subrounds are properly marked as finished. Consider adding a test case to verify this scenario.
| currentHeader := sr.Blockchain().GetCurrentBlockHeader() | ||
| if !check.IfNil(currentHeader) && int64(currentHeader.GetRound()) == sr.RoundHandler().Index() { | ||
| log.Debug("initCurrentRound: header for current consensus already committed, setting all subrounds as finished") | ||
|
|
||
| sr.SetStatus(sr.Current(), spos.SsFinished) | ||
| sr.SetStatus(bls.SrBlock, spos.SsFinished) | ||
| sr.SetStatus(bls.SrSignature, spos.SsFinished) | ||
| sr.SetStatus(bls.SrEndRound, spos.SsFinished) | ||
|
|
||
| return true | ||
| } |
Copilot
AI
Dec 30, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a potential race condition in the early exit logic. Between checking if the current header's round matches the consensus round (line 136) and setting the subround statuses (lines 139-142), the blockchain state could change due to concurrent operations. If another goroutine commits a different block for the same round during this window, the consensus state might become inconsistent. Consider using synchronization mechanisms to ensure atomicity of this check-and-set operation, or verify that existing locks already protect this critical section.
The base branch was changed.
…-header-received-early
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## feat/supernova-async-exec #7583 +/- ##
=============================================================
- Coverage 77.66% 77.65% -0.02%
=============================================================
Files 874 874
Lines 120689 120701 +12
=============================================================
- Hits 93739 93733 -6
- Misses 20777 20788 +11
- Partials 6173 6180 +7 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Reasoning behind the pull request
Proposed changes
Testing procedure
Pre-requisites
Based on the Contributing Guidelines the PR author and the reviewers must check the following requirements are met:
featbranch created?featbranch merging, do all satellite projects have a proper tag insidego.mod?