Skip to content

Commit

Permalink
feat: make isCompletable field and rename method to checkIfRoundIsCom…
Browse files Browse the repository at this point in the history
…pletable
  • Loading branch information
Grigorov-Georgi committed Feb 7, 2025
1 parent ed36e95 commit a76b3e2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
15 changes: 11 additions & 4 deletions src/main/java/com/limechain/grandpa/round/GrandpaRound.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public class GrandpaRound {
@Nullable
private Vote preCommitChoice;

/**
* Is current round completable
*/
@Nullable
private boolean isCompletable;

private Map<Hash256, SignedVote> preVotes = new ConcurrentHashMap<>();
private Map<Hash256, SignedVote> preCommits = new ConcurrentHashMap<>();
private Vote primaryVote;
Expand Down Expand Up @@ -248,7 +254,7 @@ private boolean isFinalizable() {
*
* @return if the current round is completable
*/
public boolean isCompletable() {
public boolean checkIfRoundIsCompletable() {

Map<Vote, Long> votes = getDirectVotes(SubRound.PRE_COMMIT);
long votesCount = votes.values().stream()
Expand Down Expand Up @@ -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;
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/limechain/grandpa/round/PreCommitStage.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public void start(GrandpaRound round) {

if (round.isCompletable()) {
end(round);
return;
}

long timeElapsed = System.currentTimeMillis() - round.getStartTime().toEpochMilli();
Expand All @@ -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 {

Expand All @@ -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()));
}
}
}

0 comments on commit a76b3e2

Please sign in to comment.