Skip to content

Commit

Permalink
feat: Implemented fetching and storing of disabled authority for set. (
Browse files Browse the repository at this point in the history
…#813)

# Description

Implemented fetching and storing of disabled authority for set.

Fixes #774

---------

Co-authored-by: Hristiyan Mitov <hristiyan.mitov@limechain.tech>
  • Loading branch information
hMitov and Hristiyan Mitov authored Mar 6, 2025
1 parent 23fdbf3 commit d469002
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
30 changes: 30 additions & 0 deletions src/main/java/com/limechain/beefy/BeefyService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.limechain.beefy.dto.BeefyPayloadId;
import com.limechain.beefy.dto.Commitment;
import com.limechain.beefy.dto.PayloadElement;
import com.limechain.beefy.dto.ValidatorSet;
import com.limechain.exception.storage.BlockStorageGenericException;
import com.limechain.network.protocol.beefy.messages.consensus.BeefyConsensusMessage;
import com.limechain.network.protocol.warp.DigestHelper;
Expand All @@ -15,15 +16,44 @@

import java.math.BigInteger;
import java.util.Collections;
import java.util.Objects;
import java.util.Optional;

@Log
@Component
@RequiredArgsConstructor
public class BeefyService {

private static final BigInteger THRESHOLD_DENOMINATOR = BigInteger.valueOf(3);

private final StateManager stateManager;

/**
* The threshold is determined as the numOfValidators - (numOfValidators - 1) / 3
*
* @return minimum required validators for finality.
*/
private BigInteger getThreshold() {
ValidatorSet validatorSet = stateManager.getBeefyState().getValidatorSet();

if (Objects.isNull(validatorSet)) {
log.warning("getThreshold: No validatorSet in BeefyState.");
return BigInteger.ZERO;
}

var validatorSize = validatorSet.getValidators().size();

if (validatorSize == 0) {
log.warning("getThreshold: Validator set is empty.");
return BigInteger.ZERO;
}

var numOfValidators = BigInteger.valueOf(validatorSize);
var faulty = (numOfValidators.subtract(BigInteger.ONE)).divide(THRESHOLD_DENOMINATOR);

return numOfValidators.subtract(faulty);
}

private Commitment getCommitment(BigInteger blockNumber, BigInteger setId) {
BlockState blockState = stateManager.getBlockState();
BlockHeader blockHeader = null;
Expand Down
39 changes: 28 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 @@ -133,19 +134,17 @@ private Pair<BigInteger, ValidatorSet> detectValidatorSetChange(BigInteger maxBl
}

/**
* The threshold is determined as the numOfValidators - (numOfValidators - 1) / 3
*
* @return minimum required validators for finality.
* 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;
}
var numOfValidators = BigInteger.valueOf(validatorSize);
var faulty = (numOfValidators.subtract(BigInteger.ONE)).divide(THRESHOLD_DENOMINATOR);
private void handleBeefyAuthorityConsensusMessage(BeefyConsensusMessage consensusMessage, BigInteger currentBlockNumber) {
switch (consensusMessage.getFormat()) {
case BEEFY_CHANGED_AUTHORITIES -> {
//Todo implement handle BEEFY_CHANGED_AUTHORITIES logic.

return numOfValidators.subtract(faulty);
}
case BEEFY_ON_DISABLED -> disabledAuthority = consensusMessage.getDisabledAuthority();
}
}

public void vote() {
Expand Down Expand Up @@ -228,6 +227,24 @@ private 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());
}
}

0 comments on commit d469002

Please sign in to comment.