-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
728 Implement PreCommit Stage (#749)
# Description - If ```round.isCompletable() = true``` or timer ```Time >= t + 4 * T``` is expires -> end pre-commit stage - end() method waits ```round.getGrandpaGhost()``` to have a value before proceeding to the next stage - Once GrandpaGhost is available, a vote message is broadcast - ```isCompleted``` field is added to ```GrandpaRound``` that is updated, when ```findGrandpaGhost``` is executed and the previous method for checking if the round is completable is renamed to ```checkIfCompletable``` Fixes #728 --------- Co-authored-by: Hristiyan Mitov <[email protected]> Co-authored-by: Hristiyan Mitov <[email protected]>
- Loading branch information
1 parent
3f67133
commit 0e92cf1
Showing
3 changed files
with
60 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 40 additions & 2 deletions
42
src/main/java/com/limechain/grandpa/round/PreCommitStage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.grandpa.vote.SubRound; | ||
import com.limechain.grandpa.vote.Vote; | ||
import lombok.extern.java.Log; | ||
|
||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Log | ||
public class PreCommitStage implements StageState { | ||
|
||
@Override | ||
public void start(GrandpaRound round) { | ||
log.fine(String.format("Round %d started pre-commit stage.", round.getRoundNumber())); | ||
|
||
if (round.isCompletable()) { | ||
end(round); | ||
return; | ||
} | ||
|
||
long timeElapsed = System.currentTimeMillis() - round.getStartTime().toEpochMilli(); | ||
long timeRemaining = (4 * GrandpaRound.DURATION) - timeElapsed; | ||
|
||
round.setOnStageTimerHandler(Executors.newScheduledThreadPool(1)); | ||
round.getOnStageTimerHandler().schedule(() -> { | ||
log.fine(String.format("Round %d timer triggered.", round.getRoundNumber())); | ||
end(round); | ||
}, timeRemaining, TimeUnit.MILLISECONDS); | ||
} | ||
|
||
@Override | ||
public void end(GrandpaRound round) { | ||
} | ||
} | ||
|
||
round.clearOnStageTimerHandler(); | ||
|
||
try { | ||
|
||
Vote grandpaGhost = Vote.fromBlockHeader(round.getGrandpaGhost()); | ||
log.fine(String.format("Round %d ended pre-commit stage.", round.getRoundNumber())); | ||
|
||
round.broadcastVoteMessage(grandpaGhost, SubRound.PRE_COMMIT); | ||
round.setState(new FinalizeStage()); | ||
round.getState().start(round); | ||
|
||
} catch (GrandpaGenericException e) { | ||
log.fine(String.format("Round %d cannot end now: %s", round.getRoundNumber(), e.getMessage())); | ||
} | ||
} | ||
} |