bounding staking: BoundedElectionProvider trait (#12362)

* add a bounded election provider trait

* extract common trait election provider base

* fmt

* only bound the outer support vector

* fix tests

* docs

* fix rust docs

* fmt

* fix rustdocs

* docs

* improve docs

* small doc change
This commit is contained in:
Ankan
2022-09-28 22:52:16 +02:00
committed by GitHub
parent ea43466879
commit 6137c8707b
7 changed files with 99 additions and 55 deletions
@@ -231,7 +231,8 @@
use codec::{Decode, Encode};
use frame_election_provider_support::{
ElectionDataProvider, ElectionProvider, InstantElectionProvider, NposSolution,
ElectionDataProvider, ElectionProvider, ElectionProviderBase, InstantElectionProvider,
NposSolution,
};
use frame_support::{
dispatch::DispatchClass,
@@ -289,7 +290,7 @@ pub type SolutionTargetIndexOf<T> = <SolutionOf<T> as NposSolution>::TargetIndex
pub type SolutionAccuracyOf<T> =
<SolutionOf<<T as crate::Config>::MinerConfig> as NposSolution>::Accuracy;
/// The fallback election type.
pub type FallbackErrorOf<T> = <<T as crate::Config>::Fallback as ElectionProvider>::Error;
pub type FallbackErrorOf<T> = <<T as crate::Config>::Fallback as ElectionProviderBase>::Error;
/// Configuration for the benchmarks of the pallet.
pub trait BenchmarkingConfig {
@@ -312,7 +313,7 @@ pub trait BenchmarkingConfig {
/// A fallback implementation that transitions the pallet to the emergency phase.
pub struct NoFallback<T>(sp_std::marker::PhantomData<T>);
impl<T: Config> ElectionProvider for NoFallback<T> {
impl<T: Config> ElectionProviderBase for NoFallback<T> {
type AccountId = T::AccountId;
type BlockNumber = T::BlockNumber;
type DataProvider = T::DataProvider;
@@ -321,7 +322,9 @@ impl<T: Config> ElectionProvider for NoFallback<T> {
fn ongoing() -> bool {
false
}
}
impl<T: Config> ElectionProvider for NoFallback<T> {
fn elect() -> Result<Supports<T::AccountId>, Self::Error> {
// Do nothing, this will enable the emergency phase.
Err("NoFallback.")
@@ -1563,7 +1566,7 @@ impl<T: Config> Pallet<T> {
<QueuedSolution<T>>::take()
.ok_or(ElectionError::<T>::NothingQueued)
.or_else(|_| {
T::Fallback::elect()
<T::Fallback as ElectionProvider>::elect()
.map(|supports| ReadySolution {
supports,
score: Default::default(),
@@ -1598,7 +1601,7 @@ impl<T: Config> Pallet<T> {
}
}
impl<T: Config> ElectionProvider for Pallet<T> {
impl<T: Config> ElectionProviderBase for Pallet<T> {
type AccountId = T::AccountId;
type BlockNumber = T::BlockNumber;
type Error = ElectionError<T>;
@@ -1610,7 +1613,9 @@ impl<T: Config> ElectionProvider for Pallet<T> {
_ => true,
}
}
}
impl<T: Config> ElectionProvider for Pallet<T> {
fn elect() -> Result<Supports<T::AccountId>, Self::Error> {
match Self::do_elect() {
Ok(supports) => {
@@ -1627,7 +1632,6 @@ impl<T: Config> ElectionProvider for Pallet<T> {
}
}
}
/// convert a DispatchError to a custom InvalidTransaction with the inner code being the error
/// number.
pub fn dispatch_error_to_invalid(error: DispatchError) -> InvalidTransaction {
@@ -297,7 +297,7 @@ impl onchain::Config for OnChainSeqPhragmen {
}
pub struct MockFallback;
impl ElectionProvider for MockFallback {
impl ElectionProviderBase for MockFallback {
type AccountId = AccountId;
type BlockNumber = u64;
type Error = &'static str;
@@ -306,7 +306,8 @@ impl ElectionProvider for MockFallback {
fn ongoing() -> bool {
false
}
}
impl ElectionProvider for MockFallback {
fn elect() -> Result<Supports<AccountId>, Self::Error> {
Self::elect_with_bounds(Bounded::max_value(), Bounded::max_value())
}