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

Adding Fallback on election failure #5093

Merged
merged 12 commits into from
Mar 26, 2022
29 changes: 26 additions & 3 deletions runtime/common/src/elections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

//! Code for elections.

use frame_election_provider_support::SortedListProvider;
use frame_election_provider_support::{
onchain::{BoundedExecutionConfig, ExecutionConfig, UnboundedExecution},
ElectionDataProvider, SequentialPhragmen, SortedListProvider,
};
use sp_std::{boxed::Box, marker::PhantomData};

/// Implements the weight types for the elections module and a specific
Expand Down Expand Up @@ -64,9 +67,29 @@ impl pallet_election_provider_multi_phase::BenchmarkingConfig for BenchmarkConfi
/// The accuracy type used for genesis election provider;
pub type OnOnChainAccuracy = sp_runtime::Perbill;

/// Election Configuration parameters
pub struct OnChainSeqPhragmen<T: frame_system::Config, S>(PhantomData<(T, S)>);
impl<
T: frame_system::Config,
S: ElectionDataProvider<AccountId = T::AccountId, BlockNumber = T::BlockNumber>,
> ExecutionConfig for OnChainSeqPhragmen<T, S>
{
type System = T;
type Solver = SequentialPhragmen<T::AccountId, OnOnChainAccuracy>;
type DataProvider = S;
}

impl<
T: frame_system::Config,
S: ElectionDataProvider<AccountId = T::AccountId, BlockNumber = T::BlockNumber>,
> BoundedExecutionConfig for OnChainSeqPhragmen<T, S>
{
type VotersBound = crate::ConstU32<20_000>;
type TargetsBound = crate::ConstU32<2_000>;
}

/// The election provider of the genesis
pub type GenesisElectionOf<T> =
frame_election_provider_support::onchain::OnChainSequentialPhragmen<T>;
pub type GenesisElectionOf<T, S> = UnboundedExecution<OnChainSeqPhragmen<T, S>>;

/// Implementation of `frame_election_provider_support::SortedListProvider` that updates the
/// bags-list but uses [`pallet_staking::Nominators`] for `iter`. This is meant to be a transitionary
Expand Down
26 changes: 12 additions & 14 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ use primitives::v2::{
SessionInfo, Signature, ValidationCode, ValidationCodeHash, ValidatorId, ValidatorIndex,
};
use runtime_common::{
auctions, claims, crowdloan, impl_runtime_weights, impls::DealWithFees, paras_registrar,
prod_or_fast, slots, BlockHashCount, BlockLength, CurrencyToVote, SlowAdjustingFeeUpdate,
auctions, claims, crowdloan, elections::OnChainSeqPhragmen, impl_runtime_weights,
impls::DealWithFees, paras_registrar, prod_or_fast, slots, BlockHashCount, BlockLength,
CurrencyToVote, SlowAdjustingFeeUpdate,
};
use sp_std::{cmp::Ordering, collections::btree_map::BTreeMap, prelude::*};

Expand All @@ -45,6 +46,9 @@ use runtime_parachains::{

use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId;
use beefy_primitives::crypto::AuthorityId as BeefyId;
use frame_election_provider_support::{
generate_solution_type, onchain::BoundedExecution, NposSolution, SequentialPhragmen,
};
use frame_support::{
construct_runtime, parameter_types,
traits::{
Expand Down Expand Up @@ -414,7 +418,7 @@ parameter_types! {

}

frame_election_provider_support::generate_solution_type!(
generate_solution_type!(
#[compact]
pub struct NposCompactSolution24::<
VoterIndex = u32,
Expand Down Expand Up @@ -444,10 +448,9 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type MinerTxPriority = NposSolutionPriority;
type DataProvider = Staking;
type Solution = NposCompactSolution24;
type Fallback = pallet_election_provider_multi_phase::NoFallback<Self>;
type GovernanceFallback =
frame_election_provider_support::onchain::OnChainSequentialPhragmen<Self>;
type Solver = frame_election_provider_support::SequentialPhragmen<
type Fallback = BoundedExecution<OnChainSeqPhragmen<Self, Staking>>;
type GovernanceFallback = BoundedExecution<OnChainSeqPhragmen<Self, Staking>>;
type Solver = SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Self>,
(),
Expand Down Expand Up @@ -546,26 +549,21 @@ parameter_types! {
pub const MaxNominatorRewardedPerValidator: u32 = 256;
pub const OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17);
// 24
pub const MaxNominations: u32 = <NposCompactSolution24 as frame_election_provider_support::NposSolution>::LIMIT as u32;
pub const MaxNominations: u32 = <NposCompactSolution24 as NposSolution>::LIMIT as u32;
}

type SlashCancelOrigin = EnsureOneOf<
EnsureRoot<AccountId>,
pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 1, 2>,
>;

impl frame_election_provider_support::onchain::Config for Runtime {
type Accuracy = runtime_common::elections::OnOnChainAccuracy;
type DataProvider = Staking;
}

impl pallet_staking::Config for Runtime {
type MaxNominations = MaxNominations;
type Currency = Balances;
type UnixTime = Timestamp;
type CurrencyToVote = CurrencyToVote;
type ElectionProvider = ElectionProviderMultiPhase;
type GenesisElectionProvider = runtime_common::elections::GenesisElectionOf<Self>;
type GenesisElectionProvider = runtime_common::elections::GenesisElectionOf<Self, Staking>;
type RewardRemainder = Treasury;
type Event = Event;
type Slash = Treasury;
Expand Down
24 changes: 11 additions & 13 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@

use pallet_transaction_payment::CurrencyAdapter;
use runtime_common::{
auctions, claims, crowdloan, impl_runtime_weights, impls::DealWithFees, paras_registrar,
prod_or_fast, slots, BlockHashCount, BlockLength, CurrencyToVote, SlowAdjustingFeeUpdate,
auctions, claims, crowdloan, elections::OnChainSeqPhragmen, impl_runtime_weights,
impls::DealWithFees, paras_registrar, prod_or_fast, slots, BlockHashCount, BlockLength,
CurrencyToVote, SlowAdjustingFeeUpdate,
};

use runtime_parachains::{
Expand All @@ -37,6 +38,9 @@ use runtime_parachains::{

use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId;
use beefy_primitives::crypto::AuthorityId as BeefyId;
use frame_election_provider_support::{
generate_solution_type, onchain::BoundedExecution, SequentialPhragmen,
};
use frame_support::{
construct_runtime, parameter_types,
traits::{
Expand Down Expand Up @@ -456,7 +460,7 @@ parameter_types! {
pub const MaxElectableTargets: u16 = u16::MAX;
}

frame_election_provider_support::generate_solution_type!(
generate_solution_type!(
#[compact]
pub struct NposCompactSolution16::<
VoterIndex = u32,
Expand Down Expand Up @@ -486,10 +490,9 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type MinerTxPriority = NposSolutionPriority;
type DataProvider = Staking;
type Solution = NposCompactSolution16;
type Fallback = pallet_election_provider_multi_phase::NoFallback<Self>;
type GovernanceFallback =
frame_election_provider_support::onchain::OnChainSequentialPhragmen<Self>;
type Solver = frame_election_provider_support::SequentialPhragmen<
type Fallback = BoundedExecution<OnChainSeqPhragmen<Self, Staking>>;
type GovernanceFallback = BoundedExecution<OnChainSeqPhragmen<Self, Staking>>;
type Solver = SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Self>,
(),
Expand Down Expand Up @@ -550,11 +553,6 @@ type SlashCancelOrigin = EnsureOneOf<
pallet_collective::EnsureProportionAtLeast<AccountId, CouncilCollective, 3, 4>,
>;

impl frame_election_provider_support::onchain::Config for Runtime {
type Accuracy = runtime_common::elections::OnOnChainAccuracy;
type DataProvider = Staking;
}

impl pallet_staking::Config for Runtime {
type MaxNominations = MaxNominations;
type Currency = Balances;
Expand All @@ -575,7 +573,7 @@ impl pallet_staking::Config for Runtime {
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
type NextNewSession = Session;
type ElectionProvider = ElectionProviderMultiPhase;
type GenesisElectionProvider = runtime_common::elections::GenesisElectionOf<Self>;
type GenesisElectionProvider = runtime_common::elections::GenesisElectionOf<Self, Staking>;
type SortedListProvider = BagsList;
type MaxUnlockingChunks = frame_support::traits::ConstU32<32>;
type BenchmarkingConfig = runtime_common::StakingBenchmarkingConfig;
Expand Down
15 changes: 6 additions & 9 deletions runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,6 @@ parameter_types! {
pub const MaxAuthorities: u32 = 100_000;
}

impl frame_election_provider_support::onchain::Config for Runtime {
type Accuracy = runtime_common::elections::OnOnChainAccuracy;
type DataProvider = Staking;
}

impl pallet_staking::Config for Runtime {
type MaxNominations = frame_support::pallet_prelude::ConstU32<16>;
type Currency = Balances;
Expand All @@ -342,10 +337,12 @@ impl pallet_staking::Config for Runtime {
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
type NextNewSession = Session;
type ElectionProvider =
frame_election_provider_support::onchain::OnChainSequentialPhragmen<Self>;
type GenesisElectionProvider =
frame_election_provider_support::onchain::OnChainSequentialPhragmen<Self>;
type ElectionProvider = frame_election_provider_support::onchain::UnboundedExecution<
runtime_common::elections::OnChainSeqPhragmen<Self, Staking>,
>;
type GenesisElectionProvider = frame_election_provider_support::onchain::UnboundedExecution<
runtime_common::elections::OnChainSeqPhragmen<Self, Staking>,
>;
// Use the nominator map to iter voter AND no-ops for all SortedListProvider hooks. The migration
// to bags-list is a no-op, but the storage version will be updated.
type SortedListProvider = pallet_staking::UseNominatorsMap<Runtime>;
Expand Down
20 changes: 8 additions & 12 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

use authority_discovery_primitives::AuthorityId as AuthorityDiscoveryId;
use beefy_primitives::crypto::AuthorityId as BeefyId;
use frame_election_provider_support::{onchain::BoundedExecution, SequentialPhragmen};
use frame_support::{
construct_runtime, parameter_types,
traits::{Contains, InstanceFilter, KeyOwnerProofSystem, OnRuntimeUpgrade},
Expand All @@ -42,8 +43,9 @@ use primitives::v2::{
ValidatorIndex, ValidatorSignature,
};
use runtime_common::{
assigned_slots, auctions, crowdloan, impl_runtime_weights, impls::ToAuthor, paras_registrar,
paras_sudo_wrapper, slots, BlockHashCount, BlockLength, CurrencyToVote, SlowAdjustingFeeUpdate,
assigned_slots, auctions, crowdloan, elections::OnChainSeqPhragmen, impl_runtime_weights,
impls::ToAuthor, paras_registrar, paras_sudo_wrapper, slots, BlockHashCount, BlockLength,
CurrencyToVote, SlowAdjustingFeeUpdate,
};
use runtime_parachains::{
configuration as parachains_configuration, disputes as parachains_disputes,
Expand Down Expand Up @@ -388,10 +390,9 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type MinerTxPriority = NposSolutionPriority;
type DataProvider = Staking;
type Solution = NposCompactSolution16;
type Fallback = pallet_election_provider_multi_phase::NoFallback<Self>;
type GovernanceFallback =
frame_election_provider_support::onchain::OnChainSequentialPhragmen<Self>;
type Solver = frame_election_provider_support::SequentialPhragmen<
type Fallback = BoundedExecution<OnChainSeqPhragmen<Self, Staking>>;
type GovernanceFallback = BoundedExecution<OnChainSeqPhragmen<Self, Staking>>;
type Solver = SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Self>,
(),
Expand Down Expand Up @@ -439,11 +440,6 @@ parameter_types! {
pub const MaxNominations: u32 = <NposCompactSolution16 as frame_election_provider_support::NposSolution>::LIMIT as u32;
}

impl frame_election_provider_support::onchain::Config for Runtime {
type Accuracy = runtime_common::elections::OnOnChainAccuracy;
type DataProvider = Staking;
}

impl pallet_staking::Config for Runtime {
type MaxNominations = MaxNominations;
type Currency = Balances;
Expand All @@ -464,7 +460,7 @@ impl pallet_staking::Config for Runtime {
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
type NextNewSession = Session;
type ElectionProvider = ElectionProviderMultiPhase;
type GenesisElectionProvider = runtime_common::elections::GenesisElectionOf<Self>;
type GenesisElectionProvider = runtime_common::elections::GenesisElectionOf<Self, Staking>;
type SortedListProvider = BagsList;
type MaxUnlockingChunks = frame_support::traits::ConstU32<32>;
type BenchmarkingConfig = runtime_common::StakingBenchmarkingConfig;
Expand Down