Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

runtime/inclusion: fix availability_threshold #6931

Merged
merged 1 commit into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions primitives/src/v4/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
12 changes: 5 additions & 7 deletions runtime/parachains/src/inclusion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)]
Expand Down
7 changes: 7 additions & 0 deletions runtime/parachains/src/inclusion/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down