-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement Finalize stage in Play-Grandpa-Round.
- Loading branch information
Hristiyan Mitov
committed
Feb 7, 2025
1 parent
bc0e0e0
commit c6d5625
Showing
2 changed files
with
35 additions
and
2 deletions.
There are no files selected for viewing
35 changes: 34 additions & 1 deletion
35
src/main/java/com/limechain/grandpa/round/FinalizeStage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
BlockHeader prevBestFinalCandidate = round.getPrevBestFinalCandidate(); | ||
|
||
if (finalized == null || prevBestFinalCandidate == null) { | ||
return false; | ||
} | ||
return finalized.getBlockNumber().compareTo(prevBestFinalCandidate.getBlockNumber()) >= 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters