From e1ebdf38e56376e8ba5c89a4539141f70a111b4b Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 27 Jan 2020 13:11:23 +1100 Subject: [PATCH] Add more ffg tests --- eth2/proto_array_fork_choice/src/bin.rs | 2 + .../src/fork_choice_test_definition.rs | 1 + .../ffg_updates.rs | 108 ++++++++++++++++-- 3 files changed, 104 insertions(+), 7 deletions(-) diff --git a/eth2/proto_array_fork_choice/src/bin.rs b/eth2/proto_array_fork_choice/src/bin.rs index eb730f986b..a1cc1c6835 100644 --- a/eth2/proto_array_fork_choice/src/bin.rs +++ b/eth2/proto_array_fork_choice/src/bin.rs @@ -13,6 +13,8 @@ use std::fs::File; fn main() { write_test_def_to_yaml("votes.yaml", get_votes_test_definition()); write_test_def_to_yaml("no_votes.yaml", get_no_votes_test_definition()); + write_test_def_to_yaml("no_votes.yaml", get_ffg_case_01_test_definition()); + write_test_def_to_yaml("no_votes.yaml", get_ffg_case_02_test_definition()); } fn write_test_def_to_yaml(filename: &str, def: ForkChoiceTestDefinition) { 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 919320d9f3..b016ed0421 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 @@ -6,6 +6,7 @@ use crate::proto_array_fork_choice::ProtoArrayForkChoice; use serde_derive::{Deserialize, Serialize}; use types::{Epoch, Hash256, Slot}; +pub use ffg_updates::*; pub use no_votes::*; pub use 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 index 5a3cd26357..9dd9417f29 100644 --- 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 @@ -1,7 +1,7 @@ use super::*; pub fn get_ffg_case_01_test_definition() -> ForkChoiceTestDefinition { - let mut balances = vec![1; 2]; + let balances = vec![1; 2]; let mut ops = vec![]; // Ensure that the head starts at the finalized block. @@ -106,7 +106,7 @@ pub fn get_ffg_case_01_test_definition() -> ForkChoiceTestDefinition { } pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition { - let mut balances = vec![1; 2]; + let balances = vec![1; 2]; let mut ops = vec![]; // Ensure that the head starts at the finalized block. @@ -226,6 +226,21 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition { justified_state_balances: balances.clone(), expected_head: get_hash(10), }); + // Same as above, but with justified epoch 2. + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(2), + justified_root: get_hash(0), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + expected_head: get_hash(10), + }); + // Same as above, but with justified epoch 3 (should be invalid). + ops.push(Operation::InvalidFindHead { + justified_epoch: Epoch::new(3), + justified_root: get_hash(0), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + }); // Add a vote to 1. // @@ -266,6 +281,21 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition { justified_state_balances: balances.clone(), expected_head: get_hash(9), }); + // Save as above but justified epoch 2. + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(2), + justified_root: get_hash(0), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + expected_head: get_hash(9), + }); + // Save as above but justified epoch 3 (should fail). + ops.push(Operation::InvalidFindHead { + justified_epoch: Epoch::new(3), + justified_root: get_hash(0), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + }); // Add a vote to 2. // @@ -306,14 +336,29 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition { justified_state_balances: balances.clone(), expected_head: get_hash(10), }); + // Same as above but justified epoch 2. + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(2), + justified_root: get_hash(0), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + expected_head: get_hash(10), + }); + // Same as above but justified epoch 3 (should fail). + ops.push(Operation::InvalidFindHead { + justified_epoch: Epoch::new(3), + justified_root: get_hash(0), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + }); // Ensure that if we start at 1 we find 9 (just: 0, fin: 0). // // 0 // / \ - // 1 2 + // start-> 1 2 // | | - // start -> 3 4 + // 3 4 // | | // 5 6 // | | @@ -322,13 +367,62 @@ pub fn get_ffg_case_02_test_definition() -> ForkChoiceTestDefinition { // head -> 9 10 ops.push(Operation::FindHead { justified_epoch: Epoch::new(0), - justified_root: get_hash(0), + justified_root: get_hash(1), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + expected_head: get_hash(9), + }); + // Same as above but justified epoch 2. + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(2), + justified_root: get_hash(1), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + expected_head: get_hash(9), + }); + // Same as above but justified epoch 3 (should fail). + ops.push(Operation::InvalidFindHead { + justified_epoch: Epoch::new(3), + justified_root: get_hash(1), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + }); + + // Ensure that if we start at 2 we find 10 (just: 0, fin: 0). + // + // 0 + // / \ + // 1 2 <- start + // | | + // 3 4 + // | | + // 5 6 + // | | + // 7 8 + // | | + // 9 10 <- head + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(0), + justified_root: get_hash(2), finalized_epoch: Epoch::new(0), justified_state_balances: balances.clone(), expected_head: get_hash(10), }); - - // TODO: add more tests here. + // Same as above but justified epoch 2. + ops.push(Operation::FindHead { + justified_epoch: Epoch::new(2), + justified_root: get_hash(2), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + expected_head: get_hash(10), + }); + // Same as above but justified epoch 3 (should fail). + ops.push(Operation::InvalidFindHead { + justified_epoch: Epoch::new(3), + justified_root: get_hash(2), + finalized_epoch: Epoch::new(0), + justified_state_balances: balances.clone(), + }); // END OF TESTS ForkChoiceTestDefinition {