tp_election_providers/lib.rs
1// This file is part of Tetcore.
2
3// Copyright (C) 2020 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 [`ElectionProvider`],
24//! whilst needing an associated [`ElectionProvider::DataProvider`], which needs to be fulfilled by
25//! an entity implementing [`ElectionDataProvider`]. Most often, *the data provider is* the receiver
26//! of the election, resulting in a diagram as below:
27//!
28//! ```ignore
29//! ElectionDataProvider
30//! <------------------------------------------+
31//! | |
32//! v |
33//! +-----+----+ +------+---+
34//! | | | |
35//! pallet-do-election | | | | pallet-needs-election
36//! | | | |
37//! | | | |
38//! +-----+----+ +------+---+
39//! | ^
40//! | |
41//! +------------------------------------------+
42//! ElectionProvider
43//! ```
44//!
45//! > It could also be possible that a third party pallet (C), provides the data of election to an
46//! > election provider (B), which then passes the election result to another pallet (A).
47//!
48//! ## Election Types
49//!
50//! Typically, two types of elections exist:
51//!
52//! 1. **Stateless**: Election data is provided, and the election result is immediately ready.
53//! 2. **Stateful**: Election data is is queried ahead of time, and the election result might be
54//! ready some number of blocks in the future.
55//!
56//! To accommodate both type of elections in one trait, the traits lean toward **stateful
57//! election**, as it is more general than the stateless. This is why [`ElectionProvider::elect`]
58//! has no parameters. All value and type parameter must be provided by the [`ElectionDataProvider`]
59//! trait, even if the election happens immediately.
60//!
61//! ## Election Data
62//!
63//! The data associated with an election, essentially what the [`ElectionDataProvider`] must convey
64//! is as follows:
65//!
66//! 1. A list of voters, with their stake.
67//! 2. A list of targets (i.e. _candidates_).
68//! 3. A number of desired targets to be elected (i.e. _winners_)
69//!
70//! In addition to that, the [`ElectionDataProvider`] must also hint [`ElectionProvider`] at when
71//! the next election might happen ([`ElectionDataProvider::next_election_prediction`]). A stateless
72//! election provider would probably ignore this. A stateful election provider can use this to
73//! prepare the election result in advance.
74//!
75//! Nonetheless, an [`ElectionProvider`] shan't rely on this and should preferably provide some
76//! means of fallback election as well, in case the `elect` was called immaturely early.
77//!
78//! ## Example
79//!
80//! ```rust
81//! # use tp_election_providers::*;
82//! # use tp_npos_elections::{Support, Assignment};
83//!
84//! type AccountId = u64;
85//! type Balance = u64;
86//! type BlockNumber = u32;
87//!
88//! mod data_provider {
89//! use super::*;
90//!
91//! pub trait Config: Sized {
92//! type ElectionProvider: ElectionProvider<
93//! AccountId,
94//! BlockNumber,
95//! DataProvider = Module<Self>,
96//! >;
97//! }
98//!
99//! pub struct Module<T: Config>(std::marker::PhantomData<T>);
100//!
101//! impl<T: Config> ElectionDataProvider<AccountId, BlockNumber> for Module<T> {
102//! fn desired_targets() -> u32 {
103//! 1
104//! }
105//! fn voters() -> Vec<(AccountId, VoteWeight, Vec<AccountId>)> {
106//! Default::default()
107//! }
108//! fn targets() -> Vec<AccountId> {
109//! vec![10, 20, 30]
110//! }
111//! fn next_election_prediction(now: BlockNumber) -> BlockNumber {
112//! 0
113//! }
114//! }
115//! }
116//!
117//!
118//! mod generic_election_provider {
119//! use super::*;
120//!
121//! pub struct GenericElectionProvider<T: Config>(std::marker::PhantomData<T>);
122//!
123//! pub trait Config {
124//! type DataProvider: ElectionDataProvider<AccountId, BlockNumber>;
125//! }
126//!
127//! impl<T: Config> ElectionProvider<AccountId, BlockNumber> for GenericElectionProvider<T> {
128//! type Error = ();
129//! type DataProvider = T::DataProvider;
130//!
131//! fn elect() -> Result<Supports<AccountId>, Self::Error> {
132//! Self::DataProvider::targets()
133//! .first()
134//! .map(|winner| vec![(*winner, Support::default())])
135//! .ok_or(())
136//! }
137//! }
138//! }
139//!
140//! mod runtime {
141//! use super::generic_election_provider;
142//! use super::data_provider;
143//! use super::AccountId;
144//!
145//! struct Runtime;
146//! impl generic_election_provider::Config for Runtime {
147//! type DataProvider = data_provider::Module<Runtime>;
148//! }
149//!
150//! impl data_provider::Config for Runtime {
151//! type ElectionProvider = generic_election_provider::GenericElectionProvider<Runtime>;
152//! }
153//!
154//! }
155//!
156//! # fn main() {}
157//! ```
158
159#![cfg_attr(not(feature = "std"), no_std)]
160
161pub mod onchain;
162use tetcore_std::{prelude::*, fmt::Debug};
163
164/// Re-export some type as they are used in the interface.
165pub use arithmetic::PerThing;
166pub use tp_npos_elections::{Assignment, ExtendedBalance, PerThing128, Supports, VoteWeight};
167
168/// Something that can provide the data to an [`ElectionProvider`].
169pub trait ElectionDataProvider<AccountId, BlockNumber> {
170 /// All possible targets for the election, i.e. the candidates.
171 fn targets() -> Vec<AccountId>;
172
173 /// All possible voters for the election.
174 ///
175 /// Note that if a notion of self-vote exists, it should be represented here.
176 fn voters() -> Vec<(AccountId, VoteWeight, Vec<AccountId>)>;
177
178 /// The number of targets to elect.
179 fn desired_targets() -> u32;
180
181 /// Provide a best effort prediction about when the next election is about to happen.
182 ///
183 /// In essence, the implementor should predict with this function when it will trigger the
184 /// [`ElectionProvider::elect`].
185 ///
186 /// This is only useful for stateful election providers.
187 fn next_election_prediction(now: BlockNumber) -> BlockNumber;
188
189 /// Utility function only to be used in benchmarking scenarios, to be implemented optionally,
190 /// else a noop.
191 #[cfg(any(feature = "runtime-benchmarks", test))]
192 fn put_snapshot(
193 _voters: Vec<(AccountId, VoteWeight, Vec<AccountId>)>,
194 _targets: Vec<AccountId>,
195 ) {
196 }
197}
198
199#[cfg(feature = "std")]
200impl<AccountId, BlockNumber> ElectionDataProvider<AccountId, BlockNumber> for () {
201 fn targets() -> Vec<AccountId> {
202 Default::default()
203 }
204 fn voters() -> Vec<(AccountId, VoteWeight, Vec<AccountId>)> {
205 Default::default()
206 }
207 fn desired_targets() -> u32 {
208 Default::default()
209 }
210 fn next_election_prediction(now: BlockNumber) -> BlockNumber {
211 now
212 }
213}
214
215/// Something that can compute the result of an election and pass it back to the caller.
216///
217/// This trait only provides an interface to _request_ an election, i.e.
218/// [`ElectionProvider::elect`]. That data required for the election need to be passed to the
219/// implemented of this trait through [`ElectionProvider::DataProvider`].
220pub trait ElectionProvider<AccountId, BlockNumber> {
221 /// The error type that is returned by the provider.
222 type Error: Debug;
223
224 /// The data provider of the election.
225 type DataProvider: ElectionDataProvider<AccountId, BlockNumber>;
226
227 /// Elect a new set of winners.
228 ///
229 /// The result is returned in a target major format, namely as vector of supports.
230 fn elect() -> Result<Supports<AccountId>, Self::Error>;
231}
232
233#[cfg(feature = "std")]
234impl<AccountId, BlockNumber> ElectionProvider<AccountId, BlockNumber> for () {
235 type Error = &'static str;
236 type DataProvider = ();
237
238 fn elect() -> Result<Supports<AccountId>, Self::Error> {
239 Err("<() as ElectionProvider> cannot do anything.")
240 }
241}