diff --git a/Cargo.toml b/Cargo.toml index d92b1a303c..42d69489b3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ members = [ "eth2/utils/ssz_derive", "eth2/utils/swap_or_not_shuffle", "eth2/utils/fisher_yates_shuffle", + "eth2/utils/test_random_derive", "beacon_node", "beacon_node/db", "beacon_node/beacon_chain", 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 73a587c26b..a0c8505b82 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::TreeHash; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct Attestation { pub aggregation_bitfield: Bitfield, pub data: AttestationData, @@ -36,17 +37,6 @@ impl 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 868f9743ab..e23cdab467 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::TreeHash; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; pub const SSZ_ATTESTION_DATA_LENGTH: usize = { 8 + // slot @@ -16,7 +17,9 @@ pub const SSZ_ATTESTION_DATA_LENGTH: usize = { 32 // justified_block_root }; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TreeHash)] +#[derive( + Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TreeHash, TestRandom, +)] pub struct AttestationData { pub slot: Slot, pub shard: u64, @@ -44,21 +47,6 @@ impl 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 96204edc58..2a1df9e0cc 100644 --- a/eth2/types/src/attester_slashing.rs +++ b/eth2/types/src/attester_slashing.rs @@ -2,22 +2,14 @@ use crate::{test_utils::TestRandom, SlashableAttestation}; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct AttesterSlashing { pub slashable_attestation_1: SlashableAttestation, pub slashable_attestation_2: SlashableAttestation, } -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 9d769cbed5..cb4e6668b8 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::TreeHash; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct BeaconBlock { pub slot: Slot, pub parent_root: Hash256, @@ -60,20 +61,6 @@ impl 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 915e6435c2..2b343b970d 100644 --- a/eth2/types/src/beacon_block_body.rs +++ b/eth2/types/src/beacon_block_body.rs @@ -3,8 +3,9 @@ use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct BeaconBlockBody { pub proposer_slashings: Vec, pub attester_slashings: Vec, @@ -13,18 +14,6 @@ pub struct BeaconBlockBody { pub exits: Vec, } -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/casper_slashing.rs b/eth2/types/src/casper_slashing.rs index 76a6515a21..cb1e46ee55 100644 --- a/eth2/types/src/casper_slashing.rs +++ b/eth2/types/src/casper_slashing.rs @@ -3,22 +3,14 @@ use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct CasperSlashing { pub slashable_vote_data_1: SlashableVoteData, pub slashable_vote_data_2: SlashableVoteData, } -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 9c7b8eeed0..11fb3386d4 100644 --- a/eth2/types/src/crosslink.rs +++ b/eth2/types/src/crosslink.rs @@ -3,8 +3,11 @@ use crate::{Epoch, Hash256}; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TreeHash)] +#[derive( + Debug, Clone, PartialEq, Default, Serialize, Hash, Encode, Decode, TreeHash, TestRandom, +)] pub struct Crosslink { pub epoch: Epoch, pub shard_block_root: Hash256, @@ -20,15 +23,6 @@ impl 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 de485cd2b8..02da32cfe3 100644 --- a/eth2/types/src/deposit.rs +++ b/eth2/types/src/deposit.rs @@ -3,24 +3,15 @@ use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct Deposit { pub branch: Vec, pub index: u64, pub deposit_data: DepositData, } -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 6ac40c0f84..349207791b 100644 --- a/eth2/types/src/deposit_data.rs +++ b/eth2/types/src/deposit_data.rs @@ -3,24 +3,15 @@ use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct DepositData { pub amount: u64, pub timestamp: u64, pub deposit_input: DepositInput, } -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 5baa2226d7..1f3b22779b 100644 --- a/eth2/types/src/deposit_input.rs +++ b/eth2/types/src/deposit_input.rs @@ -4,24 +4,15 @@ use bls::{PublicKey, Signature}; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct DepositInput { pub pubkey: PublicKey, pub withdrawal_credentials: Hash256, pub proof_of_possession: Signature, } -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 2cbfad7701..8eabbabc79 100644 --- a/eth2/types/src/eth1_data.rs +++ b/eth2/types/src/eth1_data.rs @@ -3,23 +3,15 @@ use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct Eth1Data { pub deposit_root: Hash256, pub block_hash: Hash256, } -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 e2a98df3c2..fa30b9052e 100644 --- a/eth2/types/src/eth1_data_vote.rs +++ b/eth2/types/src/eth1_data_vote.rs @@ -3,23 +3,15 @@ use crate::test_utils::TestRandom; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; // Note: this is refer to as DepositRootVote in specs -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct Eth1DataVote { pub eth1_data: Eth1Data, pub vote_count: u64, } -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 f564c8bb57..5b41fcc7ad 100644 --- a/eth2/types/src/exit.rs +++ b/eth2/types/src/exit.rs @@ -3,24 +3,15 @@ use bls::Signature; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct Exit { pub epoch: Epoch, pub validator_index: u64, pub signature: Signature, } -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 bf168f03f4..5b13a23888 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -2,8 +2,9 @@ use crate::{test_utils::TestRandom, Epoch}; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, Clone, PartialEq, Default, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct Fork { pub previous_version: u64, pub current_version: u64, @@ -26,16 +27,6 @@ impl 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 53f1868d63..84eb592078 100644 --- a/eth2/types/src/pending_attestation.rs +++ b/eth2/types/src/pending_attestation.rs @@ -3,8 +3,9 @@ use crate::{AttestationData, Bitfield, Slot}; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct PendingAttestation { pub aggregation_bitfield: Bitfield, pub data: AttestationData, @@ -12,17 +13,6 @@ pub struct PendingAttestation { pub inclusion_slot: Slot, } -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 164192cc92..6f6048ffc0 100644 --- a/eth2/types/src/proposal_signed_data.rs +++ b/eth2/types/src/proposal_signed_data.rs @@ -3,24 +3,15 @@ use crate::{Hash256, Slot}; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Default, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct ProposalSignedData { pub slot: Slot, pub shard: u64, pub block_root: Hash256, } -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 fc5276dfe7..610017c0cb 100644 --- a/eth2/types/src/proposer_slashing.rs +++ b/eth2/types/src/proposer_slashing.rs @@ -4,8 +4,9 @@ use bls::Signature; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct ProposerSlashing { pub proposer_index: u64, pub proposal_data_1: ProposalSignedData, @@ -14,18 +15,6 @@ pub struct ProposerSlashing { pub proposal_signature_2: Signature, } -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 e7a22e1fa9..f5dfa86762 100644 --- a/eth2/types/src/shard_reassignment_record.rs +++ b/eth2/types/src/shard_reassignment_record.rs @@ -2,24 +2,15 @@ use crate::{test_utils::TestRandom, Slot}; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct ShardReassignmentRecord { pub validator_index: u64, pub shard: u64, pub slot: Slot, } -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 3315f1d351..c4a12338a5 100644 --- a/eth2/types/src/slashable_attestation.rs +++ b/eth2/types/src/slashable_attestation.rs @@ -2,8 +2,9 @@ use crate::{test_utils::TestRandom, AggregateSignature, AttestationData, Bitfiel use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct SlashableAttestation { pub validator_indices: Vec, pub data: AttestationData, @@ -11,17 +12,6 @@ pub struct SlashableAttestation { pub aggregate_signature: AggregateSignature, } -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 e8ac61dbe0..31dd9e0a8a 100644 --- a/eth2/types/src/slashable_vote_data.rs +++ b/eth2/types/src/slashable_vote_data.rs @@ -5,8 +5,9 @@ use bls::AggregateSignature; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; -#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, PartialEq, Clone, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct SlashableVoteData { pub custody_bit_0_indices: Vec, pub custody_bit_1_indices: Vec, @@ -35,17 +36,6 @@ impl 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 86f5718d65..0746875f09 100644 --- a/eth2/types/src/validator_registry_delta_block.rs +++ b/eth2/types/src/validator_registry_delta_block.rs @@ -3,9 +3,10 @@ use bls::PublicKey; use rand::RngCore; use serde_derive::Serialize; use ssz_derive::{Decode, Encode, TreeHash}; +use test_random_derive::TestRandom; // The information gathered from the PoW chain validator registration function. -#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash)] +#[derive(Debug, Clone, PartialEq, Serialize, Encode, Decode, TreeHash, TestRandom)] pub struct ValidatorRegistryDeltaBlock { pub latest_registry_delta_root: Hash256, pub validator_index: u32, @@ -27,18 +28,6 @@ impl Default 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/Cargo.toml b/eth2/utils/test_random_derive/Cargo.toml new file mode 100644 index 0000000000..4559befafd --- /dev/null +++ b/eth2/utils/test_random_derive/Cargo.toml @@ -0,0 +1,13 @@ +[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" 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..9a456606ce --- /dev/null +++ b/eth2/utils/test_random_derive/src/lib.rs @@ -0,0 +1,43 @@ +extern crate proc_macro; + +use crate::proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, DeriveInput}; + +#[proc_macro_derive(TestRandom)] +pub fn test_random_derive(input: TokenStream) -> TokenStream { + let derived_input = parse_macro_input!(input as DeriveInput); + let name = &derived_input.ident; + + 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(&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 get_named_field_idents(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() +}