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 1 commit
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
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 = () -> {
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 (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();
Copy link
Collaborator

@Grigorov-Georgi Grigorov-Georgi Feb 7, 2025

Choose a reason for hiding this comment

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

You should use try-catch instead of null check for this one.

    public BlockHeader getFinalizedBlock() {
        if (finalizedBlock == null) throw new GrandpaGenericException("Finalized block has not been set.");
        return finalizedBlock;
    }

Copy link
Collaborator Author

@hMitov hMitov Feb 7, 2025

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Return false in the catch if getter throws an error.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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;
}
}
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