From 5f3ba42b97a2b641f1979eb871b1eb20dc938722 Mon Sep 17 00:00:00 2001 From: thojest Date: Fri, 22 Feb 2019 12:16:11 +0100 Subject: [PATCH 1/5] added first draft for lib-crates for test_random and test_random_derive (lighthouse-246) --- Cargo.toml | 2 ++ eth2/utils/test_random/Cargo.toml | 8 +++++ eth2/utils/test_random/src/lib.rs | 39 ++++++++++++++++++++++++ eth2/utils/test_random_derive/Cargo.toml | 15 +++++++++ eth2/utils/test_random_derive/src/lib.rs | 15 +++++++++ 5 files changed, 79 insertions(+) create mode 100644 eth2/utils/test_random/Cargo.toml create mode 100644 eth2/utils/test_random/src/lib.rs create mode 100644 eth2/utils/test_random_derive/Cargo.toml create mode 100644 eth2/utils/test_random_derive/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index d92b1a303c..a557339a56 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,8 @@ members = [ "eth2/utils/ssz_derive", "eth2/utils/swap_or_not_shuffle", "eth2/utils/fisher_yates_shuffle", + "eth2/utils/test_random", + "eth2/utils/test_random_derive", "beacon_node", "beacon_node/db", "beacon_node/beacon_chain", diff --git a/eth2/utils/test_random/Cargo.toml b/eth2/utils/test_random/Cargo.toml new file mode 100644 index 0000000000..6346aa6282 --- /dev/null +++ b/eth2/utils/test_random/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "test_random" +version = "0.1.0" +authors = ["thojest "] +edition = "2018" + +[dependencies] +rand = "0.5.5" \ No newline at end of file diff --git a/eth2/utils/test_random/src/lib.rs b/eth2/utils/test_random/src/lib.rs new file mode 100644 index 0000000000..aa0d38a5a5 --- /dev/null +++ b/eth2/utils/test_random/src/lib.rs @@ -0,0 +1,39 @@ +use rand::RngCore; + +pub trait TestRandom +where + T: RngCore, +{ + fn random_for_test(rng: &mut T) -> Self; +} + +impl TestRandom for u64 { + fn random_for_test(rng: &mut T) -> Self { + rng.next_u64() + } +} + +impl TestRandom for u32 { + fn random_for_test(rng: &mut T) -> Self { + rng.next_u32() + } +} + +impl TestRandom for usize { + fn random_for_test(rng: &mut T) -> Self { + rng.next_u32() as usize + } +} + +impl TestRandom for Vec +where + U: TestRandom, +{ + fn random_for_test(rng: &mut T) -> Self { + vec![ + ::random_for_test(rng), + ::random_for_test(rng), + ::random_for_test(rng), + ] + } +} diff --git a/eth2/utils/test_random_derive/Cargo.toml b/eth2/utils/test_random_derive/Cargo.toml new file mode 100644 index 0000000000..e596ad29c8 --- /dev/null +++ b/eth2/utils/test_random_derive/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "test_random_derive" +version = "0.1.0" +authors = ["thojest "] +edition = "2018" +description = "Procedural derive macros for implementation of TestRandom trait" + +[lib] +proc-macro = true + +[dependencies] +syn = "0.15" +quote = "0.6" +test_random = {path = "../test_random"} + diff --git a/eth2/utils/test_random_derive/src/lib.rs b/eth2/utils/test_random_derive/src/lib.rs new file mode 100644 index 0000000000..27e62d31ae --- /dev/null +++ b/eth2/utils/test_random_derive/src/lib.rs @@ -0,0 +1,15 @@ +extern crate proc_macro; + +use crate::proc_macro::TokenStream; +use quote::quote; +use syn; +use syn::DeriveInput; + +#[proc_macro_derive(TestRandom)] +pub fn test_random_derive(input: TokenStream) -> TokenStream { + let ast = syn::parse(input).unwrap(); + + impl_test_random(&ast) +} + +fn impl_test_random(ast: &DeriveInput) -> TokenStream {} From 7a382043e173b5ae9a7cc8e6f12323790b8e3089 Mon Sep 17 00:00:00 2001 From: thojest Date: Fri, 22 Feb 2019 15:54:18 +0100 Subject: [PATCH 2/5] added test_random_derive implementation (lighthouse-246) --- eth2/utils/test_random_derive/src/lib.rs | 38 ++++++++++++++++++++---- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/eth2/utils/test_random_derive/src/lib.rs b/eth2/utils/test_random_derive/src/lib.rs index 27e62d31ae..2c244e4488 100644 --- a/eth2/utils/test_random_derive/src/lib.rs +++ b/eth2/utils/test_random_derive/src/lib.rs @@ -2,14 +2,42 @@ extern crate proc_macro; use crate::proc_macro::TokenStream; use quote::quote; -use syn; -use syn::DeriveInput; +use syn::{parse_macro_input, DeriveInput}; #[proc_macro_derive(TestRandom)] pub fn test_random_derive(input: TokenStream) -> TokenStream { - let ast = syn::parse(input).unwrap(); + let ast = parse_macro_input!(input as DeriveInput); + let name = &ast.ident; - impl_test_random(&ast) + let struct_data = match &ast.data { + syn::Data::Struct(s) => s, + _ => panic!("test_random_derive only supports structs."), + }; + + let field_names = get_named_field_idents_and_types(&struct_data); + + let output = quote! { + impl TestRandom for #name { + fn random_for_test(rng: &mut T) -> Self { + Self { + #( + #field_names: <_>::random_for_test(rng) + )* + } + } + } + }; + + output.into() } -fn impl_test_random(ast: &DeriveInput) -> TokenStream {} +fn get_named_field_idents_and_types(struct_data: &syn::DataStruct) -> Vec<(&syn::Ident)> { + struct_data + .fields + .iter() + .map(|f| match &f.ident { + Some(ref ident) => ident, + _ => panic!("test_random_derive only supports named struct fields."), + }) + .collect() +} From 278b41c8efff564850131cd5a58815b042095a0d Mon Sep 17 00:00:00 2001 From: thojest Date: Fri, 22 Feb 2019 16:05:52 +0100 Subject: [PATCH 3/5] decided against moving test_utils (lighthouse-246) --- Cargo.toml | 1 - eth2/utils/test_random/Cargo.toml | 8 ----- eth2/utils/test_random/src/lib.rs | 39 ------------------------ eth2/utils/test_random_derive/Cargo.toml | 2 -- 4 files changed, 50 deletions(-) delete mode 100644 eth2/utils/test_random/Cargo.toml delete mode 100644 eth2/utils/test_random/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index a557339a56..42d69489b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,6 @@ members = [ "eth2/utils/ssz_derive", "eth2/utils/swap_or_not_shuffle", "eth2/utils/fisher_yates_shuffle", - "eth2/utils/test_random", "eth2/utils/test_random_derive", "beacon_node", "beacon_node/db", diff --git a/eth2/utils/test_random/Cargo.toml b/eth2/utils/test_random/Cargo.toml deleted file mode 100644 index 6346aa6282..0000000000 --- a/eth2/utils/test_random/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "test_random" -version = "0.1.0" -authors = ["thojest "] -edition = "2018" - -[dependencies] -rand = "0.5.5" \ No newline at end of file diff --git a/eth2/utils/test_random/src/lib.rs b/eth2/utils/test_random/src/lib.rs deleted file mode 100644 index aa0d38a5a5..0000000000 --- a/eth2/utils/test_random/src/lib.rs +++ /dev/null @@ -1,39 +0,0 @@ -use rand::RngCore; - -pub trait TestRandom -where - T: RngCore, -{ - fn random_for_test(rng: &mut T) -> Self; -} - -impl TestRandom for u64 { - fn random_for_test(rng: &mut T) -> Self { - rng.next_u64() - } -} - -impl TestRandom for u32 { - fn random_for_test(rng: &mut T) -> Self { - rng.next_u32() - } -} - -impl TestRandom for usize { - fn random_for_test(rng: &mut T) -> Self { - rng.next_u32() as usize - } -} - -impl TestRandom for Vec -where - U: TestRandom, -{ - fn random_for_test(rng: &mut T) -> Self { - vec![ - ::random_for_test(rng), - ::random_for_test(rng), - ::random_for_test(rng), - ] - } -} diff --git a/eth2/utils/test_random_derive/Cargo.toml b/eth2/utils/test_random_derive/Cargo.toml index e596ad29c8..4559befafd 100644 --- a/eth2/utils/test_random_derive/Cargo.toml +++ b/eth2/utils/test_random_derive/Cargo.toml @@ -11,5 +11,3 @@ proc-macro = true [dependencies] syn = "0.15" quote = "0.6" -test_random = {path = "../test_random"} - From 66b5accdc2a8369d804248670fc81a09ba2f7582 Mon Sep 17 00:00:00 2001 From: thojest Date: Fri, 22 Feb 2019 17:10:26 +0100 Subject: [PATCH 4/5] replaced manual TestRandom implementation with macro when possible; fixed typo in TestRandom macro (lighthouse-246) --- eth2/types/Cargo.toml | 1 + eth2/types/src/attestation.rs | 14 ++------ eth2/types/src/attestation_data.rs | 18 ++-------- eth2/types/src/attester_slashing.rs | 12 ++----- eth2/types/src/beacon_block.rs | 17 ++------- eth2/types/src/beacon_block_body.rs | 15 ++------ eth2/types/src/beacon_state.rs | 35 ++----------------- eth2/types/src/casper_slashing.rs | 12 ++----- eth2/types/src/crosslink.rs | 12 ++----- eth2/types/src/deposit.rs | 13 ++----- eth2/types/src/deposit_data.rs | 13 ++----- eth2/types/src/deposit_input.rs | 13 ++----- eth2/types/src/eth1_data.rs | 12 ++----- eth2/types/src/eth1_data_vote.rs | 12 ++----- eth2/types/src/exit.rs | 13 ++----- eth2/types/src/fork.rs | 13 ++----- eth2/types/src/pending_attestation.rs | 14 ++------ eth2/types/src/proposal_signed_data.rs | 13 ++----- eth2/types/src/proposer_slashing.rs | 15 ++------ eth2/types/src/shard_reassignment_record.rs | 13 ++----- eth2/types/src/slashable_attestation.rs | 14 ++------ eth2/types/src/slashable_vote_data.rs | 14 ++------ .../src/validator_registry_delta_block.rs | 15 ++------ eth2/utils/test_random_derive/src/lib.rs | 12 +++---- 24 files changed, 51 insertions(+), 284 deletions(-) diff --git a/eth2/types/Cargo.toml b/eth2/types/Cargo.toml index f51e202363..f70e8b4904 100644 --- a/eth2/types/Cargo.toml +++ b/eth2/types/Cargo.toml @@ -20,6 +20,7 @@ slog = "^2.2.3" ssz = { path = "../utils/ssz" } ssz_derive = { path = "../utils/ssz_derive" } swap_or_not_shuffle = { path = "../utils/swap_or_not_shuffle" } +test_random_derive = { path = "../utils/test_random_derive" } [dev-dependencies] env_logger = "0.6.0" diff --git a/eth2/types/src/attestation.rs b/eth2/types/src/attestation.rs index 7388a8e49d..ee573dcc0b 100644 --- a/eth2/types/src/attestation.rs +++ b/eth2/types/src/attestation.rs @@ -4,8 +4,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TestRandom)] pub struct Attestation { pub aggregation_bitfield: Bitfield, pub data: AttestationData, @@ -45,17 +46,6 @@ impl TreeHash for Attestation { } } -impl TestRandom for Attestation { - fn random_for_test(rng: &mut T) -> Self { - Self { - data: <_>::random_for_test(rng), - aggregation_bitfield: <_>::random_for_test(rng), - custody_bitfield: <_>::random_for_test(rng), - aggregate_signature: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/attestation_data.rs b/eth2/types/src/attestation_data.rs index 7edb0b72b7..ec67e30b6c 100644 --- a/eth2/types/src/attestation_data.rs +++ b/eth2/types/src/attestation_data.rs @@ -4,6 +4,7 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; pub const SSZ_ATTESTION_DATA_LENGTH: usize = { 8 + // slot @@ -16,7 +17,7 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = { 32 // justified_block_root }; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TestRandom)] pub struct AttestationData { pub slot: Slot, pub shard: u64, @@ -59,21 +60,6 @@ impl TreeHash for AttestationData { } } -impl TestRandom for AttestationData { - fn random_for_test(rng: &mut T) -> Self { - Self { - slot: <_>::random_for_test(rng), - shard: <_>::random_for_test(rng), - beacon_block_root: <_>::random_for_test(rng), - epoch_boundary_root: <_>::random_for_test(rng), - shard_block_root: <_>::random_for_test(rng), - latest_crosslink: <_>::random_for_test(rng), - justified_epoch: <_>::random_for_test(rng), - justified_block_root: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/attester_slashing.rs b/eth2/types/src/attester_slashing.rs index f84998324c..8ea6b39e8f 100644 --- a/eth2/types/src/attester_slashing.rs +++ b/eth2/types/src/attester_slashing.rs @@ -3,8 +3,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] pub struct AttesterSlashing { pub slashable_attestation_1: SlashableAttestation, pub slashable_attestation_2: SlashableAttestation, @@ -19,15 +20,6 @@ impl TreeHash for AttesterSlashing { } } -impl TestRandom for AttesterSlashing { - fn random_for_test(rng: &mut T) -> Self { - Self { - slashable_attestation_1: <_>::random_for_test(rng), - slashable_attestation_2: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/beacon_block.rs b/eth2/types/src/beacon_block.rs index c252d03f75..e5dc9e238a 100644 --- a/eth2/types/src/beacon_block.rs +++ b/eth2/types/src/beacon_block.rs @@ -5,8 +5,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] pub struct BeaconBlock { pub slot: Slot, pub parent_root: Hash256, @@ -74,20 +75,6 @@ impl TreeHash for BeaconBlock { } } -impl TestRandom for BeaconBlock { - fn random_for_test(rng: &mut T) -> Self { - Self { - slot: <_>::random_for_test(rng), - parent_root: <_>::random_for_test(rng), - state_root: <_>::random_for_test(rng), - randao_reveal: <_>::random_for_test(rng), - eth1_data: <_>::random_for_test(rng), - signature: <_>::random_for_test(rng), - body: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/beacon_block_body.rs b/eth2/types/src/beacon_block_body.rs index e051f5940d..b492ba7475 100644 --- a/eth2/types/src/beacon_block_body.rs +++ b/eth2/types/src/beacon_block_body.rs @@ -4,8 +4,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TestRandom)] pub struct BeaconBlockBody { pub proposer_slashings: Vec, pub attester_slashings: Vec, @@ -26,18 +27,6 @@ impl TreeHash for BeaconBlockBody { } } -impl TestRandom for BeaconBlockBody { - fn random_for_test(rng: &mut T) -> Self { - Self { - proposer_slashings: <_>::random_for_test(rng), - attester_slashings: <_>::random_for_test(rng), - attestations: <_>::random_for_test(rng), - deposits: <_>::random_for_test(rng), - exits: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 21deb6fe75..df40a966a9 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -12,6 +12,7 @@ use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; use swap_or_not_shuffle::get_permutated_index; +use test_random_derive::TestRandom; mod tests; @@ -52,7 +53,7 @@ macro_rules! safe_sub_assign { }; } -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TestRandom)] pub struct BeaconState { // Misc pub slot: Slot, @@ -1003,35 +1004,3 @@ impl TreeHash for BeaconState { hash(&result) } } - -impl TestRandom for BeaconState { - fn random_for_test(rng: &mut T) -> Self { - Self { - slot: <_>::random_for_test(rng), - genesis_time: <_>::random_for_test(rng), - fork: <_>::random_for_test(rng), - validator_registry: <_>::random_for_test(rng), - validator_balances: <_>::random_for_test(rng), - validator_registry_update_epoch: <_>::random_for_test(rng), - latest_randao_mixes: <_>::random_for_test(rng), - previous_epoch_start_shard: <_>::random_for_test(rng), - current_epoch_start_shard: <_>::random_for_test(rng), - previous_calculation_epoch: <_>::random_for_test(rng), - current_calculation_epoch: <_>::random_for_test(rng), - previous_epoch_seed: <_>::random_for_test(rng), - current_epoch_seed: <_>::random_for_test(rng), - previous_justified_epoch: <_>::random_for_test(rng), - justified_epoch: <_>::random_for_test(rng), - justification_bitfield: <_>::random_for_test(rng), - finalized_epoch: <_>::random_for_test(rng), - latest_crosslinks: <_>::random_for_test(rng), - latest_block_roots: <_>::random_for_test(rng), - latest_index_roots: <_>::random_for_test(rng), - latest_penalized_balances: <_>::random_for_test(rng), - latest_attestations: <_>::random_for_test(rng), - batched_block_roots: <_>::random_for_test(rng), - latest_eth1_data: <_>::random_for_test(rng), - eth1_data_votes: <_>::random_for_test(rng), - } - } -} diff --git a/eth2/types/src/casper_slashing.rs b/eth2/types/src/casper_slashing.rs index 6346db65c6..bfc87e01d6 100644 --- a/eth2/types/src/casper_slashing.rs +++ b/eth2/types/src/casper_slashing.rs @@ -4,8 +4,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] pub struct CasperSlashing { pub slashable_vote_data_1: SlashableVoteData, pub slashable_vote_data_2: SlashableVoteData, @@ -20,15 +21,6 @@ impl TreeHash for CasperSlashing { } } -impl TestRandom for CasperSlashing { - fn random_for_test(rng: &mut T) -> Self { - Self { - slashable_vote_data_1: <_>::random_for_test(rng), - slashable_vote_data_2: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/crosslink.rs b/eth2/types/src/crosslink.rs index 19c71f6048..8b0d2bc181 100644 --- a/eth2/types/src/crosslink.rs +++ b/eth2/types/src/crosslink.rs @@ -4,8 +4,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TestRandom)] pub struct Crosslink { pub epoch: Epoch, pub shard_block_root: Hash256, @@ -30,15 +31,6 @@ impl TreeHash for Crosslink { } } -impl TestRandom for Crosslink { - fn random_for_test(rng: &mut T) -> Self { - Self { - epoch: <_>::random_for_test(rng), - shard_block_root: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/deposit.rs b/eth2/types/src/deposit.rs index 78f43532aa..2b126b900b 100644 --- a/eth2/types/src/deposit.rs +++ b/eth2/types/src/deposit.rs @@ -4,8 +4,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] pub struct Deposit { pub branch: Vec, pub index: u64, @@ -22,16 +23,6 @@ impl TreeHash for Deposit { } } -impl TestRandom for Deposit { - fn random_for_test(rng: &mut T) -> Self { - Self { - branch: <_>::random_for_test(rng), - index: <_>::random_for_test(rng), - deposit_data: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/deposit_data.rs b/eth2/types/src/deposit_data.rs index 8f49deb3c2..0cce11cec7 100644 --- a/eth2/types/src/deposit_data.rs +++ b/eth2/types/src/deposit_data.rs @@ -4,8 +4,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] pub struct DepositData { pub amount: u64, pub timestamp: u64, @@ -22,16 +23,6 @@ impl TreeHash for DepositData { } } -impl TestRandom for DepositData { - fn random_for_test(rng: &mut T) -> Self { - Self { - amount: <_>::random_for_test(rng), - timestamp: <_>::random_for_test(rng), - deposit_input: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/deposit_input.rs b/eth2/types/src/deposit_input.rs index 7556fc2ca0..d14ba68ac1 100644 --- a/eth2/types/src/deposit_input.rs +++ b/eth2/types/src/deposit_input.rs @@ -5,8 +5,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] pub struct DepositInput { pub pubkey: PublicKey, pub withdrawal_credentials: Hash256, @@ -23,16 +24,6 @@ impl TreeHash for DepositInput { } } -impl TestRandom for DepositInput { - fn random_for_test(rng: &mut T) -> Self { - Self { - pubkey: <_>::random_for_test(rng), - withdrawal_credentials: <_>::random_for_test(rng), - proof_of_possession: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/eth1_data.rs b/eth2/types/src/eth1_data.rs index b0dc14e7a8..e1b9683039 100644 --- a/eth2/types/src/eth1_data.rs +++ b/eth2/types/src/eth1_data.rs @@ -4,9 +4,10 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TestRandom)] pub struct Eth1Data { pub deposit_root: Hash256, pub block_hash: Hash256, @@ -21,15 +22,6 @@ impl TreeHash for Eth1Data { } } -impl TestRandom for Eth1Data { - fn random_for_test(rng: &mut T) -> Self { - Self { - deposit_root: <_>::random_for_test(rng), - block_hash: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/eth1_data_vote.rs b/eth2/types/src/eth1_data_vote.rs index eda6e6a6a4..09d4629997 100644 --- a/eth2/types/src/eth1_data_vote.rs +++ b/eth2/types/src/eth1_data_vote.rs @@ -4,9 +4,10 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TestRandom)] pub struct Eth1DataVote { pub eth1_data: Eth1Data, pub vote_count: u64, @@ -21,15 +22,6 @@ impl TreeHash for Eth1DataVote { } } -impl TestRandom for Eth1DataVote { - fn random_for_test(rng: &mut T) -> Self { - Self { - eth1_data: <_>::random_for_test(rng), - vote_count: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/exit.rs b/eth2/types/src/exit.rs index 18d743b833..c96319d55e 100644 --- a/eth2/types/src/exit.rs +++ b/eth2/types/src/exit.rs @@ -4,8 +4,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] pub struct Exit { pub epoch: Epoch, pub validator_index: u64, @@ -22,16 +23,6 @@ impl TreeHash for Exit { } } -impl TestRandom for Exit { - fn random_for_test(rng: &mut T) -> Self { - Self { - epoch: <_>::random_for_test(rng), - validator_index: <_>::random_for_test(rng), - signature: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index 85d530e198..555237b574 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -3,8 +3,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TestRandom)] pub struct Fork { pub previous_version: u64, pub current_version: u64, @@ -21,16 +22,6 @@ impl TreeHash for Fork { } } -impl TestRandom for Fork { - fn random_for_test(rng: &mut T) -> Self { - Self { - previous_version: <_>::random_for_test(rng), - current_version: <_>::random_for_test(rng), - epoch: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/pending_attestation.rs b/eth2/types/src/pending_attestation.rs index 42f990210b..3bf6ff1ada 100644 --- a/eth2/types/src/pending_attestation.rs +++ b/eth2/types/src/pending_attestation.rs @@ -4,8 +4,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TestRandom)] pub struct PendingAttestation { pub aggregation_bitfield: Bitfield, pub data: AttestationData, @@ -24,17 +25,6 @@ impl TreeHash for PendingAttestation { } } -impl TestRandom for PendingAttestation { - fn random_for_test(rng: &mut T) -> Self { - Self { - data: <_>::random_for_test(rng), - aggregation_bitfield: <_>::random_for_test(rng), - custody_bitfield: <_>::random_for_test(rng), - inclusion_slot: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/proposal_signed_data.rs b/eth2/types/src/proposal_signed_data.rs index 63c0f1ce67..f3f369b7f2 100644 --- a/eth2/types/src/proposal_signed_data.rs +++ b/eth2/types/src/proposal_signed_data.rs @@ -4,8 +4,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TestRandom)] pub struct ProposalSignedData { pub slot: Slot, pub shard: u64, @@ -22,16 +23,6 @@ impl TreeHash for ProposalSignedData { } } -impl TestRandom for ProposalSignedData { - fn random_for_test(rng: &mut T) -> Self { - Self { - slot: <_>::random_for_test(rng), - shard: <_>::random_for_test(rng), - block_root: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/proposer_slashing.rs b/eth2/types/src/proposer_slashing.rs index b3a819a7f2..08ae27abfc 100644 --- a/eth2/types/src/proposer_slashing.rs +++ b/eth2/types/src/proposer_slashing.rs @@ -5,8 +5,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] pub struct ProposerSlashing { pub proposer_index: u64, pub proposal_data_1: ProposalSignedData, @@ -27,18 +28,6 @@ impl TreeHash for ProposerSlashing { } } -impl TestRandom for ProposerSlashing { - fn random_for_test(rng: &mut T) -> Self { - Self { - proposer_index: <_>::random_for_test(rng), - proposal_data_1: <_>::random_for_test(rng), - proposal_signature_1: <_>::random_for_test(rng), - proposal_data_2: <_>::random_for_test(rng), - proposal_signature_2: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/shard_reassignment_record.rs b/eth2/types/src/shard_reassignment_record.rs index 511fe13ca9..eb67a10c26 100644 --- a/eth2/types/src/shard_reassignment_record.rs +++ b/eth2/types/src/shard_reassignment_record.rs @@ -3,8 +3,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] pub struct ShardReassignmentRecord { pub validator_index: u64, pub shard: u64, @@ -21,16 +22,6 @@ impl TreeHash for ShardReassignmentRecord { } } -impl TestRandom for ShardReassignmentRecord { - fn random_for_test(rng: &mut T) -> Self { - Self { - validator_index: <_>::random_for_test(rng), - shard: <_>::random_for_test(rng), - slot: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/slashable_attestation.rs b/eth2/types/src/slashable_attestation.rs index 676954ec2f..db1e7fe799 100644 --- a/eth2/types/src/slashable_attestation.rs +++ b/eth2/types/src/slashable_attestation.rs @@ -3,8 +3,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] pub struct SlashableAttestation { pub validator_indices: Vec, pub data: AttestationData, @@ -23,17 +24,6 @@ impl TreeHash for SlashableAttestation { } } -impl TestRandom for SlashableAttestation { - fn random_for_test(rng: &mut T) -> Self { - Self { - validator_indices: <_>::random_for_test(rng), - data: <_>::random_for_test(rng), - custody_bitfield: <_>::random_for_test(rng), - aggregate_signature: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/slashable_vote_data.rs b/eth2/types/src/slashable_vote_data.rs index bdd1d0619c..0a79166da8 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -6,8 +6,9 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TestRandom)] pub struct SlashableVoteData { pub custody_bit_0_indices: Vec, pub custody_bit_1_indices: Vec, @@ -47,17 +48,6 @@ impl TreeHash for SlashableVoteData { } } -impl TestRandom for SlashableVoteData { - fn random_for_test(rng: &mut T) -> Self { - Self { - custody_bit_0_indices: <_>::random_for_test(rng), - custody_bit_1_indices: <_>::random_for_test(rng), - data: <_>::random_for_test(rng), - aggregate_signature: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/types/src/validator_registry_delta_block.rs b/eth2/types/src/validator_registry_delta_block.rs index 14f9c6ce57..35316a356e 100644 --- a/eth2/types/src/validator_registry_delta_block.rs +++ b/eth2/types/src/validator_registry_delta_block.rs @@ -4,9 +4,10 @@ use rand::RngCore; use serde_derive::Serialize; use ssz::{hash, TreeHash}; use ssz_derive::{Decode, Encode}; +use test_random_derive::TestRandom; // The information gathered from the PoW chain validator registration function. -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TestRandom)] pub struct ValidatorRegistryDeltaBlock { pub latest_registry_delta_root: Hash256, pub validator_index: u32, @@ -40,18 +41,6 @@ impl TreeHash for ValidatorRegistryDeltaBlock { } } -impl TestRandom for ValidatorRegistryDeltaBlock { - fn random_for_test(rng: &mut T) -> Self { - Self { - latest_registry_delta_root: <_>::random_for_test(rng), - validator_index: <_>::random_for_test(rng), - pubkey: <_>::random_for_test(rng), - slot: <_>::random_for_test(rng), - flag: <_>::random_for_test(rng), - } - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/eth2/utils/test_random_derive/src/lib.rs b/eth2/utils/test_random_derive/src/lib.rs index 2c244e4488..9a456606ce 100644 --- a/eth2/utils/test_random_derive/src/lib.rs +++ b/eth2/utils/test_random_derive/src/lib.rs @@ -6,22 +6,22 @@ use syn::{parse_macro_input, DeriveInput}; #[proc_macro_derive(TestRandom)] pub fn test_random_derive(input: TokenStream) -> TokenStream { - let ast = parse_macro_input!(input as DeriveInput); - let name = &ast.ident; + let derived_input = parse_macro_input!(input as DeriveInput); + let name = &derived_input.ident; - let struct_data = match &ast.data { + let struct_data = match &derived_input.data { syn::Data::Struct(s) => s, _ => panic!("test_random_derive only supports structs."), }; - let field_names = get_named_field_idents_and_types(&struct_data); + let field_names = get_named_field_idents(&struct_data); let output = quote! { impl TestRandom for #name { fn random_for_test(rng: &mut T) -> Self { Self { #( - #field_names: <_>::random_for_test(rng) + #field_names: <_>::random_for_test(rng), )* } } @@ -31,7 +31,7 @@ pub fn test_random_derive(input: TokenStream) -> TokenStream { output.into() } -fn get_named_field_idents_and_types(struct_data: &syn::DataStruct) -> Vec<(&syn::Ident)> { +fn get_named_field_idents(struct_data: &syn::DataStruct) -> Vec<(&syn::Ident)> { struct_data .fields .iter() From 9a892bbdfd7974a302a569de301b47b70d74922a Mon Sep 17 00:00:00 2001 From: thojest Date: Mon, 25 Feb 2019 09:26:43 +0100 Subject: [PATCH 5/5] removed TestRandom import in beacon_state (lighthouse-246) --- eth2/types/src/beacon_state.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index ccce4bfa77..85b49bf01c 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -13,7 +13,6 @@ use serde_derive::Serialize; use ssz::{hash, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use std::collections::HashMap; use swap_or_not_shuffle::get_permutated_index; -use test_random_derive::TestRandom; mod epoch_cache; mod tests;