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

[Companion] Bound Election and Staking by MaxActiveValidators #6157

Merged
merged 12 commits into from
Nov 9, 2022
18 changes: 14 additions & 4 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ parameter_types! {
pub const MaxElectableTargets: u16 = u16::MAX;
pub NposSolutionPriority: TransactionPriority =
Perbill::from_percent(90) * TransactionPriority::max_value();
pub const MaxActiveValidators: u32 = 1000;
Ank4n marked this conversation as resolved.
Show resolved Hide resolved
}

generate_solution_type!(
Expand All @@ -428,6 +429,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 {
Expand Down Expand Up @@ -475,10 +479,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>,
Expand All @@ -489,6 +498,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! {
Expand Down Expand Up @@ -586,7 +596,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;
Expand Down
18 changes: 14 additions & 4 deletions runtime/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ 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;
pub const MaxActiveValidators: u32 = 1000;
Ank4n marked this conversation as resolved.
Show resolved Hide resolved
Ank4n marked this conversation as resolved.
Show resolved Hide resolved
}

generate_solution_type!(
Expand All @@ -423,6 +424,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 {
Expand Down Expand Up @@ -470,10 +474,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>,
Expand All @@ -487,6 +496,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! {
Expand Down Expand Up @@ -557,7 +567,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>;
Expand Down
10 changes: 8 additions & 2 deletions runtime/test-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 OnChainMaxWinners: u32 = 100;
Ank4n marked this conversation as resolved.
Show resolved Hide resolved
pub const MaxElectingVoters: u32 = u32::MAX;
pub const MaxElectableTargets: u16 = u16::MAX;
}

pub struct OnChainSeqPhragmen;
Expand All @@ -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 {
Expand All @@ -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>;
Expand Down
24 changes: 18 additions & 6 deletions runtime/westend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -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 {
Expand Down Expand Up @@ -430,11 +436,16 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type OffchainRepeat = OffchainRepeat;
type MinerTxPriority = NposSolutionPriority;
type DataProvider = Staking;
#[cfg(any(feature = "fast-runtime", feature = "runtime-benchmarks"))]
type Fallback = onchain::UnboundedExecution<OnChainSeqPhragmen>;
#[cfg(not(any(feature = "fast-runtime", feature = "runtime-benchmarks")))]
type Fallback = pallet_election_provider_multi_phase::NoFallback<Self>;
type GovernanceFallback = onchain::UnboundedExecution<OnChainSeqPhragmen>;
#[cfg(feature = "fast-runtime")]
type Fallback = onchain::OnChainExecution<OnChainSeqPhragmen>;
#[cfg(not(feature = "fast-runtime"))]
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>,
Expand All @@ -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! {
Expand Down Expand Up @@ -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>;
Expand Down
12 changes: 6 additions & 6 deletions utils/staking-miner/src/emergency_solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ 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 mut supports = ready_solution.supports.clone().into_inner();
Ank4n marked this conversation as resolved.
Show resolved Hide resolved
// 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)?;

Expand Down