mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-19 12:56:12 +00:00
Rename DocCases to Cases
This commit is contained in:
76
tests/ef_tests/src/cases/ssz_generic.rs
Normal file
76
tests/ef_tests/src/cases/ssz_generic.rs
Normal file
@@ -0,0 +1,76 @@
|
||||
use super::*;
|
||||
use types::EthSpec;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct SszGeneric {
|
||||
#[serde(alias = "type")]
|
||||
pub type_name: String,
|
||||
pub valid: bool,
|
||||
pub value: Option<String>,
|
||||
pub ssz: Option<String>,
|
||||
}
|
||||
|
||||
impl YamlDecode for SszGeneric {
|
||||
fn yaml_decode(yaml: &String) -> Result<Self, Error> {
|
||||
Ok(serde_yaml::from_str(&yaml.as_str()).unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
impl Test for Cases<SszGeneric> {
|
||||
fn test<E: EthSpec>(&self) -> Vec<TestCaseResult> {
|
||||
self.test_cases
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, tc)| {
|
||||
let result = if let Some(ssz) = &tc.ssz {
|
||||
match tc.type_name.as_ref() {
|
||||
"uint8" => ssz_generic_test::<u8>(tc.valid, ssz, &tc.value),
|
||||
"uint16" => ssz_generic_test::<u16>(tc.valid, ssz, &tc.value),
|
||||
"uint32" => ssz_generic_test::<u32>(tc.valid, ssz, &tc.value),
|
||||
"uint64" => ssz_generic_test::<u64>(tc.valid, ssz, &tc.value),
|
||||
"uint128" => ssz_generic_test::<U128>(tc.valid, ssz, &tc.value),
|
||||
"uint256" => ssz_generic_test::<U256>(tc.valid, ssz, &tc.value),
|
||||
_ => Err(Error::FailedToParseTest(format!(
|
||||
"Unknown type: {}",
|
||||
tc.type_name
|
||||
))),
|
||||
}
|
||||
} else {
|
||||
// Skip tests that do not have an ssz field.
|
||||
//
|
||||
// See: https://github.com/ethereum/eth2.0-specs/issues/1079
|
||||
Ok(())
|
||||
};
|
||||
|
||||
TestCaseResult::new(i, tc, result)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
/// Execute a `ssz_generic` test case.
|
||||
fn ssz_generic_test<T>(
|
||||
should_be_ok: bool,
|
||||
ssz: &String,
|
||||
value: &Option<String>,
|
||||
) -> Result<(), Error>
|
||||
where
|
||||
T: Decode + YamlDecode + Debug + PartialEq<T>,
|
||||
{
|
||||
let ssz = hex::decode(&ssz[2..]).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
|
||||
|
||||
// We do not cater for the scenario where the test is valid but we are not passed any SSZ.
|
||||
if should_be_ok && value.is_none() {
|
||||
panic!("Unexpected test input. Cannot pass without value.")
|
||||
}
|
||||
|
||||
let expected = if let Some(string) = value {
|
||||
Some(T::yaml_decode(string)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let decoded = T::from_ssz_bytes(&ssz);
|
||||
|
||||
compare_result(&decoded, &expected)
|
||||
}
|
||||
104
tests/ef_tests/src/cases/ssz_static.rs
Normal file
104
tests/ef_tests/src/cases/ssz_static.rs
Normal file
@@ -0,0 +1,104 @@
|
||||
use super::*;
|
||||
use tree_hash::TreeHash;
|
||||
use types::{
|
||||
Attestation, AttestationData, AttestationDataAndCustodyBit, AttesterSlashing, BeaconBlock,
|
||||
BeaconBlockBody, BeaconBlockHeader, BeaconState, Crosslink, Deposit, DepositData, Eth1Data,
|
||||
EthSpec, Fork, Hash256, HistoricalBatch, IndexedAttestation, PendingAttestation,
|
||||
ProposerSlashing, Transfer, Validator, VoluntaryExit,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct SszStatic {
|
||||
pub type_name: String,
|
||||
pub serialized: String,
|
||||
pub root: String,
|
||||
#[serde(skip)]
|
||||
pub raw_yaml: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
pub struct Value<T> {
|
||||
value: T,
|
||||
}
|
||||
|
||||
impl YamlDecode for SszStatic {
|
||||
fn yaml_decode(yaml: &String) -> Result<Self, Error> {
|
||||
let mut ssz_static: SszStatic = serde_yaml::from_str(&yaml.as_str()).unwrap();
|
||||
|
||||
ssz_static.raw_yaml = yaml.clone();
|
||||
|
||||
Ok(ssz_static)
|
||||
}
|
||||
}
|
||||
|
||||
impl SszStatic {
|
||||
fn value<T: serde::de::DeserializeOwned>(&self) -> Result<T, Error> {
|
||||
let wrapper: Value<T> = serde_yaml::from_str(&self.raw_yaml.as_str()).map_err(|e| {
|
||||
Error::FailedToParseTest(format!("Unable to parse {} YAML: {:?}", self.type_name, e))
|
||||
})?;
|
||||
|
||||
Ok(wrapper.value)
|
||||
}
|
||||
}
|
||||
|
||||
impl Test for Cases<SszStatic> {
|
||||
fn test<E: EthSpec>(&self) -> Vec<TestCaseResult> {
|
||||
self.test_cases
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, tc)| {
|
||||
let result = match tc.type_name.as_ref() {
|
||||
"Fork" => ssz_static_test::<Fork>(tc),
|
||||
"Crosslink" => ssz_static_test::<Crosslink>(tc),
|
||||
"Eth1Data" => ssz_static_test::<Eth1Data>(tc),
|
||||
"AttestationData" => ssz_static_test::<AttestationData>(tc),
|
||||
"AttestationDataAndCustodyBit" => {
|
||||
ssz_static_test::<AttestationDataAndCustodyBit>(tc)
|
||||
}
|
||||
"IndexedAttestation" => ssz_static_test::<IndexedAttestation>(tc),
|
||||
"DepositData" => ssz_static_test::<DepositData>(tc),
|
||||
"BeaconBlockHeader" => ssz_static_test::<BeaconBlockHeader>(tc),
|
||||
"Validator" => ssz_static_test::<Validator>(tc),
|
||||
"PendingAttestation" => ssz_static_test::<PendingAttestation>(tc),
|
||||
"HistoricalBatch" => ssz_static_test::<HistoricalBatch<E>>(tc),
|
||||
"ProposerSlashing" => ssz_static_test::<ProposerSlashing>(tc),
|
||||
"AttesterSlashing" => ssz_static_test::<AttesterSlashing>(tc),
|
||||
"Attestation" => ssz_static_test::<Attestation>(tc),
|
||||
"Deposit" => ssz_static_test::<Deposit>(tc),
|
||||
"VoluntaryExit" => ssz_static_test::<VoluntaryExit>(tc),
|
||||
"Transfer" => ssz_static_test::<Transfer>(tc),
|
||||
"BeaconBlockBody" => ssz_static_test::<BeaconBlockBody>(tc),
|
||||
"BeaconBlock" => ssz_static_test::<BeaconBlock>(tc),
|
||||
"BeaconState" => ssz_static_test::<BeaconState<E>>(tc),
|
||||
_ => Err(Error::FailedToParseTest(format!(
|
||||
"Unknown type: {}",
|
||||
tc.type_name
|
||||
))),
|
||||
};
|
||||
|
||||
TestCaseResult::new(i, tc, result)
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
}
|
||||
|
||||
fn ssz_static_test<T>(tc: &SszStatic) -> Result<(), Error>
|
||||
where
|
||||
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(())
|
||||
}
|
||||
Reference in New Issue
Block a user