diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 1fc29f8e4..42022c89f 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -13,7 +13,7 @@ use test_random_derive::TestRandom; use tree_hash::TreeHash; use tree_hash_derive::{CachedTreeHash, TreeHash}; -pub use beacon_state_types::{BeaconStateTypes, FewValidatorsBeaconState, FoundationBeaconState}; +pub use beacon_state_types::*; pub mod beacon_state_types; mod epoch_cache; diff --git a/eth2/types/src/beacon_state/beacon_state_types.rs b/eth2/types/src/beacon_state/beacon_state_types.rs index 41f9f44c8..a77b4d4f1 100644 --- a/eth2/types/src/beacon_state/beacon_state_types.rs +++ b/eth2/types/src/beacon_state/beacon_state_types.rs @@ -7,30 +7,40 @@ pub trait BeaconStateTypes { type LatestRandaoMixesLength: Unsigned + Clone + Sync + Send; type LatestActiveIndexRootsLength: Unsigned + Clone + Sync + Send; type LatestSlashedExitLength: Unsigned + Clone + Sync + Send; + + fn spec() -> ChainSpec; } #[derive(Clone, PartialEq, Debug)] -pub struct FoundationStateParams; +pub struct FoundationStateTypes; -impl BeaconStateTypes for FoundationStateParams { +impl BeaconStateTypes for FoundationStateTypes { type ShardCount = U1024; type SlotsPerHistoricalRoot = U8192; type LatestRandaoMixesLength = U8192; type LatestActiveIndexRootsLength = U8192; type LatestSlashedExitLength = U8192; + + fn spec() -> ChainSpec { + ChainSpec::foundation() + } } -pub type FoundationBeaconState = BeaconState; +pub type FoundationBeaconState = BeaconState; #[derive(Clone, PartialEq, Debug)] -pub struct FewValidatorsStateParams; +pub struct FewValidatorsStateTypes; -impl BeaconStateTypes for FewValidatorsStateParams { +impl BeaconStateTypes for FewValidatorsStateTypes { type ShardCount = U8; type SlotsPerHistoricalRoot = U8192; type LatestRandaoMixesLength = U8192; type LatestActiveIndexRootsLength = U8192; type LatestSlashedExitLength = U8192; + + fn spec() -> ChainSpec { + ChainSpec::few_validators() + } } -pub type FewValidatorsBeaconState = BeaconState; +pub type FewValidatorsBeaconState = BeaconState; diff --git a/eth2/types/src/beacon_state/epoch_cache/tests.rs b/eth2/types/src/beacon_state/epoch_cache/tests.rs index 182817bf6..6ba7c9086 100644 --- a/eth2/types/src/beacon_state/epoch_cache/tests.rs +++ b/eth2/types/src/beacon_state/epoch_cache/tests.rs @@ -1,7 +1,7 @@ #![cfg(test)] use super::*; -use crate::beacon_state::FewValidatorsBeaconState; +use crate::beacon_state::FewValidatorsStateTypes; use crate::test_utils::*; use swap_or_not_shuffle::shuffle_list; @@ -102,10 +102,13 @@ fn setup_sane_cache_test( #[test] fn builds_sane_current_epoch_cache() { - let mut spec = ChainSpec::few_validators(); + let mut spec = FewValidatorsStateTypes::spec(); spec.shard_count = 4; let validator_count = (spec.shard_count * spec.target_committee_size) + 1; - let state: FewValidatorsBeaconState = setup_sane_cache_test(validator_count as usize, &spec); + + let state: BeaconState = + setup_sane_cache_test(validator_count as usize, &spec); + do_sane_cache_test( state.clone(), state.current_epoch(&spec), @@ -119,10 +122,13 @@ fn builds_sane_current_epoch_cache() { #[test] fn builds_sane_previous_epoch_cache() { - let mut spec = ChainSpec::few_validators(); + let mut spec = FewValidatorsStateTypes::spec(); spec.shard_count = 2; let validator_count = (spec.shard_count * spec.target_committee_size) + 1; - let state: FewValidatorsBeaconState = setup_sane_cache_test(validator_count as usize, &spec); + + let state: BeaconState = + setup_sane_cache_test(validator_count as usize, &spec); + do_sane_cache_test( state.clone(), state.previous_epoch(&spec), @@ -136,11 +142,13 @@ fn builds_sane_previous_epoch_cache() { #[test] fn builds_sane_next_without_update_epoch_cache() { - let mut spec = ChainSpec::few_validators(); + let mut spec = FewValidatorsStateTypes::spec(); spec.shard_count = 2; let validator_count = (spec.shard_count * spec.target_committee_size) + 1; - let mut state: FewValidatorsBeaconState = + + let mut state: BeaconState = setup_sane_cache_test(validator_count as usize, &spec); + state.validator_registry_update_epoch = state.slot.epoch(spec.slots_per_epoch); do_sane_cache_test( state.clone(), diff --git a/eth2/types/src/beacon_state/tests.rs b/eth2/types/src/beacon_state/tests.rs index b840663f0..8948a94f6 100644 --- a/eth2/types/src/beacon_state/tests.rs +++ b/eth2/types/src/beacon_state/tests.rs @@ -1,6 +1,6 @@ #![cfg(test)] use super::*; -use crate::beacon_state::{FewValidatorsBeaconState, FoundationBeaconState}; +use crate::beacon_state::FewValidatorsStateTypes; use crate::test_utils::*; ssz_tests!(FoundationBeaconState); @@ -46,9 +46,11 @@ fn test_cache_initialization<'a, T: BeaconStateTypes>( #[test] fn cache_initialization() { - let spec = ChainSpec::few_validators(); - let (mut state, _keypairs): (FewValidatorsBeaconState, Vec) = - TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(16, &spec).build(); + let spec = FewValidatorsStateTypes::spec(); + + let builder: TestingBeaconStateBuilder = + TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(16, &spec); + let (mut state, _keypairs) = builder.build(); state.slot = (spec.genesis_epoch + 1).start_slot(spec.slots_per_epoch); diff --git a/eth2/types/src/chain_spec.rs b/eth2/types/src/chain_spec.rs index 32f36a64d..974bcfc4a 100644 --- a/eth2/types/src/chain_spec.rs +++ b/eth2/types/src/chain_spec.rs @@ -154,7 +154,7 @@ impl ChainSpec { /// Returns a `ChainSpec` compatible with the Ethereum Foundation specification. /// /// Spec v0.5.1 - pub fn foundation() -> Self { + pub(crate) fn foundation() -> Self { let genesis_slot = Slot::new(2_u64.pow(32)); let slots_per_epoch = 64; let genesis_epoch = genesis_slot.epoch(slots_per_epoch); @@ -248,7 +248,7 @@ impl ChainSpec { /// Returns a `ChainSpec` compatible with the Lighthouse testnet specification. /// /// Spec v0.4.0 - pub fn lighthouse_testnet() -> Self { + pub(crate) fn lighthouse_testnet() -> Self { /* * Lighthouse testnet bootnodes */ @@ -264,7 +264,7 @@ impl ChainSpec { } /// Returns a `ChainSpec` compatible with the specification suitable for 8 validators. - pub fn few_validators() -> Self { + pub(crate) fn few_validators() -> Self { let genesis_slot = Slot::new(2_u64.pow(32)); let slots_per_epoch = 8; let genesis_epoch = genesis_slot.epoch(slots_per_epoch); diff --git a/eth2/types/src/historical_batch.rs b/eth2/types/src/historical_batch.rs index fef6e2715..f9886b8c8 100644 --- a/eth2/types/src/historical_batch.rs +++ b/eth2/types/src/historical_batch.rs @@ -31,9 +31,9 @@ pub struct HistoricalBatch { #[cfg(test)] mod tests { use super::*; - use crate::beacon_state::beacon_state_types::FoundationStateParams; + use crate::beacon_state::beacon_state_types::FoundationStateTypes; - pub type FoundationHistoricalBatch = HistoricalBatch; + pub type FoundationHistoricalBatch = HistoricalBatch; ssz_tests!(FoundationHistoricalBatch); cached_tree_hash_tests!(FoundationHistoricalBatch);