mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 19:51:02 +00:00
Changed map to filter map so that Phragmen ignores empty voters (#7378)
* Changed map to filter map so that Phragmen ignores empty voters * Resolve flaws and added test case * Updated test
This commit is contained in:
@@ -629,7 +629,7 @@ pub(crate) fn setup_inputs<AccountId: IdentifierT>(
|
|||||||
})
|
})
|
||||||
.collect::<Vec<CandidatePtr<AccountId>>>();
|
.collect::<Vec<CandidatePtr<AccountId>>>();
|
||||||
|
|
||||||
let voters = initial_voters.into_iter().map(|(who, voter_stake, votes)| {
|
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::with_capacity(votes.len());
|
||||||
for v in votes {
|
for v in votes {
|
||||||
if edges.iter().any(|e| e.who == v) {
|
if edges.iter().any(|e| e.who == v) {
|
||||||
@@ -650,12 +650,18 @@ pub(crate) fn setup_inputs<AccountId: IdentifierT>(
|
|||||||
);
|
);
|
||||||
} // else {} would be wrong votes. We don't really care about it.
|
} // else {} would be wrong votes. We don't really care about it.
|
||||||
}
|
}
|
||||||
Voter {
|
if edges.is_empty() {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Some(Voter {
|
||||||
who,
|
who,
|
||||||
edges: edges,
|
edges: edges,
|
||||||
budget: voter_stake.into(),
|
budget: voter_stake.into(),
|
||||||
load: Rational128::zero(),
|
load: Rational128::zero(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}).collect::<Vec<_>>();
|
}).collect::<Vec<_>>();
|
||||||
|
|
||||||
(candidates, voters,)
|
(candidates, voters,)
|
||||||
|
|||||||
@@ -72,6 +72,46 @@ fn float_phragmen_poc_works() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn phragmen_core_test_without_edges() {
|
||||||
|
let candidates = vec![1, 2, 3];
|
||||||
|
let voters = vec![
|
||||||
|
(10, 10, vec![]),
|
||||||
|
(20, 20, vec![]),
|
||||||
|
(30, 30, vec![]),
|
||||||
|
];
|
||||||
|
|
||||||
|
let (candidates, voters) = setup_inputs(candidates, voters);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
voters
|
||||||
|
.iter()
|
||||||
|
.map(|v| (
|
||||||
|
v.who,
|
||||||
|
v.budget,
|
||||||
|
(v.edges.iter().map(|e| (e.who, e.weight)).collect::<Vec<_>>()),
|
||||||
|
))
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
vec![]
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
candidates
|
||||||
|
.iter()
|
||||||
|
.map(|c_ptr| (
|
||||||
|
c_ptr.borrow().who,
|
||||||
|
c_ptr.borrow().elected,
|
||||||
|
c_ptr.borrow().round,
|
||||||
|
c_ptr.borrow().backed_stake,
|
||||||
|
)).collect::<Vec<_>>(),
|
||||||
|
vec![
|
||||||
|
(1, false, 0, 0),
|
||||||
|
(2, false, 0, 0),
|
||||||
|
(3, false, 0, 0),
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn phragmen_core_poc_works() {
|
fn phragmen_core_poc_works() {
|
||||||
let candidates = vec![1, 2, 3];
|
let candidates = vec![1, 2, 3];
|
||||||
|
|||||||
Reference in New Issue
Block a user