Genesis tests

This commit is contained in:
Michael Sproul
2019-08-30 14:10:28 +10:00
parent 6cf9b3c1a4
commit c5a22b57d2
4 changed files with 85 additions and 71 deletions

View File

@@ -1,34 +1,55 @@
use super::*;
use crate::bls_setting::BlsSetting;
use crate::case_result::compare_beacon_state_results_without_caches;
use crate::yaml_decode::{ssz_decode_file, yaml_decode_file};
use serde_derive::Deserialize;
use state_processing::initialize_beacon_state_from_eth1;
use std::path::PathBuf;
use types::{BeaconState, Deposit, EthSpec, Hash256};
#[derive(Debug, Clone, Deserialize)]
struct Metadata {
deposits_count: usize,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(bound = "E: EthSpec")]
pub struct GenesisInitialization<E: EthSpec> {
pub description: String,
pub bls_setting: Option<BlsSetting>,
pub path: PathBuf,
pub eth1_block_hash: Hash256,
pub eth1_timestamp: u64,
pub deposits: Vec<Deposit>,
pub state: Option<BeaconState<E>>,
}
impl<E: EthSpec> YamlDecode for GenesisInitialization<E> {
fn yaml_decode(yaml: &str) -> Result<Self, Error> {
Ok(serde_yaml::from_str(yaml).unwrap())
impl<E: EthSpec> LoadCase for GenesisInitialization<E> {
fn load_from_dir(path: &Path) -> Result<Self, Error> {
let eth1_block_hash = ssz_decode_file(&path.join("eth1_block_hash.ssz"))?;
let eth1_timestamp = yaml_decode_file(&path.join("eth1_timestamp.yaml"))?;
let meta: Metadata = yaml_decode_file(&path.join("meta.yaml"))?;
let deposits: Vec<Deposit> = (0..meta.deposits_count)
.map(|i| {
let filename = format!("deposits_{}.ssz", i);
ssz_decode_file(&path.join(filename))
})
.collect::<Result<_, _>>()?;
let state = ssz_decode_file(&path.join("state.ssz"))?;
Ok(Self {
path: path.into(),
eth1_block_hash,
eth1_timestamp,
deposits,
state: Some(state),
})
}
}
impl<E: EthSpec> Case for GenesisInitialization<E> {
fn description(&self) -> String {
self.description.clone()
fn path(&self) -> &Path {
&self.path
}
fn result(&self, _case_index: usize) -> Result<(), Error> {
self.bls_setting.unwrap_or_default().check()?;
let spec = &E::default_spec();
let mut result = initialize_beacon_state_from_eth1(