Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Grigorov-Georgi committed Feb 12, 2025
1 parent 433f71a commit 40e6c22
Showing 1 changed file with 90 additions and 90 deletions.
180 changes: 90 additions & 90 deletions src/main/java/com/limechain/grandpa/state/GrandpaSetState.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,6 @@ public class GrandpaSetState extends AbstractState implements ServiceConsensusSt

private GrandpaRound currentGrandpaRound;

// We keep a maximum of 3 rounds at a time
public synchronized void addNewGrandpaRound(GrandpaRound grandpaRound) {
if (currentGrandpaRound.getPrevious() != null && currentGrandpaRound.getPrevious().getPrevious() != null) {
// Setting the previous to null make it
currentGrandpaRound.getPrevious().setPrevious(null);
}

currentGrandpaRound = grandpaRound;
}

public GrandpaRound getGrandpaRound(BigInteger roundNumber) {

GrandpaRound current = currentGrandpaRound;
while (!current.getRoundNumber().equals(roundNumber)) {

current = current.getPrevious();
if (current == null) {
throw new GrandpaGenericException("Target round not found");
}
}

return current;
}

@Override
public void populateDataFromRuntime(Runtime runtime) {
this.authorities = runtime.getGrandpaApiAuthorities();
Expand All @@ -99,6 +75,15 @@ public void initializeFromDatabase() {
loadPersistedState();
}

@Override
public void persistState() {
saveGrandpaAuthorities();
saveAuthoritySetId();
saveLatestRoundNumber();
savePreCommits(getCurrentGrandpaRound().getRoundNumber());
savePreVotes(getCurrentGrandpaRound().getRoundNumber());
}

/**
* The threshold is determined as the total weight of authorities
* subtracted by the weight of potentially faulty authorities (one-third of the total weight minus one).
Expand All @@ -111,77 +96,11 @@ public BigInteger getThreshold() {
return totalWeight.subtract(faulty);
}

private BigInteger getAuthoritiesTotalWeight() {
return authorities.stream()
.map(Authority::getWeight)
.reduce(BigInteger.ZERO, BigInteger::add);
}

public BigInteger derivePrimary(BigInteger roundNumber) {
var authoritiesCount = BigInteger.valueOf(authorities.size());
return roundNumber.remainder(authoritiesCount);
}

public void saveGrandpaAuthorities() {
repository.save(StateUtil.generateAuthorityKey(DBConstants.AUTHORITY_SET, setId), authorities);
}

public Authority[] fetchGrandpaAuthorities() {
return repository.find(StateUtil.generateAuthorityKey(DBConstants.AUTHORITY_SET, setId), new Authority[0]);
}

public void saveAuthoritySetId() {
repository.save(DBConstants.SET_ID, setId);
}

public BigInteger fetchAuthoritiesSetId() {
return repository.find(DBConstants.SET_ID, BigInteger.ZERO);
}

public void saveLatestRoundNumber() {
repository.save(DBConstants.LATEST_ROUND, currentGrandpaRound.getRoundNumber());
}

public BigInteger fetchLatestRoundNumber() {
return repository.find(DBConstants.LATEST_ROUND, BigInteger.ZERO);
}

public void savePreVotes(BigInteger roundNumber) {
GrandpaRound round = getGrandpaRound(roundNumber);
Map<Hash256, SignedVote> preVotes = round.getPreVotes();
repository.save(StateUtil.generatePreVotesKey(DBConstants.GRANDPA_PREVOTES, roundNumber, setId), preVotes);
}

public Map<PubKey, Vote> fetchPreVotes(BigInteger roundNumber) {
return repository.find(StateUtil.generatePreVotesKey(DBConstants.GRANDPA_PREVOTES, roundNumber, setId),
Collections.emptyMap());
}

public void savePreCommits(BigInteger roundNumber) {
GrandpaRound round = getGrandpaRound(roundNumber);
Map<Hash256, SignedVote> preCommits = round.getPreCommits();
repository.save(StateUtil.generatePreCommitsKey(DBConstants.GRANDPA_PRECOMMITS, roundNumber, setId), preCommits);
}

public Map<PubKey, Vote> fetchPreCommits(BigInteger roundNumber) {
return repository.find(StateUtil.generatePreCommitsKey(DBConstants.GRANDPA_PRECOMMITS, roundNumber, setId),
Collections.emptyMap());
}

private void loadPersistedState() {
this.setId = fetchAuthoritiesSetId();
this.authorities = Arrays.asList(fetchGrandpaAuthorities());
}

@Override
public void persistState() {
saveGrandpaAuthorities();
saveAuthoritySetId();
saveLatestRoundNumber();
savePreCommits(getCurrentGrandpaRound().getRoundNumber());
savePreVotes(getCurrentGrandpaRound().getRoundNumber());
}

public void startNewSet(List<Authority> authorities) {
this.setId = setId.add(BigInteger.ONE);
this.authorities = authorities;
Expand Down Expand Up @@ -274,6 +193,87 @@ public void handleGrandpaConsensusMessage(GrandpaConsensusMessage consensusMessa
log.fine(String.format("Updated grandpa set config: %s", consensusMessage.getFormat().toString()));
}

// We keep a maximum of 3 rounds at a time
public synchronized void addNewGrandpaRound(GrandpaRound grandpaRound) {
if (currentGrandpaRound.getPrevious() != null && currentGrandpaRound.getPrevious().getPrevious() != null) {
// Setting the previous to null make it
currentGrandpaRound.getPrevious().setPrevious(null);
}

currentGrandpaRound = grandpaRound;
}

public GrandpaRound getGrandpaRound(BigInteger roundNumber) {

GrandpaRound current = currentGrandpaRound;
while (!current.getRoundNumber().equals(roundNumber)) {

current = current.getPrevious();
if (current == null) {
throw new GrandpaGenericException("Target round not found");
}
}

return current;
}

public void saveGrandpaAuthorities() {
repository.save(StateUtil.generateAuthorityKey(DBConstants.AUTHORITY_SET, setId), authorities);
}

public Authority[] fetchGrandpaAuthorities() {
return repository.find(StateUtil.generateAuthorityKey(DBConstants.AUTHORITY_SET, setId), new Authority[0]);
}

public void saveAuthoritySetId() {
repository.save(DBConstants.SET_ID, setId);
}

public BigInteger fetchAuthoritiesSetId() {
return repository.find(DBConstants.SET_ID, BigInteger.ZERO);
}

public void saveLatestRoundNumber() {
repository.save(DBConstants.LATEST_ROUND, currentGrandpaRound.getRoundNumber());
}

public BigInteger fetchLatestRoundNumber() {
return repository.find(DBConstants.LATEST_ROUND, BigInteger.ZERO);
}

public void savePreVotes(BigInteger roundNumber) {
GrandpaRound round = getGrandpaRound(roundNumber);
Map<Hash256, SignedVote> preVotes = round.getPreVotes();
repository.save(StateUtil.generatePreVotesKey(DBConstants.GRANDPA_PREVOTES, roundNumber, setId), preVotes);
}

public Map<PubKey, Vote> fetchPreVotes(BigInteger roundNumber) {
return repository.find(StateUtil.generatePreVotesKey(DBConstants.GRANDPA_PREVOTES, roundNumber, setId),
Collections.emptyMap());
}

public void savePreCommits(BigInteger roundNumber) {
GrandpaRound round = getGrandpaRound(roundNumber);
Map<Hash256, SignedVote> preCommits = round.getPreCommits();
repository.save(StateUtil.generatePreCommitsKey(DBConstants.GRANDPA_PRECOMMITS, roundNumber, setId), preCommits);
}

public Map<PubKey, Vote> fetchPreCommits(BigInteger roundNumber) {
return repository.find(StateUtil.generatePreCommitsKey(DBConstants.GRANDPA_PRECOMMITS, roundNumber, setId),
Collections.emptyMap());
}

private void loadPersistedState() {
this.setId = fetchAuthoritiesSetId();
this.authorities = Arrays.asList(fetchGrandpaAuthorities());
}

private BigInteger getAuthoritiesTotalWeight() {
return authorities.stream()
.map(Authority::getWeight)
.reduce(BigInteger.ZERO, BigInteger::add);
}

private void updateAuthorityStatus() {
Optional<Pair<byte[], byte[]>> keyPair = authorities.stream()
.map(a -> keyStore.getKeyPair(KeyType.GRANDPA, a.getPublicKey()))
Expand Down

0 comments on commit 40e6c22

Please sign in to comment.