Multi-Block Election part 0: preparation and some cleanup. (#9442)

* Partially applied

* Everything builds, need to implement compact encoding as well.

* Fix some tests, add a ui test as well.

* Fix everything and everything.

* small nits

* a bunch more rename

* more reorg

* more reorg

* last nit of self-review

* Seemingly fixed the build now

* Fix build

* make it work again

* Update primitives/npos-elections/solution-type/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* Update primitives/npos-elections/solution-type/src/lib.rs

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>

* nits

* factor out double type

* fix try-build

Co-authored-by: Guillaume Thiolliere <gui.thiolliere@gmail.com>
This commit is contained in:
Kian Paimani
2021-08-11 17:45:53 +02:00
committed by GitHub
parent abd08e29ce
commit f7bcbdd261
36 changed files with 1327 additions and 1364 deletions
@@ -167,11 +167,11 @@ impl<AccountId> StakedAssignment<AccountId> {
}
}
/// The [`IndexAssignment`] type is an intermediate between the assignments list
/// ([`&[Assignment<T>]`][Assignment]) and `CompactOf<T>`.
/// ([`&[Assignment<T>]`][Assignment]) and `SolutionOf<T>`.
///
/// The voter and target identifiers have already been replaced with appropriate indices,
/// making it fast to repeatedly encode into a `CompactOf<T>`. This property turns out
/// to be important when trimming for compact length.
/// making it fast to repeatedly encode into a `SolutionOf<T>`. This property turns out
/// to be important when trimming for solution length.
#[derive(RuntimeDebug, Clone, Default)]
#[cfg_attr(feature = "std", derive(PartialEq, Eq, Encode, Decode))]
pub struct IndexAssignment<VoterIndex, TargetIndex, P: PerThing> {
@@ -201,9 +201,9 @@ impl<VoterIndex, TargetIndex, P: PerThing> IndexAssignment<VoterIndex, TargetInd
}
}
/// A type alias for [`IndexAssignment`] made from [`crate::CompactSolution`].
/// A type alias for [`IndexAssignment`] made from [`crate::Solution`].
pub type IndexAssignmentOf<C> = IndexAssignment<
<C as crate::CompactSolution>::Voter,
<C as crate::CompactSolution>::Target,
<C as crate::CompactSolution>::Accuracy,
<C as crate::NposSolution>::VoterIndex,
<C as crate::NposSolution>::TargetIndex,
<C as crate::NposSolution>::Accuracy,
>;
+25 -155
View File
@@ -74,21 +74,9 @@
#![cfg_attr(not(feature = "std"), no_std)]
use sp_arithmetic::{
traits::{Bounded, UniqueSaturatedInto, Zero},
Normalizable, PerThing, Rational128, ThresholdOrd,
};
use sp_arithmetic::{traits::Zero, Normalizable, PerThing, Rational128, ThresholdOrd};
use sp_core::RuntimeDebug;
use sp_std::{
cell::RefCell,
cmp::Ordering,
collections::btree_map::BTreeMap,
convert::{TryFrom, TryInto},
fmt::Debug,
ops::Mul,
prelude::*,
rc::Rc,
};
use sp_std::{cell::RefCell, cmp::Ordering, collections::btree_map::BTreeMap, prelude::*, rc::Rc};
use codec::{Decode, Encode};
#[cfg(feature = "std")]
@@ -107,6 +95,7 @@ pub mod phragmen;
pub mod phragmms;
pub mod pjr;
pub mod reduce;
pub mod traits;
pub use assignments::{Assignment, IndexAssignment, IndexAssignmentOf, StakedAssignment};
pub use balancing::*;
@@ -115,8 +104,9 @@ pub use phragmen::*;
pub use phragmms::*;
pub use pjr::*;
pub use reduce::reduce;
pub use traits::{IdentifierT, NposSolution, PerThing128, __OrInvalidIndex};
// re-export the compact macro, with the dependencies of the macro.
// re-export for the solution macro, with the dependencies of the macro.
#[doc(hidden)]
pub use codec;
#[doc(hidden)]
@@ -124,141 +114,21 @@ pub use sp_arithmetic;
#[doc(hidden)]
pub use sp_std;
/// Simple Extension trait to easily convert `None` from index closures to `Err`.
///
/// This is only generated and re-exported for the compact solution code to use.
#[doc(hidden)]
pub trait __OrInvalidIndex<T> {
fn or_invalid_index(self) -> Result<T, Error>;
}
// re-export the solution type macro.
pub use sp_npos_elections_solution_type::generate_solution_type;
impl<T> __OrInvalidIndex<T> for Option<T> {
fn or_invalid_index(self) -> Result<T, Error> {
self.ok_or(Error::CompactInvalidIndex)
}
}
/// A common interface for all compact solutions.
///
/// See [`sp-npos-elections-compact`] for more info.
pub trait CompactSolution
where
Self: Sized + for<'a> sp_std::convert::TryFrom<&'a [IndexAssignmentOf<Self>], Error = Error>,
{
/// The maximum number of votes that are allowed.
const LIMIT: usize;
/// The voter type. Needs to be an index (convert to usize).
type Voter: UniqueSaturatedInto<usize>
+ TryInto<usize>
+ TryFrom<usize>
+ Debug
+ Copy
+ Clone
+ Bounded;
/// The target type. Needs to be an index (convert to usize).
type Target: UniqueSaturatedInto<usize>
+ TryInto<usize>
+ TryFrom<usize>
+ Debug
+ Copy
+ Clone
+ Bounded;
/// The weight/accuracy type of each vote.
type Accuracy: PerThing128;
/// Build self from a list of assignments.
fn from_assignment<FV, FT, A>(
assignments: &[Assignment<A, Self::Accuracy>],
voter_index: FV,
target_index: FT,
) -> Result<Self, Error>
where
A: IdentifierT,
for<'r> FV: Fn(&'r A) -> Option<Self::Voter>,
for<'r> FT: Fn(&'r A) -> Option<Self::Target>;
/// Convert self into a `Vec<Assignment<A, Self::Accuracy>>`
fn into_assignment<A: IdentifierT>(
self,
voter_at: impl Fn(Self::Voter) -> Option<A>,
target_at: impl Fn(Self::Target) -> Option<A>,
) -> Result<Vec<Assignment<A, Self::Accuracy>>, Error>;
/// Get the length of all the voters that this type is encoding.
///
/// This is basically the same as the number of assignments, or number of active voters.
fn voter_count(&self) -> usize;
/// Get the total count of edges.
///
/// This is effectively in the range of {[`Self::voter_count`], [`Self::voter_count`] *
/// [`Self::LIMIT`]}.
fn edge_count(&self) -> usize;
/// Get the number of unique targets in the whole struct.
///
/// Once presented with a list of winners, this set and the set of winners must be
/// equal.
fn unique_targets(&self) -> Vec<Self::Target>;
/// Get the average edge count.
fn average_edge_count(&self) -> usize {
self.edge_count().checked_div(self.voter_count()).unwrap_or(0)
}
/// Remove a certain voter.
///
/// This will only search until the first instance of `to_remove`, and return true. If
/// no instance is found (no-op), then it returns false.
///
/// In other words, if this return true, exactly **one** element must have been removed from
/// `self.len()`.
fn remove_voter(&mut self, to_remove: Self::Voter) -> bool;
/// Compute the score of this compact solution type.
fn score<A, FS>(
self,
winners: &[A],
stake_of: FS,
voter_at: impl Fn(Self::Voter) -> Option<A>,
target_at: impl Fn(Self::Target) -> Option<A>,
) -> Result<ElectionScore, Error>
where
for<'r> FS: Fn(&'r A) -> VoteWeight,
A: IdentifierT,
{
let ratio = self.into_assignment(voter_at, target_at)?;
let staked = helpers::assignment_ratio_to_staked_normalized(ratio, stake_of)?;
let supports = to_supports(winners, &staked)?;
Ok(supports.evaluate())
}
}
// re-export the compact solution type.
pub use sp_npos_elections_compact::generate_solution_type;
/// an aggregator trait for a generic type of a voter/target identifier. This usually maps to
/// substrate's account id.
pub trait IdentifierT: Clone + Eq + Default + Ord + Debug + codec::Codec {}
impl<T: Clone + Eq + Default + Ord + Debug + codec::Codec> IdentifierT for T {}
/// Aggregator trait for a PerThing that can be multiplied by u128 (ExtendedBalance).
pub trait PerThing128: PerThing + Mul<ExtendedBalance, Output = ExtendedBalance> {}
impl<T: PerThing + Mul<ExtendedBalance, Output = ExtendedBalance>> PerThing128 for T {}
/// The errors that might occur in the this crate and compact.
/// The errors that might occur in the this crate and solution-type.
#[derive(Eq, PartialEq, RuntimeDebug)]
pub enum Error {
/// While going from compact to staked, the stake of all the edges has gone above the total and
/// the last stake cannot be assigned.
CompactStakeOverflow,
/// The compact type has a voter who's number of targets is out of bound.
CompactTargetOverflow,
/// While going from solution indices to ratio, the weight of all the edges has gone above the
/// total.
SolutionWeightOverflow,
/// The solution type has a voter who's number of targets is out of bound.
SolutionTargetOverflow,
/// One of the index functions returned none.
CompactInvalidIndex,
SolutionInvalidIndex,
/// One of the page indices was invalid
SolutionInvalidPageIndex,
/// An error occurred in some arithmetic operation.
ArithmeticError(&'static str),
/// The data provided to create support map was invalid.
@@ -507,12 +377,12 @@ impl<A> FlattenSupportMap<A> for SupportMap<A> {
///
/// The list of winners is basically a redundancy for error checking only; It ensures that all the
/// targets pointed to by the [`Assignment`] are present in the `winners`.
pub fn to_support_map<A: IdentifierT>(
winners: &[A],
assignments: &[StakedAssignment<A>],
) -> Result<SupportMap<A>, Error> {
pub fn to_support_map<AccountId: IdentifierT>(
winners: &[AccountId],
assignments: &[StakedAssignment<AccountId>],
) -> Result<SupportMap<AccountId>, Error> {
// Initialize the support of each candidate.
let mut supports = <SupportMap<A>>::new();
let mut supports = <SupportMap<AccountId>>::new();
winners.iter().for_each(|e| {
supports.insert(e.clone(), Default::default());
});
@@ -535,10 +405,10 @@ pub fn to_support_map<A: IdentifierT>(
/// flat vector.
///
/// Similar to [`to_support_map`], `winners` is used for error checking.
pub fn to_supports<A: IdentifierT>(
winners: &[A],
assignments: &[StakedAssignment<A>],
) -> Result<Supports<A>, Error> {
pub fn to_supports<AccountId: IdentifierT>(
winners: &[AccountId],
assignments: &[StakedAssignment<AccountId>],
) -> Result<Supports<AccountId>, Error> {
to_support_map(winners, assignments).map(FlattenSupportMap::flatten)
}
+37 -19
View File
@@ -17,7 +17,7 @@
//! Mock file for npos-elections.
#![cfg(any(test, mocks))]
#![cfg(test)]
use std::{
collections::{HashMap, HashSet},
@@ -35,20 +35,27 @@ use sp_std::collections::btree_map::BTreeMap;
use crate::{seq_phragmen, Assignment, ElectionResult, ExtendedBalance, PerThing128, VoteWeight};
sp_npos_elections_compact::generate_solution_type!(
#[compact]
pub struct Compact::<VoterIndex = u32, TargetIndex = u16, Accuracy = Accuracy>(16)
);
pub type AccountId = u64;
/// The candidate mask allows easy disambiguation between voters and candidates: accounts
/// for which this bit is set are candidates, and without it, are voters.
pub const CANDIDATE_MASK: AccountId = 1 << ((std::mem::size_of::<AccountId>() * 8) - 1);
pub type CandidateId = AccountId;
pub type Accuracy = sp_runtime::Perbill;
pub type TestAccuracy = sp_runtime::Perbill;
pub type MockAssignment = crate::Assignment<AccountId, Accuracy>;
crate::generate_solution_type! {
pub struct TestSolution::<
VoterIndex = u32,
TargetIndex = u16,
Accuracy = TestAccuracy,
>(16)
}
pub fn p(p: u8) -> TestAccuracy {
TestAccuracy::from_percent(p.into())
}
pub type MockAssignment = crate::Assignment<AccountId, TestAccuracy>;
pub type Voter = (AccountId, VoteWeight, Vec<AccountId>);
#[derive(Default, Debug)]
@@ -422,7 +429,7 @@ pub fn generate_random_votes(
candidate_count: usize,
voter_count: usize,
mut rng: impl Rng,
) -> (Vec<Voter>, Vec<MockAssignment>, Vec<CandidateId>) {
) -> (Vec<Voter>, Vec<MockAssignment>, Vec<AccountId>) {
// cache for fast generation of unique candidate and voter ids
let mut used_ids = HashSet::with_capacity(candidate_count + voter_count);
@@ -452,7 +459,8 @@ pub fn generate_random_votes(
// it's not interesting if a voter chooses 0 or all candidates, so rule those cases out.
// also, let's not generate any cases which result in a compact overflow.
let n_candidates_chosen = rng.gen_range(1, candidates.len().min(16));
let n_candidates_chosen =
rng.gen_range(1, candidates.len().min(<TestSolution as crate::NposSolution>::LIMIT));
let mut chosen_candidates = Vec::with_capacity(n_candidates_chosen);
chosen_candidates.extend(candidates.choose_multiple(&mut rng, n_candidates_chosen));
@@ -473,16 +481,16 @@ pub fn generate_random_votes(
// distribute the available stake randomly
let stake_distribution = if num_chosen_winners == 0 {
Vec::new()
continue
} else {
let mut available_stake = 1000;
let mut stake_distribution = Vec::with_capacity(num_chosen_winners);
for _ in 0..num_chosen_winners - 1 {
let stake = rng.gen_range(0, available_stake);
stake_distribution.push(Accuracy::from_perthousand(stake));
let stake = rng.gen_range(0, available_stake).min(1);
stake_distribution.push(TestAccuracy::from_perthousand(stake));
available_stake -= stake;
}
stake_distribution.push(Accuracy::from_perthousand(available_stake));
stake_distribution.push(TestAccuracy::from_perthousand(available_stake));
stake_distribution.shuffle(&mut rng);
stake_distribution
};
@@ -514,16 +522,26 @@ where
usize: TryInto<VoterIndex>,
{
let cache = generate_cache(voters.iter().map(|(id, _, _)| *id));
move |who| cache.get(who).cloned().and_then(|i| i.try_into().ok())
move |who| {
if cache.get(who).is_none() {
println!("WARNING: voter {} will raise InvalidIndex", who);
}
cache.get(who).cloned().and_then(|i| i.try_into().ok())
}
}
/// Create a function that returns the index of a candidate in the candidates list.
pub fn make_target_fn<TargetIndex>(
candidates: &[CandidateId],
) -> impl Fn(&CandidateId) -> Option<TargetIndex>
candidates: &[AccountId],
) -> impl Fn(&AccountId) -> Option<TargetIndex>
where
usize: TryInto<TargetIndex>,
{
let cache = generate_cache(candidates.iter().cloned());
move |who| cache.get(who).cloned().and_then(|i| i.try_into().ok())
move |who| {
if cache.get(who).is_none() {
println!("WARNING: target {} will raise InvalidIndex", who);
}
cache.get(who).cloned().and_then(|i| i.try_into().ok())
}
}
+120 -188
View File
@@ -19,8 +19,8 @@
use crate::{
balancing, helpers::*, is_score_better, mock::*, seq_phragmen, seq_phragmen_core, setup_inputs,
to_support_map, to_supports, Assignment, CompactSolution, ElectionResult, EvaluateSupport,
ExtendedBalance, IndexAssignment, StakedAssignment, Support, Voter,
to_support_map, to_supports, Assignment, ElectionResult, EvaluateSupport, ExtendedBalance,
IndexAssignment, NposSolution, StakedAssignment, Support, Voter,
};
use rand::{self, SeedableRng};
use sp_arithmetic::{PerU16, Perbill, Percent, Permill};
@@ -917,30 +917,20 @@ mod score {
}
mod solution_type {
use super::AccountId;
use super::*;
use codec::{Decode, Encode};
// these need to come from the same dev-dependency `sp-npos-elections`, not from the crate.
use crate::{generate_solution_type, Assignment, CompactSolution, Error as PhragmenError};
use sp_arithmetic::Percent;
use crate::{generate_solution_type, Assignment, Error as NposError, NposSolution};
use sp_std::{convert::TryInto, fmt::Debug};
type TestAccuracy = Percent;
generate_solution_type!(pub struct TestSolutionCompact::<
VoterIndex = u32,
TargetIndex = u8,
Accuracy = TestAccuracy,
>(16));
#[allow(dead_code)]
mod __private {
// This is just to make sure that that the compact can be generated in a scope without any
// This is just to make sure that the solution can be generated in a scope without any
// imports.
use crate::generate_solution_type;
use sp_arithmetic::Percent;
generate_solution_type!(
#[compact]
struct InnerTestSolutionCompact::<VoterIndex = u32, TargetIndex = u8, Accuracy = Percent>(12)
struct InnerTestSolutionIsolated::<VoterIndex = u32, TargetIndex = u8, Accuracy = sp_runtime::Percent>(12)
);
}
@@ -948,35 +938,34 @@ mod solution_type {
fn solution_struct_works_with_and_without_compact() {
// we use u32 size to make sure compact is smaller.
let without_compact = {
generate_solution_type!(pub struct InnerTestSolution::<
VoterIndex = u32,
TargetIndex = u32,
Accuracy = Percent,
>(16));
let compact = InnerTestSolution {
generate_solution_type!(
pub struct InnerTestSolution::<
VoterIndex = u32,
TargetIndex = u32,
Accuracy = TestAccuracy,
>(16)
);
let solution = InnerTestSolution {
votes1: vec![(2, 20), (4, 40)],
votes2: vec![
(1, (10, TestAccuracy::from_percent(80)), 11),
(5, (50, TestAccuracy::from_percent(85)), 51),
],
votes2: vec![(1, [(10, p(80))], 11), (5, [(50, p(85))], 51)],
..Default::default()
};
compact.encode().len()
solution.encode().len()
};
let with_compact = {
generate_solution_type!(#[compact] pub struct InnerTestSolutionCompact::<
VoterIndex = u32,
TargetIndex = u32,
Accuracy = Percent,
>(16));
generate_solution_type!(
#[compact]
pub struct InnerTestSolutionCompact::<
VoterIndex = u32,
TargetIndex = u32,
Accuracy = TestAccuracy,
>(16)
);
let compact = InnerTestSolutionCompact {
votes1: vec![(2, 20), (4, 40)],
votes2: vec![
(1, (10, TestAccuracy::from_percent(80)), 11),
(5, (50, TestAccuracy::from_percent(85)), 51),
],
votes2: vec![(1, [(10, p(80))], 11), (5, [(50, p(85))], 51)],
..Default::default()
};
@@ -988,78 +977,64 @@ mod solution_type {
#[test]
fn solution_struct_is_codec() {
let compact = TestSolutionCompact {
let solution = TestSolution {
votes1: vec![(2, 20), (4, 40)],
votes2: vec![
(1, (10, TestAccuracy::from_percent(80)), 11),
(5, (50, TestAccuracy::from_percent(85)), 51),
],
votes2: vec![(1, [(10, p(80))], 11), (5, [(50, p(85))], 51)],
..Default::default()
};
let encoded = compact.encode();
let encoded = solution.encode();
assert_eq!(compact, Decode::decode(&mut &encoded[..]).unwrap());
assert_eq!(compact.voter_count(), 4);
assert_eq!(compact.edge_count(), 2 + 4);
assert_eq!(compact.unique_targets(), vec![10, 11, 20, 40, 50, 51]);
assert_eq!(solution, Decode::decode(&mut &encoded[..]).unwrap());
assert_eq!(solution.voter_count(), 4);
assert_eq!(solution.edge_count(), 2 + 4);
assert_eq!(solution.unique_targets(), vec![10, 11, 20, 40, 50, 51]);
}
#[test]
fn remove_voter_works() {
let mut compact = TestSolutionCompact {
let mut solution = TestSolution {
votes1: vec![(0, 2), (1, 6)],
votes2: vec![
(2, (0, TestAccuracy::from_percent(80)), 1),
(3, (7, TestAccuracy::from_percent(85)), 8),
],
votes3: vec![(
4,
[(3, TestAccuracy::from_percent(50)), (4, TestAccuracy::from_percent(25))],
5,
)],
votes2: vec![(2, [(0, p(80))], 1), (3, [(7, p(85))], 8)],
votes3: vec![(4, [(3, p(50)), (4, p(25))], 5)],
..Default::default()
};
assert!(!compact.remove_voter(11));
assert!(compact.remove_voter(2));
assert!(!solution.remove_voter(11));
assert!(solution.remove_voter(2));
assert_eq!(
compact,
TestSolutionCompact {
solution,
TestSolution {
votes1: vec![(0, 2), (1, 6)],
votes2: vec![(3, (7, TestAccuracy::from_percent(85)), 8),],
votes3: vec![(
4,
[(3, TestAccuracy::from_percent(50)), (4, TestAccuracy::from_percent(25))],
5,
),],
votes2: vec![(3, [(7, p(85))], 8)],
votes3: vec![(4, [(3, p(50)), (4, p(25))], 5,)],
..Default::default()
},
);
assert!(compact.remove_voter(4));
assert!(solution.remove_voter(4));
assert_eq!(
compact,
TestSolutionCompact {
solution,
TestSolution {
votes1: vec![(0, 2), (1, 6)],
votes2: vec![(3, (7, TestAccuracy::from_percent(85)), 8),],
votes2: vec![(3, [(7, p(85))], 8)],
..Default::default()
},
);
assert!(compact.remove_voter(1));
assert!(solution.remove_voter(1));
assert_eq!(
compact,
TestSolutionCompact {
solution,
TestSolution {
votes1: vec![(0, 2)],
votes2: vec![(3, (7, TestAccuracy::from_percent(85)), 8),],
votes2: vec![(3, [(7, p(85))], 8),],
..Default::default()
},
);
}
#[test]
fn basic_from_and_into_compact_works_assignments() {
fn from_and_into_assignment_works() {
let voters = vec![2 as AccountId, 4, 1, 5, 3];
let targets = vec![
10 as AccountId,
@@ -1074,182 +1049,144 @@ mod solution_type {
];
let assignments = vec![
Assignment {
who: 2 as AccountId,
distribution: vec![(20u64, TestAccuracy::from_percent(100))],
},
Assignment { who: 4, distribution: vec![(40, TestAccuracy::from_percent(100))] },
Assignment {
who: 1,
distribution: vec![
(10, TestAccuracy::from_percent(80)),
(11, TestAccuracy::from_percent(20)),
],
},
Assignment {
who: 5,
distribution: vec![
(50, TestAccuracy::from_percent(85)),
(51, TestAccuracy::from_percent(15)),
],
},
Assignment {
who: 3,
distribution: vec![
(30, TestAccuracy::from_percent(50)),
(31, TestAccuracy::from_percent(25)),
(32, TestAccuracy::from_percent(25)),
],
},
Assignment { who: 2 as AccountId, distribution: vec![(20u64, p(100))] },
Assignment { who: 4, distribution: vec![(40, p(100))] },
Assignment { who: 1, distribution: vec![(10, p(80)), (11, p(20))] },
Assignment { who: 5, distribution: vec![(50, p(85)), (51, p(15))] },
Assignment { who: 3, distribution: vec![(30, p(50)), (31, p(25)), (32, p(25))] },
];
let voter_index = |a: &AccountId| -> Option<u32> {
voters.iter().position(|x| x == a).map(TryInto::try_into).unwrap().ok()
};
let target_index = |a: &AccountId| -> Option<u8> {
let target_index = |a: &AccountId| -> Option<u16> {
targets.iter().position(|x| x == a).map(TryInto::try_into).unwrap().ok()
};
let compacted =
TestSolutionCompact::from_assignment(&assignments, voter_index, target_index).unwrap();
let solution =
TestSolution::from_assignment(&assignments, voter_index, target_index).unwrap();
// basically number of assignments that it is encoding.
assert_eq!(compacted.voter_count(), assignments.len());
assert_eq!(solution.voter_count(), assignments.len());
assert_eq!(
compacted.edge_count(),
solution.edge_count(),
assignments.iter().fold(0, |a, b| a + b.distribution.len()),
);
assert_eq!(
compacted,
TestSolutionCompact {
solution,
TestSolution {
votes1: vec![(0, 2), (1, 6)],
votes2: vec![
(2, (0, TestAccuracy::from_percent(80)), 1),
(3, (7, TestAccuracy::from_percent(85)), 8),
],
votes3: vec![(
4,
[(3, TestAccuracy::from_percent(50)), (4, TestAccuracy::from_percent(25))],
5,
),],
votes2: vec![(2, [(0, p(80))], 1), (3, [(7, p(85))], 8)],
votes3: vec![(4, [(3, p(50)), (4, p(25))], 5)],
..Default::default()
}
);
assert_eq!(compacted.unique_targets(), vec![0, 1, 2, 3, 4, 5, 6, 7, 8]);
assert_eq!(solution.unique_targets(), vec![0, 1, 2, 3, 4, 5, 6, 7, 8]);
let voter_at = |a: u32| -> Option<AccountId> {
voters.get(<u32 as TryInto<usize>>::try_into(a).unwrap()).cloned()
};
let target_at = |a: u8| -> Option<AccountId> {
targets.get(<u8 as TryInto<usize>>::try_into(a).unwrap()).cloned()
let target_at = |a: u16| -> Option<AccountId> {
targets.get(<u16 as TryInto<usize>>::try_into(a).unwrap()).cloned()
};
assert_eq!(compacted.into_assignment(voter_at, target_at).unwrap(), assignments);
assert_eq!(solution.into_assignment(voter_at, target_at).unwrap(), assignments);
}
#[test]
fn unique_targets_len_edge_count_works() {
const ACC: TestAccuracy = TestAccuracy::from_percent(10);
// we don't really care about voters here so all duplicates. This is not invalid per se.
let compact = TestSolutionCompact {
let solution = TestSolution {
votes1: vec![(99, 1), (99, 2)],
votes2: vec![(99, (3, ACC.clone()), 7), (99, (4, ACC.clone()), 8)],
votes3: vec![(99, [(11, ACC.clone()), (12, ACC.clone())], 13)],
votes2: vec![(99, [(3, p(10))], 7), (99, [(4, p(10))], 8)],
votes3: vec![(99, [(11, p(10)), (12, p(10))], 13)],
// ensure the last one is also counted.
votes16: vec![(
99,
[
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, ACC.clone()),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
(66, p(10)),
],
67,
)],
..Default::default()
};
assert_eq!(compact.unique_targets(), vec![1, 2, 3, 4, 7, 8, 11, 12, 13, 66, 67]);
assert_eq!(compact.edge_count(), 2 + (2 * 2) + 3 + 16);
assert_eq!(compact.voter_count(), 6);
assert_eq!(solution.unique_targets(), vec![1, 2, 3, 4, 7, 8, 11, 12, 13, 66, 67]);
assert_eq!(solution.edge_count(), 2 + (2 * 2) + 3 + 16);
assert_eq!(solution.voter_count(), 6);
// this one has some duplicates.
let compact = TestSolutionCompact {
let solution = TestSolution {
votes1: vec![(99, 1), (99, 1)],
votes2: vec![(99, (3, ACC.clone()), 7), (99, (4, ACC.clone()), 8)],
votes3: vec![(99, [(11, ACC.clone()), (11, ACC.clone())], 13)],
votes2: vec![(99, [(3, p(10))], 7), (99, [(4, p(10))], 8)],
votes3: vec![(99, [(11, p(10)), (11, p(10))], 13)],
..Default::default()
};
assert_eq!(compact.unique_targets(), vec![1, 3, 4, 7, 8, 11, 13]);
assert_eq!(compact.edge_count(), 2 + (2 * 2) + 3);
assert_eq!(compact.voter_count(), 5);
assert_eq!(solution.unique_targets(), vec![1, 3, 4, 7, 8, 11, 13]);
assert_eq!(solution.edge_count(), 2 + (2 * 2) + 3);
assert_eq!(solution.voter_count(), 5);
}
#[test]
fn compact_into_assignment_must_report_overflow() {
fn solution_into_assignment_must_report_overflow() {
// in votes2
let compact = TestSolutionCompact {
let solution = TestSolution {
votes1: Default::default(),
votes2: vec![(0, (1, TestAccuracy::from_percent(100)), 2)],
votes2: vec![(0, [(1, p(100))], 2)],
..Default::default()
};
let voter_at = |a: u32| -> Option<AccountId> { Some(a as AccountId) };
let target_at = |a: u8| -> Option<AccountId> { Some(a as AccountId) };
let target_at = |a: u16| -> Option<AccountId> { Some(a as AccountId) };
assert_eq!(
compact.into_assignment(&voter_at, &target_at).unwrap_err(),
PhragmenError::CompactStakeOverflow,
solution.into_assignment(&voter_at, &target_at).unwrap_err(),
NposError::SolutionWeightOverflow,
);
// in votes3 onwards
let compact = TestSolutionCompact {
let solution = TestSolution {
votes1: Default::default(),
votes2: Default::default(),
votes3: vec![(
0,
[(1, TestAccuracy::from_percent(70)), (2, TestAccuracy::from_percent(80))],
3,
)],
votes3: vec![(0, [(1, p(70)), (2, p(80))], 3)],
..Default::default()
};
assert_eq!(
compact.into_assignment(&voter_at, &target_at).unwrap_err(),
PhragmenError::CompactStakeOverflow,
solution.into_assignment(&voter_at, &target_at).unwrap_err(),
NposError::SolutionWeightOverflow,
);
}
#[test]
fn target_count_overflow_is_detected() {
let voter_index = |a: &AccountId| -> Option<u32> { Some(*a as u32) };
let target_index = |a: &AccountId| -> Option<u8> { Some(*a as u8) };
let target_index = |a: &AccountId| -> Option<u16> { Some(*a as u16) };
let assignments = vec![Assignment {
who: 1 as AccountId,
distribution: (10..27)
.map(|i| (i as AccountId, Percent::from_parts(i as u8)))
.collect::<Vec<_>>(),
distribution: (10..27).map(|i| (i as AccountId, p(i as u8))).collect::<Vec<_>>(),
}];
let compacted =
TestSolutionCompact::from_assignment(&assignments, voter_index, target_index);
assert_eq!(compacted.unwrap_err(), PhragmenError::CompactTargetOverflow);
let solution = TestSolution::from_assignment(&assignments, voter_index, target_index);
assert_eq!(solution.unwrap_err(), NposError::SolutionTargetOverflow);
}
#[test]
@@ -1258,31 +1195,25 @@ mod solution_type {
let targets = vec![10 as AccountId, 11];
let assignments = vec![
Assignment {
who: 1 as AccountId,
distribution: vec![
(10, Percent::from_percent(50)),
(11, Percent::from_percent(50)),
],
},
Assignment { who: 1 as AccountId, distribution: vec![(10, p(50)), (11, p(50))] },
Assignment { who: 2, distribution: vec![] },
];
let voter_index = |a: &AccountId| -> Option<u32> {
voters.iter().position(|x| x == a).map(TryInto::try_into).unwrap().ok()
};
let target_index = |a: &AccountId| -> Option<u8> {
let target_index = |a: &AccountId| -> Option<u16> {
targets.iter().position(|x| x == a).map(TryInto::try_into).unwrap().ok()
};
let compacted =
TestSolutionCompact::from_assignment(&assignments, voter_index, target_index).unwrap();
let solution =
TestSolution::from_assignment(&assignments, voter_index, target_index).unwrap();
assert_eq!(
compacted,
TestSolutionCompact {
solution,
TestSolution {
votes1: Default::default(),
votes2: vec![(0, (0, Percent::from_percent(50)), 1)],
votes2: vec![(0, [(0, p(50))], 1)],
..Default::default()
}
);
@@ -1290,14 +1221,15 @@ mod solution_type {
}
#[test]
fn index_assignments_generate_same_compact_as_plain_assignments() {
fn index_assignments_generate_same_solution_as_plain_assignments() {
let rng = rand::rngs::SmallRng::seed_from_u64(0);
let (voters, assignments, candidates) = generate_random_votes(1000, 2500, rng);
let voter_index = make_voter_fn(&voters);
let target_index = make_target_fn(&candidates);
let compact = Compact::from_assignment(&assignments, &voter_index, &target_index).unwrap();
let solution =
TestSolution::from_assignment(&assignments, &voter_index, &target_index).unwrap();
let index_assignments = assignments
.into_iter()
@@ -1307,5 +1239,5 @@ fn index_assignments_generate_same_compact_as_plain_assignments() {
let index_compact = index_assignments.as_slice().try_into().unwrap();
assert_eq!(compact, index_compact);
assert_eq!(solution, index_compact);
}
@@ -0,0 +1,155 @@
// This file is part of Substrate.
// Copyright (C) 2019-2021 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Traits for the npos-election operations.
use crate::{
Assignment, ElectionScore, Error, EvaluateSupport, ExtendedBalance, IndexAssignmentOf,
VoteWeight,
};
use codec::Encode;
use sp_arithmetic::{
traits::{Bounded, UniqueSaturatedInto},
PerThing,
};
use sp_std::{
convert::{TryFrom, TryInto},
fmt::Debug,
ops::Mul,
prelude::*,
};
/// an aggregator trait for a generic type of a voter/target identifier. This usually maps to
/// substrate's account id.
pub trait IdentifierT: Clone + Eq + Default + Ord + Debug + codec::Codec {}
impl<T: Clone + Eq + Default + Ord + Debug + codec::Codec> IdentifierT for T {}
/// Aggregator trait for a PerThing that can be multiplied by u128 (ExtendedBalance).
pub trait PerThing128: PerThing + Mul<ExtendedBalance, Output = ExtendedBalance> {}
impl<T: PerThing + Mul<ExtendedBalance, Output = ExtendedBalance>> PerThing128 for T {}
/// Simple Extension trait to easily convert `None` from index closures to `Err`.
///
/// This is only generated and re-exported for the solution code to use.
#[doc(hidden)]
pub trait __OrInvalidIndex<T> {
fn or_invalid_index(self) -> Result<T, Error>;
}
impl<T> __OrInvalidIndex<T> for Option<T> {
fn or_invalid_index(self) -> Result<T, Error> {
self.ok_or(Error::SolutionInvalidIndex)
}
}
/// An opaque index-based, NPoS solution type.
pub trait NposSolution
where
Self: Sized + for<'a> sp_std::convert::TryFrom<&'a [IndexAssignmentOf<Self>], Error = Error>,
{
/// The maximum number of votes that are allowed.
const LIMIT: usize;
/// The voter type. Needs to be an index (convert to usize).
type VoterIndex: UniqueSaturatedInto<usize>
+ TryInto<usize>
+ TryFrom<usize>
+ Debug
+ Copy
+ Clone
+ Bounded
+ Encode;
/// The target type. Needs to be an index (convert to usize).
type TargetIndex: UniqueSaturatedInto<usize>
+ TryInto<usize>
+ TryFrom<usize>
+ Debug
+ Copy
+ Clone
+ Bounded
+ Encode;
/// The weight/accuracy type of each vote.
type Accuracy: PerThing128;
/// Get the length of all the voters that this type is encoding.
///
/// This is basically the same as the number of assignments, or number of active voters.
fn voter_count(&self) -> usize;
/// Get the total count of edges.
///
/// This is effectively in the range of {[`Self::voter_count`], [`Self::voter_count`] *
/// [`Self::LIMIT`]}.
fn edge_count(&self) -> usize;
/// Get the number of unique targets in the whole struct.
///
/// Once presented with a list of winners, this set and the set of winners must be
/// equal.
fn unique_targets(&self) -> Vec<Self::TargetIndex>;
/// Get the average edge count.
fn average_edge_count(&self) -> usize {
self.edge_count().checked_div(self.voter_count()).unwrap_or(0)
}
/// Compute the score of this solution type.
fn score<A, FS>(
self,
winners: &[A],
stake_of: FS,
voter_at: impl Fn(Self::VoterIndex) -> Option<A>,
target_at: impl Fn(Self::TargetIndex) -> Option<A>,
) -> Result<ElectionScore, Error>
where
for<'r> FS: Fn(&'r A) -> VoteWeight,
A: IdentifierT,
{
let ratio = self.into_assignment(voter_at, target_at)?;
let staked = crate::helpers::assignment_ratio_to_staked_normalized(ratio, stake_of)?;
let supports = crate::to_supports(winners, &staked)?;
Ok(supports.evaluate())
}
/// Remove a certain voter.
///
/// This will only search until the first instance of `to_remove`, and return true. If
/// no instance is found (no-op), then it returns false.
///
/// In other words, if this return true, exactly **one** element must have been removed self.
fn remove_voter(&mut self, to_remove: Self::VoterIndex) -> bool;
/// Build self from a list of assignments.
fn from_assignment<FV, FT, A>(
assignments: &[Assignment<A, Self::Accuracy>],
voter_index: FV,
target_index: FT,
) -> Result<Self, Error>
where
A: IdentifierT,
for<'r> FV: Fn(&'r A) -> Option<Self::VoterIndex>,
for<'r> FT: Fn(&'r A) -> Option<Self::TargetIndex>;
/// Convert self into a `Vec<Assignment<A, Self::Accuracy>>`
fn into_assignment<A: IdentifierT>(
self,
voter_at: impl Fn(Self::VoterIndex) -> Option<A>,
target_at: impl Fn(Self::TargetIndex) -> Option<A>,
) -> Result<Vec<Assignment<A, Self::Accuracy>>, Error>;
}