[Companion] Bound Election and Staking by MaxActiveValidators (#6157)

* add maximum winners to multi phase election provider

* fallback to noelection

* fmt

* missing values

* convert boundedvec to inner before sort

* dont clone

* pr feedback

* update lockfile for {"substrate"}

* run onchain election on westend benchmark

Co-authored-by: parity-processbot <>
This commit is contained in:
Ankan
2022-11-09 12:00:03 +01:00
committed by GitHub
parent 6d7f33e612
commit a3fefccbeb
6 changed files with 252 additions and 246 deletions
+187 -225
View File
File diff suppressed because it is too large Load Diff
+16 -4
View File
@@ -411,6 +411,9 @@ parameter_types! {
pub const MaxElectableTargets: u16 = u16::MAX;
pub NposSolutionPriority: TransactionPriority =
Perbill::from_percent(90) * TransactionPriority::max_value();
/// Setup election pallet to support maximum winners upto 2000. This will mean Staking Pallet
/// cannot have active validators higher than this count.
pub const MaxActiveValidators: u32 = 2000;
}
generate_solution_type!(
@@ -429,6 +432,9 @@ impl onchain::Config for OnChainSeqPhragmen {
type Solver = SequentialPhragmen<AccountId, runtime_common::elections::OnChainAccuracy>;
type DataProvider = Staking;
type WeightInfo = weights::frame_election_provider_support::WeightInfo<Runtime>;
type MaxWinners = MaxActiveValidators;
type VotersBound = MaxElectingVoters;
type TargetsBound = MaxElectableTargets;
}
impl pallet_election_provider_multi_phase::MinerConfig for Runtime {
@@ -476,10 +482,15 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type MinerTxPriority = NposSolutionPriority;
type DataProvider = Staking;
#[cfg(feature = "fast-runtime")]
type Fallback = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type Fallback = onchain::OnChainExecution<OnChainSeqPhragmen>;
#[cfg(not(feature = "fast-runtime"))]
type Fallback = pallet_election_provider_multi_phase::NoFallback<Self>;
type GovernanceFallback = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type Fallback = frame_election_provider_support::NoElection<(
AccountId,
BlockNumber,
Staking,
MaxActiveValidators,
)>;
type GovernanceFallback = onchain::OnChainExecution<OnChainSeqPhragmen>;
type Solver = SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Self>,
@@ -490,6 +501,7 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type WeightInfo = weights::pallet_election_provider_multi_phase::WeightInfo<Self>;
type MaxElectingVoters = MaxElectingVoters;
type MaxElectableTargets = MaxElectableTargets;
type MaxWinners = MaxActiveValidators;
}
parameter_types! {
@@ -553,7 +565,7 @@ impl pallet_staking::Config for Runtime {
type UnixTime = Timestamp;
type CurrencyToVote = CurrencyToVote;
type ElectionProvider = ElectionProviderMultiPhase;
type GenesisElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type GenesisElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
type RewardRemainder = Treasury;
type RuntimeEvent = RuntimeEvent;
type Slash = Treasury;
+16 -4
View File
@@ -406,6 +406,9 @@ parameter_types! {
/// ... and all of the validators as electable targets. Whilst this is the case, we cannot and
/// shall not increase the size of the validator intentions.
pub const MaxElectableTargets: u16 = u16::MAX;
/// Setup election pallet to support maximum winners upto 1200. This will mean Staking Pallet
/// cannot have active validators higher than this count.
pub const MaxActiveValidators: u32 = 1200;
}
generate_solution_type!(
@@ -424,6 +427,9 @@ impl onchain::Config for OnChainSeqPhragmen {
type Solver = SequentialPhragmen<AccountId, runtime_common::elections::OnChainAccuracy>;
type DataProvider = Staking;
type WeightInfo = weights::frame_election_provider_support::WeightInfo<Runtime>;
type MaxWinners = MaxActiveValidators;
type VotersBound = MaxElectingVoters;
type TargetsBound = MaxElectableTargets;
}
impl pallet_election_provider_multi_phase::MinerConfig for Runtime {
@@ -471,10 +477,15 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type MinerTxPriority = NposSolutionPriority;
type DataProvider = Staking;
#[cfg(feature = "fast-runtime")]
type Fallback = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type Fallback = onchain::OnChainExecution<OnChainSeqPhragmen>;
#[cfg(not(feature = "fast-runtime"))]
type Fallback = pallet_election_provider_multi_phase::NoFallback<Self>;
type GovernanceFallback = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type Fallback = frame_election_provider_support::NoElection<(
AccountId,
BlockNumber,
Staking,
MaxActiveValidators,
)>;
type GovernanceFallback = onchain::OnChainExecution<OnChainSeqPhragmen>;
type Solver = SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Self>,
@@ -488,6 +499,7 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type WeightInfo = weights::pallet_election_provider_multi_phase::WeightInfo<Self>;
type MaxElectingVoters = MaxElectingVoters;
type MaxElectableTargets = MaxElectableTargets;
type MaxWinners = MaxActiveValidators;
}
parameter_types! {
@@ -587,7 +599,7 @@ impl pallet_staking::Config for Runtime {
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
type NextNewSession = Session;
type ElectionProvider = ElectionProviderMultiPhase;
type GenesisElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type GenesisElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
type VoterList = VoterList;
type TargetList = UseValidatorsMap<Self>;
type MaxUnlockingChunks = frame_support::traits::ConstU32<32>;
+8 -2
View File
@@ -319,6 +319,9 @@ parameter_types! {
pub storage MaxNominatorRewardedPerValidator: u32 = 64;
pub storage OffendingValidatorsThreshold: Perbill = Perbill::from_percent(17);
pub const MaxAuthorities: u32 = 100_000;
pub const OnChainMaxWinners: u32 = u32::MAX;
pub const MaxElectingVoters: u32 = u32::MAX;
pub const MaxElectableTargets: u16 = u16::MAX;
}
pub struct OnChainSeqPhragmen;
@@ -327,6 +330,9 @@ impl onchain::Config for OnChainSeqPhragmen {
type Solver = SequentialPhragmen<AccountId, runtime_common::elections::OnChainAccuracy>;
type DataProvider = Staking;
type WeightInfo = ();
type MaxWinners = OnChainMaxWinners;
type VotersBound = MaxElectingVoters;
type TargetsBound = MaxElectableTargets;
}
impl pallet_staking::Config for Runtime {
@@ -349,8 +355,8 @@ impl pallet_staking::Config for Runtime {
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
type NextNewSession = Session;
type ElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type GenesisElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type ElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
type GenesisElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
// 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 VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Runtime>;
+16 -4
View File
@@ -366,6 +366,9 @@ parameter_types! {
/// ... and all of the validators as electable targets. Whilst this is the case, we cannot and
/// shall not increase the size of the validator intentions.
pub const MaxElectableTargets: u16 = u16::MAX;
// Maximum winners that can be chosen as active validators
pub const MaxActiveValidators: u32 = 1000;
}
frame_election_provider_support::generate_solution_type!(
@@ -384,6 +387,9 @@ impl onchain::Config for OnChainSeqPhragmen {
type Solver = SequentialPhragmen<AccountId, OnChainAccuracy>;
type DataProvider = Staking;
type WeightInfo = weights::frame_election_provider_support::WeightInfo<Runtime>;
type MaxWinners = MaxActiveValidators;
type VotersBound = MaxElectingVoters;
type TargetsBound = MaxElectableTargets;
}
impl pallet_election_provider_multi_phase::MinerConfig for Runtime {
@@ -431,10 +437,15 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type MinerTxPriority = NposSolutionPriority;
type DataProvider = Staking;
#[cfg(any(feature = "fast-runtime", feature = "runtime-benchmarks"))]
type Fallback = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type Fallback = onchain::OnChainExecution<OnChainSeqPhragmen>;
#[cfg(not(any(feature = "fast-runtime", feature = "runtime-benchmarks")))]
type Fallback = pallet_election_provider_multi_phase::NoFallback<Self>;
type GovernanceFallback = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type Fallback = frame_election_provider_support::NoElection<(
AccountId,
BlockNumber,
Staking,
MaxActiveValidators,
)>;
type GovernanceFallback = onchain::OnChainExecution<OnChainSeqPhragmen>;
type Solver = SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Self>,
@@ -445,6 +456,7 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type WeightInfo = weights::pallet_election_provider_multi_phase::WeightInfo<Self>;
type MaxElectingVoters = MaxElectingVoters;
type MaxElectableTargets = MaxElectableTargets;
type MaxWinners = MaxActiveValidators;
}
parameter_types! {
@@ -505,7 +517,7 @@ impl pallet_staking::Config for Runtime {
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
type NextNewSession = Session;
type ElectionProvider = ElectionProviderMultiPhase;
type GenesisElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type GenesisElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
type VoterList = VoterList;
type TargetList = UseValidatorsMap<Self>;
type MaxUnlockingChunks = frame_support::traits::ConstU32<32>;
@@ -36,21 +36,23 @@ macro_rules! emergency_solution_cmd_for { ($runtime:ident) => { paste::paste! {
log::info!(target: LOG_TARGET, "mined solution with {:?}", &raw_solution.score);
let mut ready_solution = EPM::Pallet::<Runtime>::feasibility_check(raw_solution, EPM::ElectionCompute::Signed)?;
let ready_solution = EPM::Pallet::<Runtime>::feasibility_check(raw_solution, EPM::ElectionCompute::Signed)?;
let encoded_size = ready_solution.encoded_size();
let score = ready_solution.score;
let mut supports = ready_solution.supports.into_inner();
// maybe truncate.
if let Some(take) = config.take {
log::info!(target: LOG_TARGET, "truncating {} winners to {}", ready_solution.supports.len(), take);
ready_solution.supports.sort_unstable_by_key(|(_, s)| s.total);
ready_solution.supports.truncate(take);
log::info!(target: LOG_TARGET, "truncating {} winners to {}", supports.len(), take);
supports.sort_unstable_by_key(|(_, s)| s.total);
supports.truncate(take);
}
// write to file and stdout.
let encoded_support = ready_solution.supports.encode();
let encoded_support = supports.encode();
let mut supports_file = std::fs::File::create("solution.supports.bin")?;
supports_file.write_all(&encoded_support)?;
log::info!(target: LOG_TARGET, "ReadySolution: size {:?} / score = {:?}", ready_solution.encoded_size(), ready_solution.score);
log::info!(target: LOG_TARGET, "ReadySolution: size {:?} / score = {:?}", encoded_size, score);
log::trace!(target: LOG_TARGET, "Supports: {}", sp_core::hexdisplay::HexDisplay::from(&encoded_support));
Ok(())