Add a bounded fallback on failed elections (#10988)

* Allow `pallet-election-provider` to accept smaller
solutions, issue #9478

* Fixing a typo

* Adding some more tests
Removing a seemingly outdated comment

* making it a URL

* Updating test name as per suggestion

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Updating documentation to be more explicit

And to follow the general guidelines

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Fixing formatting

* `Fallback` now of type `InstantElectionProvider`
Some cleanups

* Allow `pallet-election-provider` to accept smaller
solutions, issue #9478

* Fixing a typo

* Adding some more tests
Removing a seemingly outdated comment

* making it a URL

* Updating test name as per suggestion

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Updating documentation to be more explicit

And to follow the general guidelines

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* Fixing formatting

* `Fallback` now of type `InstantElectionProvider`
Some cleanups

* Merging types into one type with generics

* Removing `ConstUSize` and use `ConstU32`

* cleaning up the code

* deprecating `OnChainSequentialPhragmen`
Renaming it to `UnboundedSequentialPhragmen` which should only be used
at genesis and for testing.
Use preferrably `BoundedOnChainSequentialPhragmen`

* Amending docs

* Adding some explicit imports

* Implementing generic `BoundedOnchainExecution`
Removing the deprecated `OnChainSequentialPhragmen`

* Use the right Balancing strategy

* Refactoring `onchain::Config`
Creating `onchain::ExecutionConfig`

* Merge master

* fmt

* Name cleanups after review suggestions

* cosmetics

* renaming `instant_elect` to `elect_with_bounds`
Other corresponding changes as per @kianenigma feedback

* `BoundedOnchainExecution` -> `BoundedExecution`
And `UnboundedOnchainExecution` -> `UnboundedExecution`

* feedback from kian

* fmt + unneeded import

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: kianenigma <kian@parity.io>
This commit is contained in:
Georges
2022-03-25 20:15:50 +00:00
committed by GitHub
parent cbfb1f0e3b
commit e9bfdd9a0d
11 changed files with 225 additions and 137 deletions
+21 -13
View File
@@ -23,7 +23,7 @@
#![recursion_limit = "256"]
use codec::{Decode, Encode, MaxEncodedLen};
use frame_election_provider_support::{onchain, ExtendedBalance, VoteWeight};
use frame_election_provider_support::{onchain, ExtendedBalance, SequentialPhragmen, VoteWeight};
use frame_support::{
construct_runtime,
pallet_prelude::Get,
@@ -557,7 +557,7 @@ impl pallet_staking::Config for Runtime {
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
type OffendingValidatorsThreshold = OffendingValidatorsThreshold;
type ElectionProvider = ElectionProviderMultiPhase;
type GenesisElectionProvider = onchain::OnChainSequentialPhragmen<Self>;
type GenesisElectionProvider = onchain::UnboundedExecution<OnChainSeqPhragmen>;
type VoterList = BagsList;
type MaxUnlockingChunks = ConstU32<32>;
type WeightInfo = pallet_staking::weights::SubstrateWeight<Runtime>;
@@ -642,9 +642,19 @@ impl Get<Option<(usize, ExtendedBalance)>> for OffchainRandomBalancing {
}
}
impl onchain::Config for Runtime {
type Accuracy = Perbill;
type DataProvider = <Self as pallet_election_provider_multi_phase::Config>::DataProvider;
pub struct OnChainSeqPhragmen;
impl onchain::ExecutionConfig for OnChainSeqPhragmen {
type System = Runtime;
type Solver = SequentialPhragmen<
AccountId,
pallet_election_provider_multi_phase::SolutionAccuracyOf<Runtime>,
>;
type DataProvider = <Runtime as pallet_election_provider_multi_phase::Config>::DataProvider;
}
impl onchain::BoundedExecutionConfig for OnChainSeqPhragmen {
type VotersBound = ConstU32<20_000>;
type TargetsBound = ConstU32<2_000>;
}
impl pallet_election_provider_multi_phase::Config for Runtime {
@@ -668,13 +678,9 @@ impl pallet_election_provider_multi_phase::Config for Runtime {
type RewardHandler = (); // nothing to do upon rewards
type DataProvider = Staking;
type Solution = NposSolution16;
type Fallback = pallet_election_provider_multi_phase::NoFallback<Self>;
type GovernanceFallback = onchain::OnChainSequentialPhragmen<Self>;
type Solver = frame_election_provider_support::SequentialPhragmen<
AccountId,
SolutionAccuracyOf<Self>,
OffchainRandomBalancing,
>;
type Fallback = onchain::BoundedExecution<OnChainSeqPhragmen>;
type GovernanceFallback = onchain::BoundedExecution<OnChainSeqPhragmen>;
type Solver = SequentialPhragmen<AccountId, SolutionAccuracyOf<Self>, OffchainRandomBalancing>;
type ForceOrigin = EnsureRootOrHalfCouncil;
type MaxElectableTargets = ConstU16<{ u16::MAX }>;
type MaxElectingVoters = MaxElectingVoters;
@@ -1899,6 +1905,7 @@ impl_runtime_apis! {
#[cfg(test)]
mod tests {
use super::*;
use frame_election_provider_support::NposSolution;
use frame_system::offchain::CreateSignedTransaction;
use sp_runtime::UpperOf;
@@ -1915,7 +1922,8 @@ mod tests {
#[test]
fn perbill_as_onchain_accuracy() {
type OnChainAccuracy = <Runtime as onchain::Config>::Accuracy;
type OnChainAccuracy =
<<Runtime as pallet_election_provider_multi_phase::Config>::Solution as NposSolution>::Accuracy;
let maximum_chain_accuracy: Vec<UpperOf<OnChainAccuracy>> = (0..MaxNominations::get())
.map(|_| <UpperOf<OnChainAccuracy>>::from(OnChainAccuracy::one().deconstruct()))
.collect();