From 9fcf17d31bd6a6e3545ef7f86135b37edc955095 Mon Sep 17 00:00:00 2001 From: Michal Swietek Date: Tue, 22 Nov 2022 09:28:47 +0100 Subject: [PATCH 1/7] Remove validators from candidates in elections when they're got benned --- pallets/elections/src/impls.rs | 3 ++- pallets/elections/src/lib.rs | 6 +++++- pallets/elections/src/traits.rs | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/pallets/elections/src/impls.rs b/pallets/elections/src/impls.rs index ee7585b478..286e074bfa 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, @@ -352,6 +352,7 @@ where 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 a8b96b8f82..ec1e9a063f 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/traits.rs b/pallets/elections/src/traits.rs index f60172f266..a475740319 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 valiators 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); + } +} From ba2c361dacc4ce0185ab9f97ed6e8e6606d3d1e5 Mon Sep 17 00:00:00 2001 From: Michal Swietek Date: Tue, 22 Nov 2022 09:32:26 +0100 Subject: [PATCH 2/7] Add missing config item --- bin/runtime/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index b929d4b36b..c78e4232b6 100644 --- a/bin/runtime/src/lib.rs +++ b/bin/runtime/src/lib.rs @@ -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; } From 0004003df66303ee30c5229590f08bf63a595040 Mon Sep 17 00:00:00 2001 From: Michal Swietek Date: Tue, 22 Nov 2022 09:34:54 +0100 Subject: [PATCH 3/7] bump spec version --- bin/runtime/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/runtime/src/lib.rs b/bin/runtime/src/lib.rs index c78e4232b6..65e3ee8182 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: 38, + spec_version: 39, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 13, From 8c481a3962ff60ef79e26c4e47fc2094fe3424ef Mon Sep 17 00:00:00 2001 From: Michal Swietek Date: Tue, 22 Nov 2022 10:03:59 +0100 Subject: [PATCH 4/7] Adjust mock --- pallets/elections/src/mock.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pallets/elections/src/mock.rs b/pallets/elections/src/mock.rs index daf629c084..5365cd2c7e 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 Event = Event; @@ -176,6 +184,7 @@ impl Config for Test { type SessionManager = (); type SessionInfoProvider = MockProvider; type ValidatorRewardsHandler = MockProvider; + type ValidatorExtractor = MockProvider; type MaximumBanReasonLength = ConstU32<300>; } From b6b56d0ba6ee0279f6981ba8ffecd68b8d90008f Mon Sep 17 00:00:00 2001 From: Michal Swietek Date: Mon, 28 Nov 2022 15:47:08 +0000 Subject: [PATCH 5/7] Don't ban nice guys --- pallets/elections/src/impls.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pallets/elections/src/impls.rs b/pallets/elections/src/impls.rs index 286e074bfa..4b93c11878 100644 --- a/pallets/elections/src/impls.rs +++ b/pallets/elections/src/impls.rs @@ -347,6 +347,10 @@ 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() From 74f334b2f762eae54a3ac7ffc1a5ece021767681 Mon Sep 17 00:00:00 2001 From: Michal Swietek Date: Mon, 28 Nov 2022 15:54:20 +0000 Subject: [PATCH 6/7] fmt --- pallets/elections/src/impls.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/elections/src/impls.rs b/pallets/elections/src/impls.rs index 4b93c11878..64bbbf7fe8 100644 --- a/pallets/elections/src/impls.rs +++ b/pallets/elections/src/impls.rs @@ -349,7 +349,7 @@ where pub fn ban_validator(validator: &T::AccountId, reason: BanReason) { // we do not ban reserved validators if NextEraReservedValidators::::get().contains(validator) { - return + return; } // current era is the latest planned era for which validators are already chosen // so we ban from the next era From 07ff5fb8538b4eb6dc22e8da10ecabd20907b638 Mon Sep 17 00:00:00 2001 From: Michal Swietek Date: Tue, 29 Nov 2022 08:35:13 +0000 Subject: [PATCH 7/7] typo --- pallets/elections/src/traits.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/elections/src/traits.rs b/pallets/elections/src/traits.rs index a475740319..9767622fe6 100644 --- a/pallets/elections/src/traits.rs +++ b/pallets/elections/src/traits.rs @@ -90,7 +90,7 @@ where pub trait ValidatorExtractor { type AccountId; - /// Removes given validator from pallet's staking valiators list + /// Removes given validator from pallet's staking validators list fn remove_validator(who: &Self::AccountId); }