Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Changes from 1 commit
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
25bddc9
mockup
kianenigma Sep 11, 2020
7bef52f
Add files
kianenigma Sep 11, 2020
4f3dfa4
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Sep 23, 2020
bc30ce1
More mockup
kianenigma Sep 24, 2020
9e52fbc
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Oct 1, 2020
14ebc95
Lots more changes and tests. Initial design of signed is forseable now.
kianenigma Oct 2, 2020
0587fc9
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Oct 8, 2020
233d4e1
More changes for the signed phase, now going to the cluster-fu*k of g…
kianenigma Oct 9, 2020
9c880f3
Finally decoupled solution from the pallet.. such a PITA.
kianenigma Oct 9, 2020
0cb35eb
Much simplifications and cleanups.
kianenigma Oct 9, 2020
c1ff3ec
A skeleton for the unsigned version.
kianenigma Oct 9, 2020
85a346e
Master.into()
kianenigma Oct 13, 2020
6a2542d
Staking builds and tests compile, some tests fail
kianenigma Oct 13, 2020
c361911
All staking tests work
kianenigma Oct 14, 2020
218db10
Unsigned almost done, primitive crate added.
kianenigma Oct 15, 2020
5e21a79
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Oct 15, 2020
25c1bc3
Use decl_error
kianenigma Oct 15, 2020
d53785c
Add basic benchmark setup, needs more work
kianenigma Oct 16, 2020
525f6e0
Comment.
kianenigma Oct 16, 2020
3026316
Checkpoint.
kianenigma Oct 20, 2020
9841e3b
node-runtime builds.
kianenigma Oct 20, 2020
16c0550
Builds and all good?
kianenigma Dec 3, 2020
f197b6b
Undo some formatting mistakes
kianenigma Dec 3, 2020
5005281
Working on benchmarks'
kianenigma Dec 7, 2020
bd0d331
Fix bench
kianenigma Dec 8, 2020
67dd4d1
Strange merge -- everything builds.
kianenigma Dec 8, 2020
eac4571
Everything compiles and good -- need to fix benches.
kianenigma Dec 8, 2020
a7c4841
Some Staking stuff left to do.
kianenigma Dec 9, 2020
2ca3dcf
Master.into()
kianenigma Dec 14, 2020
4922897
Split the snapshot; type for CompactSolution and MaxNominations
kianenigma Dec 15, 2020
ef593a7
Fix a bunch more issues.
kianenigma Dec 15, 2020
8661814
check new design
kianenigma Dec 18, 2020
57f7294
Everything within the module works and compiles.
kianenigma Dec 21, 2020
af304d1
Stkaing also builds
kianenigma Dec 21, 2020
80e7450
Everything builds with the new scheme.
kianenigma Dec 25, 2020
4b4712a
Master.into()
kianenigma Dec 26, 2020
206933a
Simplify elect API.
kianenigma Dec 26, 2020
af9b97d
Fix all other tests.
kianenigma Dec 26, 2020
9663fea
make the staking api better and well tested.
kianenigma Jan 4, 2021
7f4f5b7
Almost done with conversion
kianenigma Jan 4, 2021
8fb11f1
Move to pallet V2 + construct_runtime
kianenigma Jan 5, 2021
bd6d4d9
Master.into()
kianenigma Jan 5, 2021
65f5ac1
Revert staking changes;
kianenigma Jan 5, 2021
3860885
Revert Staking fuzzer.
kianenigma Jan 5, 2021
392f35a
Fix fuzzers
kianenigma Jan 5, 2021
1337e1b
Update frame/election-providers/src/two_phase/mod.rs
kianenigma Jan 6, 2021
4f6ec0d
a round of self review
kianenigma Jan 6, 2021
b3113f7
A large number of bugfixes after local test and self-review
kianenigma Jan 7, 2021
abca039
Upstream.into()
kianenigma Jan 7, 2021
6bc578a
Master.into()
kianenigma Jan 7, 2021
876f666
Apply suggestions from code review
kianenigma Jan 7, 2021
81df1df
Update frame/election-providers/src/two_phase/benchmarking.rs
kianenigma Jan 7, 2021
e3862ce
Fix some review grumbles.
kianenigma Jan 8, 2021
ca06f8e
Fix some docs
kianenigma Jan 8, 2021
6206c32
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 8, 2021
95d7a3c
Master.into()
kianenigma Jan 8, 2021
63776ff
Maybe relocalte the code?
kianenigma Jan 8, 2021
4732854
Master.into()
kianenigma Jan 8, 2021
a922f0b
Witness -> SoltionSize
kianenigma Jan 8, 2021
6d8247a
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 8, 2021
e9f4437
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 8, 2021
02069e1
Update frame/session/src/lib.rs
kianenigma Jan 12, 2021
000ecdc
Revamp macros
kianenigma Jan 12, 2021
cb6f156
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 12, 2021
df04ef3
Merge remote-tracking branch 'origin/master' into kiz-election-provider
Jan 12, 2021
ee2a316
Master.into()
kianenigma Jan 12, 2021
0d92ed5
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 12, 2021
88a96a0
Fix build
kianenigma Jan 12, 2021
6480ce1
Merge remote-tracking branch 'origin/master' into kiz-election-provider
Jan 12, 2021
8a68915
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 12, 2021
f4e2c03
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Jan 12, 2021
c41ecb0
fix all the tests.
kianenigma Jan 13, 2021
b42336a
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 13, 2021
f5e23f9
Fix the strange issue with cargo lock
kianenigma Jan 13, 2021
56bb93c
revamp closures.
kianenigma Jan 14, 2021
257795c
Update frame/two-phase-election-provider/src/benchmarking.rs
kianenigma Jan 14, 2021
9c114bc
Merge remote-tracking branch 'origin/master' into kiz-election-provider
Jan 14, 2021
1ac97f5
Some review grumbles.
kianenigma Jan 14, 2021
366b15b
Merge branch 'master' of github.com:paritytech/substrate into kiz-ele…
kianenigma Jan 14, 2021
afa5507
Upstream.into()
kianenigma Jan 14, 2021
5d206ab
Fix build
kianenigma Jan 14, 2021
ea193c7
Bring back benches
kianenigma Jan 14, 2021
60e57d7
cargo run --release --features=runtime-benchmarks --manifest-path=bin…
Jan 14, 2021
77f53a3
Add a bunch of more fixes: signed weight check, tests etc.
kianenigma Jan 15, 2021
9ce9d6b
Merge branch 'kiz-election-provider' of github.com:paritytech/substra…
kianenigma Jan 15, 2021
b30dfb3
Fix build
kianenigma Jan 15, 2021
6001d7d
Master.into()
kianenigma Jan 15, 2021
a066e4a
Last small tweaks
kianenigma Jan 15, 2021
10f6e7b
Fix all tests.
kianenigma Jan 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
check new design
  • Loading branch information
kianenigma committed Dec 18, 2020
commit 8661814f28f3dcfa6916889e1030acd9c5af739b
218 changes: 168 additions & 50 deletions primitives/election-providers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,166 @@

//! Primitive traits for providing election functionality.
//!
//! This crate provides two traits that could potentially be implemented by two struct. The struct
//! receiving the functionality election could potentially implement [`ElectionDataProvider`] and
//! the struct providing the election functionality implements [`ElectionProvider`].
//! This crate provides two traits that could potentially be implemented by two entities. The struct
//! receiving the functionality election should implement [`ElectionDataProvider`] and the struct
//! providing the election functionality implements [`ElectionProvider`], as such:
//!
//! ```ignore
//! ElectionDataProvider
//! <------------------------------------------+
//! | |
//! v |
//! +-----+----+ +------+---+
//! | | | |
//! pallet-do-election | | | |pallet-elect-winner
//! | | | |
//! | | | |
//! +-----+----+ +------+---+
//! | ^
//! | |
//! +------------------------------------------+
//! ElectionProvider
//!
//! ```
//!
//! > It could also be possible that a third party pallet (C), provides the data of election to an
//! > election provider (B), which then passes the election result to another pallet (A).
//!
//! Note that the [`ElectionProvider`] does not have a hard tie to the [`ElectionDataProvider`],
//! rather the link must be created by other means during implementation (i.e. an associated type in
//! `Config` trait the case of FRAME pallets).
//!
//! ## Election Types
//!
//! Typically, two types of elections exist:
//!
//! 1. Stateless: Election data is provided, and the election result is immediately ready.
//! 2. Stateful: Election data is is provided, and the election result might be ready some number of
//! blocks in the future.
//!
//! To accommodate both type of elections, the traits lean toward stateless election, as it is more
//! general than the stateless. This translates to the [`ElectionProvider::elect`] to have no
//! parameters. All value and type parameter must be provided by the [`ElectionDataProvider`] trait.
//!
//! ## Election Data
//!
//! The data associated with an election, essentially what the [`ElectionDataProvider`] must convey
//! is as follows:
//!
//! 1. A list of voters, with their stake.
//! 2. A list of targets (i.e. _candidates_).
//! 3. A number of desired targets to be elected (i.e. _winners_)
//! 4. An accuracy for the election's fixed point arithmetic.
//!
//! In addition to that, the [`ElectionDataProvider`] must also hint [`ElectionProvider`] at when
//! the next election might happen ([`ElectionDataProvider::next_election_prediction`]).
//! Nonetheless, an [`ElectionProvider`] shan't rely on this and should preferably provide some
//! means of fallback election as well.
//!
//! ## Example
//!
//! ```rust
//! # use sp_election_providers::*;
//! # use sp_npos_elections::Support;
//!
//! type AccountId = u64;
//! type Balance = u64;
//! type BlockNumber = u32;
//!
//! mod data_provider {
//! use super::*;
//!
//! pub trait Config {
//! type AccountId;
//! type ElectionProvider: ElectionProvider<Self::AccountId>;
//! }
//!
//! pub struct Module<T: Config>(std::marker::PhantomData<T>);
//!
//! impl<T: Config> ElectionDataProvider<AccountId, BlockNumber> for Module<T> {
//! fn desired_targets() -> u32 {
//! 1
//! }
//! fn voters() -> Vec<(AccountId, VoteWeight, Vec<AccountId>)> {
//! Default::default()
//! }
//! fn targets() -> Vec<AccountId> {
//! vec![10, 20, 30]
//! }
//! fn feasibility_check_assignment<P: PerThing>(
//! who: &AccountId,
//! distribution: &[(AccountId, P)],
//! ) -> bool {
//! true
//! }
//! fn next_election_prediction(now: BlockNumber) -> BlockNumber {
//! 0
//! }
//! }
//! }
//!
//!
//! mod election_provider {
//! use super::*;
//!
//! pub struct SomeElectionProvider<T: Config>(std::marker::PhantomData<T>);
//!
//! pub trait Config {
//! type DataProvider: ElectionDataProvider<AccountId, BlockNumber>;
//! }
//!
//! impl<T: Config> ElectionProvider<AccountId> for SomeElectionProvider<T> {
//! type Error = ();
//!
//! fn elect<P: PerThing128>() -> Result<Supports<AccountId>, Self::Error> {
//! T::DataProvider::targets()
//! .first()
//! .map(|winner| vec![(*winner, Support::default())])
//! .ok_or(())
//! }
//! fn ongoing() -> bool {
//! false
//! }
//! }
//! }
//!
//! mod runtime {
//! use super::election_provider;
//! use super::data_provider;
//! use super::AccountId;
//!
//! struct Runtime;
//! impl election_provider::Config for Runtime {
//! type DataProvider = data_provider::Module<Runtime>;
//! }
//!
//! impl data_provider::Config for Runtime {
//! type AccountId = AccountId;
//! type ElectionProvider = election_provider::SomeElectionProvider<Runtime>;
//! }
//!
//! }
//!
//! # fn main() {}
//! ```
//!
//!
//! ### ['ElectionDataProvider']'s side

#![cfg_attr(not(feature = "std"), no_std)]

use sp_std::prelude::*;

use sp_arithmetic::PerThing;
/// Re-export some type as they are used in the interface.
pub use sp_npos_elections::{CompactSolution, ExtendedBalance, PerThing128, Supports, VoteWeight};
pub use sp_arithmetic::PerThing;

/// Something that can provide the data to something else that implements [`ElectionProvider`], such
/// as the [`two_phase`] module.
///
/// The underlying purpose of this is to provide auxillary data to long-lasting election providers.
/// For example, the [`two_phase`] election provider needs to know the voters/targets list well in
/// advance and before a call to [`ElectionProvider::elect`].
///
/// For example, if pallet A wants to use the two-phase election:
///
/// ```rust,ignore
/// pub trait TraitA {
/// type ElectionProvider: ElectionProvider<_, _>;
/// }
///
/// // these function will be called by `Self::ElectionProvider` whenever needed.
/// impl ElectionDataProvider for PalletA { /* .. */ }
/// Something that can provide the data to something else that implements [`ElectionProvider`].
///
/// impl<T: TraitA> Module<T> {
/// fn do_election() {
/// // finalize the election.
/// T::ElectionProvider::elect( /* .. */ );
/// }
/// }
/// ```
pub trait ElectionDataProvider<AccountId, B> {
/// The underlying purpose of this is to provide auxillary data to stateful election providers. For
/// example, multi-block election provider needs to know the voters/targets list well in advance and
/// before a call to [`ElectionProvider::elect`].
pub trait ElectionDataProvider<AccountId, BlockNumber> {
/// All possible targets for the election, i.e. the candidates.
fn targets() -> Vec<AccountId>;

Expand All @@ -69,48 +192,43 @@ pub trait ElectionDataProvider<AccountId, B> {
/// words, check if `who` having a weight distribution described as `distribution` is correct or
/// not.
///
/// This might be called by the [`ElectionProvider`] upon processing solutions.
/// This might be called by the [`ElectionProvider`] upon processing election solutions.
///
/// Note that this is any feasibility check specific to `Self` that `ElectionProvider` is not
/// aware of. Simple feasibility (such as "did voter X actually vote for Y") should be checked
/// by `ElectionProvider` in any case.
fn feasibility_check_assignment<P: PerThing>(
who: &AccountId,
distribution: &[(AccountId, P)],
) -> bool;

/// Provide a best effort prediction about when the next election is about to happen.
///
/// In essence, `Self` should predict with this function when it will trigger the
/// `ElectionDataProvider::elect`.
fn next_election_prediction(now: B) -> B;
/// In essence, the implementor should predict with this function when it will trigger the
/// [`ElectionDataProvider::elect`].
fn next_election_prediction(now: BlockNumber) -> BlockNumber;
}

/// Something that can compute the result of an election and pass it back to the caller.
///
/// This trait only provides an interface to _request_ an election, i.e.
/// [`ElectionProvider::elect`]. That data required for the election need to be passed to the
/// implemented of this trait through some other way. One example of such is the
/// [`ElectionDataProvider`] traits.
pub trait ElectionProvider<AccountId> {
/// Indicate weather this election provider needs data when calling [`elect`] or not. If
/// `false`, then the call site can ignore all parameters of [`elect`].
const NEEDS_ELECT_DATA: bool;

/// The error type that is returned by the provider.
type Error;

/// Elect a new set of winners.
///
/// The result is returned in a target major format, namely as a support map.
///
/// Note that based on the logic of the type that will implement this trait, the input data may
/// or may not be used. To hint about this to the call site, [`NEEDS_ELECT_DATA`] should be
/// properly set.
/// The result is returned in a target major format, namely as vector of supports.
///
/// The implementation should, if possible, use the accuracy `P` to compute the election result.
fn elect<P: PerThing128>(
to_elect: usize,
targets: Vec<AccountId>,
voters: Vec<(AccountId, VoteWeight, Vec<AccountId>)>,
) -> Result<Supports<AccountId>, Self::Error>
where
ExtendedBalance: From<<P as PerThing>::Inner>;
fn elect<P: PerThing128>() -> Result<Supports<AccountId>, Self::Error>;

/// Returns true if an election is still ongoing.
///
/// This can be used by the call site to dynamically check of a long-lasting election (such as
/// [`two_phase`]) is still on-going or not.
/// This can be used by the call site to dynamically check of a stateful is still on-going or
/// not.
fn ongoing() -> bool;
}