-
Notifications
You must be signed in to change notification settings - Fork 1
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
feat: Implement Finalize stage in Play-Grandpa-Round. #750
Changes from 1 commit
c6d5625
20fe021
3f10834
816ca38
f5828fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,46 @@ | ||
package com.limechain.grandpa.round; | ||
|
||
import com.limechain.network.protocol.warp.dto.BlockHeader; | ||
import lombok.extern.java.Log; | ||
|
||
@Log | ||
public class FinalizeStage implements StageState { | ||
|
||
@Override | ||
public void start(GrandpaRound round) { | ||
|
||
log.fine(String.format("Round %d entered the Finalize stage.", round.getRoundNumber())); | ||
|
||
if (isRoundReadyToEnd(round)) { | ||
end(round); | ||
return; | ||
} | ||
|
||
Runnable onFinalizeHandler = () -> { | ||
if (isRoundReadyToEnd(round)) { | ||
end(round); | ||
} | ||
}; | ||
round.setOnFinalizeHandler(onFinalizeHandler); | ||
} | ||
|
||
@Override | ||
public void end(GrandpaRound round) { | ||
log.info(String.format("Round %d met finalization conditions and will be finalized.", round.getRoundNumber())); | ||
round.setOnFinalizeHandler(null); | ||
round.attemptToFinalizeAt(); | ||
log.fine(String.format("Round %d exits Finalize stage.", round.getRoundNumber())); | ||
|
||
round.end(); | ||
} | ||
} | ||
|
||
private boolean isRoundReadyToEnd(GrandpaRound round) { | ||
BlockHeader finalized = round.getFinalizedBlock(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use try-catch instead of null check for this one.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea here is that if finalized is null, it means that round is not ready to be finalized. The method should return false(we are in waiting for preCommits as not eligible to finalize) and then the callback should be passed to the GrandpaRound handler. Then on each preCommit message, we attemptToFinalize the round and execute the logic from the callback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return false in the catch if getter throws an error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I probably didn't describe well what I meant, but I guess after @Zurcusa's clarification you get the point. |
||
BlockHeader prevBestFinalCandidate = round.getPrevBestFinalCandidate(); | ||
|
||
if (finalized == null || prevBestFinalCandidate == null) { | ||
return false; | ||
} | ||
return finalized.getBlockNumber().compareTo(prevBestFinalCandidate.getBlockNumber()) >= 0; | ||
} | ||
} |
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.
I think we have this inline when we invoke
setOnFinalizeHandler