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, DecodeWithMemTracking, Encode, MaxEncodedLen};
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::{collections::btree_set::BTreeSet, vec::Vec};
209 pub use codec;
210 pub use scale_info;
211 pub use sp_arithmetic;
212
213 // Simple Extension trait to easily convert `None` from index closures to `Err`.
214 //
215 // This is only generated and re-exported for the solution code to use.
216 pub trait __OrInvalidIndex<T> {
217 fn or_invalid_index(self) -> Result<T, crate::Error>;
218 }
219
220 impl<T> __OrInvalidIndex<T> for Option<T> {
221 fn or_invalid_index(self) -> Result<T, crate::Error> {
222 self.ok_or(crate::Error::SolutionInvalidIndex)
223 }
224 }
225}
226
227use private::__OrInvalidIndex;
228
229pub mod weights;
230pub use weights::WeightInfo;
231
232#[cfg(test)]
233mod mock;
234#[cfg(test)]
235mod tests;
236
237/// The [`IndexAssignment`] type is an intermediate between the assignments list
238/// ([`&[Assignment<T>]`][Assignment]) and `SolutionOf<T>`.
239///
240/// The voter and target identifiers have already been replaced with appropriate indices,
241/// making it fast to repeatedly encode into a `SolutionOf<T>`. This property turns out
242/// to be important when trimming for solution length.
243#[derive(RuntimeDebug, Clone, Default)]
244#[cfg_attr(feature = "std", derive(PartialEq, Eq, Encode, Decode))]
245pub struct IndexAssignment<VoterIndex, TargetIndex, P: PerThing> {
246 /// Index of the voter among the voters list.
247 pub who: VoterIndex,
248 /// The distribution of the voter's stake among winning targets.
249 ///
250 /// Targets are identified by their index in the canonical list.
251 pub distribution: Vec<(TargetIndex, P)>,
252}
253
254impl<VoterIndex, TargetIndex, P: PerThing> IndexAssignment<VoterIndex, TargetIndex, P> {
255 pub fn new<AccountId: IdentifierT>(
256 assignment: &Assignment<AccountId, P>,
257 voter_index: impl Fn(&AccountId) -> Option<VoterIndex>,
258 target_index: impl Fn(&AccountId) -> Option<TargetIndex>,
259 ) -> Result<Self, Error> {
260 Ok(Self {
261 who: voter_index(&assignment.who).or_invalid_index()?,
262 distribution: assignment
263 .distribution
264 .iter()
265 .map(|(target, proportion)| Some((target_index(target)?, *proportion)))
266 .collect::<Option<Vec<_>>>()
267 .or_invalid_index()?,
268 })
269 }
270}
271
272/// A type alias for [`IndexAssignment`] made from [`NposSolution`].
273pub type IndexAssignmentOf<C> = IndexAssignment<
274 <C as NposSolution>::VoterIndex,
275 <C as NposSolution>::TargetIndex,
276 <C as NposSolution>::Accuracy,
277>;
278
279/// Types that are used by the data provider trait.
280pub mod data_provider {
281 /// Alias for the result type of the election data provider.
282 pub type Result<T> = core::result::Result<T, &'static str>;
283}
284
285/// Something that can provide the data to an [`ElectionProvider`].
286pub trait ElectionDataProvider {
287 /// The account identifier type.
288 type AccountId: Encode;
289
290 /// The block number type.
291 type BlockNumber;
292
293 /// Maximum number of votes per voter that this data provider is providing.
294 type MaxVotesPerVoter: Get<u32>;
295
296 /// All possible targets for the election, i.e. the targets that could become elected, thus
297 /// "electable".
298 ///
299 /// This should be implemented as a self-weighing function. The implementor should register its
300 /// appropriate weight at the end of execution with the system pallet directly.
301 fn electable_targets(bounds: DataProviderBounds)
302 -> data_provider::Result<Vec<Self::AccountId>>;
303
304 /// All the voters that participate in the election, thus "electing".
305 ///
306 /// Note that if a notion of self-vote exists, it should be represented here.
307 ///
308 /// This should be implemented as a self-weighing function. The implementor should register its
309 /// appropriate weight at the end of execution with the system pallet directly.
310 fn electing_voters(bounds: DataProviderBounds) -> data_provider::Result<Vec<VoterOf<Self>>>;
311
312 /// The number of targets to elect.
313 ///
314 /// This should be implemented as a self-weighing function. The implementor should register its
315 /// appropriate weight at the end of execution with the system pallet directly.
316 ///
317 /// A sensible implementation should use the minimum between this value and
318 /// [`Self::targets().len()`], since desiring a winner set larger than candidates is not
319 /// feasible.
320 ///
321 /// This is documented further in issue: <https://github.com/paritytech/substrate/issues/9478>
322 fn desired_targets() -> data_provider::Result<u32>;
323
324 /// Provide a best effort prediction about when the next election is about to happen.
325 ///
326 /// In essence, the implementor should predict with this function when it will trigger the
327 /// [`ElectionProvider::elect`].
328 ///
329 /// This is only useful for stateful election providers.
330 fn next_election_prediction(now: Self::BlockNumber) -> Self::BlockNumber;
331
332 /// Utility function only to be used in benchmarking scenarios, to be implemented optionally,
333 /// else a noop.
334 #[cfg(any(feature = "runtime-benchmarks", test))]
335 fn put_snapshot(
336 _voters: Vec<VoterOf<Self>>,
337 _targets: Vec<Self::AccountId>,
338 _target_stake: Option<VoteWeight>,
339 ) {
340 }
341
342 /// Utility function only to be used in benchmarking scenarios, to be implemented optionally,
343 /// else a noop.
344 ///
345 /// Same as `put_snapshot`, but can add a single voter one by one.
346 #[cfg(any(feature = "runtime-benchmarks", test))]
347 fn add_voter(
348 _voter: Self::AccountId,
349 _weight: VoteWeight,
350 _targets: BoundedVec<Self::AccountId, Self::MaxVotesPerVoter>,
351 ) {
352 }
353
354 /// Utility function only to be used in benchmarking scenarios, to be implemented optionally,
355 /// else a noop.
356 ///
357 /// Same as `put_snapshot`, but can add a single voter one by one.
358 #[cfg(any(feature = "runtime-benchmarks", test))]
359 fn add_target(_target: Self::AccountId) {}
360
361 /// Clear all voters and targets.
362 #[cfg(any(feature = "runtime-benchmarks", test))]
363 fn clear() {}
364}
365
366/// Base trait for types that can provide election
367pub trait ElectionProviderBase {
368 /// The account identifier type.
369 type AccountId;
370
371 /// The block number type.
372 type BlockNumber;
373
374 /// The error type that is returned by the provider.
375 type Error: Debug;
376
377 /// The upper bound on election winners that can be returned.
378 ///
379 /// # WARNING
380 ///
381 /// when communicating with the data provider, one must ensure that
382 /// `DataProvider::desired_targets` returns a value less than this bound. An
383 /// implementation can chose to either return an error and/or sort and
384 /// truncate the output to meet this bound.
385 type MaxWinners: Get<u32>;
386
387 /// The data provider of the election.
388 type DataProvider: ElectionDataProvider<
389 AccountId = Self::AccountId,
390 BlockNumber = Self::BlockNumber,
391 >;
392
393 /// checked call to `Self::DataProvider::desired_targets()` ensuring the value never exceeds
394 /// [`Self::MaxWinners`].
395 fn desired_targets_checked() -> data_provider::Result<u32> {
396 Self::DataProvider::desired_targets().and_then(|desired_targets| {
397 if desired_targets <= Self::MaxWinners::get() {
398 Ok(desired_targets)
399 } else {
400 Err("desired_targets must not be greater than MaxWinners.")
401 }
402 })
403 }
404}
405
406/// Elect a new set of winners, bounded by `MaxWinners`.
407///
408/// It must always use [`ElectionProviderBase::DataProvider`] to fetch the data it needs.
409///
410/// This election provider that could function asynchronously. This implies that this election might
411/// needs data ahead of time (ergo, receives no arguments to `elect`), and might be `ongoing` at
412/// times.
413pub trait ElectionProvider: ElectionProviderBase {
414 /// Indicate if this election provider is currently ongoing an asynchronous election or not.
415 fn ongoing() -> bool;
416
417 /// Performs the election. This should be implemented as a self-weighing function. The
418 /// implementor should register its appropriate weight at the end of execution with the
419 /// system pallet directly.
420 fn elect() -> Result<BoundedSupportsOf<Self>, Self::Error>;
421}
422
423/// A (almost) marker trait that signifies an election provider as working synchronously. i.e. being
424/// *instant*.
425///
426/// This must still use the same data provider as with [`ElectionProviderBase::DataProvider`].
427/// However, it can optionally overwrite the amount of voters and targets that are fetched from the
428/// data provider at runtime via `forced_input_voters_bound` and `forced_input_target_bound`.
429pub trait InstantElectionProvider: ElectionProviderBase {
430 fn instant_elect(
431 forced_input_voters_bound: DataProviderBounds,
432 forced_input_target_bound: DataProviderBounds,
433 ) -> Result<BoundedSupportsOf<Self>, Self::Error>;
434}
435
436/// An election provider that does nothing whatsoever.
437pub struct NoElection<X>(core::marker::PhantomData<X>);
438
439impl<AccountId, BlockNumber, DataProvider, MaxWinners> ElectionProviderBase
440 for NoElection<(AccountId, BlockNumber, DataProvider, MaxWinners)>
441where
442 DataProvider: ElectionDataProvider<AccountId = AccountId, BlockNumber = BlockNumber>,
443 MaxWinners: Get<u32>,
444{
445 type AccountId = AccountId;
446 type BlockNumber = BlockNumber;
447 type Error = &'static str;
448 type MaxWinners = MaxWinners;
449 type DataProvider = DataProvider;
450}
451
452impl<AccountId, BlockNumber, DataProvider, MaxWinners> ElectionProvider
453 for NoElection<(AccountId, BlockNumber, DataProvider, MaxWinners)>
454where
455 DataProvider: ElectionDataProvider<AccountId = AccountId, BlockNumber = BlockNumber>,
456 MaxWinners: Get<u32>,
457{
458 fn ongoing() -> bool {
459 false
460 }
461
462 fn elect() -> Result<BoundedSupportsOf<Self>, Self::Error> {
463 Err("`NoElection` cannot do anything.")
464 }
465}
466
467impl<AccountId, BlockNumber, DataProvider, MaxWinners> InstantElectionProvider
468 for NoElection<(AccountId, BlockNumber, DataProvider, MaxWinners)>
469where
470 DataProvider: ElectionDataProvider<AccountId = AccountId, BlockNumber = BlockNumber>,
471 MaxWinners: Get<u32>,
472{
473 fn instant_elect(
474 _: DataProviderBounds,
475 _: DataProviderBounds,
476 ) -> Result<BoundedSupportsOf<Self>, Self::Error> {
477 Err("`NoElection` cannot do anything.")
478 }
479}
480
481/// A utility trait for something to implement `ElectionDataProvider` in a sensible way.
482///
483/// This is generic over `AccountId` and it can represent a validator, a nominator, or any other
484/// entity.
485///
486/// The scores (see [`Self::Score`]) are ascending, the higher, the better.
487///
488/// Something that implements this trait will do a best-effort sort over ids, and thus can be
489/// used on the implementing side of [`ElectionDataProvider`].
490pub trait SortedListProvider<AccountId> {
491 /// The list's error type.
492 type Error: core::fmt::Debug;
493
494 /// The type used by the list to compare nodes for ordering.
495 type Score: Bounded + Saturating + Zero;
496
497 /// An iterator over the list, which can have `take` called on it.
498 fn iter() -> Box<dyn Iterator<Item = AccountId>>;
499
500 /// Returns an iterator over the list, starting right after from the given voter.
501 ///
502 /// May return an error if `start` is invalid.
503 fn iter_from(start: &AccountId) -> Result<Box<dyn Iterator<Item = AccountId>>, Self::Error>;
504
505 /// The current count of ids in the list.
506 fn count() -> u32;
507
508 /// Return true if the list already contains `id`.
509 fn contains(id: &AccountId) -> bool;
510
511 /// Hook for inserting a new id.
512 ///
513 /// Implementation should return an error if duplicate item is being inserted.
514 fn on_insert(id: AccountId, score: Self::Score) -> Result<(), Self::Error>;
515
516 /// Hook for updating a single id.
517 ///
518 /// The `new` score is given.
519 ///
520 /// Returns `Ok(())` iff it successfully updates an item, an `Err(_)` otherwise.
521 fn on_update(id: &AccountId, score: Self::Score) -> Result<(), Self::Error>;
522
523 /// Get the score of `id`.
524 fn get_score(id: &AccountId) -> Result<Self::Score, Self::Error>;
525
526 /// Same as `on_update`, but incorporate some increased score.
527 fn on_increase(id: &AccountId, additional: Self::Score) -> Result<(), Self::Error> {
528 let old_score = Self::get_score(id)?;
529 let new_score = old_score.saturating_add(additional);
530 Self::on_update(id, new_score)
531 }
532
533 /// Same as `on_update`, but incorporate some decreased score.
534 ///
535 /// If the new score of the item is `Zero`, it is removed.
536 fn on_decrease(id: &AccountId, decreased: Self::Score) -> Result<(), Self::Error> {
537 let old_score = Self::get_score(id)?;
538 let new_score = old_score.saturating_sub(decreased);
539 if new_score.is_zero() {
540 Self::on_remove(id)
541 } else {
542 Self::on_update(id, new_score)
543 }
544 }
545
546 /// Hook for removing am id from the list.
547 ///
548 /// Returns `Ok(())` iff it successfully removes an item, an `Err(_)` otherwise.
549 fn on_remove(id: &AccountId) -> Result<(), Self::Error>;
550
551 /// Regenerate this list from scratch. Returns the count of items inserted.
552 ///
553 /// This should typically only be used at a runtime upgrade.
554 ///
555 /// ## WARNING
556 ///
557 /// This function should be called with care, regenerate will remove the current list write the
558 /// new list, which can lead to too many storage accesses, exhausting the block weight.
559 fn unsafe_regenerate(
560 all: impl IntoIterator<Item = AccountId>,
561 score_of: Box<dyn Fn(&AccountId) -> Self::Score>,
562 ) -> u32;
563
564 /// Remove all items from the list.
565 ///
566 /// ## WARNING
567 ///
568 /// This function should never be called in production settings because it can lead to an
569 /// unbounded amount of storage accesses.
570 fn unsafe_clear();
571
572 /// Check internal state of the list. Only meant for debugging.
573 #[cfg(feature = "try-runtime")]
574 fn try_state() -> Result<(), TryRuntimeError>;
575
576 /// If `who` changes by the returned amount they are guaranteed to have a worst case change
577 /// in their list position.
578 #[cfg(feature = "runtime-benchmarks")]
579 fn score_update_worst_case(_who: &AccountId, _is_increase: bool) -> Self::Score;
580}
581
582/// Something that can provide the `Score` of an account. Similar to [`ElectionProvider`] and
583/// [`ElectionDataProvider`], this should typically be implementing by whoever is supposed to *use*
584/// `SortedListProvider`.
585pub trait ScoreProvider<AccountId> {
586 type Score;
587
588 /// Get the current `Score` of `who`.
589 fn score(who: &AccountId) -> Self::Score;
590
591 /// For tests, benchmarks and fuzzing, set the `score`.
592 #[cfg(any(feature = "runtime-benchmarks", feature = "fuzz", feature = "std"))]
593 fn set_score_of(_: &AccountId, _: Self::Score) {}
594}
595
596/// Something that can compute the result to an NPoS solution.
597pub trait NposSolver {
598 /// The account identifier type of this solver.
599 type AccountId: sp_npos_elections::IdentifierT;
600 /// The accuracy of this solver. This will affect the accuracy of the output.
601 type Accuracy: PerThing128;
602 /// The error type of this implementation.
603 type Error: core::fmt::Debug + core::cmp::PartialEq;
604
605 /// Solve an NPoS solution with the given `voters`, `targets`, and select `to_elect` count
606 /// of `targets`.
607 fn solve(
608 to_elect: usize,
609 targets: Vec<Self::AccountId>,
610 voters: Vec<(Self::AccountId, VoteWeight, impl IntoIterator<Item = Self::AccountId>)>,
611 ) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error>;
612
613 /// Measure the weight used in the calculation of the solver.
614 /// - `voters` is the number of voters.
615 /// - `targets` is the number of targets.
616 /// - `vote_degree` is the degree ie the maximum numbers of votes per voter.
617 fn weight<T: WeightInfo>(voters: u32, targets: u32, vote_degree: u32) -> Weight;
618}
619
620/// A wrapper for [`sp_npos_elections::seq_phragmen`] that implements [`NposSolver`]. See the
621/// documentation of [`sp_npos_elections::seq_phragmen`] for more info.
622pub struct SequentialPhragmen<AccountId, Accuracy, Balancing = ()>(
623 core::marker::PhantomData<(AccountId, Accuracy, Balancing)>,
624);
625
626impl<AccountId: IdentifierT, Accuracy: PerThing128, Balancing: Get<Option<BalancingConfig>>>
627 NposSolver for SequentialPhragmen<AccountId, Accuracy, Balancing>
628{
629 type AccountId = AccountId;
630 type Accuracy = Accuracy;
631 type Error = sp_npos_elections::Error;
632 fn solve(
633 winners: usize,
634 targets: Vec<Self::AccountId>,
635 voters: Vec<(Self::AccountId, VoteWeight, impl IntoIterator<Item = Self::AccountId>)>,
636 ) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error> {
637 sp_npos_elections::seq_phragmen(winners, targets, voters, Balancing::get())
638 }
639
640 fn weight<T: WeightInfo>(voters: u32, targets: u32, vote_degree: u32) -> Weight {
641 T::phragmen(voters, targets, vote_degree)
642 }
643}
644
645/// A wrapper for [`sp_npos_elections::phragmms()`] that implements [`NposSolver`]. See the
646/// documentation of [`sp_npos_elections::phragmms()`] for more info.
647pub struct PhragMMS<AccountId, Accuracy, Balancing = ()>(
648 core::marker::PhantomData<(AccountId, Accuracy, Balancing)>,
649);
650
651impl<AccountId: IdentifierT, Accuracy: PerThing128, Balancing: Get<Option<BalancingConfig>>>
652 NposSolver for PhragMMS<AccountId, Accuracy, Balancing>
653{
654 type AccountId = AccountId;
655 type Accuracy = Accuracy;
656 type Error = sp_npos_elections::Error;
657 fn solve(
658 winners: usize,
659 targets: Vec<Self::AccountId>,
660 voters: Vec<(Self::AccountId, VoteWeight, impl IntoIterator<Item = Self::AccountId>)>,
661 ) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error> {
662 sp_npos_elections::phragmms(winners, targets, voters, Balancing::get())
663 }
664
665 fn weight<T: WeightInfo>(voters: u32, targets: u32, vote_degree: u32) -> Weight {
666 T::phragmms(voters, targets, vote_degree)
667 }
668}
669
670/// A voter, at the level of abstraction of this crate.
671pub type Voter<AccountId, Bound> = (AccountId, VoteWeight, BoundedVec<AccountId, Bound>);
672
673/// Same as [`Voter`], but parameterized by an [`ElectionDataProvider`].
674pub type VoterOf<D> =
675 Voter<<D as ElectionDataProvider>::AccountId, <D as ElectionDataProvider>::MaxVotesPerVoter>;
676
677/// Same as `BoundedSupports` but parameterized by a `ElectionProviderBase`.
678pub type BoundedSupportsOf<E> = BoundedSupports<
679 <E as ElectionProviderBase>::AccountId,
680 <E as ElectionProviderBase>::MaxWinners,
681>;
682
683sp_core::generate_feature_enabled_macro!(
684 runtime_benchmarks_enabled,
685 feature = "runtime-benchmarks",
686 $
687);
688
689sp_core::generate_feature_enabled_macro!(
690 runtime_benchmarks_or_std_enabled,
691 any(feature = "runtime-benchmarks", feature = "std"),
692 $
693);