From 40e6c223c4280e44114780dfe7ad1e292fa0f38f Mon Sep 17 00:00:00 2001 From: Grigorov-Georgi Date: Wed, 12 Feb 2025 15:07:12 +0200 Subject: [PATCH] chore: refactor --- .../grandpa/state/GrandpaSetState.java | 180 +++++++++--------- 1 file changed, 90 insertions(+), 90 deletions(-) diff --git a/src/main/java/com/limechain/grandpa/state/GrandpaSetState.java b/src/main/java/com/limechain/grandpa/state/GrandpaSetState.java index e905e4985..df883bb43 100644 --- a/src/main/java/com/limechain/grandpa/state/GrandpaSetState.java +++ b/src/main/java/com/limechain/grandpa/state/GrandpaSetState.java @@ -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(); @@ -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). @@ -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 preVotes = round.getPreVotes(); - repository.save(StateUtil.generatePreVotesKey(DBConstants.GRANDPA_PREVOTES, roundNumber, setId), preVotes); - } - - public Map 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 preCommits = round.getPreCommits(); - repository.save(StateUtil.generatePreCommitsKey(DBConstants.GRANDPA_PRECOMMITS, roundNumber, setId), preCommits); - } - - public Map 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 authorities) { this.setId = setId.add(BigInteger.ONE); this.authorities = authorities; @@ -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 preVotes = round.getPreVotes(); + repository.save(StateUtil.generatePreVotesKey(DBConstants.GRANDPA_PREVOTES, roundNumber, setId), preVotes); + } + + public Map 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 preCommits = round.getPreCommits(); + repository.save(StateUtil.generatePreCommitsKey(DBConstants.GRANDPA_PRECOMMITS, roundNumber, setId), preCommits); + } + + public Map 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> keyPair = authorities.stream() .map(a -> keyStore.getKeyPair(KeyType.GRANDPA, a.getPublicKey()))