frame_election_provider_support/
lib.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Primitive traits for providing election functionality.
19//!
20//! This crate provides two traits that could interact to enable extensible election functionality
21//! within FRAME pallets.
22//!
23//! Something that will provide the functionality of election will implement
24//! [`ElectionProvider`] and its parent-trait [`ElectionProviderBase`], whilst needing an
25//! associated [`ElectionProviderBase::DataProvider`], which needs to be
26//! fulfilled by an entity implementing [`ElectionDataProvider`]. Most often, *the data provider is*
27//! the receiver of the election, resulting in a diagram as below:
28//!
29//! ```ignore
30//!                                         ElectionDataProvider
31//!                          <------------------------------------------+
32//!                          |                                          |
33//!                          v                                          |
34//!                    +-----+----+                              +------+---+
35//!                    |          |                              |          |
36//! pallet-do-election |          |                              |          | pallet-needs-election
37//!                    |          |                              |          |
38//!                    |          |                              |          |
39//!                    +-----+----+                              +------+---+
40//!                          |                                          ^
41//!                          |                                          |
42//!                          +------------------------------------------+
43//!                                         ElectionProvider
44//! ```
45//!
46//! > It could also be possible that a third party pallet (C), provides the data of election to an
47//! > election provider (B), which then passes the election result to another pallet (A).
48//!
49//! ## Election Types
50//!
51//! Typically, two types of elections exist:
52//!
53//! 1. **Stateless**: Election data is provided, and the election result is immediately ready.
54//! 2. **Stateful**: Election data is is queried ahead of time, and the election result might be
55//!    ready some number of blocks in the future.
56//!
57//! To accommodate both type of elections in one trait, the traits lean toward **stateful
58//! election**, as it is more general than the stateless. This is why [`ElectionProvider::elect`]
59//! has no parameters. All value and type parameter must be provided by the [`ElectionDataProvider`]
60//! trait, even if the election happens immediately.
61//!
62//! ## Election Data
63//!
64//! The data associated with an election, essentially what the [`ElectionDataProvider`] must convey
65//! is as follows:
66//!
67//! 1. A list of voters, with their stake.
68//! 2. A list of targets (i.e. _candidates_).
69//! 3. A number of desired targets to be elected (i.e. _winners_)
70//!
71//! In addition to that, the [`ElectionDataProvider`] must also hint [`ElectionProvider`] at when
72//! the next election might happen ([`ElectionDataProvider::next_election_prediction`]). A stateless
73//! election provider would probably ignore this. A stateful election provider can use this to
74//! prepare the election result in advance.
75//!
76//! Nonetheless, an [`ElectionProvider`] shan't rely on this and should preferably provide some
77//! means of fallback election as well, in case the `elect` was called immaturely early.
78//!
79//! ## Example
80//!
81//! ```rust
82//! # use frame_election_provider_support::{*, data_provider};
83//! # use sp_npos_elections::{Support, Assignment};
84//! # use frame_support::traits::ConstU32;
85//! # use sp_runtime::bounded_vec;
86//!
87//! type AccountId = u64;
88//! type Balance = u64;
89//! type BlockNumber = u32;
90//!
91//! mod data_provider_mod {
92//!     use super::*;
93//!
94//!     pub trait Config: Sized {
95//!         type ElectionProvider: ElectionProvider<
96//!             AccountId = AccountId,
97//!             BlockNumber = BlockNumber,
98//!             DataProvider = Pallet<Self>,
99//!         >;
100//!     }
101//!
102//!     pub struct Pallet<T: Config>(std::marker::PhantomData<T>);
103//!
104//!     impl<T: Config> ElectionDataProvider for Pallet<T> {
105//!         type AccountId = AccountId;
106//!         type BlockNumber = BlockNumber;
107//!         type MaxVotesPerVoter = ConstU32<1>;
108//!
109//!         fn desired_targets() -> data_provider::Result<u32> {
110//!             Ok(1)
111//!         }
112//!         fn electing_voters(bounds: DataProviderBounds)
113//!           -> data_provider::Result<Vec<VoterOf<Self>>>
114//!         {
115//!             Ok(Default::default())
116//!         }
117//!         fn electable_targets(bounds: DataProviderBounds) -> data_provider::Result<Vec<AccountId>> {
118//!             Ok(vec![10, 20, 30])
119//!         }
120//!         fn next_election_prediction(now: BlockNumber) -> BlockNumber {
121//!             0
122//!         }
123//!     }
124//! }
125//!
126//!
127//! mod generic_election_provider {
128//!     use super::*;
129//!
130//!     pub struct GenericElectionProvider<T: Config>(std::marker::PhantomData<T>);
131//!
132//!     pub trait Config {
133//!         type DataProvider: ElectionDataProvider<AccountId=AccountId, BlockNumber = BlockNumber>;
134//!     }
135//!
136//!     impl<T: Config> ElectionProviderBase for GenericElectionProvider<T> {
137//!         type AccountId = AccountId;
138//!         type BlockNumber = BlockNumber;
139//!         type Error = &'static str;
140//!         type DataProvider = T::DataProvider;
141//!         type MaxWinners = ConstU32<{ u32::MAX }>;
142//!
143//!     }
144//!
145//!     impl<T: Config> ElectionProvider for GenericElectionProvider<T> {
146//!         fn ongoing() -> bool { false }
147//!         fn elect() -> Result<BoundedSupportsOf<Self>, Self::Error> {
148//!             Self::DataProvider::electable_targets(DataProviderBounds::default())
149//!                 .map_err(|_| "failed to elect")
150//!                 .map(|t| bounded_vec![(t[0], Support::default())])
151//!         }
152//!     }
153//! }
154//!
155//! mod runtime {
156//!     use super::generic_election_provider;
157//!     use super::data_provider_mod;
158//!     use super::AccountId;
159//!
160//!     struct Runtime;
161//!     impl generic_election_provider::Config for Runtime {
162//!         type DataProvider = data_provider_mod::Pallet<Runtime>;
163//!     }
164//!
165//!     impl data_provider_mod::Config for Runtime {
166//!         type ElectionProvider = generic_election_provider::GenericElectionProvider<Runtime>;
167//!     }
168//!
169//! }
170//!
171//! # fn main() {}
172//! ```
173
174#![cfg_attr(not(feature = "std"), no_std)]
175
176pub mod bounds;
177pub mod onchain;
178pub mod traits;
179
180extern crate alloc;
181
182use alloc::{boxed::Box, vec::Vec};
183use core::fmt::Debug;
184use sp_runtime::{
185	traits::{Bounded, Saturating, Zero},
186	RuntimeDebug,
187};
188
189pub use bounds::DataProviderBounds;
190pub use codec::{Decode, Encode};
191/// Re-export the solution generation macro.
192pub use frame_election_provider_solution_type::generate_solution_type;
193pub use frame_support::{traits::Get, weights::Weight, BoundedVec};
194/// Re-export some type as they are used in the interface.
195pub use sp_arithmetic::PerThing;
196pub use sp_npos_elections::{
197	Assignment, BalancingConfig, BoundedSupports, ElectionResult, Error, ExtendedBalance,
198	IdentifierT, PerThing128, Support, Supports, VoteWeight,
199};
200pub use traits::NposSolution;
201
202#[cfg(feature = "try-runtime")]
203use sp_runtime::TryRuntimeError;
204
205// re-export for the solution macro, with the dependencies of the macro.
206#[doc(hidden)]
207pub mod private {
208	pub use alloc::{
209		collections::{btree_map::BTreeMap, btree_set::BTreeSet},
210		vec::Vec,
211	};
212	pub use codec;
213	pub use scale_info;
214	pub use sp_arithmetic;
215
216	// Simple Extension trait to easily convert `None` from index closures to `Err`.
217	//
218	// This is only generated and re-exported for the solution code to use.
219	pub trait __OrInvalidIndex<T> {
220		fn or_invalid_index(self) -> Result<T, crate::Error>;
221	}
222
223	impl<T> __OrInvalidIndex<T> for Option<T> {
224		fn or_invalid_index(self) -> Result<T, crate::Error> {
225			self.ok_or(crate::Error::SolutionInvalidIndex)
226		}
227	}
228}
229
230use private::__OrInvalidIndex;
231
232pub mod weights;
233pub use weights::WeightInfo;
234
235#[cfg(test)]
236mod mock;
237#[cfg(test)]
238mod tests;
239
240/// The [`IndexAssignment`] type is an intermediate between the assignments list
241/// ([`&[Assignment<T>]`][Assignment]) and `SolutionOf<T>`.
242///
243/// The voter and target identifiers have already been replaced with appropriate indices,
244/// making it fast to repeatedly encode into a `SolutionOf<T>`. This property turns out
245/// to be important when trimming for solution length.
246#[derive(RuntimeDebug, Clone, Default)]
247#[cfg_attr(feature = "std", derive(PartialEq, Eq, Encode, Decode))]
248pub struct IndexAssignment<VoterIndex, TargetIndex, P: PerThing> {
249	/// Index of the voter among the voters list.
250	pub who: VoterIndex,
251	/// The distribution of the voter's stake among winning targets.
252	///
253	/// Targets are identified by their index in the canonical list.
254	pub distribution: Vec<(TargetIndex, P)>,
255}
256
257impl<VoterIndex, TargetIndex, P: PerThing> IndexAssignment<VoterIndex, TargetIndex, P> {
258	pub fn new<AccountId: IdentifierT>(
259		assignment: &Assignment<AccountId, P>,
260		voter_index: impl Fn(&AccountId) -> Option<VoterIndex>,
261		target_index: impl Fn(&AccountId) -> Option<TargetIndex>,
262	) -> Result<Self, Error> {
263		Ok(Self {
264			who: voter_index(&assignment.who).or_invalid_index()?,
265			distribution: assignment
266				.distribution
267				.iter()
268				.map(|(target, proportion)| Some((target_index(target)?, *proportion)))
269				.collect::<Option<Vec<_>>>()
270				.or_invalid_index()?,
271		})
272	}
273}
274
275/// A type alias for [`IndexAssignment`] made from [`NposSolution`].
276pub type IndexAssignmentOf<C> = IndexAssignment<
277	<C as NposSolution>::VoterIndex,
278	<C as NposSolution>::TargetIndex,
279	<C as NposSolution>::Accuracy,
280>;
281
282/// Types that are used by the data provider trait.
283pub mod data_provider {
284	/// Alias for the result type of the election data provider.
285	pub type Result<T> = core::result::Result<T, &'static str>;
286}
287
288/// Something that can provide the data to an [`ElectionProvider`].
289pub trait ElectionDataProvider {
290	/// The account identifier type.
291	type AccountId: Encode;
292
293	/// The block number type.
294	type BlockNumber;
295
296	/// Maximum number of votes per voter that this data provider is providing.
297	type MaxVotesPerVoter: Get<u32>;
298
299	/// All possible targets for the election, i.e. the targets that could become elected, thus
300	/// "electable".
301	///
302	/// This should be implemented as a self-weighing function. The implementor should register its
303	/// appropriate weight at the end of execution with the system pallet directly.
304	fn electable_targets(bounds: DataProviderBounds)
305		-> data_provider::Result<Vec<Self::AccountId>>;
306
307	/// All the voters that participate in the election, thus "electing".
308	///
309	/// Note that if a notion of self-vote exists, it should be represented here.
310	///
311	/// This should be implemented as a self-weighing function. The implementor should register its
312	/// appropriate weight at the end of execution with the system pallet directly.
313	fn electing_voters(bounds: DataProviderBounds) -> data_provider::Result<Vec<VoterOf<Self>>>;
314
315	/// The number of targets to elect.
316	///
317	/// This should be implemented as a self-weighing function. The implementor should register its
318	/// appropriate weight at the end of execution with the system pallet directly.
319	///
320	/// A sensible implementation should use the minimum between this value and
321	/// [`Self::targets().len()`], since desiring a winner set larger than candidates is not
322	/// feasible.
323	///
324	/// This is documented further in issue: <https://github.com/paritytech/substrate/issues/9478>
325	fn desired_targets() -> data_provider::Result<u32>;
326
327	/// Provide a best effort prediction about when the next election is about to happen.
328	///
329	/// In essence, the implementor should predict with this function when it will trigger the
330	/// [`ElectionProvider::elect`].
331	///
332	/// This is only useful for stateful election providers.
333	fn next_election_prediction(now: Self::BlockNumber) -> Self::BlockNumber;
334
335	/// Utility function only to be used in benchmarking scenarios, to be implemented optionally,
336	/// else a noop.
337	#[cfg(any(feature = "runtime-benchmarks", test))]
338	fn put_snapshot(
339		_voters: Vec<VoterOf<Self>>,
340		_targets: Vec<Self::AccountId>,
341		_target_stake: Option<VoteWeight>,
342	) {
343	}
344
345	/// Utility function only to be used in benchmarking scenarios, to be implemented optionally,
346	/// else a noop.
347	///
348	/// Same as `put_snapshot`, but can add a single voter one by one.
349	#[cfg(any(feature = "runtime-benchmarks", test))]
350	fn add_voter(
351		_voter: Self::AccountId,
352		_weight: VoteWeight,
353		_targets: BoundedVec<Self::AccountId, Self::MaxVotesPerVoter>,
354	) {
355	}
356
357	/// Utility function only to be used in benchmarking scenarios, to be implemented optionally,
358	/// else a noop.
359	///
360	/// Same as `put_snapshot`, but can add a single voter one by one.
361	#[cfg(any(feature = "runtime-benchmarks", test))]
362	fn add_target(_target: Self::AccountId) {}
363
364	/// Clear all voters and targets.
365	#[cfg(any(feature = "runtime-benchmarks", test))]
366	fn clear() {}
367}
368
369/// Base trait for types that can provide election
370pub trait ElectionProviderBase {
371	/// The account identifier type.
372	type AccountId;
373
374	/// The block number type.
375	type BlockNumber;
376
377	/// The error type that is returned by the provider.
378	type Error: Debug;
379
380	/// The upper bound on election winners that can be returned.
381	///
382	/// # WARNING
383	///
384	/// when communicating with the data provider, one must ensure that
385	/// `DataProvider::desired_targets` returns a value less than this bound. An
386	/// implementation can chose to either return an error and/or sort and
387	/// truncate the output to meet this bound.
388	type MaxWinners: Get<u32>;
389
390	/// The data provider of the election.
391	type DataProvider: ElectionDataProvider<
392		AccountId = Self::AccountId,
393		BlockNumber = Self::BlockNumber,
394	>;
395
396	/// checked call to `Self::DataProvider::desired_targets()` ensuring the value never exceeds
397	/// [`Self::MaxWinners`].
398	fn desired_targets_checked() -> data_provider::Result<u32> {
399		Self::DataProvider::desired_targets().and_then(|desired_targets| {
400			if desired_targets <= Self::MaxWinners::get() {
401				Ok(desired_targets)
402			} else {
403				Err("desired_targets must not be greater than MaxWinners.")
404			}
405		})
406	}
407}
408
409/// Elect a new set of winners, bounded by `MaxWinners`.
410///
411/// It must always use [`ElectionProviderBase::DataProvider`] to fetch the data it needs.
412///
413/// This election provider that could function asynchronously. This implies that this election might
414/// needs data ahead of time (ergo, receives no arguments to `elect`), and might be `ongoing` at
415/// times.
416pub trait ElectionProvider: ElectionProviderBase {
417	/// Indicate if this election provider is currently ongoing an asynchronous election or not.
418	fn ongoing() -> bool;
419
420	/// Performs the election. This should be implemented as a self-weighing function. The
421	/// implementor should register its appropriate weight at the end of execution with the
422	/// system pallet directly.
423	fn elect() -> Result<BoundedSupportsOf<Self>, Self::Error>;
424}
425
426/// A (almost) marker trait that signifies an election provider as working synchronously. i.e. being
427/// *instant*.
428///
429/// This must still use the same data provider as with [`ElectionProviderBase::DataProvider`].
430/// However, it can optionally overwrite the amount of voters and targets that are fetched from the
431/// data provider at runtime via `forced_input_voters_bound` and `forced_input_target_bound`.
432pub trait InstantElectionProvider: ElectionProviderBase {
433	fn instant_elect(
434		forced_input_voters_bound: DataProviderBounds,
435		forced_input_target_bound: DataProviderBounds,
436	) -> Result<BoundedSupportsOf<Self>, Self::Error>;
437}
438
439/// An election provider that does nothing whatsoever.
440pub struct NoElection<X>(core::marker::PhantomData<X>);
441
442impl<AccountId, BlockNumber, DataProvider, MaxWinners> ElectionProviderBase
443	for NoElection<(AccountId, BlockNumber, DataProvider, MaxWinners)>
444where
445	DataProvider: ElectionDataProvider<AccountId = AccountId, BlockNumber = BlockNumber>,
446	MaxWinners: Get<u32>,
447{
448	type AccountId = AccountId;
449	type BlockNumber = BlockNumber;
450	type Error = &'static str;
451	type MaxWinners = MaxWinners;
452	type DataProvider = DataProvider;
453}
454
455impl<AccountId, BlockNumber, DataProvider, MaxWinners> ElectionProvider
456	for NoElection<(AccountId, BlockNumber, DataProvider, MaxWinners)>
457where
458	DataProvider: ElectionDataProvider<AccountId = AccountId, BlockNumber = BlockNumber>,
459	MaxWinners: Get<u32>,
460{
461	fn ongoing() -> bool {
462		false
463	}
464
465	fn elect() -> Result<BoundedSupportsOf<Self>, Self::Error> {
466		Err("`NoElection` cannot do anything.")
467	}
468}
469
470impl<AccountId, BlockNumber, DataProvider, MaxWinners> InstantElectionProvider
471	for NoElection<(AccountId, BlockNumber, DataProvider, MaxWinners)>
472where
473	DataProvider: ElectionDataProvider<AccountId = AccountId, BlockNumber = BlockNumber>,
474	MaxWinners: Get<u32>,
475{
476	fn instant_elect(
477		_: DataProviderBounds,
478		_: DataProviderBounds,
479	) -> Result<BoundedSupportsOf<Self>, Self::Error> {
480		Err("`NoElection` cannot do anything.")
481	}
482}
483
484/// A utility trait for something to implement `ElectionDataProvider` in a sensible way.
485///
486/// This is generic over `AccountId` and it can represent a validator, a nominator, or any other
487/// entity.
488///
489/// The scores (see [`Self::Score`]) are ascending, the higher, the better.
490///
491/// Something that implements this trait will do a best-effort sort over ids, and thus can be
492/// used on the implementing side of [`ElectionDataProvider`].
493pub trait SortedListProvider<AccountId> {
494	/// The list's error type.
495	type Error: core::fmt::Debug;
496
497	/// The type used by the list to compare nodes for ordering.
498	type Score: Bounded + Saturating + Zero;
499
500	/// An iterator over the list, which can have `take` called on it.
501	fn iter() -> Box<dyn Iterator<Item = AccountId>>;
502
503	/// Returns an iterator over the list, starting right after from the given voter.
504	///
505	/// May return an error if `start` is invalid.
506	fn iter_from(start: &AccountId) -> Result<Box<dyn Iterator<Item = AccountId>>, Self::Error>;
507
508	/// The current count of ids in the list.
509	fn count() -> u32;
510
511	/// Return true if the list already contains `id`.
512	fn contains(id: &AccountId) -> bool;
513
514	/// Hook for inserting a new id.
515	///
516	/// Implementation should return an error if duplicate item is being inserted.
517	fn on_insert(id: AccountId, score: Self::Score) -> Result<(), Self::Error>;
518
519	/// Hook for updating a single id.
520	///
521	/// The `new` score is given.
522	///
523	/// Returns `Ok(())` iff it successfully updates an item, an `Err(_)` otherwise.
524	fn on_update(id: &AccountId, score: Self::Score) -> Result<(), Self::Error>;
525
526	/// Get the score of `id`.
527	fn get_score(id: &AccountId) -> Result<Self::Score, Self::Error>;
528
529	/// Same as `on_update`, but incorporate some increased score.
530	fn on_increase(id: &AccountId, additional: Self::Score) -> Result<(), Self::Error> {
531		let old_score = Self::get_score(id)?;
532		let new_score = old_score.saturating_add(additional);
533		Self::on_update(id, new_score)
534	}
535
536	/// Same as `on_update`, but incorporate some decreased score.
537	///
538	/// If the new score of the item is `Zero`, it is removed.
539	fn on_decrease(id: &AccountId, decreased: Self::Score) -> Result<(), Self::Error> {
540		let old_score = Self::get_score(id)?;
541		let new_score = old_score.saturating_sub(decreased);
542		if new_score.is_zero() {
543			Self::on_remove(id)
544		} else {
545			Self::on_update(id, new_score)
546		}
547	}
548
549	/// Hook for removing am id from the list.
550	///
551	/// Returns `Ok(())` iff it successfully removes an item, an `Err(_)` otherwise.
552	fn on_remove(id: &AccountId) -> Result<(), Self::Error>;
553
554	/// Regenerate this list from scratch. Returns the count of items inserted.
555	///
556	/// This should typically only be used at a runtime upgrade.
557	///
558	/// ## WARNING
559	///
560	/// This function should be called with care, regenerate will remove the current list write the
561	/// new list, which can lead to too many storage accesses, exhausting the block weight.
562	fn unsafe_regenerate(
563		all: impl IntoIterator<Item = AccountId>,
564		score_of: Box<dyn Fn(&AccountId) -> Self::Score>,
565	) -> u32;
566
567	/// Remove all items from the list.
568	///
569	/// ## WARNING
570	///
571	/// This function should never be called in production settings because it can lead to an
572	/// unbounded amount of storage accesses.
573	fn unsafe_clear();
574
575	/// Check internal state of the list. Only meant for debugging.
576	#[cfg(feature = "try-runtime")]
577	fn try_state() -> Result<(), TryRuntimeError>;
578
579	/// If `who` changes by the returned amount they are guaranteed to have a worst case change
580	/// in their list position.
581	#[cfg(feature = "runtime-benchmarks")]
582	fn score_update_worst_case(_who: &AccountId, _is_increase: bool) -> Self::Score;
583}
584
585/// Something that can provide the `Score` of an account. Similar to [`ElectionProvider`] and
586/// [`ElectionDataProvider`], this should typically be implementing by whoever is supposed to *use*
587/// `SortedListProvider`.
588pub trait ScoreProvider<AccountId> {
589	type Score;
590
591	/// Get the current `Score` of `who`.
592	fn score(who: &AccountId) -> Self::Score;
593
594	/// For tests, benchmarks and fuzzing, set the `score`.
595	#[cfg(any(feature = "runtime-benchmarks", feature = "fuzz", feature = "std"))]
596	fn set_score_of(_: &AccountId, _: Self::Score) {}
597}
598
599/// Something that can compute the result to an NPoS solution.
600pub trait NposSolver {
601	/// The account identifier type of this solver.
602	type AccountId: sp_npos_elections::IdentifierT;
603	/// The accuracy of this solver. This will affect the accuracy of the output.
604	type Accuracy: PerThing128;
605	/// The error type of this implementation.
606	type Error: core::fmt::Debug + core::cmp::PartialEq;
607
608	/// Solve an NPoS solution with the given `voters`, `targets`, and select `to_elect` count
609	/// of `targets`.
610	fn solve(
611		to_elect: usize,
612		targets: Vec<Self::AccountId>,
613		voters: Vec<(Self::AccountId, VoteWeight, impl IntoIterator<Item = Self::AccountId>)>,
614	) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error>;
615
616	/// Measure the weight used in the calculation of the solver.
617	/// - `voters` is the number of voters.
618	/// - `targets` is the number of targets.
619	/// - `vote_degree` is the degree ie the maximum numbers of votes per voter.
620	fn weight<T: WeightInfo>(voters: u32, targets: u32, vote_degree: u32) -> Weight;
621}
622
623/// A wrapper for [`sp_npos_elections::seq_phragmen`] that implements [`NposSolver`]. See the
624/// documentation of [`sp_npos_elections::seq_phragmen`] for more info.
625pub struct SequentialPhragmen<AccountId, Accuracy, Balancing = ()>(
626	core::marker::PhantomData<(AccountId, Accuracy, Balancing)>,
627);
628
629impl<AccountId: IdentifierT, Accuracy: PerThing128, Balancing: Get<Option<BalancingConfig>>>
630	NposSolver for SequentialPhragmen<AccountId, Accuracy, Balancing>
631{
632	type AccountId = AccountId;
633	type Accuracy = Accuracy;
634	type Error = sp_npos_elections::Error;
635	fn solve(
636		winners: usize,
637		targets: Vec<Self::AccountId>,
638		voters: Vec<(Self::AccountId, VoteWeight, impl IntoIterator<Item = Self::AccountId>)>,
639	) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error> {
640		sp_npos_elections::seq_phragmen(winners, targets, voters, Balancing::get())
641	}
642
643	fn weight<T: WeightInfo>(voters: u32, targets: u32, vote_degree: u32) -> Weight {
644		T::phragmen(voters, targets, vote_degree)
645	}
646}
647
648/// A wrapper for [`sp_npos_elections::phragmms()`] that implements [`NposSolver`]. See the
649/// documentation of [`sp_npos_elections::phragmms()`] for more info.
650pub struct PhragMMS<AccountId, Accuracy, Balancing = ()>(
651	core::marker::PhantomData<(AccountId, Accuracy, Balancing)>,
652);
653
654impl<AccountId: IdentifierT, Accuracy: PerThing128, Balancing: Get<Option<BalancingConfig>>>
655	NposSolver for PhragMMS<AccountId, Accuracy, Balancing>
656{
657	type AccountId = AccountId;
658	type Accuracy = Accuracy;
659	type Error = sp_npos_elections::Error;
660	fn solve(
661		winners: usize,
662		targets: Vec<Self::AccountId>,
663		voters: Vec<(Self::AccountId, VoteWeight, impl IntoIterator<Item = Self::AccountId>)>,
664	) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error> {
665		sp_npos_elections::phragmms(winners, targets, voters, Balancing::get())
666	}
667
668	fn weight<T: WeightInfo>(voters: u32, targets: u32, vote_degree: u32) -> Weight {
669		T::phragmms(voters, targets, vote_degree)
670	}
671}
672
673/// A voter, at the level of abstraction of this crate.
674pub type Voter<AccountId, Bound> = (AccountId, VoteWeight, BoundedVec<AccountId, Bound>);
675
676/// Same as [`Voter`], but parameterized by an [`ElectionDataProvider`].
677pub type VoterOf<D> =
678	Voter<<D as ElectionDataProvider>::AccountId, <D as ElectionDataProvider>::MaxVotesPerVoter>;
679
680/// Same as `BoundedSupports` but parameterized by a `ElectionProviderBase`.
681pub type BoundedSupportsOf<E> = BoundedSupports<
682	<E as ElectionProviderBase>::AccountId,
683	<E as ElectionProviderBase>::MaxWinners,
684>;
685
686sp_core::generate_feature_enabled_macro!(
687	runtime_benchmarks_enabled,
688	feature = "runtime-benchmarks",
689	$
690);
691
692sp_core::generate_feature_enabled_macro!(
693	runtime_benchmarks_or_std_enabled,
694	any(feature = "runtime-benchmarks", feature = "std"),
695	$
696);