From ad91648f30248e0e62146de14112e9fd50987cd4 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Fri, 4 Jan 2019 18:10:14 +1100 Subject: [PATCH] Tidy up SSZ testing on types crate --- beacon_chain/types/src/attestation.rs | 3 +-- beacon_chain/types/src/attestation_data.rs | 3 +-- beacon_chain/types/src/beacon_block.rs | 3 +-- beacon_chain/types/src/beacon_block_body.rs | 3 +-- beacon_chain/types/src/beacon_state.rs | 18 ++++++++++++++++++ .../src/candidate_pow_receipt_root_record.rs | 3 +-- beacon_chain/types/src/casper_slashing.rs | 3 +-- beacon_chain/types/src/crosslink_record.rs | 3 +-- beacon_chain/types/src/deposit_data.rs | 3 +-- beacon_chain/types/src/deposit_input.rs | 3 +-- beacon_chain/types/src/exit.rs | 3 +-- beacon_chain/types/src/fork_data.rs | 3 +-- .../types/src/pending_attestation_record.rs | 3 +-- beacon_chain/types/src/proposal_signed_data.rs | 3 +-- beacon_chain/types/src/proposer_slashing.rs | 3 +-- beacon_chain/types/src/readers/block_reader.rs | 6 +++--- beacon_chain/types/src/readers/state_reader.rs | 6 +++--- beacon_chain/types/src/shard_committee.rs | 3 +-- .../types/src/shard_reassignment_record.rs | 3 +-- beacon_chain/types/src/slashable_vote_data.rs | 3 +-- beacon_chain/types/src/validator_record.rs | 3 +-- 21 files changed, 42 insertions(+), 42 deletions(-) diff --git a/beacon_chain/types/src/attestation.rs b/beacon_chain/types/src/attestation.rs index 7a94eee51..fb8b946a5 100644 --- a/beacon_chain/types/src/attestation.rs +++ b/beacon_chain/types/src/attestation.rs @@ -64,8 +64,7 @@ impl TestRandom for Attestation { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/attestation_data.rs b/beacon_chain/types/src/attestation_data.rs index 616c52d79..e1d70e2b0 100644 --- a/beacon_chain/types/src/attestation_data.rs +++ b/beacon_chain/types/src/attestation_data.rs @@ -104,8 +104,7 @@ impl TestRandom for AttestationData { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/beacon_block.rs b/beacon_chain/types/src/beacon_block.rs index 826667d5e..30a1bd83b 100644 --- a/beacon_chain/types/src/beacon_block.rs +++ b/beacon_chain/types/src/beacon_block.rs @@ -79,8 +79,7 @@ impl TestRandom for BeaconBlock { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/beacon_block_body.rs b/beacon_chain/types/src/beacon_block_body.rs index f5ac22343..8bcf1af33 100644 --- a/beacon_chain/types/src/beacon_block_body.rs +++ b/beacon_chain/types/src/beacon_block_body.rs @@ -59,8 +59,7 @@ impl TestRandom for BeaconBlockBody { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/beacon_state.rs b/beacon_chain/types/src/beacon_state.rs index f2d8f6aeb..7bd0283c5 100644 --- a/beacon_chain/types/src/beacon_state.rs +++ b/beacon_chain/types/src/beacon_state.rs @@ -166,3 +166,21 @@ impl TestRandom for BeaconState { } } } + +#[cfg(test)] +mod tests { + use super::super::ssz::ssz_encode; + use super::*; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; + + #[test] + pub fn test_ssz_round_trip() { + let mut rng = XorShiftRng::from_seed([42; 16]); + let original = BeaconState::random_for_test(&mut rng); + + let bytes = ssz_encode(&original); + let (decoded, _) = <_>::ssz_decode(&bytes, 0).unwrap(); + + assert_eq!(original, decoded); + } +} diff --git a/beacon_chain/types/src/candidate_pow_receipt_root_record.rs b/beacon_chain/types/src/candidate_pow_receipt_root_record.rs index 663c3e1db..5f260c423 100644 --- a/beacon_chain/types/src/candidate_pow_receipt_root_record.rs +++ b/beacon_chain/types/src/candidate_pow_receipt_root_record.rs @@ -44,8 +44,7 @@ impl TestRandom for CandidatePoWReceiptRootRecord { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/casper_slashing.rs b/beacon_chain/types/src/casper_slashing.rs index f3c1b5d18..08dbd9ff3 100644 --- a/beacon_chain/types/src/casper_slashing.rs +++ b/beacon_chain/types/src/casper_slashing.rs @@ -44,8 +44,7 @@ impl TestRandom for CasperSlashing { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/crosslink_record.rs b/beacon_chain/types/src/crosslink_record.rs index 9e525664a..ae6c5bf92 100644 --- a/beacon_chain/types/src/crosslink_record.rs +++ b/beacon_chain/types/src/crosslink_record.rs @@ -54,8 +54,7 @@ impl TestRandom for CrosslinkRecord { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/deposit_data.rs b/beacon_chain/types/src/deposit_data.rs index b236709d2..5dcd20c7c 100644 --- a/beacon_chain/types/src/deposit_data.rs +++ b/beacon_chain/types/src/deposit_data.rs @@ -49,8 +49,7 @@ impl TestRandom for DepositData { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/deposit_input.rs b/beacon_chain/types/src/deposit_input.rs index 3a9a08412..d7e2272e9 100644 --- a/beacon_chain/types/src/deposit_input.rs +++ b/beacon_chain/types/src/deposit_input.rs @@ -55,8 +55,7 @@ impl TestRandom for DepositInput { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/exit.rs b/beacon_chain/types/src/exit.rs index 6a6d57305..eeac11ce7 100644 --- a/beacon_chain/types/src/exit.rs +++ b/beacon_chain/types/src/exit.rs @@ -49,8 +49,7 @@ impl TestRandom for Exit { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/fork_data.rs b/beacon_chain/types/src/fork_data.rs index e779c797e..7cd4dc3ac 100644 --- a/beacon_chain/types/src/fork_data.rs +++ b/beacon_chain/types/src/fork_data.rs @@ -48,8 +48,7 @@ impl TestRandom for ForkData { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/pending_attestation_record.rs b/beacon_chain/types/src/pending_attestation_record.rs index aa289e904..3bebf5676 100644 --- a/beacon_chain/types/src/pending_attestation_record.rs +++ b/beacon_chain/types/src/pending_attestation_record.rs @@ -54,8 +54,7 @@ impl TestRandom for PendingAttestationRecord { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/proposal_signed_data.rs b/beacon_chain/types/src/proposal_signed_data.rs index 7a01fc10a..e38a9cadb 100644 --- a/beacon_chain/types/src/proposal_signed_data.rs +++ b/beacon_chain/types/src/proposal_signed_data.rs @@ -49,8 +49,7 @@ impl TestRandom for ProposalSignedData { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/proposer_slashing.rs b/beacon_chain/types/src/proposer_slashing.rs index 0ae1c6e66..3754c3b32 100644 --- a/beacon_chain/types/src/proposer_slashing.rs +++ b/beacon_chain/types/src/proposer_slashing.rs @@ -60,8 +60,7 @@ impl TestRandom for ProposerSlashing { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/readers/block_reader.rs b/beacon_chain/types/src/readers/block_reader.rs index e1cba224d..1698a0380 100644 --- a/beacon_chain/types/src/readers/block_reader.rs +++ b/beacon_chain/types/src/readers/block_reader.rs @@ -5,7 +5,7 @@ pub trait BeaconBlockReader { fn parent_root(&self) -> Hash256; fn state_root(&self) -> Hash256; fn canonical_root(&self) -> Hash256; - fn to_beacon_block(self) -> BeaconBlock; + fn into_beacon_block(self) -> Option; } impl BeaconBlockReader for BeaconBlock { @@ -25,7 +25,7 @@ impl BeaconBlockReader for BeaconBlock { self.canonical_root() } - fn to_beacon_block(self) -> BeaconBlock { - self + fn into_beacon_block(self) -> Option { + Some(self) } } diff --git a/beacon_chain/types/src/readers/state_reader.rs b/beacon_chain/types/src/readers/state_reader.rs index a43ef9930..cbbf0854c 100644 --- a/beacon_chain/types/src/readers/state_reader.rs +++ b/beacon_chain/types/src/readers/state_reader.rs @@ -3,7 +3,7 @@ use crate::{BeaconState, Hash256}; pub trait BeaconStateReader { fn slot(&self) -> u64; fn canonical_root(&self) -> Hash256; - fn to_beacon_state(self) -> BeaconState; + fn into_beacon_state(self) -> Option; } impl BeaconStateReader for BeaconState { @@ -15,7 +15,7 @@ impl BeaconStateReader for BeaconState { self.canonical_root() } - fn to_beacon_state(self) -> BeaconState { - self + fn into_beacon_state(self) -> Option { + Some(self) } } diff --git a/beacon_chain/types/src/shard_committee.rs b/beacon_chain/types/src/shard_committee.rs index 073dd0176..d920f7db3 100644 --- a/beacon_chain/types/src/shard_committee.rs +++ b/beacon_chain/types/src/shard_committee.rs @@ -37,8 +37,7 @@ impl TestRandom for ShardCommittee { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/shard_reassignment_record.rs b/beacon_chain/types/src/shard_reassignment_record.rs index e990afeac..9bc014689 100644 --- a/beacon_chain/types/src/shard_reassignment_record.rs +++ b/beacon_chain/types/src/shard_reassignment_record.rs @@ -48,8 +48,7 @@ impl TestRandom for ShardReassignmentRecord { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/slashable_vote_data.rs b/beacon_chain/types/src/slashable_vote_data.rs index 4d8e2eab3..5703853b2 100644 --- a/beacon_chain/types/src/slashable_vote_data.rs +++ b/beacon_chain/types/src/slashable_vote_data.rs @@ -55,8 +55,7 @@ impl TestRandom for SlashableVoteData { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() { diff --git a/beacon_chain/types/src/validator_record.rs b/beacon_chain/types/src/validator_record.rs index 45b6c301a..374d95623 100644 --- a/beacon_chain/types/src/validator_record.rs +++ b/beacon_chain/types/src/validator_record.rs @@ -150,8 +150,7 @@ impl TestRandom for ValidatorRecord { mod tests { use super::super::ssz::ssz_encode; use super::*; - use crate::test_utils::TestRandom; - use rand::{prng::XorShiftRng, SeedableRng}; + use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; #[test] pub fn test_ssz_round_trip() {