runtime/inclusion: fix availability_threshold (#6931)

This commit is contained in:
ordian
2023-03-21 17:20:59 +01:00
committed by GitHub
parent 6414284ff5
commit 4ed3997938
3 changed files with 14 additions and 9 deletions
+2 -2
View File
@@ -1609,13 +1609,13 @@ where
/// The maximum number of validators `f` which may safely be faulty.
///
/// The total number of validators is `n = 3f + e` where `e in { 1, 2, 3 }`.
pub fn byzantine_threshold(n: usize) -> usize {
pub const fn byzantine_threshold(n: usize) -> usize {
n.saturating_sub(1) / 3
}
/// The supermajority threshold of validators which represents a subset
/// guaranteed to have at least f+1 honest validators.
pub fn supermajority_threshold(n: usize) -> usize {
pub const fn supermajority_threshold(n: usize) -> usize {
n - byzantine_threshold(n)
}
@@ -28,10 +28,10 @@ use bitvec::{order::Lsb0 as BitOrderLsb0, vec::BitVec};
use frame_support::pallet_prelude::*;
use parity_scale_codec::{Decode, Encode};
use primitives::{
AvailabilityBitfield, BackedCandidate, CandidateCommitments, CandidateDescriptor,
CandidateHash, CandidateReceipt, CommittedCandidateReceipt, CoreIndex, GroupIndex, Hash,
HeadData, Id as ParaId, SigningContext, UncheckedSignedAvailabilityBitfields, ValidatorId,
ValidatorIndex, ValidityAttestation,
supermajority_threshold, AvailabilityBitfield, BackedCandidate, CandidateCommitments,
CandidateDescriptor, CandidateHash, CandidateReceipt, CommittedCandidateReceipt, CoreIndex,
GroupIndex, Hash, HeadData, Id as ParaId, SigningContext, UncheckedSignedAvailabilityBitfields,
ValidatorId, ValidatorIndex, ValidityAttestation,
};
use scale_info::TypeInfo;
use sp_runtime::{traits::One, DispatchError};
@@ -899,9 +899,7 @@ impl<T: Config> Pallet<T> {
}
const fn availability_threshold(n_validators: usize) -> usize {
let mut threshold = (n_validators * 2) / 3;
threshold += (n_validators * 2) % 3;
threshold
supermajority_threshold(n_validators)
}
#[derive(derive_more::From, Debug)]
@@ -710,6 +710,13 @@ fn bitfield_checks() {
});
}
#[test]
fn availability_threshold_is_supermajority() {
assert_eq!(3, availability_threshold(4));
assert_eq!(5, availability_threshold(6));
assert_eq!(7, availability_threshold(9));
}
#[test]
fn supermajority_bitfields_trigger_availability() {
let chain_a = ParaId::from(1_u32);