Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implemented fetching and storing of disabled authority for set. #813

Merged
merged 7 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 44 additions & 11 deletions src/main/java/com/limechain/beefy/state/BeefyState.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.limechain.beefy.dto.SignedCommitment;
import com.limechain.beefy.dto.ValidatorSet;
import com.limechain.beefy.dto.VoteMessage;
import com.limechain.network.protocol.beefy.messages.consensus.BeefyConsensusMessage;
import com.limechain.runtime.Runtime;
import com.limechain.state.AbstractState;
import com.limechain.storage.DBConstants;
Expand Down Expand Up @@ -90,6 +91,22 @@ public void persistState() {
persistRoundNumber(roundNumber);
}

/**
* The threshold is determined as the numOfValidators - (numOfValidators - 1) / 3
*
* @return minimum required validators for finality.
*/
public BigInteger getThreshold() {
var validatorSize = validatorSet.getValidators().size();
if (validatorSize == 0) {
return BigInteger.ZERO;
}
var numOfValidators = BigInteger.valueOf(validatorSize);
var faulty = (numOfValidators.subtract(BigInteger.ONE)).divide(THRESHOLD_DENOMINATOR);

return numOfValidators.subtract(faulty);
}

private void initializeNextDigest() {
if (beefyGenesis != null) {
nextDigest = beefyFinalized != null
Expand Down Expand Up @@ -129,19 +146,17 @@ private Pair<BigInteger, ValidatorSet> detectValidatorSetChange(BigInteger maxBl
}

/**
* The threshold is determined as the numOfValidators - (numOfValidators - 1) / 3
*
* @return minimum required validators for finality.
* Method is being called after processing all the accumulated justifications.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment related to the method below?

* It checks for mandatory blocks by detecting set changes in all blocks
* between the last BEEFY finalized and GRANDPA finalized blocks.
*/
public BigInteger getThreshold() {
var validatorSize = validatorSet.getValidators().size();
if (validatorSize == 0) {
return BigInteger.ZERO;
private void handleBeefyAuthorityConsensusMessage(BeefyConsensusMessage consensusMessage, BigInteger currentBlockNumber) {
switch (consensusMessage.getFormat()) {
case BEEFY_CHANGED_AUTHORITIES -> {
//Todo implement handle BEEFY_CHANGED_AUTHORITIES logic.
}
case BEEFY_ON_DISABLED -> disabledAuthority = consensusMessage.getDisabledAuthority();
}
var numOfValidators = BigInteger.valueOf(validatorSize);
var faulty = (numOfValidators.subtract(BigInteger.ONE)).divide(THRESHOLD_DENOMINATOR);

return numOfValidators.subtract(faulty);
}

private void reportDoubleVoting(VoteMessage voteMessage) {
Expand Down Expand Up @@ -178,6 +193,24 @@ public void persistBeefyValidators() {
);
}

private BigInteger fetchDisabledAuthority(BigInteger setId) {
return repository.find(
StateUtil.generateBeefyDisabledAuthorityKey(
DBConstants.BEEFY_DISABLED_AUTHORITY, setId
),
BigInteger.ZERO
);
}

private void persistDisabledAuthority() {
repository.save(
StateUtil.generateBeefyDisabledAuthorityKey(
DBConstants.BEEFY_DISABLED_AUTHORITY, validatorSet.getSetId()
),
disabledAuthority
);
}

private BigInteger fetchBeefyFinalized() {
return repository.find(DBConstants.BEEFY_FINALIZED, BigInteger.ZERO);
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/limechain/storage/DBConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class DBConstants {
//BeefyState keys
public static final String BEEFY_AUTHORITY_SET = "gs::beefyAuthoritySet";
public static final String BEEFY_SET_ID = "gs::beefySetId";
public static final String BEEFY_DISABLED_AUTHORITY = "bs:beefyDisabledAuthority";
public static final String BEEFY_FINALIZED = "bs:beefyFinalized";
public static final String BEEFY_ROUND = "bs:beefyRound";
public static final String BEEFY_JUSTIFICATION = "bs:justification";
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/limechain/storage/StateUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public String generatePreCommitsKey(String preCommitsKey, BigInteger roundNumber
public String generateBeefyJustificationKey(String justificationKey, BigInteger blockNumber) {
return prepareKey(justificationKey, blockNumber.toString());
}

public String generateBeefyDisabledAuthorityKey(String disabledAuthorityKey, BigInteger setId) {
return prepareKey(disabledAuthorityKey, setId.toString());
}
}
Loading