Genesis tests
This commit is contained in:
parent
6cf9b3c1a4
commit
c5a22b57d2
@ -1,34 +1,55 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::bls_setting::BlsSetting;
|
|
||||||
use crate::case_result::compare_beacon_state_results_without_caches;
|
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 serde_derive::Deserialize;
|
||||||
use state_processing::initialize_beacon_state_from_eth1;
|
use state_processing::initialize_beacon_state_from_eth1;
|
||||||
|
use std::path::PathBuf;
|
||||||
use types::{BeaconState, Deposit, EthSpec, Hash256};
|
use types::{BeaconState, Deposit, EthSpec, Hash256};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
|
struct Metadata {
|
||||||
|
deposits_count: usize,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
#[serde(bound = "E: EthSpec")]
|
#[serde(bound = "E: EthSpec")]
|
||||||
pub struct GenesisInitialization<E: EthSpec> {
|
pub struct GenesisInitialization<E: EthSpec> {
|
||||||
pub description: String,
|
pub path: PathBuf,
|
||||||
pub bls_setting: Option<BlsSetting>,
|
|
||||||
pub eth1_block_hash: Hash256,
|
pub eth1_block_hash: Hash256,
|
||||||
pub eth1_timestamp: u64,
|
pub eth1_timestamp: u64,
|
||||||
pub deposits: Vec<Deposit>,
|
pub deposits: Vec<Deposit>,
|
||||||
pub state: Option<BeaconState<E>>,
|
pub state: Option<BeaconState<E>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: EthSpec> YamlDecode for GenesisInitialization<E> {
|
impl<E: EthSpec> LoadCase for GenesisInitialization<E> {
|
||||||
fn yaml_decode(yaml: &str) -> Result<Self, Error> {
|
fn load_from_dir(path: &Path) -> Result<Self, Error> {
|
||||||
Ok(serde_yaml::from_str(yaml).unwrap())
|
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> {
|
impl<E: EthSpec> Case for GenesisInitialization<E> {
|
||||||
fn description(&self) -> String {
|
fn path(&self) -> &Path {
|
||||||
self.description.clone()
|
&self.path
|
||||||
}
|
}
|
||||||
|
|
||||||
fn result(&self, _case_index: usize) -> Result<(), Error> {
|
fn result(&self, _case_index: usize) -> Result<(), Error> {
|
||||||
self.bls_setting.unwrap_or_default().check()?;
|
|
||||||
let spec = &E::default_spec();
|
let spec = &E::default_spec();
|
||||||
|
|
||||||
let mut result = initialize_beacon_state_from_eth1(
|
let mut result = initialize_beacon_state_from_eth1(
|
||||||
|
@ -1,31 +1,37 @@
|
|||||||
use super::*;
|
use super::*;
|
||||||
use crate::bls_setting::BlsSetting;
|
use crate::yaml_decode::{ssz_decode_file, yaml_decode_file};
|
||||||
use serde_derive::Deserialize;
|
use serde_derive::Deserialize;
|
||||||
use state_processing::is_valid_genesis_state;
|
use state_processing::is_valid_genesis_state;
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
use types::{BeaconState, EthSpec};
|
use types::{BeaconState, EthSpec};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Deserialize)]
|
#[derive(Debug, Clone, Deserialize)]
|
||||||
#[serde(bound = "E: EthSpec")]
|
#[serde(bound = "E: EthSpec")]
|
||||||
pub struct GenesisValidity<E: EthSpec> {
|
pub struct GenesisValidity<E: EthSpec> {
|
||||||
pub description: String,
|
pub path: PathBuf,
|
||||||
pub bls_setting: Option<BlsSetting>,
|
|
||||||
pub genesis: BeaconState<E>,
|
pub genesis: BeaconState<E>,
|
||||||
pub is_valid: bool,
|
pub is_valid: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: EthSpec> YamlDecode for GenesisValidity<E> {
|
impl<E: EthSpec> LoadCase for GenesisValidity<E> {
|
||||||
fn yaml_decode(yaml: &str) -> Result<Self, Error> {
|
fn load_from_dir(path: &Path) -> Result<Self, Error> {
|
||||||
Ok(serde_yaml::from_str(yaml).unwrap())
|
let genesis = ssz_decode_file(&path.join("genesis.ssz"))?;
|
||||||
|
let is_valid = yaml_decode_file(&path.join("is_valid.yaml"))?;
|
||||||
|
|
||||||
|
Ok(Self {
|
||||||
|
path: path.into(),
|
||||||
|
genesis,
|
||||||
|
is_valid,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: EthSpec> Case for GenesisValidity<E> {
|
impl<E: EthSpec> Case for GenesisValidity<E> {
|
||||||
fn description(&self) -> String {
|
fn path(&self) -> &Path {
|
||||||
self.description.clone()
|
&self.path
|
||||||
}
|
}
|
||||||
|
|
||||||
fn result(&self, _case_index: usize) -> Result<(), Error> {
|
fn result(&self, _case_index: usize) -> Result<(), Error> {
|
||||||
self.bls_setting.unwrap_or_default().check()?;
|
|
||||||
let spec = &E::default_spec();
|
let spec = &E::default_spec();
|
||||||
|
|
||||||
let is_valid = is_valid_genesis_state(&self.genesis, spec);
|
let is_valid = is_valid_genesis_state(&self.genesis, spec);
|
||||||
|
@ -202,3 +202,39 @@ impl<E: EthSpec + TypeName, T: EpochTransition<E>> Handler for EpochProcessingHa
|
|||||||
T::name()
|
T::name()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct GenesisValidityHandler<E>(PhantomData<E>);
|
||||||
|
|
||||||
|
impl<E: EthSpec + TypeName> Handler for GenesisValidityHandler<E> {
|
||||||
|
type Case = cases::GenesisValidity<E>;
|
||||||
|
|
||||||
|
fn config_name() -> &'static str {
|
||||||
|
E::name()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn runner_name() -> &'static str {
|
||||||
|
"genesis"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handler_name() -> &'static str {
|
||||||
|
"validity"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct GenesisInitializationHandler<E>(PhantomData<E>);
|
||||||
|
|
||||||
|
impl<E: EthSpec + TypeName> Handler for GenesisInitializationHandler<E> {
|
||||||
|
type Case = cases::GenesisInitialization<E>;
|
||||||
|
|
||||||
|
fn config_name() -> &'static str {
|
||||||
|
E::name()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn runner_name() -> &'static str {
|
||||||
|
"genesis"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handler_name() -> &'static str {
|
||||||
|
"initialization"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -1,52 +1,11 @@
|
|||||||
use ef_tests::*;
|
use ef_tests::*;
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use std::path::{Path, PathBuf};
|
|
||||||
use types::{
|
use types::{
|
||||||
Attestation, AttestationData, AttestationDataAndCustodyBit, AttesterSlashing, BeaconBlock,
|
Attestation, AttestationData, AttestationDataAndCustodyBit, AttesterSlashing, BeaconBlock,
|
||||||
BeaconBlockBody, BeaconBlockHeader, BeaconState, Checkpoint, CompactCommittee, Crosslink,
|
BeaconBlockBody, BeaconBlockHeader, BeaconState, Checkpoint, CompactCommittee, Crosslink,
|
||||||
Deposit, DepositData, Eth1Data, Fork, HistoricalBatch, IndexedAttestation, MainnetEthSpec,
|
Deposit, DepositData, Eth1Data, Fork, HistoricalBatch, IndexedAttestation, MainnetEthSpec,
|
||||||
MinimalEthSpec, PendingAttestation, ProposerSlashing, Transfer, Validator, VoluntaryExit,
|
MinimalEthSpec, PendingAttestation, ProposerSlashing, Transfer, Validator, VoluntaryExit,
|
||||||
};
|
};
|
||||||
use walkdir::WalkDir;
|
|
||||||
|
|
||||||
fn yaml_files_in_test_dir(dir: &Path) -> Vec<PathBuf> {
|
|
||||||
let base_path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
||||||
.join("eth2.0-spec-tests")
|
|
||||||
.join("tests")
|
|
||||||
.join("general")
|
|
||||||
.join("phase0")
|
|
||||||
.join(dir);
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
base_path.exists(),
|
|
||||||
format!(
|
|
||||||
"Unable to locate {:?}. Did you init git submodules?",
|
|
||||||
base_path
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
let mut paths: Vec<PathBuf> = WalkDir::new(base_path)
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|e| e.ok())
|
|
||||||
.filter_map(|entry| {
|
|
||||||
if entry.file_type().is_file() {
|
|
||||||
match entry.file_name().to_str() {
|
|
||||||
Some(f) if f.ends_with(".yaml") => Some(entry.path().to_path_buf()),
|
|
||||||
Some(f) if f.ends_with(".yml") => Some(entry.path().to_path_buf()),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
|
|
||||||
// Reverse the file order. Assuming files come in lexicographical order, executing tests in
|
|
||||||
// reverse means we get the "minimal" tests before the "mainnet" tests. This makes life easier
|
|
||||||
// for debugging.
|
|
||||||
paths.reverse();
|
|
||||||
paths
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
#[test]
|
#[test]
|
||||||
@ -292,22 +251,14 @@ fn epoch_processing_final_updates() {
|
|||||||
EpochProcessingHandler::<MainnetEthSpec, FinalUpdates>::run();
|
EpochProcessingHandler::<MainnetEthSpec, FinalUpdates>::run();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
#[test]
|
#[test]
|
||||||
fn genesis_initialization() {
|
fn genesis_initialization() {
|
||||||
yaml_files_in_test_dir(&Path::new("genesis").join("initialization"))
|
GenesisInitializationHandler::<MinimalEthSpec>::run();
|
||||||
.into_par_iter()
|
|
||||||
.for_each(|file| {
|
|
||||||
Doc::assert_tests_pass(file);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn genesis_validity() {
|
fn genesis_validity() {
|
||||||
yaml_files_in_test_dir(&Path::new("genesis").join("validity"))
|
GenesisValidityHandler::<MinimalEthSpec>::run();
|
||||||
.into_par_iter()
|
// TODO: mainnet tests don't exist yet
|
||||||
.for_each(|file| {
|
// GenesisValidityHandler::<MainnetEthSpec>::run();
|
||||||
Doc::assert_tests_pass(file);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
Loading…
Reference in New Issue
Block a user