Fix fork_choice tests

This commit is contained in:
Paul Hauner 2019-05-27 17:54:32 +10:00
parent b28fa3d20b
commit 9e6503c326
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF

View File

@ -1,14 +1,11 @@
#![cfg(not(debug_assertions))] #![cfg(not(debug_assertions))]
// Tests the available fork-choice algorithms /// Tests the available fork-choice algorithms
pub use beacon_chain::BeaconChain; pub use beacon_chain::BeaconChain;
use bls::Signature; use bls::Signature;
use store::MemoryStore; use store::MemoryStore;
use store::Store; use store::Store;
// use env_logger::{Builder, Env}; // use env_logger::{Builder, Env};
use fork_choice::{ use fork_choice::{BitwiseLMDGhost, ForkChoice, LongestChain, OptimizedLMDGhost, SlowLMDGhost};
BitwiseLMDGhost, ForkChoice, ForkChoiceAlgorithm, LongestChain, OptimizedLMDGhost, SlowLMDGhost,
};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc; use std::sync::Arc;
use std::{fs::File, io::prelude::*, path::PathBuf}; use std::{fs::File, io::prelude::*, path::PathBuf};
@ -25,8 +22,7 @@ fn test_optimized_lmd_ghost() {
// set up logging // set up logging
// Builder::from_env(Env::default().default_filter_or("trace")).init(); // Builder::from_env(Env::default().default_filter_or("trace")).init();
test_yaml_vectors( test_yaml_vectors::<OptimizedLMDGhost<MemoryStore, FoundationEthSpec>>(
ForkChoiceAlgorithm::OptimizedLMDGhost,
"tests/lmd_ghost_test_vectors.yaml", "tests/lmd_ghost_test_vectors.yaml",
100, 100,
); );
@ -37,8 +33,7 @@ fn test_bitwise_lmd_ghost() {
// set up logging // set up logging
//Builder::from_env(Env::default().default_filter_or("trace")).init(); //Builder::from_env(Env::default().default_filter_or("trace")).init();
test_yaml_vectors( test_yaml_vectors::<BitwiseLMDGhost<MemoryStore, FoundationEthSpec>>(
ForkChoiceAlgorithm::BitwiseLMDGhost,
"tests/bitwise_lmd_ghost_test_vectors.yaml", "tests/bitwise_lmd_ghost_test_vectors.yaml",
100, 100,
); );
@ -46,8 +41,7 @@ fn test_bitwise_lmd_ghost() {
#[test] #[test]
fn test_slow_lmd_ghost() { fn test_slow_lmd_ghost() {
test_yaml_vectors( test_yaml_vectors::<SlowLMDGhost<MemoryStore, FoundationEthSpec>>(
ForkChoiceAlgorithm::SlowLMDGhost,
"tests/lmd_ghost_test_vectors.yaml", "tests/lmd_ghost_test_vectors.yaml",
100, 100,
); );
@ -55,16 +49,11 @@ fn test_slow_lmd_ghost() {
#[test] #[test]
fn test_longest_chain() { fn test_longest_chain() {
test_yaml_vectors( test_yaml_vectors::<LongestChain<MemoryStore>>("tests/longest_chain_test_vectors.yaml", 100);
ForkChoiceAlgorithm::LongestChain,
"tests/longest_chain_test_vectors.yaml",
100,
);
} }
// run a generic test over given YAML test vectors // run a generic test over given YAML test vectors
fn test_yaml_vectors( fn test_yaml_vectors<T: ForkChoice<MemoryStore>>(
fork_choice_algo: ForkChoiceAlgorithm,
yaml_file_path: &str, yaml_file_path: &str,
emulated_validators: usize, // the number of validators used to give weights. emulated_validators: usize, // the number of validators used to give weights.
) { ) {
@ -94,8 +83,7 @@ fn test_yaml_vectors(
// process the tests // process the tests
for test_case in test_cases { for test_case in test_cases {
// setup a fresh test // setup a fresh test
let (mut fork_choice, store, state_root) = let (mut fork_choice, store, state_root) = setup_inital_state::<T>(emulated_validators);
setup_inital_state(&fork_choice_algo, emulated_validators);
// keep a hashmap of block_id's to block_hashes (random hashes to abstract block_id) // keep a hashmap of block_id's to block_hashes (random hashes to abstract block_id)
//let mut block_id_map: HashMap<String, Hash256> = HashMap::new(); //let mut block_id_map: HashMap<String, Hash256> = HashMap::new();
@ -204,32 +192,16 @@ fn load_test_cases_from_yaml(file_path: &str) -> Vec<yaml_rust::Yaml> {
doc["test_cases"].as_vec().unwrap().clone() doc["test_cases"].as_vec().unwrap().clone()
} }
// initialise a single validator and state. All blocks will reference this state root. fn setup_inital_state<T>(
fn setup_inital_state( // fork_choice_algo: &ForkChoiceAlgorithm,
fork_choice_algo: &ForkChoiceAlgorithm, num_validators: usize
num_validators: usize, ) -> (T, Arc<MemoryStore>, Hash256)
) -> (Box<ForkChoice>, Arc<MemoryStore>, Hash256) { where
T: ForkChoice<MemoryStore>,
{
let store = Arc::new(MemoryStore::open()); let store = Arc::new(MemoryStore::open());
// the fork choice instantiation let fork_choice = ForkChoice::new(store.clone());
let fork_choice: Box<ForkChoice> = match fork_choice_algo {
ForkChoiceAlgorithm::OptimizedLMDGhost => {
let f: OptimizedLMDGhost<MemoryStore, FoundationEthSpec> =
OptimizedLMDGhost::new(store.clone());
Box::new(f)
}
ForkChoiceAlgorithm::BitwiseLMDGhost => {
let f: BitwiseLMDGhost<MemoryStore, FoundationEthSpec> =
BitwiseLMDGhost::new(store.clone());
Box::new(f)
}
ForkChoiceAlgorithm::SlowLMDGhost => {
let f: SlowLMDGhost<MemoryStore, FoundationEthSpec> = SlowLMDGhost::new(store.clone());
Box::new(f)
}
ForkChoiceAlgorithm::LongestChain => Box::new(LongestChain::new(store.clone())),
};
let spec = FoundationEthSpec::spec(); let spec = FoundationEthSpec::spec();
let mut state_builder: TestingBeaconStateBuilder<FoundationEthSpec> = let mut state_builder: TestingBeaconStateBuilder<FoundationEthSpec> =