From 0b7082e2b91aa7bc8fa940b50df13ccd0353d977 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 11 Mar 2019 11:17:27 +1100 Subject: [PATCH] Move `benching_utils` structs into `types` --- Cargo.toml | 1 - eth2/state_processing/Cargo.toml | 1 - .../benches/block_processing_benches.rs | 6 +++--- .../benches/epoch_processing_benches.rs | 4 ++-- eth2/state_processing/benching_utils/Cargo.toml | 17 ----------------- eth2/state_processing/benching_utils/src/lib.rs | 5 ----- .../src/per_epoch_processing/tests.rs | 4 ++-- eth2/types/src/beacon_state/builder.rs | 2 +- eth2/types/src/beacon_state/tests.rs | 17 ++++------------- eth2/types/src/test_utils/mod.rs | 4 ++++ .../test_utils/testing_beacon_block_builder.rs} | 10 +++++----- .../test_utils/testing_beacon_state_builder.rs} | 8 ++++---- 12 files changed, 25 insertions(+), 54 deletions(-) delete mode 100644 eth2/state_processing/benching_utils/Cargo.toml delete mode 100644 eth2/state_processing/benching_utils/src/lib.rs rename eth2/{state_processing/benching_utils/src/beacon_block_bencher.rs => types/src/test_utils/testing_beacon_block_builder.rs} (99%) rename eth2/{state_processing/benching_utils/src/beacon_state_bencher.rs => types/src/test_utils/testing_beacon_state_builder.rs} (98%) diff --git a/Cargo.toml b/Cargo.toml index 8f4dbb268..c5aae7f43 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,6 @@ members = [ "eth2/block_proposer", "eth2/fork_choice", "eth2/state_processing", - "eth2/state_processing/benching_utils", "eth2/types", "eth2/utils/bls", "eth2/utils/boolean-bitfield", diff --git a/eth2/state_processing/Cargo.toml b/eth2/state_processing/Cargo.toml index 962d23a77..f6692b259 100644 --- a/eth2/state_processing/Cargo.toml +++ b/eth2/state_processing/Cargo.toml @@ -11,7 +11,6 @@ harness = false [dev-dependencies] criterion = "0.2" env_logger = "0.6.0" -benching_utils = { path = "./benching_utils" } [dependencies] bls = { path = "../utils/bls" } diff --git a/eth2/state_processing/benches/block_processing_benches.rs b/eth2/state_processing/benches/block_processing_benches.rs index 840fcaeba..3c59e51e5 100644 --- a/eth2/state_processing/benches/block_processing_benches.rs +++ b/eth2/state_processing/benches/block_processing_benches.rs @@ -1,4 +1,3 @@ -use benching_utils::{BeaconBlockBencher, BeaconStateBencher}; use criterion::Criterion; use criterion::{black_box, Benchmark}; use ssz::TreeHash; @@ -10,6 +9,7 @@ use state_processing::{ verify_block_signature, }, }; +use types::test_utils::{TestingBeaconBlockBuilder, TestingBeaconStateBuilder}; use types::*; /// Run the benchmarking suite on a foundation spec with 16,384 validators. @@ -82,7 +82,7 @@ pub fn block_processing_16k_validators(c: &mut Criterion) { } fn build_state(validator_count: usize, spec: &ChainSpec) -> (BeaconState, Vec) { - let mut builder = BeaconStateBencher::new(validator_count, &spec); + let mut builder = TestingBeaconStateBuilder::new(validator_count, &spec); // Set the state to be just before an epoch transition. let target_slot = (spec.genesis_epoch + 4).end_slot(spec.slots_per_epoch); @@ -95,7 +95,7 @@ fn build_state(validator_count: usize, spec: &ChainSpec) -> (BeaconState, Vec BeaconBlock { - let mut builder = BeaconBlockBencher::new(spec); + let mut builder = TestingBeaconBlockBuilder::new(spec); builder.set_slot(state.slot); diff --git a/eth2/state_processing/benches/epoch_processing_benches.rs b/eth2/state_processing/benches/epoch_processing_benches.rs index e97dfde58..85922fa07 100644 --- a/eth2/state_processing/benches/epoch_processing_benches.rs +++ b/eth2/state_processing/benches/epoch_processing_benches.rs @@ -1,4 +1,3 @@ -use benching_utils::BeaconStateBencher; use criterion::Criterion; use criterion::{black_box, Benchmark}; use ssz::TreeHash; @@ -11,6 +10,7 @@ use state_processing::{ update_latest_slashed_balances, }, }; +use types::test_utils::TestingBeaconStateBuilder; use types::{validator_registry::get_active_validator_indices, *}; pub const BENCHING_SAMPLE_SIZE: usize = 10; @@ -22,7 +22,7 @@ pub fn epoch_processing_16k_validators(c: &mut Criterion) { let validator_count = 300_032; - let mut builder = BeaconStateBencher::new(validator_count, &spec); + let mut builder = TestingBeaconStateBuilder::new(validator_count, &spec); // Set the state to be just before an epoch transition. let target_slot = (spec.genesis_epoch + 4).end_slot(spec.slots_per_epoch); diff --git a/eth2/state_processing/benching_utils/Cargo.toml b/eth2/state_processing/benching_utils/Cargo.toml deleted file mode 100644 index 00815406a..000000000 --- a/eth2/state_processing/benching_utils/Cargo.toml +++ /dev/null @@ -1,17 +0,0 @@ -[package] -name = "benching_utils" -version = "0.1.0" -authors = ["Paul Hauner "] -edition = "2018" - -[dependencies] -bls = { path = "../../utils/bls" } -hashing = { path = "../../utils/hashing" } -int_to_bytes = { path = "../../utils/int_to_bytes" } -integer-sqrt = "0.1" -log = "0.4" -merkle_proof = { path = "../../utils/merkle_proof" } -ssz = { path = "../../utils/ssz" } -ssz_derive = { path = "../../utils/ssz_derive" } -types = { path = "../../types" } -rayon = "1.0" diff --git a/eth2/state_processing/benching_utils/src/lib.rs b/eth2/state_processing/benching_utils/src/lib.rs deleted file mode 100644 index ba9548814..000000000 --- a/eth2/state_processing/benching_utils/src/lib.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod beacon_block_bencher; -mod beacon_state_bencher; - -pub use beacon_block_bencher::BeaconBlockBencher; -pub use beacon_state_bencher::BeaconStateBencher; diff --git a/eth2/state_processing/src/per_epoch_processing/tests.rs b/eth2/state_processing/src/per_epoch_processing/tests.rs index f3c68a173..18c888e78 100644 --- a/eth2/state_processing/src/per_epoch_processing/tests.rs +++ b/eth2/state_processing/src/per_epoch_processing/tests.rs @@ -1,7 +1,7 @@ #![cfg(test)] use crate::per_epoch_processing; -use benching_utils::BeaconStateBencher; use env_logger::{Builder, Env}; +use types::test_utils::TestingBeaconStateBuilder; use types::*; #[test] @@ -10,7 +10,7 @@ fn runs_without_error() { let spec = ChainSpec::few_validators(); - let mut builder = BeaconStateBencher::new(8, &spec); + let mut builder = TestingBeaconStateBuilder::new(8, &spec); let target_slot = (spec.genesis_epoch + 4).end_slot(spec.slots_per_epoch); builder.teleport_to_slot(target_slot, &spec); diff --git a/eth2/types/src/beacon_state/builder.rs b/eth2/types/src/beacon_state/builder.rs index 372f0d43d..c36cd11f4 100644 --- a/eth2/types/src/beacon_state/builder.rs +++ b/eth2/types/src/beacon_state/builder.rs @@ -6,7 +6,7 @@ use ssz::TreeHash; /// Builds a `BeaconState` for use in production. /// -/// This struct should not be modified for use in testing scenarios. Use `TestingBeaconStateBuilder` for that purpose. +/// This struct should _not_ be modified for use in testing scenarios. Use `TestingBeaconStateBuilder` for that purpose. /// /// This struct should remain safe and sensible for production usage. pub struct BeaconStateBuilder { diff --git a/eth2/types/src/beacon_state/tests.rs b/eth2/types/src/beacon_state/tests.rs index 40bfd146c..fc55520bb 100644 --- a/eth2/types/src/beacon_state/tests.rs +++ b/eth2/types/src/beacon_state/tests.rs @@ -1,29 +1,20 @@ #![cfg(test)] use super::*; +use crate::test_utils::TestingBeaconStateBuilder; use crate::test_utils::{SeedableRng, TestRandom, XorShiftRng}; use crate::{BeaconState, ChainSpec}; use ssz::{ssz_encode, Decodable}; -#[test] -pub fn can_produce_genesis_block() { - let mut builder = BeaconStateBuilder::new(2); - builder.build().unwrap(); -} - /// Tests that `get_attestation_participants` is consistent with the result of /// get_crosslink_committees_at_slot` with a full bitfield. #[test] pub fn get_attestation_participants_consistency() { let mut rng = XorShiftRng::from_seed([42; 16]); - let mut builder = BeaconStateBuilder::new(8); - builder.spec = ChainSpec::few_validators(); - - builder.build().unwrap(); - - let mut state = builder.cloned_state(); - let spec = builder.spec.clone(); + let spec = ChainSpec::few_validators(); + let builder = TestingBeaconStateBuilder::new(8, &spec); + let (mut state, _keypairs) = builder.build(); state .build_epoch_cache(RelativeEpoch::Previous, &spec) diff --git a/eth2/types/src/test_utils/mod.rs b/eth2/types/src/test_utils/mod.rs index 2145f684a..01d966841 100644 --- a/eth2/types/src/test_utils/mod.rs +++ b/eth2/types/src/test_utils/mod.rs @@ -1,5 +1,7 @@ mod test_random; mod testing_attestation_builder; +mod testing_beacon_block_builder; +mod testing_beacon_state_builder; mod testing_deposit_builder; mod testing_transfer_builder; mod testing_voluntary_exit_builder; @@ -7,6 +9,8 @@ mod testing_voluntary_exit_builder; pub use rand::{prng::XorShiftRng, SeedableRng}; pub use test_random::TestRandom; pub use testing_attestation_builder::TestingAttestationBuilder; +pub use testing_beacon_block_builder::TestingBeaconBlockBuilder; +pub use testing_beacon_state_builder::TestingBeaconStateBuilder; pub use testing_deposit_builder::TestingDepositBuilder; pub use testing_transfer_builder::TestingTransferBuilder; pub use testing_voluntary_exit_builder::TestingVoluntaryExitBuilder; diff --git a/eth2/state_processing/benching_utils/src/beacon_block_bencher.rs b/eth2/types/src/test_utils/testing_beacon_block_builder.rs similarity index 99% rename from eth2/state_processing/benching_utils/src/beacon_block_bencher.rs rename to eth2/types/src/test_utils/testing_beacon_block_builder.rs index 46e822baa..db4d887d4 100644 --- a/eth2/state_processing/benching_utils/src/beacon_block_bencher.rs +++ b/eth2/types/src/test_utils/testing_beacon_block_builder.rs @@ -1,6 +1,4 @@ -use rayon::prelude::*; -use ssz::{SignedRoot, TreeHash}; -use types::{ +use crate::{ attester_slashing::AttesterSlashingBuilder, proposer_slashing::ProposerSlashingBuilder, test_utils::{ @@ -9,12 +7,14 @@ use types::{ }, *, }; +use rayon::prelude::*; +use ssz::{SignedRoot, TreeHash}; -pub struct BeaconBlockBencher { +pub struct TestingBeaconBlockBuilder { block: BeaconBlock, } -impl BeaconBlockBencher { +impl TestingBeaconBlockBuilder { pub fn new(spec: &ChainSpec) -> Self { Self { block: BeaconBlock::genesis(spec.zero_hash, spec), diff --git a/eth2/state_processing/benching_utils/src/beacon_state_bencher.rs b/eth2/types/src/test_utils/testing_beacon_state_builder.rs similarity index 98% rename from eth2/state_processing/benching_utils/src/beacon_state_bencher.rs rename to eth2/types/src/test_utils/testing_beacon_state_builder.rs index 8ad4810e7..b3cfea6c0 100644 --- a/eth2/state_processing/benching_utils/src/beacon_state_bencher.rs +++ b/eth2/types/src/test_utils/testing_beacon_state_builder.rs @@ -1,15 +1,15 @@ +use crate::beacon_state::BeaconStateBuilder; +use crate::*; use bls::get_withdrawal_credentials; use int_to_bytes::int_to_bytes48; use rayon::prelude::*; -use types::beacon_state::BeaconStateBuilder; -use types::*; -pub struct BeaconStateBencher { +pub struct TestingBeaconStateBuilder { state: BeaconState, keypairs: Vec, } -impl BeaconStateBencher { +impl TestingBeaconStateBuilder { pub fn new(validator_count: usize, spec: &ChainSpec) -> Self { let keypairs: Vec = (0..validator_count) .collect::>()