2019-04-14 23:59:35 +00:00
|
|
|
#![cfg(not(debug_assertions))]
|
|
|
|
|
2019-03-15 02:32:23 +00:00
|
|
|
use serde_derive::Deserialize;
|
2019-03-26 07:28:29 +00:00
|
|
|
use serde_yaml;
|
2019-04-03 06:14:11 +00:00
|
|
|
use state_processing::{per_block_processing, per_slot_processing};
|
2019-03-26 07:28:29 +00:00
|
|
|
use std::{fs::File, io::prelude::*, path::PathBuf};
|
2019-03-15 02:32:23 +00:00
|
|
|
use types::*;
|
|
|
|
|
2019-04-02 07:46:08 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct ExpectedState {
|
|
|
|
pub slot: Option<Slot>,
|
|
|
|
pub genesis_time: Option<u64>,
|
|
|
|
pub fork: Option<Fork>,
|
|
|
|
pub validator_registry: Option<Vec<Validator>>,
|
|
|
|
pub validator_balances: Option<Vec<u64>>,
|
2019-04-03 06:14:11 +00:00
|
|
|
pub previous_epoch_attestations: Option<Vec<PendingAttestation>>,
|
|
|
|
pub current_epoch_attestations: Option<Vec<PendingAttestation>>,
|
|
|
|
pub historical_roots: Option<Vec<Hash256>>,
|
|
|
|
pub finalized_epoch: Option<Epoch>,
|
2019-04-17 08:07:28 +00:00
|
|
|
pub latest_block_roots: Option<TreeHashVector<Hash256>>,
|
2019-04-02 07:46:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ExpectedState {
|
|
|
|
// Return a list of fields that differ, and a string representation of the beacon state's field.
|
|
|
|
fn check(&self, state: &BeaconState) -> Vec<(&str, String)> {
|
|
|
|
// Check field equality
|
|
|
|
macro_rules! cfe {
|
|
|
|
($field_name:ident) => {
|
|
|
|
if self.$field_name.as_ref().map_or(true, |$field_name| {
|
|
|
|
println!(" > Checking {}", stringify!($field_name));
|
2019-04-17 08:00:14 +00:00
|
|
|
$field_name == &state.$field_name
|
2019-04-02 07:46:08 +00:00
|
|
|
}) {
|
|
|
|
vec![]
|
|
|
|
} else {
|
|
|
|
vec![(stringify!($field_name), format!("{:#?}", state.$field_name))]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
vec![
|
|
|
|
cfe!(slot),
|
|
|
|
cfe!(genesis_time),
|
|
|
|
cfe!(fork),
|
|
|
|
cfe!(validator_registry),
|
|
|
|
cfe!(validator_balances),
|
2019-04-03 06:14:11 +00:00
|
|
|
cfe!(previous_epoch_attestations),
|
|
|
|
cfe!(current_epoch_attestations),
|
|
|
|
cfe!(historical_roots),
|
|
|
|
cfe!(finalized_epoch),
|
|
|
|
cfe!(latest_block_roots),
|
2019-04-02 07:46:08 +00:00
|
|
|
]
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|x| x)
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-15 02:32:23 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
pub struct TestCase {
|
|
|
|
pub name: String,
|
|
|
|
pub config: ChainSpec,
|
|
|
|
pub verify_signatures: bool,
|
|
|
|
pub initial_state: BeaconState,
|
|
|
|
pub blocks: Vec<BeaconBlock>,
|
2019-04-02 07:46:08 +00:00
|
|
|
pub expected_state: ExpectedState,
|
2019-03-15 02:32:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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 {
|
2019-03-15 02:32:23 +00:00
|
|
|
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));
|
2019-03-15 02:32:23 +00:00
|
|
|
|
|
|
|
File::open(file_path_buf).unwrap()
|
|
|
|
};
|
|
|
|
let mut yaml_str = String::new();
|
2019-03-25 07:02:37 +00:00
|
|
|
file.read_to_string(&mut yaml_str).unwrap();
|
|
|
|
yaml_str = yaml_str.to_lowercase();
|
2019-03-15 02:32:23 +00:00
|
|
|
|
2019-04-02 03:17:41 +00:00
|
|
|
serde_yaml::from_str(&yaml_str.as_str()).unwrap()
|
2019-03-15 02:32:23 +00:00
|
|
|
}
|
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 {
|
2019-04-08 04:37:01 +00:00
|
|
|
per_slot_processing(&mut state, &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-04-03 06:14:11 +00:00
|
|
|
}
|
2019-03-26 07:28:29 +00:00
|
|
|
}
|
2019-04-02 07:46:08 +00:00
|
|
|
|
|
|
|
let mismatched_fields = test_case.expected_state.check(&state);
|
|
|
|
if !mismatched_fields.is_empty() {
|
|
|
|
println!(
|
|
|
|
"Error in expected state, these fields didn't match: {:?}",
|
|
|
|
mismatched_fields.iter().map(|(f, _)| f).collect::<Vec<_>>()
|
|
|
|
);
|
|
|
|
for (field_name, state_val) in mismatched_fields {
|
|
|
|
println!("state.{} was: {}", field_name, state_val);
|
|
|
|
}
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2019-04-14 23:33:54 +00:00
|
|
|
// Run with --ignored to run this test
|
2019-04-02 03:17:41 +00:00
|
|
|
#[test]
|
2019-04-14 23:38:04 +00:00
|
|
|
#[ignore]
|
2019-04-02 03:17:41 +00:00
|
|
|
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
|
|
|
}
|