diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index d7dbda782..1a77d3449 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -1,13 +1,12 @@ -use self::epoch_cache::EpochCache; +use self::epoch_cache::{EpochCache, Error as EpochCacheError}; use crate::test_utils::TestRandom; use crate::*; use int_to_bytes::int_to_bytes32; use pubkey_cache::PubkeyCache; use rand::RngCore; use serde_derive::{Deserialize, Serialize}; -use ssz::{hash, ssz_encode, SignedRoot, TreeHash}; +use ssz::{hash, ssz_encode, TreeHash}; use ssz_derive::{Decode, Encode, TreeHash}; -use std::collections::HashMap; use test_random_derive::TestRandom; mod epoch_cache; @@ -44,6 +43,7 @@ pub enum Error { registry_len: usize, }, RelativeEpochError(RelativeEpochError), + EpochCacheError(EpochCacheError), } macro_rules! safe_add_assign { @@ -883,3 +883,9 @@ impl From for Error { Error::RelativeEpochError(e) } } + +impl From for Error { + fn from(e: EpochCacheError) -> Error { + Error::EpochCacheError(e) + } +} diff --git a/eth2/types/src/beacon_state/epoch_cache.rs b/eth2/types/src/beacon_state/epoch_cache.rs index 0dbdf4054..75d791e8f 100644 --- a/eth2/types/src/beacon_state/epoch_cache.rs +++ b/eth2/types/src/beacon_state/epoch_cache.rs @@ -1,9 +1,16 @@ -use super::{BeaconState, Error}; +use super::BeaconState; use crate::*; use honey_badger_split::SplitExt; use serde_derive::{Deserialize, Serialize}; use swap_or_not_shuffle::shuffle_list; +#[derive(Debug, PartialEq)] +pub enum Error { + UnableToShuffle, + NoValidators { epoch: Epoch }, + UnableToGenerateSeed, +} + mod tests; #[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)] @@ -212,7 +219,7 @@ impl EpochCrosslinkCommitteesBuilder { active_validator_indices: Vec, registry_change: bool, spec: &ChainSpec, - ) -> Result { + ) -> Result { let current_epoch = state.current_epoch(spec); let next_epoch = state.next_epoch(spec); let committees_per_epoch = spec.get_epoch_committee_count(active_validator_indices.len()); @@ -221,7 +228,9 @@ impl EpochCrosslinkCommitteesBuilder { current_epoch - state.validator_registry_update_epoch; let (seed, shuffling_start_shard) = if registry_change { - let next_seed = state.generate_seed(next_epoch, spec)?; + let next_seed = state + .generate_seed(next_epoch, spec) + .map_err(|_| Error::UnableToGenerateSeed)?; ( next_seed, (state.current_shuffling_start_shard + committees_per_epoch) % spec.shard_count, @@ -229,7 +238,9 @@ impl EpochCrosslinkCommitteesBuilder { } else if (epochs_since_last_registry_update > 1) & epochs_since_last_registry_update.is_power_of_two() { - let next_seed = state.generate_seed(next_epoch, spec)?; + let next_seed = state + .generate_seed(next_epoch, spec) + .map_err(|_| Error::UnableToGenerateSeed)?; (next_seed, state.current_shuffling_start_shard) } else { ( @@ -247,9 +258,9 @@ impl EpochCrosslinkCommitteesBuilder { }) } - pub fn build(self, spec: &ChainSpec) -> Result { + pub fn build(self, spec: &ChainSpec) -> Result { if self.active_validator_indices.is_empty() { - return Err(Error::NoValidators); + return Err(Error::NoValidators { epoch: self.epoch }); } let shuffled_active_validator_indices = shuffle_list(