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
@@ -272,11 +272,13 @@ parameter_types! {
pub static MaxElectableTargets: TargetIndex = TargetIndex::max_value();
pub static EpochLength: u64 = 30;
pub static OnChianFallback: bool = true;
pub static OnChainFallback: bool = true;
}
impl onchain::Config for Runtime {
type Accuracy = sp_runtime::Perbill;
pub struct OnChainSeqPhragmen;
impl onchain::ExecutionConfig for OnChainSeqPhragmen {
type System = Runtime;
type Solver = SequentialPhragmen<AccountId, SolutionAccuracyOf<Runtime>, Balancing>;
type DataProvider = StakingMock;
}
@@ -288,11 +290,23 @@ impl ElectionProvider for MockFallback {
type DataProvider = StakingMock;
fn elect() -> Result<Supports<AccountId>, Self::Error> {
if OnChianFallback::get() {
onchain::OnChainSequentialPhragmen::<Runtime>::elect()
.map_err(|_| "OnChainSequentialPhragmen failed")
Self::elect_with_bounds(Bounded::max_value(), Bounded::max_value())
}
}
impl InstantElectionProvider for MockFallback {
fn elect_with_bounds(
max_voters: usize,
max_targets: usize,
) -> Result<Supports<Self::AccountId>, Self::Error> {
if OnChainFallback::get() {
onchain::UnboundedExecution::<OnChainSeqPhragmen>::elect_with_bounds(
max_voters,
max_targets,
)
.map_err(|_| "UnboundedExecution failed")
} else {
super::NoFallback::<Runtime>::elect()
super::NoFallback::<Runtime>::elect_with_bounds(max_voters, max_targets)
}
}
}
@@ -532,7 +546,7 @@ impl ExtBuilder {
self
}
pub fn onchain_fallback(self, onchain: bool) -> Self {
<OnChianFallback>::set(onchain);
<OnChainFallback>::set(onchain);
self
}
pub fn miner_weight(self, weight: Weight) -> Self {