mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 02:21:04 +00:00
Use proper bounded vector type for nominations (#10601)
* Use proper bounded vector type for nominations * add docs and tweak chill_other for cleanup purposes * Fix the build * remove TODO * add a bit more doc * even more docs gushc * Update frame/staking/src/pallet/mod.rs Co-authored-by: Zeke Mostov <z.mostov@gmail.com> * Update frame/staking/src/pallet/mod.rs Co-authored-by: Zeke Mostov <z.mostov@gmail.com> * Fix the nasty bug * also bound the Snapshot type * fix doc test * document bounded_vec * self-review * remove unused * Fix build * frame-support: repetition overload for bounded_vec Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> * fix * remove the need to allocate into unbounded voters etc etc * Don't expect * unbreal the build again * handle macro a bit better Co-authored-by: Zeke Mostov <z.mostov@gmail.com> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
This commit is contained in:
@@ -80,6 +80,7 @@
|
||||
//! ```rust
|
||||
//! # use frame_election_provider_support::{*, data_provider};
|
||||
//! # use sp_npos_elections::{Support, Assignment};
|
||||
//! # use frame_support::traits::ConstU32;
|
||||
//!
|
||||
//! type AccountId = u64;
|
||||
//! type Balance = u64;
|
||||
@@ -101,13 +102,13 @@
|
||||
//! impl<T: Config> ElectionDataProvider for Pallet<T> {
|
||||
//! type AccountId = AccountId;
|
||||
//! type BlockNumber = BlockNumber;
|
||||
//! const MAXIMUM_VOTES_PER_VOTER: u32 = 1;
|
||||
//! type MaxVotesPerVoter = ConstU32<1>;
|
||||
//!
|
||||
//! fn desired_targets() -> data_provider::Result<u32> {
|
||||
//! Ok(1)
|
||||
//! }
|
||||
//! fn voters(maybe_max_len: Option<usize>)
|
||||
//! -> data_provider::Result<Vec<(AccountId, VoteWeight, Vec<AccountId>)>>
|
||||
//! -> data_provider::Result<Vec<VoterOf<Self>>>
|
||||
//! {
|
||||
//! Ok(Default::default())
|
||||
//! }
|
||||
@@ -166,7 +167,7 @@
|
||||
#![cfg_attr(not(feature = "std"), no_std)]
|
||||
|
||||
pub mod onchain;
|
||||
use frame_support::traits::Get;
|
||||
use frame_support::{traits::Get, BoundedVec};
|
||||
use sp_std::{fmt::Debug, prelude::*};
|
||||
|
||||
/// Re-export some type as they are used in the interface.
|
||||
@@ -191,7 +192,7 @@ pub trait ElectionDataProvider {
|
||||
type BlockNumber;
|
||||
|
||||
/// Maximum number of votes per voter that this data provider is providing.
|
||||
const MAXIMUM_VOTES_PER_VOTER: u32;
|
||||
type MaxVotesPerVoter: Get<u32>;
|
||||
|
||||
/// All possible targets for the election, i.e. the candidates.
|
||||
///
|
||||
@@ -211,9 +212,7 @@ pub trait ElectionDataProvider {
|
||||
///
|
||||
/// This should be implemented as a self-weighing function. The implementor should register its
|
||||
/// appropriate weight at the end of execution with the system pallet directly.
|
||||
fn voters(
|
||||
maybe_max_len: Option<usize>,
|
||||
) -> data_provider::Result<Vec<(Self::AccountId, VoteWeight, Vec<Self::AccountId>)>>;
|
||||
fn voters(maybe_max_len: Option<usize>) -> data_provider::Result<Vec<VoterOf<Self>>>;
|
||||
|
||||
/// The number of targets to elect.
|
||||
///
|
||||
@@ -233,7 +232,7 @@ pub trait ElectionDataProvider {
|
||||
/// else a noop.
|
||||
#[cfg(any(feature = "runtime-benchmarks", test))]
|
||||
fn put_snapshot(
|
||||
_voters: Vec<(Self::AccountId, VoteWeight, Vec<Self::AccountId>)>,
|
||||
_voters: Vec<VoterOf<Self>>,
|
||||
_targets: Vec<Self::AccountId>,
|
||||
_target_stake: Option<VoteWeight>,
|
||||
) {
|
||||
@@ -244,7 +243,12 @@ pub trait ElectionDataProvider {
|
||||
///
|
||||
/// Same as `put_snapshot`, but can add a single voter one by one.
|
||||
#[cfg(any(feature = "runtime-benchmarks", test))]
|
||||
fn add_voter(_voter: Self::AccountId, _weight: VoteWeight, _targets: Vec<Self::AccountId>) {}
|
||||
fn add_voter(
|
||||
_voter: Self::AccountId,
|
||||
_weight: VoteWeight,
|
||||
_targets: BoundedVec<Self::AccountId, Self::MaxVotesPerVoter>,
|
||||
) {
|
||||
}
|
||||
|
||||
/// Utility function only to be used in benchmarking scenarios, to be implemented optionally,
|
||||
/// else a noop.
|
||||
@@ -266,19 +270,20 @@ pub struct TestDataProvider<X>(sp_std::marker::PhantomData<X>);
|
||||
impl<AccountId, BlockNumber> ElectionDataProvider for TestDataProvider<(AccountId, BlockNumber)> {
|
||||
type AccountId = AccountId;
|
||||
type BlockNumber = BlockNumber;
|
||||
type MaxVotesPerVoter = ();
|
||||
|
||||
const MAXIMUM_VOTES_PER_VOTER: u32 = 0;
|
||||
fn targets(_maybe_max_len: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
fn voters(
|
||||
_maybe_max_len: Option<usize>,
|
||||
) -> data_provider::Result<Vec<(AccountId, VoteWeight, Vec<AccountId>)>> {
|
||||
|
||||
fn voters(_maybe_max_len: Option<usize>) -> data_provider::Result<Vec<VoterOf<Self>>> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
fn desired_targets() -> data_provider::Result<u32> {
|
||||
Ok(Default::default())
|
||||
}
|
||||
|
||||
fn next_election_prediction(now: BlockNumber) -> BlockNumber {
|
||||
now
|
||||
}
|
||||
@@ -421,7 +426,7 @@ pub trait NposSolver {
|
||||
fn solve(
|
||||
to_elect: usize,
|
||||
targets: Vec<Self::AccountId>,
|
||||
voters: Vec<(Self::AccountId, VoteWeight, Vec<Self::AccountId>)>,
|
||||
voters: Vec<(Self::AccountId, VoteWeight, impl IntoIterator<Item = Self::AccountId>)>,
|
||||
) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error>;
|
||||
}
|
||||
|
||||
@@ -443,7 +448,7 @@ impl<
|
||||
fn solve(
|
||||
winners: usize,
|
||||
targets: Vec<Self::AccountId>,
|
||||
voters: Vec<(Self::AccountId, VoteWeight, Vec<Self::AccountId>)>,
|
||||
voters: Vec<(Self::AccountId, VoteWeight, impl IntoIterator<Item = Self::AccountId>)>,
|
||||
) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error> {
|
||||
sp_npos_elections::seq_phragmen(winners, targets, voters, Balancing::get())
|
||||
}
|
||||
@@ -467,8 +472,15 @@ impl<
|
||||
fn solve(
|
||||
winners: usize,
|
||||
targets: Vec<Self::AccountId>,
|
||||
voters: Vec<(Self::AccountId, VoteWeight, Vec<Self::AccountId>)>,
|
||||
voters: Vec<(Self::AccountId, VoteWeight, impl IntoIterator<Item = Self::AccountId>)>,
|
||||
) -> Result<ElectionResult<Self::AccountId, Self::Accuracy>, Self::Error> {
|
||||
sp_npos_elections::phragmms(winners, targets, voters, Balancing::get())
|
||||
}
|
||||
}
|
||||
|
||||
/// A voter, at the level of abstraction of this crate.
|
||||
pub type Voter<AccountId, Bound> = (AccountId, VoteWeight, BoundedVec<AccountId, Bound>);
|
||||
|
||||
/// Same as [`Voter`], but parameterized by an [`ElectionDataProvider`].
|
||||
pub type VoterOf<D> =
|
||||
Voter<<D as ElectionDataProvider>::AccountId, <D as ElectionDataProvider>::MaxVotesPerVoter>;
|
||||
|
||||
@@ -87,9 +87,8 @@ impl<T: Config> ElectionProvider for OnChainSequentialPhragmen<T> {
|
||||
let stake_of =
|
||||
|w: &T::AccountId| -> VoteWeight { stake_map.get(w).cloned().unwrap_or_default() };
|
||||
|
||||
let ElectionResult { winners: _, assignments } =
|
||||
seq_phragmen::<_, T::Accuracy>(desired_targets as usize, targets, voters, None)
|
||||
.map_err(Error::from)?;
|
||||
let ElectionResult::<_, T::Accuracy> { winners: _, assignments } =
|
||||
seq_phragmen(desired_targets as usize, targets, voters, None).map_err(Error::from)?;
|
||||
|
||||
let staked = assignment_ratio_to_staked_normalized(assignments, &stake_of)?;
|
||||
|
||||
@@ -161,18 +160,22 @@ mod tests {
|
||||
type OnChainPhragmen = OnChainSequentialPhragmen<Runtime>;
|
||||
|
||||
mod mock_data_provider {
|
||||
use frame_support::{bounded_vec, traits::ConstU32};
|
||||
|
||||
use super::*;
|
||||
use crate::data_provider;
|
||||
use crate::{data_provider, VoterOf};
|
||||
|
||||
pub struct DataProvider;
|
||||
impl ElectionDataProvider for DataProvider {
|
||||
type AccountId = AccountId;
|
||||
type BlockNumber = BlockNumber;
|
||||
const MAXIMUM_VOTES_PER_VOTER: u32 = 2;
|
||||
fn voters(
|
||||
_: Option<usize>,
|
||||
) -> data_provider::Result<Vec<(AccountId, VoteWeight, Vec<AccountId>)>> {
|
||||
Ok(vec![(1, 10, vec![10, 20]), (2, 20, vec![30, 20]), (3, 30, vec![10, 30])])
|
||||
type MaxVotesPerVoter = ConstU32<2>;
|
||||
fn voters(_: Option<usize>) -> data_provider::Result<Vec<VoterOf<Self>>> {
|
||||
Ok(vec![
|
||||
(1, 10, bounded_vec![10, 20]),
|
||||
(2, 20, bounded_vec![30, 20]),
|
||||
(3, 30, bounded_vec![10, 30]),
|
||||
])
|
||||
}
|
||||
|
||||
fn targets(_: Option<usize>) -> data_provider::Result<Vec<AccountId>> {
|
||||
|
||||
Reference in New Issue
Block a user