29 major breaking releases

32.0.0 Apr 8, 2024
31.0.0 Mar 18, 2024
30.0.0 Feb 26, 2024
29.0.0 Feb 13, 2024
0.0.0 Nov 21, 2022

#817 in Magic Beans

Download history 274/week @ 2023-12-22 715/week @ 2023-12-29 1033/week @ 2024-01-05 870/week @ 2024-01-12 885/week @ 2024-01-19 727/week @ 2024-01-26 759/week @ 2024-02-02 1418/week @ 2024-02-09 1914/week @ 2024-02-16 2164/week @ 2024-02-23 1124/week @ 2024-03-01 1073/week @ 2024-03-08 2150/week @ 2024-03-15 2027/week @ 2024-03-22 2008/week @ 2024-03-29 2436/week @ 2024-04-05

8,780 downloads per month
Used in 87 crates (15 directly)

Apache-2.0

2.5MB
50K SLoC

Primitive traits for providing election functionality.

This crate provides two traits that could interact to enable extensible election functionality within FRAME pallets.

Something that will provide the functionality of election will implement ElectionProvider and its parent-trait ElectionProviderBase, whilst needing an associated ElectionProviderBase::DataProvider, which needs to be fulfilled by an entity implementing ElectionDataProvider. Most often, the data provider is the receiver of the election, resulting in a diagram as below:

                                        ElectionDataProvider
                         <------------------------------------------+
                         |                                          |
                         v                                          |
                   +-----+----+                              +------+---+
                   |          |                              |          |
pallet-do-election |          |                              |          | pallet-needs-election
                   |          |                              |          |
                   |          |                              |          |
                   +-----+----+                              +------+---+
                         |                                          ^
                         |                                          |
                         +------------------------------------------+
                                        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).

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 queried ahead of time, and the election result might be ready some number of blocks in the future.

To accommodate both type of elections in one trait, the traits lean toward stateful election, as it is more general than the stateless. This is why ElectionProvider::elect has no parameters. All value and type parameter must be provided by the ElectionDataProvider trait, even if the election happens immediately.

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)

In addition to that, the ElectionDataProvider must also hint ElectionProvider at when the next election might happen (ElectionDataProvider::next_election_prediction). A stateless election provider would probably ignore this. A stateful election provider can use this to prepare the election result in advance.

Nonetheless, an ElectionProvider shan't rely on this and should preferably provide some means of fallback election as well, in case the elect was called immaturely early.

Example


type AccountId = u64;
type Balance = u64;
type BlockNumber = u32;

mod data_provider_mod {
    use super::*;

    pub trait Config: Sized {
        type ElectionProvider: ElectionProvider<
            AccountId = AccountId,
            BlockNumber = BlockNumber,
            DataProvider = Pallet<Self>,
        >;
    }

    pub struct Pallet<T: Config>(std::marker::PhantomData<T>);

    impl<T: Config> ElectionDataProvider for Pallet<T> {
        type AccountId = AccountId;
        type BlockNumber = BlockNumber;
        type MaxVotesPerVoter = ConstU32<1>;

        fn desired_targets() -> data_provider::Result<u32> {
            Ok(1)
        }
        fn electing_voters(bounds: DataProviderBounds)
          -> data_provider::Result<Vec<VoterOf<Self>>>
        {
            Ok(Default::default())
        }
        fn electable_targets(bounds: DataProviderBounds) -> data_provider::Result<Vec<AccountId>> {
            Ok(vec![10, 20, 30])
        }
        fn next_election_prediction(now: BlockNumber) -> BlockNumber {
            0
        }
    }
}


mod generic_election_provider {
    use super::*;

    pub struct GenericElectionProvider<T: Config>(std::marker::PhantomData<T>);

    pub trait Config {
        type DataProvider: ElectionDataProvider<AccountId=AccountId, BlockNumber = BlockNumber>;
    }

    impl<T: Config> ElectionProviderBase for GenericElectionProvider<T> {
        type AccountId = AccountId;
        type BlockNumber = BlockNumber;
        type Error = &'static str;
        type DataProvider = T::DataProvider;
        type MaxWinners = ConstU32<{ u32::MAX }>;

    }

    impl<T: Config> ElectionProvider for GenericElectionProvider<T> {
        fn ongoing() -> bool { false }
        fn elect() -> Result<BoundedSupportsOf<Self>, Self::Error> {
            Self::DataProvider::electable_targets(DataProviderBounds::default())
                .map_err(|_| "failed to elect")
                .map(|t| bounded_vec![(t[0], Support::default())])
        }
    }
}

mod runtime {
    use super::generic_election_provider;
    use super::data_provider_mod;
    use super::AccountId;

    struct Runtime;
    impl generic_election_provider::Config for Runtime {
        type DataProvider = data_provider_mod::Pallet<Runtime>;
    }

    impl data_provider_mod::Config for Runtime {
        type ElectionProvider = generic_election_provider::GenericElectionProvider<Runtime>;
    }

}

Dependencies

~17–31MB
~499K SLoC