Skip to content

Commit

Permalink
refactor finalizable method
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigorov-Georgi committed Jan 9, 2025
1 parent be6f091 commit c58d5c0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 43 deletions.
63 changes: 20 additions & 43 deletions src/main/java/com/limechain/grandpa/GrandpaService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.limechain.grandpa;

import com.limechain.exception.grandpa.GhostExecutionException;
import com.limechain.exception.grandpa.GrandpaGenericException;
import com.limechain.exception.storage.BlockStorageGenericException;
import com.limechain.grandpa.state.RoundState;
import com.limechain.network.protocol.grandpa.messages.catchup.res.SignedVote;
Expand Down Expand Up @@ -32,55 +31,33 @@ public GrandpaService(RoundState roundState, BlockState blockState) {
this.blockState = blockState;
}

//TODO
private boolean finalisable() {
Vote bestFinalCandidate = getBestFinalCandidate();

Vote preVotedBlock = getPreVotedBlock();

var roundNumber = roundState.getRoundNumber();
roundState.addPreVotedBlockToArchive(roundNumber, preVotedBlock);
roundState.addBestFinalCandidateToArchive(roundNumber, bestFinalCandidate);

return true;
}

private Vote getPreVotedBlock() {
return null;
}
private boolean finalizable(BigInteger roundNumber) {
if (!isCompletable(roundNumber)) {
return false;
}

private List<SignedVote> createJustification(Hash256 bestFinalCandidateHash, Subround subround) {
Map<PubKey, SignedVote> votes;
Map<PubKey, List<SignedVote>> equivocations;
Vote ghostVote = getGrandpaGhost();
if (ghostVote == null) {
return false;
}

switch (subround) {
case PREVOTE -> {
votes = roundState.getPrevotes();
equivocations = roundState.getPvEquivocations();
}
case PRECOMMIT -> {
votes = roundState.getPrecommits();
equivocations = roundState.getPcEquivocations();
}
default -> throw new GrandpaGenericException("Unsupported subround: " + subround);
Vote bestFinalCandidate = getBestFinalCandidate();
if (bestFinalCandidate == null) {
return false;
}

List<SignedVote> justifications = new ArrayList<>();
roundState.addBestFinalCandidateToArchive(roundNumber, bestFinalCandidate);

votes.values().forEach((vote) -> {
try {
boolean isDescendant = blockState.isDescendantOf(bestFinalCandidateHash, vote.getVote().getBlockHash());
if (isDescendant) {
justifications.add(vote);
}
} catch (Exception e) {
log.warning("Error checking descendant relationship: " + e.getMessage());
}
});
var prevRoundNumber = roundNumber.subtract(BigInteger.ONE);
Vote previousBestFinalCandidate = roundState.getBestFinalCandidateForRound(prevRoundNumber);

equivocations.values().forEach(justifications::addAll);
return previousBestFinalCandidate != null
&& previousBestFinalCandidate.getBlockNumber().compareTo(bestFinalCandidate.getBlockNumber()) <= 0
&& bestFinalCandidate.getBlockNumber().compareTo(ghostVote.getBlockNumber()) <= 0;
}

return justifications;
private boolean isCompletable(BigInteger round) {
return false;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/limechain/grandpa/state/RoundState.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,8 @@ public void addPreVotedBlockToArchive(BigInteger roundNumber, Vote vote) {
public void addBestFinalCandidateToArchive(BigInteger roundNumber, Vote vote) {
this.bestFinalCandidateArchive.put(roundNumber, vote);
}

public Vote getBestFinalCandidateForRound(BigInteger roundNumber) {
return this.bestFinalCandidateArchive.get(roundNumber);
}
}

0 comments on commit c58d5c0

Please sign in to comment.