From a76b3e23ec20b4069c4da72488f9e768ed5b62cb Mon Sep 17 00:00:00 2001 From: Grigorov-Georgi Date: Fri, 7 Feb 2025 14:20:06 +0200 Subject: [PATCH] feat: make isCompletable field and rename method to checkIfRoundIsCompletable --- .../com/limechain/grandpa/round/GrandpaRound.java | 15 +++++++++++---- .../limechain/grandpa/round/PreCommitStage.java | 7 ++++--- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/limechain/grandpa/round/GrandpaRound.java b/src/main/java/com/limechain/grandpa/round/GrandpaRound.java index a6906d45..1712e9ed 100644 --- a/src/main/java/com/limechain/grandpa/round/GrandpaRound.java +++ b/src/main/java/com/limechain/grandpa/round/GrandpaRound.java @@ -103,6 +103,12 @@ public class GrandpaRound { @Nullable private Vote preCommitChoice; + /** + * Is current round completable + */ + @Nullable + private boolean isCompletable; + private Map preVotes = new ConcurrentHashMap<>(); private Map preCommits = new ConcurrentHashMap<>(); private Vote primaryVote; @@ -248,7 +254,7 @@ private boolean isFinalizable() { * * @return if the current round is completable */ - public boolean isCompletable() { + public boolean checkIfRoundIsCompletable() { Map votes = getDirectVotes(SubRound.PRE_COMMIT); long votesCount = votes.values().stream() @@ -362,10 +368,11 @@ public BlockHeader findGrandpaGhost() { throw new GhostExecutionException("GHOST not found"); } - BlockHeader grandpaGhost = selectBlockWithMostVotes(blocks, getPrevBestFinalCandidate()); - this.grandpaGhost = grandpaGhost; + BlockHeader result = selectBlockWithMostVotes(blocks, getPrevBestFinalCandidate()); + this.grandpaGhost = result; + this.isCompletable = checkIfRoundIsCompletable(); - return grandpaGhost; + return result; } /** diff --git a/src/main/java/com/limechain/grandpa/round/PreCommitStage.java b/src/main/java/com/limechain/grandpa/round/PreCommitStage.java index 5121667d..86e83b96 100644 --- a/src/main/java/com/limechain/grandpa/round/PreCommitStage.java +++ b/src/main/java/com/limechain/grandpa/round/PreCommitStage.java @@ -17,6 +17,7 @@ public void start(GrandpaRound round) { if (round.isCompletable()) { end(round); + return; } long timeElapsed = System.currentTimeMillis() - round.getStartTime().toEpochMilli(); @@ -33,10 +34,10 @@ public void start(GrandpaRound round) { public void end(GrandpaRound round) { log.fine(String.format("Round %d ended pre-commit stage.", round.getRoundNumber())); - if (!round.getOnStageTimerHandler().isShutdown()) { + if (round.getOnStageTimerHandler() != null && !round.getOnStageTimerHandler().isShutdown()) { round.getOnStageTimerHandler().shutdown(); + round.setOnStageTimerHandler(null); } - round.setOnStageTimerHandler(null); try { @@ -47,7 +48,7 @@ public void end(GrandpaRound round) { log.fine(String.format("Round %d ended start stage.", round.getRoundNumber())); } catch (GrandpaGenericException e) { - log.fine(String.format("Round %d could not end now: %s", round.getRoundNumber(), e.getMessage())); + log.fine(String.format("Round %d cannot end now: %s", round.getRoundNumber(), e.getMessage())); } } } \ No newline at end of file