From 2fd45e093ca066326de6a7e2ec09991274d6eadc Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Mon, 20 May 2019 14:00:47 +1000 Subject: [PATCH] Remove shard_count from ChainSpec --- .../src/beacon_state/beacon_state_types.rs | 17 +++++++++++++++++ eth2/types/src/beacon_state/epoch_cache.rs | 9 ++++----- eth2/types/src/beacon_state/tests.rs | 2 +- eth2/types/src/chain_spec.rs | 18 +----------------- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/eth2/types/src/beacon_state/beacon_state_types.rs b/eth2/types/src/beacon_state/beacon_state_types.rs index e76ea9253..07eda2435 100644 --- a/eth2/types/src/beacon_state/beacon_state_types.rs +++ b/eth2/types/src/beacon_state/beacon_state_types.rs @@ -14,6 +14,23 @@ pub trait EthSpec: fn spec() -> ChainSpec; + /// Return the number of committees in one epoch. + /// + /// Spec v0.6.1 + fn get_epoch_committee_count(active_validator_count: usize) -> usize { + let target_committee_size = Self::spec().target_committee_size; + let shard_count = Self::shard_count(); + let slots_per_epoch = Self::slots_per_epoch() as usize; + + std::cmp::max( + 1, + std::cmp::min( + shard_count / slots_per_epoch, + active_validator_count / slots_per_epoch / target_committee_size, + ), + ) * slots_per_epoch + } + /// Returns the minimum number of validators required for this spec. /// /// This is the _absolute_ minimum, the number required to make the chain operate in the most diff --git a/eth2/types/src/beacon_state/epoch_cache.rs b/eth2/types/src/beacon_state/epoch_cache.rs index 8e1593f14..3c4cb6c05 100644 --- a/eth2/types/src/beacon_state/epoch_cache.rs +++ b/eth2/types/src/beacon_state/epoch_cache.rs @@ -43,14 +43,13 @@ impl EpochCache { return Err(BeaconStateError::InsufficientValidators); } - let committee_count = - spec.get_epoch_committee_count(active_validator_indices.len()) as usize; + let committee_count = T::get_epoch_committee_count(active_validator_indices.len()) as usize; let shuffling_start_shard = match relative_epoch { RelativeEpoch::Current => state.latest_start_shard, RelativeEpoch::Previous => { let committees_in_previous_epoch = - spec.get_epoch_committee_count(active_validator_indices.len()); + T::get_epoch_committee_count(active_validator_indices.len()) as u64; (state.latest_start_shard + T::shard_count() as u64 - committees_in_previous_epoch) % T::shard_count() as u64 @@ -59,9 +58,9 @@ impl EpochCache { let current_active_validators = get_active_validator_count(&state.validator_registry, state.current_epoch()); let committees_in_current_epoch = - spec.get_epoch_committee_count(current_active_validators); + T::get_epoch_committee_count(current_active_validators) as u64; - (state.latest_start_shard + committees_in_current_epoch) % T::shard_count as u64 + (state.latest_start_shard + committees_in_current_epoch) % T::shard_count() as u64 } }; diff --git a/eth2/types/src/beacon_state/tests.rs b/eth2/types/src/beacon_state/tests.rs index 93391fc2b..753f8bf8e 100644 --- a/eth2/types/src/beacon_state/tests.rs +++ b/eth2/types/src/beacon_state/tests.rs @@ -266,7 +266,7 @@ mod committees { fn committee_consistency_test_suite(cached_epoch: RelativeEpoch) { let spec = T::spec(); - let validator_count = (spec.shard_count * spec.target_committee_size) + 1; + let validator_count = (T::shard_count() * spec.target_committee_size) + 1; committee_consistency_test::(validator_count as usize, Epoch::new(0), cached_epoch); diff --git a/eth2/types/src/chain_spec.rs b/eth2/types/src/chain_spec.rs index a2ee15d07..20aa6fcdb 100644 --- a/eth2/types/src/chain_spec.rs +++ b/eth2/types/src/chain_spec.rs @@ -24,8 +24,7 @@ pub struct ChainSpec { /* * Misc */ - pub shard_count: u64, - pub target_committee_size: u64, + pub target_committee_size: usize, pub max_indices_per_attestation: u64, pub min_per_epoch_churn_limit: u64, pub churn_limit_quotient: u64, @@ -113,19 +112,6 @@ pub struct ChainSpec { } impl ChainSpec { - /// Return the number of committees in one epoch. - /// - /// Spec v0.6.1 - pub fn get_epoch_committee_count(&self, active_validator_count: usize) -> u64 { - std::cmp::max( - 1, - std::cmp::min( - self.shard_count / self.slots_per_epoch, - active_validator_count as u64 / self.slots_per_epoch / self.target_committee_size, - ), - ) * self.slots_per_epoch - } - /// Get the domain number that represents the fork meta and signature domain. /// /// Spec v0.6.1 @@ -156,7 +142,6 @@ impl ChainSpec { /* * Misc */ - shard_count: 1_024, target_committee_size: 128, max_indices_per_attestation: 4096, min_per_epoch_churn_limit: 4, @@ -263,7 +248,6 @@ impl ChainSpec { let genesis_epoch = genesis_slot.epoch(slots_per_epoch); Self { - shard_count: 8, target_committee_size: 1, genesis_slot, genesis_epoch,