mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 23:21:06 +00:00
Properly filter out duplicate voters in elections. (#6693)
* Prevent duplicate voter * Update primitives/npos-elections/src/lib.rs Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com> Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
@@ -371,6 +371,10 @@ pub fn seq_phragmen<AccountId, R>(
|
||||
voters.extend(initial_voters.into_iter().map(|(who, voter_stake, votes)| {
|
||||
let mut edges: Vec<Edge<AccountId>> = Vec::with_capacity(votes.len());
|
||||
for v in votes {
|
||||
if edges.iter().any(|e| e.who == v) {
|
||||
// duplicate edge.
|
||||
continue;
|
||||
}
|
||||
if let Some(idx) = c_idx_cache.get(&v) {
|
||||
// This candidate is valid + already cached.
|
||||
candidates[*idx].approval_stake = candidates[*idx].approval_stake
|
||||
|
||||
@@ -588,6 +588,66 @@ fn self_votes_should_be_kept() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
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,
|
||||
2,
|
||||
candidates,
|
||||
voters,
|
||||
).unwrap();
|
||||
let winners = to_without_backing(winners);
|
||||
|
||||
assert_eq!(winners, vec![(2), (3)]);
|
||||
assert_eq!(
|
||||
assignments
|
||||
.into_iter()
|
||||
.map(|x| (x.who, x.distribution.into_iter().map(|(w, _)| w).collect::<Vec<_>>()))
|
||||
.collect::<Vec<_>>(),
|
||||
vec![
|
||||
(10, vec![2, 3]),
|
||||
(20, vec![2, 3]),
|
||||
(30, vec![2]),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
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,
|
||||
2,
|
||||
candidates,
|
||||
voters,
|
||||
).unwrap();
|
||||
let winners = to_without_backing(winners);
|
||||
|
||||
assert_eq!(winners, vec![1, 2]);
|
||||
assert_eq!(
|
||||
assignments
|
||||
.into_iter()
|
||||
.map(|x| (x.who, x.distribution.into_iter().map(|(w, _)| w).collect::<Vec<_>>()))
|
||||
.collect::<Vec<_>>(),
|
||||
vec![
|
||||
(10, vec![1, 2]),
|
||||
(20, vec![1, 2]),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
mod assignment_convert_normalize {
|
||||
use super::*;
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user