Create test def concept

This commit is contained in:
Paul Hauner
2020-01-16 13:29:40 +11:00
parent d8afb0fc73
commit c33d358cc9

View File

@@ -3,14 +3,14 @@ use types::{Epoch, Hash256, Slot};
#[test] #[test]
fn no_votes() { fn no_votes() {
let tester = ForkChoiceTester::new(Slot::new(0), Epoch::new(1), Epoch::new(1), get_hash(0)); let test = get_no_votes_test_definition();
tester.process_operations(get_no_votes_ops()) test.run();
} }
#[test] #[test]
fn votes() { fn votes() {
let tester = ForkChoiceTester::new(Slot::new(0), Epoch::new(1), Epoch::new(1), get_hash(0)); let test = get_votes_test_definition();
tester.process_operations(get_votes_ops()) test.run();
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -48,132 +48,122 @@ pub enum Operation {
}, },
} }
pub struct ForkChoiceTester { pub struct ForkChoiceTestDefinition {
fork_choice: ProtoArrayForkChoice, pub finalized_block_slot: Slot,
pub justified_epoch: Epoch,
pub finalized_epoch: Epoch,
pub finalized_root: Hash256,
pub operations: Vec<Operation>,
} }
impl ForkChoiceTester { impl ForkChoiceTestDefinition {
pub fn new( fn run(self) {
finalized_block_slot: Slot, let fork_choice = ProtoArrayForkChoice::new(
justified_epoch: Epoch, self.finalized_block_slot,
finalized_epoch: Epoch, self.justified_epoch,
finalized_root: Hash256, self.finalized_epoch,
) -> Self { self.finalized_root,
Self { )
fork_choice: ProtoArrayForkChoice::new( .expect("should create fork choice struct");
finalized_block_slot,
justified_epoch,
finalized_epoch,
finalized_root,
)
.expect("should create fork choice"),
}
}
pub fn process_operations(&self, ops: Vec<Operation>) { for (op_index, op) in self.operations.into_iter().enumerate() {
for (i, op) in ops.into_iter().enumerate() { match op.clone() {
self.process_operation(i, op) Operation::FindHead {
} justified_epoch,
} justified_root,
finalized_epoch,
justified_state_balances,
expected_head,
} => {
let head = fork_choice
.find_head(
justified_epoch,
justified_root,
finalized_epoch,
&justified_state_balances,
)
.expect(&format!(
"find_head op at index {} returned error",
op_index
));
fn process_operation(&self, op_index: usize, op: Operation) { assert_eq!(
match op.clone() { head, expected_head,
Operation::FindHead { "Operation at index {} failed checks. Operation: {:?}",
justified_epoch, op_index, op
justified_root, );
finalized_epoch, check_bytes_round_trip(&fork_choice);
justified_state_balances, }
expected_head, Operation::InvalidFindHead {
} => { justified_epoch,
let head = self justified_root,
.fork_choice finalized_epoch,
.find_head( justified_state_balances,
} => {
let result = fork_choice.find_head(
justified_epoch, justified_epoch,
justified_root, justified_root,
finalized_epoch, finalized_epoch,
&justified_state_balances, &justified_state_balances,
) );
.expect(&format!(
"find_head op at index {} returned error",
op_index
));
assert_eq!( assert!(
head, expected_head, result.is_err(),
"Operation at index {} failed checks. Operation: {:?}", "Operation at index {} . Operation: {:?}",
op_index, op op_index,
); op
check_bytes_round_trip(&self.fork_choice); );
} check_bytes_round_trip(&fork_choice);
Operation::InvalidFindHead { }
justified_epoch, Operation::ProcessBlock {
justified_root, slot,
finalized_epoch, root,
justified_state_balances, parent_root,
} => {
let result = self.fork_choice.find_head(
justified_epoch, justified_epoch,
justified_root,
finalized_epoch, finalized_epoch,
&justified_state_balances, } => {
); fork_choice
.process_block(slot, root, parent_root, justified_epoch, finalized_epoch)
assert!( .expect(&format!(
result.is_err(), "process_block op at index {} returned error",
"Operation at index {} . Operation: {:?}", op_index
op_index, ));
op check_bytes_round_trip(&fork_choice);
); }
check_bytes_round_trip(&self.fork_choice); Operation::ProcessAttestation {
} validator_index,
Operation::ProcessBlock { block_root,
slot, target_epoch,
root, } => {
parent_root, fork_choice
justified_epoch, .process_attestation(validator_index, block_root, target_epoch)
finalized_epoch, .expect(&format!(
} => { "process_attestation op at index {} returned error",
self.fork_choice op_index
.process_block(slot, root, parent_root, justified_epoch, finalized_epoch) ));
.expect(&format!( check_bytes_round_trip(&fork_choice);
"process_block op at index {} returned error", }
op_index Operation::Prune {
)); finalized_epoch,
check_bytes_round_trip(&self.fork_choice); finalized_root,
} prune_threshold,
Operation::ProcessAttestation {
validator_index,
block_root,
target_epoch,
} => {
self.fork_choice
.process_attestation(validator_index, block_root, target_epoch)
.expect(&format!(
"process_attestation op at index {} returned error",
op_index
));
check_bytes_round_trip(&self.fork_choice);
}
Operation::Prune {
finalized_epoch,
finalized_root,
prune_threshold,
expected_len,
} => {
self.fork_choice.set_prune_threshold(prune_threshold);
self.fork_choice
.update_finalized_root(finalized_epoch, finalized_root)
.expect("update_finalized_root op at index {} returned error");
// Ensure that no pruning happened.
assert_eq!(
self.fork_choice.len(),
expected_len, expected_len,
"Prune op at index {} failed with {} instead of {}", } => {
op_index, fork_choice.set_prune_threshold(prune_threshold);
self.fork_choice.len(), fork_choice
expected_len .update_finalized_root(finalized_epoch, finalized_root)
); .expect("update_finalized_root op at index {} returned error");
// Ensure that no pruning happened.
assert_eq!(
fork_choice.len(),
expected_len,
"Prune op at index {} failed with {} instead of {}",
op_index,
fork_choice.len(),
expected_len
);
}
} }
} }
} }
@@ -194,10 +184,10 @@ fn check_bytes_round_trip(original: &ProtoArrayForkChoice) {
); );
} }
fn get_no_votes_ops() -> Vec<Operation> { fn get_no_votes_test_definition() -> ForkChoiceTestDefinition {
let balances = vec![0; 16]; let balances = vec![0; 16];
vec![ let operations = vec![
// Check that the head is the finalized block. // Check that the head is the finalized block.
Operation::FindHead { Operation::FindHead {
justified_epoch: Epoch::new(1), justified_epoch: Epoch::new(1),
@@ -409,10 +399,18 @@ fn get_no_votes_ops() -> Vec<Operation> {
justified_state_balances: balances.clone(), justified_state_balances: balances.clone(),
expected_head: get_hash(6), expected_head: get_hash(6),
}, },
] ];
ForkChoiceTestDefinition {
finalized_block_slot: Slot::new(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
finalized_root: get_hash(0),
operations,
}
} }
fn get_votes_ops() -> Vec<Operation> { fn get_votes_test_definition() -> ForkChoiceTestDefinition {
let mut balances = vec![1; 2]; let mut balances = vec![1; 2];
let mut ops = vec![]; let mut ops = vec![];
@@ -1091,5 +1089,11 @@ fn get_votes_ops() -> Vec<Operation> {
expected_head: get_hash(11), expected_head: get_hash(11),
}); });
ops ForkChoiceTestDefinition {
finalized_block_slot: Slot::new(0),
justified_epoch: Epoch::new(1),
finalized_epoch: Epoch::new(1),
finalized_root: get_hash(0),
operations: ops,
}
} }