Skip to content

Commit cbb48e5

Browse files
Ank4nkianenigma
andauthored
Bound Election and Staking by MaxActiveValidators (paritytech#12436)
* bounding election provider with kian * multi phase implement bounded election provider * election provider blanket implementation * staking compiles * fix test for election provider support * fmt * fixing epmp tests, does not compile yet * fix epmp tests * fix staking tests * fmt * fix runtime tests * fmt * remove outdated wip tags * add enum error * sort and truncate supports * comment * error when unsupported number of election winners * compiling wip after kian's suggestions * fix TODOs * remove,fix tags * ensure validator count does not exceed maxwinners * clean up * some more clean up and todos * handle too many winners * rename parameter for mock * todo * add sort and truncate rule if there are too many winners * fmt * fail, not swallow emergency result bound not met * remove too many winners resolution as it can be guaranteed to be bounded * fix benchmark * give MaxWinners more contextual name * make ready solution generic over T * kian feedback * fix stuff * Kian's way of solvign this * comment fix * fix compile * remove use of BoundedExecution * fmt * comment out failing integrity test * cap validator count increment to max winners * dont panic * add test for bad data provider * Update frame/staking/src/pallet/impls.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * fix namespace conflict and add test for onchain max winners less than desired targets * defensive unwrap * early convert to bounded vec * fix syntax * fmt * fix doc * fix rustdoc * fmt * fix maxwinner count for benchmarking * add instant election for noelection * fmt * fix compile * pr feedbacks * always error at validator count exceeding max winners * add useful error message * pr comments * import fix * add checked_desired_targets * fmt * fmt * fix rust doc Co-authored-by: parity-processbot <> Co-authored-by: kianenigma <kian@parity.io> Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
1 parent daf72d1 commit cbb48e5

File tree

21 files changed

+538
-312
lines changed

21 files changed

+538
-312
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ impl pallet_staking::Config for Runtime {
570570
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
571571
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
572572
type ElectionProvider = ElectionProviderMultiPhase;
573-
type GenesisElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
573+
type GenesisElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
574574
type VoterList = VoterList;
575575
// This a placeholder, to be introduced in the next PR as an instance of bags-list
576576
type TargetList = pallet_staking::UseValidatorsMap<Self>;
@@ -628,7 +628,14 @@ frame_election_provider_support::generate_solution_type!(
628628

629629
parameter_types! {
630630
pub MaxNominations: u32 = <NposSolution16 as frame_election_provider_support::NposSolution>::LIMIT as u32;
631-
pub MaxElectingVoters: u32 = 10_000;
631+
pub MaxElectingVoters: u32 = 40_000;
632+
pub MaxElectableTargets: u16 = 10_000;
633+
// OnChain values are lower.
634+
pub MaxOnChainElectingVoters: u32 = 5000;
635+
pub MaxOnChainElectableTargets: u16 = 1250;
636+
// The maximum winners that can be elected by the Election pallet which is equivalent to the
637+
// maximum active validators the staking pallet can have.
638+
pub MaxActiveValidators: u32 = 1000;
632639
}
633640

634641
/// The numbers configured here could always be more than the the maximum limits of staking pallet
@@ -679,11 +686,9 @@ impl onchain::Config for OnChainSeqPhragmen {
679686
>;
680687
type DataProvider = <Runtime as pallet_election_provider_multi_phase::Config>::DataProvider;
681688
type WeightInfo = frame_election_provider_support::weights::SubstrateWeight<Runtime>;
682-
}
683-
684-
impl onchain::BoundedConfig for OnChainSeqPhragmen {
685-
type VotersBound = MaxElectingVoters;
686-
type TargetsBound = ConstU32<2_000>;
689+
type MaxWinners = <Runtime as pallet_election_provider_multi_phase::Config>::MaxWinners;
690+
type VotersBound = MaxOnChainElectingVoters;
691+
type TargetsBound = MaxOnChainElectableTargets;
687692
}
688693

689694
impl pallet_election_provider_multi_phase::MinerConfig for Runtime {
@@ -726,11 +731,12 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
726731
type SlashHandler = (); // burn slashes
727732
type RewardHandler = (); // nothing to do upon rewards
728733
type DataProvider = Staking;
729-
type Fallback = onchain::BoundedExecution<OnChainSeqPhragmen>;
730-
type GovernanceFallback = onchain::BoundedExecution<OnChainSeqPhragmen>;
734+
type Fallback = onchain::OnChainExecution<OnChainSeqPhragmen>;
735+
type GovernanceFallback = onchain::OnChainExecution<OnChainSeqPhragmen>;
731736
type Solver = SequentialPhragmen<AccountId, SolutionAccuracyOf<Self>, OffchainRandomBalancing>;
732737
type ForceOrigin = EnsureRootOrHalfCouncil;
733-
type MaxElectableTargets = ConstU16<{ u16::MAX }>;
738+
type MaxElectableTargets = MaxElectableTargets;
739+
type MaxWinners = MaxActiveValidators;
734740
type MaxElectingVoters = MaxElectingVoters;
735741
type BenchmarkingConfig = ElectionProviderBenchmarkConfig;
736742
type WeightInfo = pallet_election_provider_multi_phase::weights::SubstrateWeight<Self>;

frame/babe/src/mock.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ impl onchain::Config for OnChainSeqPhragmen {
178178
type Solver = SequentialPhragmen<DummyValidatorId, Perbill>;
179179
type DataProvider = Staking;
180180
type WeightInfo = ();
181+
type MaxWinners = ConstU32<100>;
182+
type VotersBound = ConstU32<{ u32::MAX }>;
183+
type TargetsBound = ConstU32<{ u32::MAX }>;
181184
}
182185

183186
impl pallet_staking::Config for Test {
@@ -199,7 +202,7 @@ impl pallet_staking::Config for Test {
199202
type MaxNominatorRewardedPerValidator = ConstU32<64>;
200203
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
201204
type NextNewSession = Session;
202-
type ElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
205+
type ElectionProvider = onchain::OnChainExecution<OnChainSeqPhragmen>;
203206
type GenesisElectionProvider = Self::ElectionProvider;
204207
type VoterList = pallet_staking::UseNominatorsAndValidatorsMap<Self>;
205208
type TargetList = pallet_staking::UseValidatorsMap<Self>;

frame/election-provider-multi-phase/src/benchmarking.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,7 @@ frame_benchmarking::benchmarks! {
220220
let receiver = account("receiver", 0, SEED);
221221
let initial_balance = T::Currency::minimum_balance() * 10u32.into();
222222
T::Currency::make_free_balance_be(&receiver, initial_balance);
223-
let ready = ReadySolution {
224-
supports: vec![],
225-
score: Default::default(),
226-
compute: Default::default()
227-
};
223+
let ready = Default::default();
228224
let deposit: BalanceOf<T> = 10u32.into();
229225

230226
let reward: BalanceOf<T> = T::SignedRewardBase::get();
@@ -403,7 +399,7 @@ frame_benchmarking::benchmarks! {
403399
assert_eq!(raw_solution.solution.voter_count() as u32, a);
404400
assert_eq!(raw_solution.solution.unique_targets().len() as u32, d);
405401
}: {
406-
assert_ok!(<MultiPhase<T>>::feasibility_check(raw_solution, ElectionCompute::Unsigned));
402+
assert!(<MultiPhase<T>>::feasibility_check(raw_solution, ElectionCompute::Unsigned).is_ok());
407403
}
408404

409405
// NOTE: this weight is not used anywhere, but the fact that it should succeed when execution in

0 commit comments

Comments
 (0)