From caddeba81b7aa8e88eec3c47c0a5fc8ddd243eb3 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Sat, 8 Jun 2019 08:17:42 -0400 Subject: [PATCH] Remove `genesis_epoch` from ChainSpec --- beacon_node/client/src/client_config.rs | 2 +- eth2/operation_pool/src/lib.rs | 2 +- .../benches/bench_epoch_processing.rs | 2 +- eth2/state_processing/benches/benches.rs | 4 ++-- eth2/state_processing/src/get_genesis_state.rs | 4 ++-- .../src/per_block_processing/tests.rs | 2 +- eth2/state_processing/src/per_epoch_processing.rs | 2 +- .../src/per_epoch_processing/apply_rewards.rs | 2 +- .../src/per_epoch_processing/tests.rs | 3 ++- eth2/types/src/beacon_state.rs | 10 +++++----- eth2/types/src/beacon_state/beacon_state_types.rs | 4 ++-- eth2/types/src/beacon_state/committee_cache/tests.rs | 2 +- eth2/types/src/beacon_state/tests.rs | 7 ++++--- eth2/types/src/chain_spec.rs | 12 ++++-------- eth2/types/src/fork.rs | 10 ++++------ .../builders/testing_beacon_state_builder.rs | 4 ++-- 16 files changed, 34 insertions(+), 38 deletions(-) diff --git a/beacon_node/client/src/client_config.rs b/beacon_node/client/src/client_config.rs index a729531ad..6c42f1ea6 100644 --- a/beacon_node/client/src/client_config.rs +++ b/beacon_node/client/src/client_config.rs @@ -31,7 +31,7 @@ impl Default for ClientConfig { network: NetworkConfig::new(vec![]), rpc: rpc::RPCConfig::default(), http: HttpServerConfig::default(), - spec: ChainSpec::lighthouse_testnet(8), + spec: ChainSpec::lighthouse_testnet(), } } } diff --git a/eth2/operation_pool/src/lib.rs b/eth2/operation_pool/src/lib.rs index 0cd46e018..ae8a6ae11 100644 --- a/eth2/operation_pool/src/lib.rs +++ b/eth2/operation_pool/src/lib.rs @@ -680,7 +680,7 @@ mod tests { let mut state = BeaconState::random_for_test(rng); - state.fork = Fork::genesis(&spec); + state.fork = Fork::genesis(FoundationEthSpec::genesis_epoch()); (spec, state) } diff --git a/eth2/state_processing/benches/bench_epoch_processing.rs b/eth2/state_processing/benches/bench_epoch_processing.rs index 23e662e09..665aabbff 100644 --- a/eth2/state_processing/benches/bench_epoch_processing.rs +++ b/eth2/state_processing/benches/bench_epoch_processing.rs @@ -23,7 +23,7 @@ pub fn bench_epoch_processing_n_validators(c: &mut Criterion, validator_count: u TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(validator_count, &spec); // Set the state to be just before an epoch transition. - let target_slot = (spec.genesis_epoch + 4).end_slot(T::slots_per_epoch()); + let target_slot = (T::genesis_epoch() + 4).end_slot(T::slots_per_epoch()); builder.teleport_to_slot(target_slot, &spec); // Builds all caches; benches will not contain shuffling/committee building times. diff --git a/eth2/state_processing/benches/benches.rs b/eth2/state_processing/benches/benches.rs index 1a153abee..656ad9e4e 100644 --- a/eth2/state_processing/benches/benches.rs +++ b/eth2/state_processing/benches/benches.rs @@ -34,7 +34,7 @@ pub fn block_processing_worst_case(c: &mut Criterion) { bench_builder.maximize_block_operations(&spec); // Set the state and block to be in the last slot of the 4th epoch. - let last_slot_of_epoch = (spec.genesis_epoch + 4).end_slot(T::slots_per_epoch()); + let last_slot_of_epoch = (T::genesis_epoch() + 4).end_slot(T::slots_per_epoch()); bench_builder.set_slot(last_slot_of_epoch, &spec); // Build all the state caches so the build times aren't included in the benches. @@ -73,7 +73,7 @@ pub fn block_processing_reasonable_case(c: &mut Criterion) { bench_builder.num_transfers = 2; // Set the state and block to be in the last slot of the 4th epoch. - let last_slot_of_epoch = (spec.genesis_epoch + 4).end_slot(T::slots_per_epoch()); + let last_slot_of_epoch = (T::genesis_epoch() + 4).end_slot(T::slots_per_epoch()); bench_builder.set_slot(last_slot_of_epoch, &spec); // Build all the state caches so the build times aren't included in the benches. diff --git a/eth2/state_processing/src/get_genesis_state.rs b/eth2/state_processing/src/get_genesis_state.rs index 0fe78c1ed..18a1e7c35 100644 --- a/eth2/state_processing/src/get_genesis_state.rs +++ b/eth2/state_processing/src/get_genesis_state.rs @@ -25,8 +25,8 @@ pub fn get_genesis_beacon_state( // Process genesis activations. for validator in &mut state.validator_registry { if validator.effective_balance >= spec.max_effective_balance { - validator.activation_eligibility_epoch = spec.genesis_epoch; - validator.activation_epoch = spec.genesis_epoch; + validator.activation_eligibility_epoch = T::genesis_epoch(); + validator.activation_epoch = T::genesis_epoch(); } } diff --git a/eth2/state_processing/src/per_block_processing/tests.rs b/eth2/state_processing/src/per_block_processing/tests.rs index fe30560c6..c2a9840aa 100644 --- a/eth2/state_processing/src/per_block_processing/tests.rs +++ b/eth2/state_processing/src/per_block_processing/tests.rs @@ -105,7 +105,7 @@ fn get_builder(spec: &ChainSpec) -> (BlockProcessingBuilder) // Set the state and block to be in the last slot of the 4th epoch. let last_slot_of_epoch = - (spec.genesis_epoch + 4).end_slot(FoundationEthSpec::slots_per_epoch()); + (FoundationEthSpec::genesis_epoch() + 4).end_slot(FoundationEthSpec::slots_per_epoch()); builder.set_slot(last_slot_of_epoch, &spec); builder.build_caches(&spec); diff --git a/eth2/state_processing/src/per_epoch_processing.rs b/eth2/state_processing/src/per_epoch_processing.rs index 290c33804..2d76577e1 100644 --- a/eth2/state_processing/src/per_epoch_processing.rs +++ b/eth2/state_processing/src/per_epoch_processing.rs @@ -86,7 +86,7 @@ pub fn process_justification_and_finalization( total_balances: &TotalBalances, spec: &ChainSpec, ) -> Result<(), Error> { - if state.current_epoch() == spec.genesis_epoch { + if state.current_epoch() == T::genesis_epoch() { return Ok(()); } diff --git a/eth2/state_processing/src/per_epoch_processing/apply_rewards.rs b/eth2/state_processing/src/per_epoch_processing/apply_rewards.rs index 7f98f3ae5..7ddba6f38 100644 --- a/eth2/state_processing/src/per_epoch_processing/apply_rewards.rs +++ b/eth2/state_processing/src/per_epoch_processing/apply_rewards.rs @@ -39,7 +39,7 @@ pub fn process_rewards_and_penalties( winning_root_for_shards: &WinningRootHashSet, spec: &ChainSpec, ) -> Result<(), Error> { - if state.current_epoch() == spec.genesis_epoch { + if state.current_epoch() == T::genesis_epoch() { return Ok(()); } diff --git a/eth2/state_processing/src/per_epoch_processing/tests.rs b/eth2/state_processing/src/per_epoch_processing/tests.rs index 0e5a81cce..dffe2b593 100644 --- a/eth2/state_processing/src/per_epoch_processing/tests.rs +++ b/eth2/state_processing/src/per_epoch_processing/tests.rs @@ -13,7 +13,8 @@ fn runs_without_error() { let mut builder: TestingBeaconStateBuilder = TestingBeaconStateBuilder::from_deterministic_keypairs(8, &spec); - let target_slot = (spec.genesis_epoch + 4).end_slot(FewValidatorsEthSpec::slots_per_epoch()); + let target_slot = (FewValidatorsEthSpec::genesis_epoch() + 4) + .end_slot(FewValidatorsEthSpec::slots_per_epoch()); builder.teleport_to_slot(target_slot, &spec); let (mut state, _keypairs) = builder.build(); diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 27a2e7a0e..1c1fca6fb 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -162,7 +162,7 @@ impl BeaconState { spec: &ChainSpec, ) -> BeaconState { let initial_crosslink = Crosslink { - epoch: spec.genesis_epoch, + epoch: T::genesis_epoch(), previous_crosslink_root: spec.zero_hash, crosslink_data_root: spec.zero_hash, }; @@ -171,7 +171,7 @@ impl BeaconState { // Misc slot: spec.genesis_slot, genesis_time, - fork: Fork::genesis(spec), + fork: Fork::genesis(T::genesis_epoch()), // Validator registry validator_registry: vec![], // Set later in the function. @@ -187,12 +187,12 @@ impl BeaconState { // Finality previous_epoch_attestations: vec![], current_epoch_attestations: vec![], - previous_justified_epoch: spec.genesis_epoch, - current_justified_epoch: spec.genesis_epoch, + previous_justified_epoch: T::genesis_epoch(), + current_justified_epoch: T::genesis_epoch(), previous_justified_root: spec.zero_hash, current_justified_root: spec.zero_hash, justification_bitfield: 0, - finalized_epoch: spec.genesis_epoch, + finalized_epoch: T::genesis_epoch(), finalized_root: spec.zero_hash, // Recent state diff --git a/eth2/types/src/beacon_state/beacon_state_types.rs b/eth2/types/src/beacon_state/beacon_state_types.rs index fce0c6dff..6d0dd4768 100644 --- a/eth2/types/src/beacon_state/beacon_state_types.rs +++ b/eth2/types/src/beacon_state/beacon_state_types.rs @@ -127,7 +127,7 @@ impl EthSpec for FewValidatorsEthSpec { type GenesisEpoch = U0; fn default_spec() -> ChainSpec { - ChainSpec::few_validators(Self::slots_per_epoch()) + ChainSpec::few_validators() } } @@ -147,7 +147,7 @@ impl EthSpec for LighthouseTestnetEthSpec { type GenesisEpoch = U0; fn default_spec() -> ChainSpec { - ChainSpec::lighthouse_testnet(Self::slots_per_epoch()) + ChainSpec::lighthouse_testnet() } } diff --git a/eth2/types/src/beacon_state/committee_cache/tests.rs b/eth2/types/src/beacon_state/committee_cache/tests.rs index e9b417e3d..8f1fbe187 100644 --- a/eth2/types/src/beacon_state/committee_cache/tests.rs +++ b/eth2/types/src/beacon_state/committee_cache/tests.rs @@ -154,7 +154,7 @@ impl EthSpec for ExcessShardsEthSpec { type GenesisEpoch = U0; fn default_spec() -> ChainSpec { - ChainSpec::few_validators(Self::slots_per_epoch()) + ChainSpec::few_validators() } } diff --git a/eth2/types/src/beacon_state/tests.rs b/eth2/types/src/beacon_state/tests.rs index 581b9662e..a668f99fe 100644 --- a/eth2/types/src/beacon_state/tests.rs +++ b/eth2/types/src/beacon_state/tests.rs @@ -172,7 +172,8 @@ fn cache_initialization() { TestingBeaconStateBuilder::from_default_keypairs_file_if_exists(16, &spec); let (mut state, _keypairs) = builder.build(); - state.slot = (spec.genesis_epoch + 1).start_slot(FewValidatorsEthSpec::slots_per_epoch()); + state.slot = (FewValidatorsEthSpec::genesis_epoch() + 1) + .start_slot(FewValidatorsEthSpec::slots_per_epoch()); test_cache_initialization(&mut state, RelativeEpoch::Previous, &spec); test_cache_initialization(&mut state, RelativeEpoch::Current, &spec); @@ -333,13 +334,13 @@ mod committees { committee_consistency_test::( validator_count as usize, - spec.genesis_epoch + 4, + T::genesis_epoch() + 4, cached_epoch, ); committee_consistency_test::( validator_count as usize, - spec.genesis_epoch + T::slots_per_historical_root() as u64 * T::slots_per_epoch() * 4, + T::genesis_epoch() + T::slots_per_historical_root() as u64 * T::slots_per_epoch() * 4, cached_epoch, ); } diff --git a/eth2/types/src/chain_spec.rs b/eth2/types/src/chain_spec.rs index d9a0785a1..a1267e37d 100644 --- a/eth2/types/src/chain_spec.rs +++ b/eth2/types/src/chain_spec.rs @@ -48,7 +48,6 @@ pub struct ChainSpec { * Initial Values */ pub genesis_slot: Slot, - pub genesis_epoch: Epoch, pub far_future_epoch: Epoch, pub zero_hash: Hash256, #[serde(deserialize_with = "u8_from_hex_str")] @@ -166,7 +165,6 @@ impl ChainSpec { * Initial Values */ genesis_slot: Slot::new(0), - genesis_epoch: Epoch::new(0), far_future_epoch: Epoch::new(u64::max_value()), zero_hash: Hash256::zero(), bls_withdrawal_prefix_byte: 0, @@ -226,7 +224,7 @@ impl ChainSpec { /// Returns a `ChainSpec` compatible with the Lighthouse testnet specification. /// /// Spec v0.4.0 - pub fn lighthouse_testnet(slots_per_epoch: u64) -> Self { + pub fn lighthouse_testnet() -> Self { /* * Lighthouse testnet bootnodes */ @@ -237,19 +235,17 @@ impl ChainSpec { Self { boot_nodes, chain_id: 2, // lighthouse testnet chain id - ..ChainSpec::few_validators(slots_per_epoch) + ..ChainSpec::few_validators() } } /// Returns a `ChainSpec` compatible with the specification suitable for 8 validators. - pub fn few_validators(slots_per_epoch: u64) -> Self { + pub fn few_validators() -> Self { let genesis_slot = Slot::new(0); - let genesis_epoch = genesis_slot.epoch(slots_per_epoch); Self { target_committee_size: 1, genesis_slot, - genesis_epoch, ..ChainSpec::foundation() } } @@ -272,7 +268,7 @@ mod tests { } fn test_domain(domain_type: Domain, raw_domain: u32, spec: &ChainSpec) { - let fork = Fork::genesis(&spec); + let fork = Fork::genesis(Epoch::new(0)); let epoch = Epoch::new(0); let domain = spec.get_domain(epoch, domain_type, &fork); diff --git a/eth2/types/src/fork.rs b/eth2/types/src/fork.rs index eb4e183f2..ec5f3af4c 100644 --- a/eth2/types/src/fork.rs +++ b/eth2/types/src/fork.rs @@ -36,11 +36,11 @@ impl Fork { /// Initialize the `Fork` from the genesis parameters in the `spec`. /// /// Spec v0.6.1 - pub fn genesis(spec: &ChainSpec) -> Self { + pub fn genesis(genesis_epoch: Epoch) -> Self { Self { previous_version: [0; 4], current_version: [0; 4], - epoch: spec.genesis_epoch, + epoch: genesis_epoch, } } @@ -65,11 +65,9 @@ mod tests { fn test_genesis(epoch: Epoch) { let mut spec = ChainSpec::foundation(); - spec.genesis_epoch = epoch; + let fork = Fork::genesis(epoch); - let fork = Fork::genesis(&spec); - - assert_eq!(fork.epoch, spec.genesis_epoch, "epoch incorrect"); + assert_eq!(fork.epoch, epoch, "epoch incorrect"); assert_eq!( fork.previous_version, fork.current_version, "previous and current are not identical" diff --git a/eth2/types/src/test_utils/builders/testing_beacon_state_builder.rs b/eth2/types/src/test_utils/builders/testing_beacon_state_builder.rs index dc2dd0a7c..f9a4ea00c 100644 --- a/eth2/types/src/test_utils/builders/testing_beacon_state_builder.rs +++ b/eth2/types/src/test_utils/builders/testing_beacon_state_builder.rs @@ -113,8 +113,8 @@ impl TestingBeaconStateBuilder { pubkey: keypair.pk.clone(), withdrawal_credentials, // All validators start active. - activation_eligibility_epoch: spec.genesis_epoch, - activation_epoch: spec.genesis_epoch, + activation_eligibility_epoch: T::genesis_epoch(), + activation_epoch: T::genesis_epoch(), exit_epoch: spec.far_future_epoch, withdrawable_epoch: spec.far_future_epoch, slashed: false,