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

session, staking: introduce static limit on the number of validators #11967

Closed
wants to merge 17 commits into from
Closed
Prev Previous commit
Next Next commit
cargo fmt
  • Loading branch information
Doordashcon committed Aug 14, 2022
commit 2b6e182096cba352a0c3c572c220386b4a183bfd
7 changes: 6 additions & 1 deletion bin/node/cli/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,12 @@ pub fn testnet_genesis(
staking: StakingConfig {
validator_count: initial_authorities.len() as u32,
minimum_validator_count: initial_authorities.len() as u32,
invulnerables: initial_authorities.iter().map(|x| x.0.clone()).collect::<Vec<_>>().try_into().unwrap(),
invulnerables: initial_authorities
.iter()
.map(|x| x.0.clone())
.collect::<Vec<_>>()
.try_into()
.unwrap(),
slash_reward_fraction: Perbill::from_percent(10),
stakers,
..Default::default()
Expand Down
16 changes: 9 additions & 7 deletions frame/session/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ use frame_support::{
ValidatorRegistration, ValidatorSet,
},
weights::Weight,
Parameter,
WeakBoundedVec,
Parameter, WeakBoundedVec,
};
use sp_runtime::{
traits::{AtLeast32BitUnsigned, Convert, Member, One, OpaqueKeys, Zero},
Expand Down Expand Up @@ -470,7 +469,8 @@ pub mod pallet {
);
self.keys.iter().map(|x| x.1.clone()).collect()
});
let initial_validators_bounded: WeakBoundedVec<T::ValidatorId, T::MaxValidators> = initial_validators_0.clone().try_into().expect("Too many initial validators");
let initial_validators_bounded: WeakBoundedVec<T::ValidatorId, T::MaxValidators> =
initial_validators_0.clone().try_into().expect("Too many initial validators");
assert!(
!initial_validators_0.is_empty(),
"Empty validator set for session 0 in genesis block!"
Expand Down Expand Up @@ -507,7 +507,8 @@ pub mod pallet {
/// The current set of validators.
#[pallet::storage]
#[pallet::getter(fn validators)]
pub type Validators<T: Config> = StorageValue<_, WeakBoundedVec<T::ValidatorId, T::MaxValidators>, ValueQuery>;
pub type Validators<T: Config> =
StorageValue<_, WeakBoundedVec<T::ValidatorId, T::MaxValidators>, ValueQuery>;

/// Current index of the session.
#[pallet::storage]
Expand All @@ -532,7 +533,8 @@ pub mod pallet {
/// a new set of identities.
#[pallet::storage]
#[pallet::getter(fn disabled_validators)]
pub type DisabledValidators<T: Config> = StorageValue<_, BoundedVec<u32, T::MaxValidators>, ValueQuery>;
pub type DisabledValidators<T: Config> =
StorageValue<_, BoundedVec<u32, T::MaxValidators>, ValueQuery>;

/// The next session keys for a validator.
#[pallet::storage]
Expand Down Expand Up @@ -654,7 +656,8 @@ impl<T: Config> Pallet<T> {
session_keys.iter().map(|(validator, _)| validator.clone()).collect::<Vec<_>>();

// TODO: Handle defensifely?
let bounded_validator_set: WeakBoundedVec<T::ValidatorId, T::MaxValidators> = validators.clone().try_into().expect("Max amount of validators reached");
let bounded_validator_set: WeakBoundedVec<T::ValidatorId, T::MaxValidators> =
validators.clone().try_into().expect("Max amount of validators reached");
Validators::<T>::put(bounded_validator_set);

if changed {
Expand Down Expand Up @@ -901,7 +904,6 @@ impl<T: Config> Pallet<T> {

fn clear_key_owner(id: KeyTypeId, key_data: &[u8]) {
<KeyOwner<T>>::remove((id, key_data));

}
}

Expand Down
6 changes: 4 additions & 2 deletions frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ pub mod pallet {
/// invulnerables) and restricted to testnets.
#[pallet::storage]
#[pallet::getter(fn invulnerables)]
pub type Invulnerables<T: Config> = StorageValue<_, BoundedVec<T::AccountId, T::MaxValidators>, ValueQuery>;
pub type Invulnerables<T: Config> =
StorageValue<_, BoundedVec<T::AccountId, T::MaxValidators>, ValueQuery>;

/// Map from all locked "stash" accounts to the controller account.
#[pallet::storage]
Expand Down Expand Up @@ -499,7 +500,8 @@ pub mod pallet {
/// the era ends.
#[pallet::storage]
#[pallet::getter(fn offending_validators)]
pub type OffendingValidators<T: Config> = StorageValue<_, BoundedVec<(u32, bool), T::MaxValidators>, ValueQuery>;
pub type OffendingValidators<T: Config> =
StorageValue<_, BoundedVec<(u32, bool), T::MaxValidators>, ValueQuery>;

/// True if network has been upgraded to this version.
/// Storage version of the pallet.
Expand Down
4 changes: 3 additions & 1 deletion frame/staking/src/slashing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,9 @@ fn add_offending_validator<T: Config>(stash: &T::AccountId, disable: bool) {
match offending.binary_search_by_key(&validator_index_u32, |(index, _)| *index) {
// this is a new offending validator
Err(index) => {
offending.try_insert(index, (validator_index_u32, disable)).expect("Limit reached");
offending
.try_insert(index, (validator_index_u32, disable))
.expect("Limit reached");

let offending_threshold =
T::OffendingValidatorsThreshold::get() * validators.len() as u32;
Expand Down