Extend ssz-static testing

This commit is contained in:
Paul Hauner
2019-05-15 09:50:05 +10:00
parent b6d8db3f72
commit 9f42d4d764
8 changed files with 46 additions and 17 deletions

View File

@@ -0,0 +1,23 @@
use types::{EthSpec, typenum::{U64, U8}, ChainSpec, FewValidatorsEthSpec};
use serde_derive::{Serialize, Deserialize};
/// "Minimal" testing specification, as defined here:
///
/// https://github.com/ethereum/eth2.0-specs/blob/v0.6.1/configs/constant_presets/minimal.yaml
///
/// Spec v0.6.1
#[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)]
pub struct MinimalEthSpec;
impl EthSpec for MinimalEthSpec {
type ShardCount = U8;
type SlotsPerHistoricalRoot = U64;
type LatestRandaoMixesLength = U64;
type LatestActiveIndexRootsLength = U64;
type LatestSlashedExitLength = U64;
fn spec() -> ChainSpec {
// TODO: this spec is likely incorrect!
FewValidatorsEthSpec::spec()
}
}

View File

@@ -6,6 +6,7 @@ use std::fmt::Debug;
use test_decode::TestDecode;
pub use crate::error::*;
pub use crate::eth_specs::*;
pub use crate::test_case_result::*;
pub use crate::test_doc::*;
pub use crate::test_doc_cases::*;
@@ -13,6 +14,7 @@ pub use crate::test_doc_header::*;
pub use crate::yaml_utils::*;
mod error;
mod eth_specs;
mod test_case_result;
mod test_decode;
mod test_doc;

View File

@@ -26,7 +26,7 @@ pub trait Test {
///
/// If `expected.is_none()` then `result` is expected to be `Err`. Otherwise, `T` in `result` and
/// `expected` must be equal.
pub fn compare_result<T, E>(result: Result<T, E>, expected: Option<T>) -> Result<(), Error>
pub fn compare_result<T, E>(result: &Result<T, E>, expected: &Option<T>) -> Result<(), Error>
where
T: PartialEq<T> + Debug,
E: Debug,
@@ -36,7 +36,7 @@ where
(Err(_), None) => Ok(()),
// Fail: The test failed when it should have produced a result (fail).
(Err(e), Some(expected)) => Err(Error::NotEqual(format!(
"Got {:?} expected {:?}",
"Got {:?} Expected {:?}",
e, expected
))),
// Fail: The test produced a result when it should have failed (fail).

View File

@@ -1,6 +1,6 @@
use super::*;
use std::{fs::File, io::prelude::*, path::PathBuf};
use types::{EthSpec, FewValidatorsEthSpec, FoundationEthSpec};
use types::{EthSpec, FoundationEthSpec};
#[derive(Debug, Deserialize)]
pub struct TestDoc {
@@ -28,7 +28,7 @@ impl TestDoc {
header.config.as_ref(),
) {
("ssz", "uint", _) => run_test::<SszGeneric, FoundationEthSpec>(&doc.yaml),
("ssz", "static", "minimal") => run_test::<SszStatic, FewValidatorsEthSpec>(&doc.yaml),
("ssz", "static", "minimal") => run_test::<SszStatic, MinimalEthSpec>(&doc.yaml),
(runner, handler, config) => panic!(
"No implementation for runner: \"{}\", handler: \"{}\", config: \"{}\"",
runner, handler, config

View File

@@ -72,5 +72,5 @@ where
let decoded = T::from_ssz_bytes(&ssz);
compare_result(decoded, expected)
compare_result(&decoded, &expected)
}

View File

@@ -1,10 +1,10 @@
use super::*;
use serde::de::{Deserialize, Deserializer};
use tree_hash::TreeHash;
use types::{
Attestation, AttestationData, AttestationDataAndCustodyBit, AttesterSlashing, BeaconBlock,
BeaconBlockBody, BeaconBlockHeader, BeaconState, Crosslink, Deposit, DepositData, Eth1Data,
EthSpec, Fork, HistoricalBatch, IndexedAttestation, PendingAttestation, ProposerSlashing,
Transfer, Validator, VoluntaryExit,
EthSpec, Fork, Hash256, HistoricalBatch, IndexedAttestation, PendingAttestation,
ProposerSlashing, Transfer, Validator, VoluntaryExit,
};
#[derive(Debug, Clone, Deserialize)]
@@ -84,18 +84,21 @@ impl Test for TestDocCases<SszStatic> {
fn ssz_static_test<T>(tc: &SszStatic) -> Result<(), Error>
where
T: Decode + Debug + PartialEq<T> + serde::de::DeserializeOwned,
T: Decode + Debug + PartialEq<T> + serde::de::DeserializeOwned + TreeHash,
{
// Verify we can decode SSZ in the same way we can decode YAML.
let ssz = hex::decode(&tc.serialized[2..])
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
let expected = tc.value::<T>()?;
let decode_result = T::from_ssz_bytes(&ssz);
compare_result(&decode_result, &Some(expected))?;
// Verify the tree hash root is identical to the decoded struct.
let root_bytes =
&hex::decode(&tc.root[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
let expected_root = Hash256::from_slice(&root_bytes);
let root = Hash256::from_slice(&decode_result.unwrap().tree_hash_root());
compare_result::<Hash256, Error>(&Ok(root), &Some(expected_root))?;
Ok(())
/*
let decoded = T::from_ssz_bytes(&ssz);
compare_result(decoded, Some(expected))
*/
}