Skip to content

Commit

Permalink
Chillout (#749)
Browse files Browse the repository at this point in the history
* Remove validators from candidates in elections when they're got benned

* Add missing config item

* bump spec version

* Adjust mock

* Don't ban nice guys

* fmt

* typo

Co-authored-by: Marcin <[email protected]>
  • Loading branch information
mike1729 and Marcin-Radecki authored Nov 30, 2022
1 parent 4ca1123 commit 9415bd7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
3 changes: 2 additions & 1 deletion bin/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -337,6 +337,7 @@ impl pallet_elections::Config for Runtime {
type SessionPeriod = SessionPeriod;
type SessionManager = pallet_session::historical::NoteHistoricalRoot<Runtime, Staking>;
type ValidatorRewardsHandler = Staking;
type ValidatorExtractor = Staking;
type MaximumBanReasonLength = MaximumBanReasonLength;
}

Expand Down
7 changes: 6 additions & 1 deletion pallets/elections/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -347,11 +347,16 @@ where
}

pub fn ban_validator(validator: &T::AccountId, reason: BanReason) {
// we do not ban reserved validators
if NextEraReservedValidators::<T>::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::<T>::insert(validator, BanInfo { reason, start });
}

Expand Down
6 changes: 5 additions & 1 deletion pallets/elections/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -100,6 +102,8 @@ pub mod pallet {
type SessionInfoProvider: SessionInfoProvider<Self>;
/// Something that handles addition of rewards for validators.
type ValidatorRewardsHandler: ValidatorRewardsHandler<Self>;
/// Something that removes validators from candidates in elections
type ValidatorExtractor: ValidatorExtractor<AccountId = Self::AccountId>;

/// Maximum acceptable ban reason length.
#[pallet::constant]
Expand Down
11 changes: 10 additions & 1 deletion pallets/elections/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Test>;
type Block = frame_system::mocking::MockBlock<Test>;
Expand Down Expand Up @@ -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;
Expand All @@ -176,6 +184,7 @@ impl Config for Test {
type SessionManager = ();
type SessionInfoProvider = MockProvider;
type ValidatorRewardsHandler = MockProvider;
type ValidatorExtractor = MockProvider;
type MaximumBanReasonLength = ConstU32<300>;
}

Expand Down
18 changes: 18 additions & 0 deletions pallets/elections/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,21 @@ where
pallet_staking::ErasStakers::<T>::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<T> ValidatorExtractor for pallet_staking::Pallet<T>
where
T: pallet_staking::Config,
{
type AccountId = T::AccountId;

fn remove_validator(who: &Self::AccountId) {
pallet_staking::Pallet::<T>::do_remove_validator(who);
}
}

0 comments on commit 9415bd7

Please sign in to comment.