From 2052bcbf8e6691c8362a76f861d3668b6f6918ad Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 16 Jan 2020 15:49:26 +1100 Subject: [PATCH] Start adding ffg tests --- .../src/fork_choice_test_definition.rs | 1 + .../ffg_updates.rs | 255 ++++++++++++++++++ .../fork_choice_test_definition/no_votes.rs | 10 + .../src/fork_choice_test_definition/votes.rs | 7 +- .../src/proto_array_fork_choice.rs | 2 +- 5 files changed, 268 insertions(+), 7 deletions(-) create mode 100644 eth2/proto_array_fork_choice/src/fork_choice_test_definition/ffg_updates.rs diff --git a/eth2/proto_array_fork_choice/src/fork_choice_test_definition.rs b/eth2/proto_array_fork_choice/src/fork_choice_test_definition.rs index 566585930d..1890391c97 100644 --- a/eth2/proto_array_fork_choice/src/fork_choice_test_definition.rs +++ b/eth2/proto_array_fork_choice/src/fork_choice_test_definition.rs @@ -1,3 +1,4 @@ +mod ffg_updates; mod no_votes; mod votes; diff --git a/eth2/proto_array_fork_choice/src/fork_choice_test_definition/ffg_updates.rs b/eth2/proto_array_fork_choice/src/fork_choice_test_definition/ffg_updates.rs new file mode 100644 index 0000000000..b4589c6362 --- /dev/null +++ b/eth2/proto_array_fork_choice/src/fork_choice_test_definition/ffg_updates.rs @@ -0,0 +1,255 @@ +use super::*; + +pub fn get_ffg_case_01_test_definition() -> ForkChoiceTestDefinition { + let mut balances = vec![1; 2]; + let mut ops = vec![]; + + // Ensure that the head starts at the finalized block. + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(0), + justified_root: get_hash(0), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + expected_head: get_hash(0), + }); + + // Build the following tree (stick? lol). + // + // 0 <- just: 0, fin: 0 + // | + // 1 <- just: 0, fin: 0 + // | + // 2 <- just: 1, fin: 0 + // | + // 3 <- just: 2, fin: 1 + ops.push(Operation::ProcessBlock { + slot: Slot::new(1), + root: get_hash(1), + parent_root: get_hash(0), + justified_epoch: Epoch::new(0), + finalized_epoch: Epoch::new(0), + }); + ops.push(Operation::ProcessBlock { + slot: Slot::new(2), + root: get_hash(2), + parent_root: get_hash(1), + justified_epoch: Epoch::new(1), + finalized_epoch: Epoch::new(0), + }); + ops.push(Operation::ProcessBlock { + slot: Slot::new(3), + root: get_hash(3), + parent_root: get_hash(2), + justified_epoch: Epoch::new(2), + finalized_epoch: Epoch::new(1), + }); + + // Ensure that with justified epoch 0 we find 3 + // + // 0 <- start + // | + // 1 + // | + // 2 + // | + // 3 <- head + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(0), + justified_root: get_hash(0), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + expected_head: get_hash(3), + }); + + // Ensure that with justified epoch 1 we find 2 + // + // 0 + // | + // 1 + // | + // 2 <- start + // | + // 3 <- head + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(1), + justified_root: get_hash(2), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + expected_head: get_hash(2), + }); + + // Ensure that with justified epoch 2 we find 3 + // + // 0 + // | + // 1 + // | + // 2 + // | + // 3 <- start + head + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(2), + justified_root: get_hash(3), + finalized_epoch: Epoch::new(1), + justified_state_balances: balances.clone(), + expected_head: get_hash(3), + }); + + // END OF TESTS + ForkChoiceTestDefinition { + finalized_block_slot: Slot::new(0), + justified_epoch: Epoch::new(1), + finalized_epoch: Epoch::new(1), + finalized_root: get_hash(0), + operations: ops, + } +} + +pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition { + let mut balances = vec![1; 2]; + let mut ops = vec![]; + + // Ensure that the head starts at the finalized block. + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(1), + justified_root: get_hash(0), + finalized_epoch: Epoch::new(1), + justified_state_balances: balances.clone(), + expected_head: get_hash(0), + }); + + // Build the following tree. + // + // 0 + // / \ + // just: 0, fin: 0 -> 1 2 <- just: 0, fin: 0 + // | | + // just: 1, fin: 0 -> 3 4 <- just: 0, fin: 0 + // | | + // just: 1, fin: 0 -> 5 6 <- just: 0, fin: 0 + // | | + // just: 1, fin: 0 -> 7 8 <- just: 1, fin: 0 + // | | + // just: 2, fin: 0 -> 9 10 <- just: 2, fin: 0 + + // Left branch + ops.push(Operation::ProcessBlock { + slot: Slot::new(1), + root: get_hash(1), + parent_root: get_hash(0), + justified_epoch: Epoch::new(0), + finalized_epoch: Epoch::new(0), + }); + ops.push(Operation::ProcessBlock { + slot: Slot::new(2), + root: get_hash(3), + parent_root: get_hash(1), + justified_epoch: Epoch::new(1), + finalized_epoch: Epoch::new(0), + }); + ops.push(Operation::ProcessBlock { + slot: Slot::new(3), + root: get_hash(5), + parent_root: get_hash(3), + justified_epoch: Epoch::new(1), + finalized_epoch: Epoch::new(0), + }); + ops.push(Operation::ProcessBlock { + slot: Slot::new(4), + root: get_hash(7), + parent_root: get_hash(5), + justified_epoch: Epoch::new(1), + finalized_epoch: Epoch::new(0), + }); + ops.push(Operation::ProcessBlock { + slot: Slot::new(4), + root: get_hash(9), + parent_root: get_hash(7), + justified_epoch: Epoch::new(2), + finalized_epoch: Epoch::new(0), + }); + + // Right branch + ops.push(Operation::ProcessBlock { + slot: Slot::new(1), + root: get_hash(2), + parent_root: get_hash(0), + justified_epoch: Epoch::new(0), + finalized_epoch: Epoch::new(0), + }); + ops.push(Operation::ProcessBlock { + slot: Slot::new(2), + root: get_hash(4), + parent_root: get_hash(2), + justified_epoch: Epoch::new(0), + finalized_epoch: Epoch::new(0), + }); + ops.push(Operation::ProcessBlock { + slot: Slot::new(3), + root: get_hash(6), + parent_root: get_hash(4), + justified_epoch: Epoch::new(0), + finalized_epoch: Epoch::new(0), + }); + ops.push(Operation::ProcessBlock { + slot: Slot::new(4), + root: get_hash(8), + parent_root: get_hash(6), + justified_epoch: Epoch::new(1), + finalized_epoch: Epoch::new(0), + }); + ops.push(Operation::ProcessBlock { + slot: Slot::new(4), + root: get_hash(10), + parent_root: get_hash(8), + justified_epoch: Epoch::new(2), + finalized_epoch: Epoch::new(0), + }); + + // Ensure that if we start at 0 we find 10. + // + // 0 <-- start + // / \ + // 1 2 + // | | + // 3 4 + // | | + // 5 6 + // | | + // 7 8 + // | | + // 9 10 <-- head + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(0), + justified_root: get_hash(0), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + expected_head: get_hash(10), + }); + + // END OF TESTS + ForkChoiceTestDefinition { + finalized_block_slot: Slot::new(0), + justified_epoch: Epoch::new(1), + finalized_epoch: Epoch::new(1), + finalized_root: get_hash(0), + operations: ops, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn ffg_case_01() { + let test = get_ffg_case_01_test_definition(); + test.run(); + } + + #[test] + fn ffg_case_02() { + let test = get_ffg_case_02_test_definition(); + test.run(); + } +} diff --git a/eth2/proto_array_fork_choice/src/fork_choice_test_definition/no_votes.rs b/eth2/proto_array_fork_choice/src/fork_choice_test_definition/no_votes.rs index 495286b0fe..279cde52cd 100644 --- a/eth2/proto_array_fork_choice/src/fork_choice_test_definition/no_votes.rs +++ b/eth2/proto_array_fork_choice/src/fork_choice_test_definition/no_votes.rs @@ -225,3 +225,13 @@ pub fn get_no_votes_test_definition() -> ForkChoiceTestDefinition { operations, } } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn test() { + let test = get_no_votes_test_definition(); + test.run(); + } +} diff --git a/eth2/proto_array_fork_choice/src/fork_choice_test_definition/votes.rs b/eth2/proto_array_fork_choice/src/fork_choice_test_definition/votes.rs index ca7bce00b1..3c7909c62b 100644 --- a/eth2/proto_array_fork_choice/src/fork_choice_test_definition/votes.rs +++ b/eth2/proto_array_fork_choice/src/fork_choice_test_definition/votes.rs @@ -691,14 +691,9 @@ pub fn get_votes_test_definition() -> ForkChoiceTestDefinition { #[cfg(test)] mod tests { use super::*; - #[test] - fn no_votes() { - let test = get_no_votes_test_definition(); - test.run(); - } #[test] - fn votes() { + fn test() { let test = get_votes_test_definition(); test.run(); } diff --git a/eth2/proto_array_fork_choice/src/proto_array_fork_choice.rs b/eth2/proto_array_fork_choice/src/proto_array_fork_choice.rs index 74c1253bf1..1fc8dfaa7a 100644 --- a/eth2/proto_array_fork_choice/src/proto_array_fork_choice.rs +++ b/eth2/proto_array_fork_choice/src/proto_array_fork_choice.rs @@ -692,7 +692,7 @@ mod test_compute_deltas { for vote in votes.0 { assert_eq!( vote.current_root, vote.next_root, - "the vote shoulds should have been updated" + "the vote should have been updated" ); } }