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:
Kian Paimani
2022-01-25 15:44:10 +01:00
committed by GitHub
parent d94b5e32c5
commit 38d94d6323
34 changed files with 419 additions and 252 deletions
@@ -158,20 +158,10 @@ pub fn generate_random_npos_result(
(
match election_type {
ElectionType::Phragmen(conf) => seq_phragmen::<AccountId, sp_runtime::Perbill>(
to_elect,
candidates.clone(),
voters.clone(),
conf,
)
.unwrap(),
ElectionType::Phragmms(conf) => phragmms::<AccountId, sp_runtime::Perbill>(
to_elect,
candidates.clone(),
voters.clone(),
conf,
)
.unwrap(),
ElectionType::Phragmen(conf) =>
seq_phragmen(to_elect, candidates.clone(), voters.clone(), conf).unwrap(),
ElectionType::Phragmms(conf) =>
phragmms(to_elect, candidates.clone(), voters.clone(), conf).unwrap(),
},
candidates,
voters,
@@ -24,7 +24,7 @@ use honggfuzz::fuzz;
use rand::{self, SeedableRng};
use sp_npos_elections::{
assignment_ratio_to_staked_normalized, is_score_better, seq_phragmen, to_supports,
EvaluateSupport, VoteWeight,
ElectionResult, EvaluateSupport, VoteWeight,
};
use sp_runtime::Perbill;
@@ -68,13 +68,8 @@ fn main() {
};
if iterations > 0 {
let balanced = seq_phragmen::<AccountId, sp_runtime::Perbill>(
to_elect,
candidates,
voters,
Some((iterations, 0)),
)
.unwrap();
let balanced: ElectionResult<AccountId, sp_runtime::Perbill> =
seq_phragmen(to_elect, candidates, voters, Some((iterations, 0))).unwrap();
let balanced_score = {
let staked = assignment_ratio_to_staked_normalized(
@@ -23,8 +23,8 @@ use common::*;
use honggfuzz::fuzz;
use rand::{self, SeedableRng};
use sp_npos_elections::{
assignment_ratio_to_staked_normalized, is_score_better, phragmms, to_supports, EvaluateSupport,
VoteWeight,
assignment_ratio_to_staked_normalized, is_score_better, phragmms, to_supports, ElectionResult,
EvaluateSupport, VoteWeight,
};
use sp_runtime::Perbill;
@@ -67,13 +67,8 @@ fn main() {
score
};
let balanced = phragmms::<AccountId, sp_runtime::Perbill>(
to_elect,
candidates,
voters,
Some((iterations, 0)),
)
.unwrap();
let balanced: ElectionResult<AccountId, Perbill> =
phragmms(to_elect, candidates, voters, Some((iterations, 0))).unwrap();
let balanced_score = {
let staked =
@@ -471,7 +471,7 @@ pub fn is_score_better<P: PerThing>(this: ElectionScore, that: ElectionScore, ep
/// - It drops duplicate targets within a voter.
pub fn setup_inputs<AccountId: IdentifierT>(
initial_candidates: Vec<AccountId>,
initial_voters: Vec<(AccountId, VoteWeight, Vec<AccountId>)>,
initial_voters: Vec<(AccountId, VoteWeight, impl IntoIterator<Item = AccountId>)>,
) -> (Vec<CandidatePtr<AccountId>>, Vec<Voter<AccountId>>) {
// used to cache and access candidates index.
let mut c_idx_cache = BTreeMap::<AccountId, usize>::new();
@@ -496,7 +496,7 @@ pub fn setup_inputs<AccountId: IdentifierT>(
let voters = initial_voters
.into_iter()
.filter_map(|(who, voter_stake, votes)| {
let mut edges: Vec<Edge<AccountId>> = Vec::with_capacity(votes.len());
let mut edges: Vec<Edge<AccountId>> = Vec::new();
for v in votes {
if edges.iter().any(|e| e.who == v) {
// duplicate edge.
@@ -344,7 +344,7 @@ pub(crate) fn run_and_compare<Output: PerThing128, FS>(
FS: Fn(&AccountId) -> VoteWeight,
{
// run fixed point code.
let ElectionResult { winners, assignments } = seq_phragmen::<_, Output>(
let ElectionResult::<_, Output> { winners, assignments } = seq_phragmen(
to_elect,
candidates.clone(),
voters
@@ -70,7 +70,7 @@ const DEN: ExtendedBalance = ExtendedBalance::max_value();
pub fn seq_phragmen<AccountId: IdentifierT, P: PerThing128>(
to_elect: usize,
candidates: Vec<AccountId>,
voters: Vec<(AccountId, VoteWeight, Vec<AccountId>)>,
voters: Vec<(AccountId, VoteWeight, impl IntoIterator<Item = AccountId>)>,
balancing: Option<(usize, ExtendedBalance)>,
) -> Result<ElectionResult<AccountId, P>, crate::Error> {
let (candidates, voters) = setup_inputs(candidates, voters);
@@ -44,7 +44,7 @@ use sp_std::{prelude::*, rc::Rc};
pub fn phragmms<AccountId: IdentifierT, P: PerThing128>(
to_elect: usize,
candidates: Vec<AccountId>,
voters: Vec<(AccountId, VoteWeight, Vec<AccountId>)>,
voters: Vec<(AccountId, VoteWeight, impl IntoIterator<Item = AccountId>)>,
balancing: Option<(usize, ExtendedBalance)>,
) -> Result<ElectionResult<AccountId, P>, crate::Error> {
let (candidates, mut voters) = setup_inputs(candidates, voters);
@@ -351,8 +351,8 @@ mod tests {
let candidates = vec![1, 2, 3];
let voters = vec![(10, 10, vec![1, 2]), (20, 20, vec![1, 3]), (30, 30, vec![2, 3])];
let ElectionResult { winners, assignments } =
phragmms::<_, Perbill>(2, candidates, voters, Some((2, 0))).unwrap();
let ElectionResult::<_, Perbill> { winners, assignments } =
phragmms(2, candidates, voters, Some((2, 0))).unwrap();
assert_eq!(winners, vec![(3, 30), (2, 30)]);
assert_eq!(
assignments,
@@ -383,8 +383,8 @@ mod tests {
(130, 1000, vec![61, 71]),
];
let ElectionResult { winners, assignments: _ } =
phragmms::<_, Perbill>(4, candidates, voters, Some((2, 0))).unwrap();
let ElectionResult::<_, Perbill> { winners, assignments: _ } =
phragmms(4, candidates, voters, Some((2, 0))).unwrap();
assert_eq!(winners, vec![(11, 3000), (31, 2000), (51, 1500), (61, 1500),]);
}
@@ -396,8 +396,8 @@ mod tests {
// give a bit more to 1 and 3.
voters.push((2, u64::MAX, vec![1, 3]));
let ElectionResult { winners, assignments: _ } =
phragmms::<_, Perbill>(2, candidates, voters, Some((2, 0))).unwrap();
let ElectionResult::<_, Perbill> { winners, assignments: _ } =
phragmms(2, candidates, voters, Some((2, 0))).unwrap();
assert_eq!(winners.into_iter().map(|(w, _)| w).collect::<Vec<_>>(), vec![1u32, 3]);
}
}
@@ -230,7 +230,7 @@ fn phragmen_poc_works() {
let voters = vec![(10, vec![1, 2]), (20, vec![1, 3]), (30, vec![2, 3])];
let stake_of = create_stake_of(&[(10, 10), (20, 20), (30, 30)]);
let ElectionResult { winners, assignments } = seq_phragmen::<_, Perbill>(
let ElectionResult::<_, Perbill> { winners, assignments } = seq_phragmen(
2,
candidates,
voters
@@ -285,7 +285,7 @@ fn phragmen_poc_works_with_balancing() {
let voters = vec![(10, vec![1, 2]), (20, vec![1, 3]), (30, vec![2, 3])];
let stake_of = create_stake_of(&[(10, 10), (20, 20), (30, 30)]);
let ElectionResult { winners, assignments } = seq_phragmen::<_, Perbill>(
let ElectionResult::<_, Perbill> { winners, assignments } = seq_phragmen(
2,
candidates,
voters
@@ -372,7 +372,7 @@ fn phragmen_accuracy_on_large_scale_only_candidates() {
(5, (u64::MAX - 2).into()),
]);
let ElectionResult { winners, assignments } = seq_phragmen::<_, Perbill>(
let ElectionResult::<_, Perbill> { winners, assignments } = seq_phragmen(
2,
candidates.clone(),
auto_generate_self_voters(&candidates)
@@ -403,7 +403,7 @@ fn phragmen_accuracy_on_large_scale_voters_and_candidates() {
(14, u64::MAX.into()),
]);
let ElectionResult { winners, assignments } = seq_phragmen::<_, Perbill>(
let ElectionResult::<_, Perbill> { winners, assignments } = seq_phragmen(
2,
candidates,
voters
@@ -435,7 +435,7 @@ fn phragmen_accuracy_on_small_scale_self_vote() {
let voters = auto_generate_self_voters(&candidates);
let stake_of = create_stake_of(&[(40, 0), (10, 1), (20, 2), (30, 1)]);
let ElectionResult { winners, assignments } = seq_phragmen::<_, Perbill>(
let ElectionResult::<_, Perbill> { winners, assignments } = seq_phragmen(
3,
candidates,
voters
@@ -465,7 +465,7 @@ fn phragmen_accuracy_on_small_scale_no_self_vote() {
(3, 1),
]);
let ElectionResult { winners, assignments } = seq_phragmen::<_, Perbill>(
let ElectionResult::<_, Perbill> { winners, assignments } = seq_phragmen(
3,
candidates,
voters
@@ -501,7 +501,7 @@ fn phragmen_large_scale_test() {
(50, 990000000000000000),
]);
let ElectionResult { winners, assignments } = seq_phragmen::<_, Perbill>(
let ElectionResult::<_, Perbill> { winners, assignments } = seq_phragmen(
2,
candidates,
voters
@@ -528,7 +528,7 @@ fn phragmen_large_scale_test_2() {
let stake_of =
create_stake_of(&[(2, c_budget.into()), (4, c_budget.into()), (50, nom_budget.into())]);
let ElectionResult { winners, assignments } = seq_phragmen::<_, Perbill>(
let ElectionResult::<_, Perbill> { winners, assignments } = seq_phragmen(
2,
candidates,
voters
@@ -597,7 +597,7 @@ fn elect_has_no_entry_barrier() {
let voters = vec![(1, vec![10]), (2, vec![20])];
let stake_of = create_stake_of(&[(1, 10), (2, 10)]);
let ElectionResult { winners, assignments: _ } = seq_phragmen::<_, Perbill>(
let ElectionResult::<_, Perbill> { winners, assignments: _ } = seq_phragmen(
3,
candidates,
voters
@@ -618,7 +618,7 @@ fn phragmen_self_votes_should_be_kept() {
let voters = vec![(5, vec![5]), (10, vec![10]), (20, vec![20]), (1, vec![10, 20])];
let stake_of = create_stake_of(&[(5, 5), (10, 10), (20, 20), (1, 8)]);
let result = seq_phragmen::<_, Perbill>(
let result: ElectionResult<_, Perbill> = seq_phragmen(
2,
candidates,
voters
@@ -664,8 +664,8 @@ fn duplicate_target_is_ignored() {
let candidates = vec![1, 2, 3];
let voters = vec![(10, 100, vec![1, 1, 2, 3]), (20, 100, vec![2, 3]), (30, 50, vec![1, 1, 2])];
let ElectionResult { winners, assignments } =
seq_phragmen::<_, Perbill>(2, candidates, voters, None).unwrap();
let ElectionResult::<_, Perbill> { winners, assignments } =
seq_phragmen(2, candidates, voters, None).unwrap();
assert_eq!(winners, vec![(2, 140), (3, 110)]);
assert_eq!(
@@ -682,8 +682,8 @@ fn duplicate_target_is_ignored_when_winner() {
let candidates = vec![1, 2, 3];
let voters = vec![(10, 100, vec![1, 1, 2, 3]), (20, 100, vec![1, 2])];
let ElectionResult { winners, assignments } =
seq_phragmen::<_, Perbill>(2, candidates, voters, None).unwrap();
let ElectionResult::<_, Perbill> { winners, assignments } =
seq_phragmen(2, candidates, voters, None).unwrap();
assert_eq!(winners, vec![(1, 100), (2, 100)]);
assert_eq!(