diff --git a/consensus/src/aggregator.rs b/consensus/src/aggregator.rs index 5ead7cd8f8..9bdf036757 100644 --- a/consensus/src/aggregator.rs +++ b/consensus/src/aggregator.rs @@ -213,51 +213,42 @@ mod tests { dbg!("{:?}", p); - // Provisioner 0 should cast 1 vote, so total should be 1 - let (signature, h) = input.get(0).expect("invalid index"); - assert!(a.collect_vote(&c, h, signature).is_none()); - assert_eq!(a.get_total(block_hash), Some(1)); - - // Ensure a duplicated vote is discarded - assert!(a.collect_vote(&c, h, signature).is_none()); - assert_eq!(a.get_total(block_hash), Some(1)); - - // Provisioner 1 should cast 1 vote, so total should be 2 - let (signature, h) = input.get(1).expect("invalid index"); - assert!(a.collect_vote(&c, h, signature).is_none()); - assert_eq!(a.get_total(block_hash), Some(2)); - - // Provisioner 2 should cast 2 votes, so total should be 4 - let (signature, h) = input.get(2).expect("invalid index"); - assert!(a.collect_vote(&c, h, signature).is_none()); - assert_eq!(a.get_total(block_hash), Some(4)); - - // Provisioner 3 should cast 0 votes, so total should be 4 - let (signature, h) = input.get(3).expect("invalid index"); - assert!(a.collect_vote(&c, h, signature).is_none()); - assert_eq!(a.get_total(block_hash), Some(4)); - - // Provisioner 4 should cast 1 vote, so total should be 5 - let (signature, h) = input.get(4).expect("invalid index"); - assert!(a.collect_vote(&c, h, signature).is_none()); - assert_eq!(a.get_total(block_hash), Some(5)); - - // Provisioner 5 should cast 3 votes, so total should reach a quorum - let (signature, h) = input.get(5).expect("invalid index"); - let (hash, sv) = a - .collect_vote(&c, h, signature) - .expect("failed to reach quorum"); - assert_eq!(a.get_total(block_hash), Some(8)); - - // Double check we actually reached a quorum - assert!(a.get_total(block_hash).unwrap() >= c.quorum()); - - // Check block hash is as expected - assert_eq!(hash, block_hash); - - // Check StepVotes bitset - // bitset: 0b00000000000000000000000000000000000000000000000000000000011111 - println!("bitset: {:#064b}", sv.bitset); - assert_eq!(sv.bitset, 31); + // Collect votes from expected committee members + let expected_members = vec![0, 1, 2, 4, 5]; + let expected_votes = vec![1, 1, 2, 1, 3]; + let mut collected_votes = 0; + for i in 0..expected_members.len()-1 { + // Select provisioner + let (signature, h) = input.get(expected_members[i]).expect("invalid index"); + + // Last member's vote should reach the quorum + if i == expected_members.len()-1 { + // (hash, sv) is only returned in case we reach the quorum + let (hash, sv) = a + .collect_vote(&c, h, signature) + .expect("failed to reach quorum"); + + // Check expected block hash + assert_eq!(hash, block_hash); + + // Check expected StepVotes bitset + // bitset: 0b00000000000000000000000000000000000000000000000000000000011111 + println!("bitset: {:#064b}", sv.bitset); + assert_eq!(sv.bitset, 31); + + break + } + + // Check collected votes + assert!(a.collect_vote(&c, h, signature).is_none()); + collected_votes += expected_votes[i]; + assert_eq!(a.get_total(block_hash), Some(collected_votes)); + + // Ensure a duplicated vote is discarded + if i == 0 { + assert!(a.collect_vote(&c, h, signature).is_none()); + assert_eq!(a.get_total(block_hash), Some(collected_votes)); + } + } } }