Skip to content

Commit

Permalink
feat: Implement Finalize stage in Play-Grandpa-Round.
Browse files Browse the repository at this point in the history
  • Loading branch information
Hristiyan Mitov committed Feb 7, 2025
1 parent bc0e0e0 commit c6d5625
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
35 changes: 34 additions & 1 deletion src/main/java/com/limechain/grandpa/round/FinalizeStage.java
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public BlockHeader getBestFinalCandidate() {
return Optional.ofNullable(finalizeEstimate).orElse(lastFinalizedBlock);
}

private void attemptToFinalizeAt() {
public void attemptToFinalizeAt() {

BlockState blockState = stateManager.getBlockState();
BigInteger lastFinalizedBlockNumber = blockState.getHighestFinalizedNumber();
Expand Down

0 comments on commit c6d5625

Please sign in to comment.