diff --git a/substrate/primitives/npos-elections/src/lib.rs b/substrate/primitives/npos-elections/src/lib.rs index 11951d2065989abd487e4b648d454cd8427336aa..d82839f02086d25cddc67facf2574a8164d56c8b 100644 --- a/substrate/primitives/npos-elections/src/lib.rs +++ b/substrate/primitives/npos-elections/src/lib.rs @@ -629,7 +629,7 @@ pub(crate) fn setup_inputs<AccountId: IdentifierT>( }) .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()); for v in votes { 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. } - Voter { - who, - edges: edges, - budget: voter_stake.into(), - load: Rational128::zero(), + if edges.is_empty() { + None + } + else { + Some(Voter { + who, + edges: edges, + budget: voter_stake.into(), + load: Rational128::zero(), + }) } + }).collect::<Vec<_>>(); (candidates, voters,) diff --git a/substrate/primitives/npos-elections/src/tests.rs b/substrate/primitives/npos-elections/src/tests.rs index dc7a1a5fdfb979ce6ee54695254f3d1701100a34..79f95a469adf42ed49221689b1f0edf85812fa38 100644 --- a/substrate/primitives/npos-elections/src/tests.rs +++ b/substrate/primitives/npos-elections/src/tests.rs @@ -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] fn phragmen_core_poc_works() { let candidates = vec![1, 2, 3];