Skip to content

Commit

Permalink
Update tests to new sortitoin hash
Browse files Browse the repository at this point in the history
Fixed tests:
 - test_collect_votes
 - test_deterministic_sortition_1
 - test_deterministic_sortition_2

test_collect_votes code and comments have been made cleaner.
  • Loading branch information
fed-franz committed Jun 2, 2023
1 parent 7bda642 commit 38d4d0f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 34 deletions.
57 changes: 25 additions & 32 deletions consensus/src/aggregator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,60 +211,53 @@ mod tests {

let mut a = Aggregator::default();

// Ensure total value for this block_hash is increased with 2 on a vote
// from a provisioner at pos 1.
let (signature, h) = input.get(1).expect("invalid index");
assert!(a.collect_vote(&c, h, signature).is_none());
dbg!("{:?}", p);

// this provisioner vote has weight of 2
assert_eq!(a.get_total(block_hash), Some(2));

// Ensure total value for this block_hash is increased with 1 on a vote
// from a provisioner at pos 0.
// 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));

// this provisioner vote has weight of 1
assert_eq!(a.get_total(block_hash), Some(3));

// Ensure total value for this block_hash is increased with 1 on a vote
// from a provisioner at pos 5.
let (signature, h) = input.get(5).expect("invalid index");
// Ensure a duplicated vote is discarded
assert!(a.collect_vote(&c, h, signature).is_none());
assert_eq!(a.get_total(block_hash), Some(1));

// this provisioner has weight of 1. Total should be sum of weights of
// all prior votes.
assert_eq!(a.get_total(block_hash), Some(4));
// 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));

// Ensure a duplicated vote is discarded
let (signature, h) = input.get(5).expect("invalid index");
// 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));

let (signature, h) = input.get(6).expect("invalid index");
// 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());
// this provisioner vote has weight of 0.

assert_eq!(a.get_total(block_hash), Some(4));

let (signature, h) = input.get(8).expect("invalid index");
// 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());
// this provisioner vote has weight of 0. Quorum threshold still not
// reached
assert_eq!(a.get_total(block_hash), Some(4));
assert_eq!(a.get_total(block_hash), Some(5));

let (signature, h) = input.get(9).expect("invalid index");
// 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));

assert_eq!(hash, block_hash);
// this provisioner vote has weight of 3. Quorum reached
// Double check we actually reached a quorum
assert!(a.get_total(block_hash).unwrap() >= c.quorum());

// bitset: 0b00000000000000000000000000000000000000000000000000000000100111
// 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, 39);
assert_eq!(sv.bitset, 31);
}
}
7 changes: 5 additions & 2 deletions consensus/tests/sortition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ fn test_deterministic_sortition_1() {

let committee = Committee::new(PublicKey::default(), &mut p, cfg);

// Verify expected committee size
assert_eq!(
committee_size,
committee.get_occurrences().iter().sum::<usize>()
);
assert_eq!(vec![7, 23, 13, 21], committee.get_occurrences());

// Verify expected distribution
assert_eq!(vec![7, 32, 7, 18], committee.get_occurrences());
}

#[test]
Expand All @@ -45,7 +48,7 @@ fn test_deterministic_sortition_2() {
committee_size,
committee.get_occurrences().iter().sum::<usize>()
);
assert_eq!(vec![5, 13, 14, 13], committee.get_occurrences());
assert_eq!(vec![7, 15, 10, 13], committee.get_occurrences());
}

#[test]
Expand Down

0 comments on commit 38d4d0f

Please sign in to comment.