diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index 7fa0990ed8..1fe077cf4a 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -108,7 +108,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("aleph-node"), impl_name: create_runtime_str!("aleph-node"), authoring_version: 1, - spec_version: 41, + spec_version: 42, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 13, @@ -337,6 +337,7 @@ impl pallet_elections::Config for Runtime { type SessionPeriod = SessionPeriod; type SessionManager = pallet_session::historical::NoteHistoricalRoot; type ValidatorRewardsHandler = Staking; + type ValidatorExtractor = Staking; type MaximumBanReasonLength = MaximumBanReasonLength; } diff --git a/pallets/elections/src/impls.rs b/pallets/elections/src/impls.rs index ee7585b478..64bbbf7fe8 100644 --- a/pallets/elections/src/impls.rs +++ b/pallets/elections/src/impls.rs @@ -12,7 +12,7 @@ use sp_std::{ }; use crate::{ - traits::{EraInfoProvider, SessionInfoProvider, ValidatorRewardsHandler}, + traits::{EraInfoProvider, SessionInfoProvider, ValidatorExtractor, ValidatorRewardsHandler}, BanConfig, Banned, CommitteeSize, Config, CurrentEraValidators, NextEraCommitteeSize, NextEraNonReservedValidators, NextEraReservedValidators, Pallet, SessionValidatorBlockCount, UnderperformedValidatorSessionCount, ValidatorEraTotalReward, ValidatorTotalRewards, @@ -347,11 +347,16 @@ where } pub fn ban_validator(validator: &T::AccountId, reason: BanReason) { + // we do not ban reserved validators + if NextEraReservedValidators::::get().contains(validator) { + return; + } // current era is the latest planned era for which validators are already chosen // so we ban from the next era let start: EraIndex = T::EraInfoProvider::current_era() .unwrap_or(0) .saturating_add(1); + T::ValidatorExtractor::remove_validator(validator); Banned::::insert(validator, BanInfo { reason, start }); } diff --git a/pallets/elections/src/lib.rs b/pallets/elections/src/lib.rs index 41e3e8eb68..2f8ab75ca8 100644 --- a/pallets/elections/src/lib.rs +++ b/pallets/elections/src/lib.rs @@ -79,7 +79,9 @@ pub mod pallet { use sp_runtime::Perbill; use super::*; - use crate::traits::{EraInfoProvider, SessionInfoProvider, ValidatorRewardsHandler}; + use crate::traits::{ + EraInfoProvider, SessionInfoProvider, ValidatorExtractor, ValidatorRewardsHandler, + }; #[pallet::config] pub trait Config: frame_system::Config { @@ -100,6 +102,8 @@ pub mod pallet { type SessionInfoProvider: SessionInfoProvider; /// Something that handles addition of rewards for validators. type ValidatorRewardsHandler: ValidatorRewardsHandler; + /// Something that removes validators from candidates in elections + type ValidatorExtractor: ValidatorExtractor; /// Maximum acceptable ban reason length. #[pallet::constant] diff --git a/pallets/elections/src/mock.rs b/pallets/elections/src/mock.rs index 666787a9b0..492823bfb5 100644 --- a/pallets/elections/src/mock.rs +++ b/pallets/elections/src/mock.rs @@ -18,7 +18,9 @@ use sp_std::{cell::RefCell, collections::btree_set::BTreeSet}; use super::*; use crate as pallet_elections; -use crate::traits::{EraInfoProvider, SessionInfoProvider, ValidatorRewardsHandler}; +use crate::traits::{ + EraInfoProvider, SessionInfoProvider, ValidatorExtractor, ValidatorRewardsHandler, +}; type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic; type Block = frame_system::mocking::MockBlock; @@ -168,6 +170,12 @@ impl EraInfoProvider for MockProvider { } } +impl ValidatorExtractor for MockProvider { + type AccountId = AccountId; + + fn remove_validator(_who: &AccountId) {} +} + impl Config for Test { type EraInfoProvider = MockProvider; type RuntimeEvent = RuntimeEvent; @@ -176,6 +184,7 @@ impl Config for Test { type SessionManager = (); type SessionInfoProvider = MockProvider; type ValidatorRewardsHandler = MockProvider; + type ValidatorExtractor = MockProvider; type MaximumBanReasonLength = ConstU32<300>; } diff --git a/pallets/elections/src/traits.rs b/pallets/elections/src/traits.rs index f60172f266..9767622fe6 100644 --- a/pallets/elections/src/traits.rs +++ b/pallets/elections/src/traits.rs @@ -86,3 +86,21 @@ where pallet_staking::ErasStakers::::iter_key_prefix(era).collect() } } + +pub trait ValidatorExtractor { + type AccountId; + + /// Removes given validator from pallet's staking validators list + fn remove_validator(who: &Self::AccountId); +} + +impl ValidatorExtractor for pallet_staking::Pallet +where + T: pallet_staking::Config, +{ + type AccountId = T::AccountId; + + fn remove_validator(who: &Self::AccountId) { + pallet_staking::Pallet::::do_remove_validator(who); + } +}