Skip to content
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

Merged
merged 5 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 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,51 @@
package com.limechain.grandpa.round;

import com.limechain.exception.grandpa.GrandpaGenericException;
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 (isRoundReadyToBeFinalized(round)) {
end(round);
return;
}

Runnable onFinalizeHandler = () -> {
Copy link
Collaborator

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

if (isRoundReadyToBeFinalized(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 isRoundReadyToBeFinalized(GrandpaRound round) {
BlockHeader finalized;
try {
finalized = round.getFinalizedBlock();
} catch (GrandpaGenericException e) {
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a warning log here.

}

BlockHeader prevBestFinalCandidate = round.getPrevBestFinalCandidate();

return prevBestFinalCandidate != null
&& 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
Loading