lighthouse/eth2/state_processing/tests/tests.rs

96 lines
2.9 KiB
Rust
Raw Normal View History

use serde_derive::Deserialize;
2019-03-26 07:28:29 +00:00
use serde_yaml;
2019-04-03 06:27:32 +00:00
#[cfg(not(debug_assertions))]
2019-04-02 03:30:32 +00:00
use state_processing::{
per_block_processing, per_block_processing_without_verifying_block_signature,
per_slot_processing,
};
2019-03-26 07:28:29 +00:00
use std::{fs::File, io::prelude::*, path::PathBuf};
use types::*;
#[allow(unused_imports)]
use yaml_utils;
#[derive(Debug, Deserialize)]
pub struct TestCase {
pub name: String,
pub config: ChainSpec,
pub verify_signatures: bool,
pub initial_state: BeaconState,
pub blocks: Vec<BeaconBlock>,
}
#[derive(Debug, Deserialize)]
pub struct TestDoc {
pub title: String,
pub summary: String,
pub fork: String,
pub test_cases: Vec<TestCase>,
}
2019-04-02 03:17:41 +00:00
fn load_test_case(test_name: &str) -> TestDoc {
let mut file = {
let mut file_path_buf = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
2019-04-02 03:17:41 +00:00
file_path_buf.push(format!("yaml_utils/specs/{}", test_name));
File::open(file_path_buf).unwrap()
};
let mut yaml_str = String::new();
file.read_to_string(&mut yaml_str).unwrap();
yaml_str = yaml_str.to_lowercase();
2019-04-02 03:17:41 +00:00
serde_yaml::from_str(&yaml_str.as_str()).unwrap()
}
2019-03-26 07:28:29 +00:00
2019-04-02 03:17:41 +00:00
fn run_state_transition_test(test_name: &str) {
let doc = load_test_case(test_name);
2019-03-26 07:28:29 +00:00
// Run Tests
2019-04-02 03:17:41 +00:00
let mut ok = true;
2019-03-26 07:28:29 +00:00
for (i, test_case) in doc.test_cases.iter().enumerate() {
2019-04-02 06:51:12 +00:00
let fake_crypto = cfg!(feature = "fake_crypto");
if !test_case.verify_signatures == fake_crypto {
println!("Running {}", test_case.name);
} else {
println!(
"Skipping {} (fake_crypto: {}, need fake: {})",
test_case.name, fake_crypto, !test_case.verify_signatures
);
continue;
}
2019-03-26 07:28:29 +00:00
let mut state = test_case.initial_state.clone();
2019-04-02 03:17:41 +00:00
for (j, block) in test_case.blocks.iter().enumerate() {
2019-03-26 07:28:29 +00:00
while block.slot > state.slot {
let latest_block_header = state.latest_block_header.clone();
2019-04-03 05:23:09 +00:00
per_slot_processing(&mut state, &latest_block_header, &test_case.config).unwrap();
2019-03-26 07:28:29 +00:00
}
2019-04-02 06:51:12 +00:00
let res = per_block_processing(&mut state, &block, &test_case.config);
if res.is_err() {
println!("Error in {} (#{}), on block {}", test_case.name, i, j);
println!("{:?}", res);
ok = false;
};
2019-03-26 07:28:29 +00:00
}
}
2019-04-02 03:17:41 +00:00
assert!(ok, "one or more tests failed, see above");
}
#[test]
#[cfg(not(debug_assertions))]
fn test_read_yaml() {
load_test_case("sanity-check_small-config_32-vals.yaml");
load_test_case("sanity-check_default-config_100-vals.yaml");
}
#[test]
#[cfg(not(debug_assertions))]
fn run_state_transition_tests_small() {
run_state_transition_test("sanity-check_small-config_32-vals.yaml");
}
#[test]
#[cfg(not(debug_assertions))]
fn run_state_transition_tests_large() {
run_state_transition_test("sanity-check_default-config_100-vals.yaml");
2019-03-26 07:28:29 +00:00
}