SSZ generic tests for big uints

This commit is contained in:
Michael Sproul 2019-09-04 13:03:44 +10:00
parent f47eaf5730
commit d511c939eb
No known key found for this signature in database
GPG Key ID: 77B1309D2E54E914
23 changed files with 185 additions and 165 deletions

View File

@ -1,5 +1,5 @@
use super::*;
use ethereum_types::H256;
use ethereum_types::{H256, U128, U256};
macro_rules! impl_for_bitsize {
($type: ident, $bit_size: expr) => {
@ -73,6 +73,46 @@ macro_rules! impl_for_u8_array {
impl_for_u8_array!(4);
impl_for_u8_array!(32);
impl TreeHash for U128 {
fn tree_hash_type() -> TreeHashType {
TreeHashType::Basic
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
let mut result = vec![0; 16];
self.to_little_endian(&mut result);
result
}
fn tree_hash_packing_factor() -> usize {
2
}
fn tree_hash_root(&self) -> Vec<u8> {
merkle_root(&self.tree_hash_packed_encoding(), 0)
}
}
impl TreeHash for U256 {
fn tree_hash_type() -> TreeHashType {
TreeHashType::Basic
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
let mut result = vec![0; 32];
self.to_little_endian(&mut result);
result
}
fn tree_hash_packing_factor() -> usize {
1
}
fn tree_hash_root(&self) -> Vec<u8> {
merkle_root(&self.tree_hash_packed_encoding(), 0)
}
}
impl TreeHash for H256 {
fn tree_hash_type() -> TreeHashType {
TreeHashType::Vector

View File

@ -18,7 +18,9 @@ serde_derive = "1.0"
serde_repr = "0.1"
serde_yaml = "0.8"
eth2_ssz = "0.1"
eth2_ssz_derive = "0.1"
tree_hash = "0.1"
tree_hash_derive = "0.2"
state_processing = { path = "../../eth2/state_processing" }
swap_or_not_shuffle = { path = "../../eth2/utils/swap_or_not_shuffle" }
types = { path = "../../eth2/types" }

View File

@ -9,6 +9,7 @@ mod bls_g2_compressed;
mod bls_g2_uncompressed;
mod bls_priv_to_pub;
mod bls_sign_msg;
mod common;
mod epoch_processing;
mod genesis_initialization;
mod genesis_validity;
@ -25,6 +26,7 @@ pub use bls_g2_compressed::*;
pub use bls_g2_uncompressed::*;
pub use bls_priv_to_pub::*;
pub use bls_sign_msg::*;
pub use common::SszStaticType;
pub use epoch_processing::*;
pub use genesis_initialization::*;
pub use genesis_validity::*;
@ -61,20 +63,6 @@ pub trait Case: Debug + Sync {
fn result(&self, case_index: usize) -> Result<(), Error>;
}
pub trait BlsCase: serde::de::DeserializeOwned {}
impl<T: BlsCase> YamlDecode for T {
fn yaml_decode(string: &str) -> Result<Self, Error> {
serde_yaml::from_str(string).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
}
}
impl<T: BlsCase> LoadCase for T {
fn load_from_dir(path: &Path) -> Result<Self, Error> {
Self::yaml_decode_file(&path.join("data.yaml"))
}
}
#[derive(Debug)]
pub struct Cases<T> {
pub test_cases: Vec<T>,

View File

@ -1,5 +1,6 @@
use super::*;
use crate::case_result::compare_result;
use crate::cases::common::BlsCase;
use bls::{AggregatePublicKey, PublicKey};
use serde_derive::Deserialize;

View File

@ -1,5 +1,6 @@
use super::*;
use crate::case_result::compare_result;
use crate::cases::common::BlsCase;
use bls::{AggregateSignature, Signature};
use serde_derive::Deserialize;

View File

@ -1,5 +1,6 @@
use super::*;
use crate::case_result::compare_result;
use crate::cases::common::BlsCase;
use bls::{compress_g2, hash_on_g2};
use serde_derive::Deserialize;

View File

@ -1,5 +1,6 @@
use super::*;
use crate::case_result::compare_result;
use crate::cases::common::BlsCase;
use bls::hash_on_g2;
use serde_derive::Deserialize;
@ -9,18 +10,14 @@ pub struct BlsG2UncompressedInput {
pub domain: String,
}
impl BlsCase for BlsG2UncompressedInput {}
#[derive(Debug, Clone, Deserialize)]
pub struct BlsG2Uncompressed {
pub input: BlsG2UncompressedInput,
pub output: Vec<Vec<String>>,
}
impl YamlDecode for BlsG2Uncompressed {
fn yaml_decode(yaml: &str) -> Result<Self, Error> {
Ok(serde_yaml::from_str(yaml).unwrap())
}
}
impl Case for BlsG2Uncompressed {
fn result(&self, _case_index: usize) -> Result<(), Error> {
// Convert message and domain to required types

View File

@ -1,5 +1,6 @@
use super::*;
use crate::case_result::compare_result;
use crate::cases::common::BlsCase;
use bls::{PublicKey, SecretKey};
use serde_derive::Deserialize;

View File

@ -1,5 +1,6 @@
use super::*;
use crate::case_result::compare_result;
use crate::cases::common::BlsCase;
use bls::{SecretKey, Signature};
use serde_derive::Deserialize;

View File

@ -0,0 +1,72 @@
use crate::cases::LoadCase;
use crate::decode::yaml_decode_file;
use crate::error::Error;
use serde_derive::Deserialize;
use ssz::{Decode, Encode};
use ssz_derive::{Decode, Encode};
use std::convert::TryFrom;
use std::fmt::Debug;
use std::path::Path;
use tree_hash::TreeHash;
/// Trait for all BLS cases to eliminate some boilerplate.
pub trait BlsCase: serde::de::DeserializeOwned {}
impl<T: BlsCase> LoadCase for T {
fn load_from_dir(path: &Path) -> Result<Self, Error> {
yaml_decode_file(&path.join("data.yaml"))
}
}
/// Macro to wrap U128 and U256 so they deserialize correctly.
macro_rules! uint_wrapper {
($wrapper_name:ident, $wrapped_type:ty) => {
#[derive(Debug, Clone, Copy, Default, PartialEq, Decode, Encode, Deserialize)]
#[serde(try_from = "String")]
pub struct $wrapper_name {
pub x: $wrapped_type,
}
impl TryFrom<String> for $wrapper_name {
type Error = String;
fn try_from(s: String) -> Result<Self, Self::Error> {
<$wrapped_type>::from_dec_str(&s)
.map(|x| Self { x })
.map_err(|e| format!("{:?}", e))
}
}
impl tree_hash::TreeHash for $wrapper_name {
fn tree_hash_type() -> tree_hash::TreeHashType {
<$wrapped_type>::tree_hash_type()
}
fn tree_hash_packed_encoding(&self) -> Vec<u8> {
self.x.tree_hash_packed_encoding()
}
fn tree_hash_packing_factor() -> usize {
<$wrapped_type>::tree_hash_packing_factor()
}
fn tree_hash_root(&self) -> Vec<u8> {
self.x.tree_hash_root()
}
}
};
}
uint_wrapper!(TestU128, ethereum_types::U128);
uint_wrapper!(TestU256, ethereum_types::U256);
/// Trait alias for all deez bounds
pub trait SszStaticType:
serde::de::DeserializeOwned + Decode + Encode + TreeHash + Clone + PartialEq + Debug + Sync
{
}
impl<T> SszStaticType for T where
T: serde::de::DeserializeOwned + Decode + Encode + TreeHash + Clone + PartialEq + Debug + Sync
{
}

View File

@ -1,9 +1,9 @@
use super::*;
use crate::bls_setting::BlsSetting;
use crate::case_result::compare_beacon_state_results_without_caches;
use crate::decode::{ssz_decode_file, yaml_decode_file};
use crate::type_name;
use crate::type_name::TypeName;
use crate::yaml_decode::{ssz_decode_file, yaml_decode_file};
use serde_derive::Deserialize;
use state_processing::per_epoch_processing::{
errors::EpochProcessingError, process_crosslinks, process_final_updates,

View File

@ -1,6 +1,6 @@
use super::*;
use crate::case_result::compare_beacon_state_results_without_caches;
use crate::yaml_decode::{ssz_decode_file, yaml_decode_file};
use crate::decode::{ssz_decode_file, yaml_decode_file};
use serde_derive::Deserialize;
use state_processing::initialize_beacon_state_from_eth1;
use std::path::PathBuf;

View File

@ -1,5 +1,5 @@
use super::*;
use crate::yaml_decode::{ssz_decode_file, yaml_decode_file};
use crate::decode::{ssz_decode_file, yaml_decode_file};
use serde_derive::Deserialize;
use state_processing::is_valid_genesis_state;
use std::path::{Path, PathBuf};

View File

@ -1,8 +1,8 @@
use super::*;
use crate::bls_setting::BlsSetting;
use crate::case_result::compare_beacon_state_results_without_caches;
use crate::decode::{ssz_decode_file, yaml_decode_file};
use crate::type_name::TypeName;
use crate::yaml_decode::{ssz_decode_file, yaml_decode_file};
use serde_derive::Deserialize;
use ssz::Decode;
use state_processing::per_block_processing::{

View File

@ -1,7 +1,7 @@
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 crate::decode::{ssz_decode_file, yaml_decode_file};
use serde_derive::Deserialize;
use state_processing::{
per_block_processing, per_slot_processing, BlockInvalid, BlockProcessingError,

View File

@ -1,7 +1,7 @@
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 crate::decode::{ssz_decode_file, yaml_decode_file};
use serde_derive::Deserialize;
use state_processing::per_slot_processing;
use std::path::PathBuf;

View File

@ -1,5 +1,6 @@
use super::*;
use crate::case_result::compare_result;
use crate::decode::yaml_decode_file;
use serde_derive::Deserialize;
use std::marker::PhantomData;
use swap_or_not_shuffle::{get_permutated_index, shuffle_list};
@ -13,15 +14,9 @@ pub struct Shuffling<T> {
_phantom: PhantomData<T>,
}
impl<T> YamlDecode for Shuffling<T> {
fn yaml_decode(yaml: &str) -> Result<Self, Error> {
Ok(serde_yaml::from_str(yaml).unwrap())
}
}
impl<T: EthSpec> LoadCase for Shuffling<T> {
fn load_from_dir(path: &Path) -> Result<Self, Error> {
Self::yaml_decode_file(&path.join("mapping.yaml"))
yaml_decode_file(&path.join("mapping.yaml"))
}
}

View File

@ -1,11 +1,12 @@
use super::*;
use crate::cases::ssz_static::{check_serialization, check_tree_hash, SszStaticType};
use crate::yaml_decode::yaml_decode_file;
use crate::cases::common::{SszStaticType, TestU128, TestU256};
use crate::cases::ssz_static::{check_serialization, check_tree_hash};
use crate::decode::yaml_decode_file;
use serde_derive::Deserialize;
use std::fs;
use std::path::{Path, PathBuf};
use types::typenum::*;
use types::{BitList, BitVector, FixedVector, VariableList};
use types::{BitList, BitVector, FixedVector};
#[derive(Debug, Clone, Deserialize)]
struct Metadata {
@ -51,9 +52,8 @@ macro_rules! type_dispatch {
"uint16" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* u16>, $($rest)*),
"uint32" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* u32>, $($rest)*),
"uint64" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* u64>, $($rest)*),
// FIXME(michael): implement tree hash for big ints
// "uint128" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* etherum_types::U128>, $($rest)*),
// "uint256" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* ethereum_types::U256>, $($rest)*),
"uint128" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* TestU128>, $($rest)*),
"uint256" => type_dispatch!($function, ($($arg),*), $base_ty, <$($param_ty,)* TestU256>, $($rest)*),
_ => { println!("unsupported: {}", $value); Ok(()) },
}
};
@ -121,9 +121,13 @@ impl Case for SszGeneric {
)?;
}
"bitlist" => {
let limit = parts[1];
let mut limit = parts[1];
// FIXME(michael): mark length "no" cases as known failures
// Test format is inconsistent, pretend the limit is 32 (arbitrary)
// https://github.com/ethereum/eth2.0-spec-tests
if limit == "no" {
limit = "32";
}
type_dispatch!(
ssz_generic_test,

View File

@ -1,10 +1,10 @@
use super::*;
use crate::case_result::compare_result;
use crate::cases::common::SszStaticType;
use crate::decode::yaml_decode_file;
use serde_derive::Deserialize;
use ssz::{Decode, Encode};
use std::fmt::Debug;
use std::fs;
use tree_hash::{SignedRoot, TreeHash};
use tree_hash::SignedRoot;
use types::Hash256;
#[derive(Debug, Clone, Deserialize)]
@ -13,12 +13,6 @@ struct SszStaticRoots {
signing_root: Option<String>,
}
impl YamlDecode for SszStaticRoots {
fn yaml_decode(yaml: &str) -> Result<Self, Error> {
Ok(serde_yaml::from_str(yaml).unwrap())
}
}
#[derive(Debug, Clone)]
pub struct SszStatic<T> {
roots: SszStaticRoots,
@ -33,26 +27,11 @@ pub struct SszStaticSR<T> {
value: T,
}
// Trait alias for all deez bounds
pub trait SszStaticType:
serde::de::DeserializeOwned + Decode + Encode + TreeHash + Clone + PartialEq + Debug + Sync
{
}
impl<T> SszStaticType for T where
T: serde::de::DeserializeOwned + Decode + Encode + TreeHash + Clone + PartialEq + Debug + Sync
{
}
fn load_from_dir<T: SszStaticType>(path: &Path) -> Result<(SszStaticRoots, Vec<u8>, T), Error> {
// FIXME: set description/name
let roots = SszStaticRoots::yaml_decode_file(&path.join("roots.yaml"))?;
// FIXME(michael): set description/name
let roots = yaml_decode_file(&path.join("roots.yaml"))?;
let serialized = fs::read(&path.join("serialized.ssz")).expect("serialized.ssz exists");
let yaml = fs::read_to_string(&path.join("value.yaml")).expect("value.yaml exists");
let value =
serde_yaml::from_str(&yaml).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))?;
let value = yaml_decode_file(&path.join("value.yaml"))?;
Ok((roots, serialized, value))
}

View File

@ -0,0 +1,31 @@
use super::*;
use std::fs;
use std::path::Path;
pub fn yaml_decode<T: serde::de::DeserializeOwned>(string: &str) -> Result<T, Error> {
serde_yaml::from_str(string).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
}
pub fn yaml_decode_file<T: serde::de::DeserializeOwned>(path: &Path) -> Result<T, Error> {
fs::read_to_string(path)
.map_err(|e| {
Error::FailedToParseTest(format!("Unable to load {}: {:?}", path.display(), e))
})
.and_then(|s| yaml_decode(&s))
}
pub fn ssz_decode_file<T: ssz::Decode>(path: &Path) -> Result<T, Error> {
fs::read(path)
.map_err(|e| {
Error::FailedToParseTest(format!("Unable to load {}: {:?}", path.display(), e))
})
.and_then(|s| {
T::from_ssz_bytes(&s).map_err(|e| {
Error::FailedToParseTest(format!(
"Unable to parse SSZ at {}: {:?}",
path.display(),
e
))
})
})
}

View File

@ -7,13 +7,12 @@ pub use cases::{
};
pub use error::Error;
pub use handler::*;
pub use yaml_decode::YamlDecode;
mod bls_setting;
mod case_result;
mod cases;
mod decode;
mod error;
mod handler;
mod results;
mod type_name;
mod yaml_decode;

View File

@ -80,7 +80,8 @@ pub fn print_results(
println!("-------");
println!(
"case ({}) from {} failed with {}:",
"case {} ({}) from {} failed with {}:",
failure.case_index,
failure.desc,
failure.path.display(),
error.name()

View File

@ -1,93 +0,0 @@
use super::*;
use ethereum_types::{U128, U256};
use std::fs;
use std::path::Path;
use types::Fork;
pub fn yaml_decode<T: serde::de::DeserializeOwned>(string: &str) -> Result<T, Error> {
serde_yaml::from_str(string).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
}
pub fn yaml_decode_file<T: serde::de::DeserializeOwned>(path: &Path) -> Result<T, Error> {
fs::read_to_string(path)
.map_err(|e| {
Error::FailedToParseTest(format!("Unable to load {}: {:?}", path.display(), e))
})
.and_then(|s| yaml_decode(&s))
}
pub fn ssz_decode_file<T: ssz::Decode>(path: &Path) -> Result<T, Error> {
fs::read(path)
.map_err(|e| {
Error::FailedToParseTest(format!("Unable to load {}: {:?}", path.display(), e))
})
.and_then(|s| {
T::from_ssz_bytes(&s).map_err(|e| {
Error::FailedToParseTest(format!(
"Unable to parse SSZ at {}: {:?}",
path.display(),
e
))
})
})
}
pub trait YamlDecode: Sized {
/// Decode an object from the test specification YAML.
fn yaml_decode(string: &str) -> Result<Self, Error>;
fn yaml_decode_file(path: &Path) -> Result<Self, Error> {
fs::read_to_string(path)
.map_err(|e| {
Error::FailedToParseTest(format!("Unable to load {}: {:?}", path.display(), e))
})
.and_then(|s| Self::yaml_decode(&s))
}
}
/// Basic types can general be decoded with the `parse` fn if they implement `str::FromStr`.
macro_rules! impl_via_parse {
($ty: ty) => {
impl YamlDecode for $ty {
fn yaml_decode(string: &str) -> Result<Self, Error> {
string
.parse::<Self>()
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
}
}
};
}
impl_via_parse!(u8);
impl_via_parse!(u16);
impl_via_parse!(u32);
impl_via_parse!(u64);
/// Some `ethereum-types` methods have a `str::FromStr` implementation that expects `0x`-prefixed:
/// hex, so we use `from_dec_str` instead.
macro_rules! impl_via_from_dec_str {
($ty: ty) => {
impl YamlDecode for $ty {
fn yaml_decode(string: &str) -> Result<Self, Error> {
Self::from_dec_str(string).map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
}
}
};
}
impl_via_from_dec_str!(U128);
impl_via_from_dec_str!(U256);
/// Types that already implement `serde::Deserialize` can be decoded using `serde_yaml`.
macro_rules! impl_via_serde_yaml {
($ty: ty) => {
impl YamlDecode for $ty {
fn yaml_decode(string: &str) -> Result<Self, Error> {
serde_yaml::from_str(string)
.map_err(|e| Error::FailedToParseTest(format!("{:?}", e)))
}
}
};
}
impl_via_serde_yaml!(Fork);