From c214bec3446d0a5f8aa7d980d125d8abdff1e441 Mon Sep 17 00:00:00 2001 From: Paul Hauner Date: Thu, 23 May 2019 16:52:51 +1000 Subject: [PATCH] Add ability to get shuffling from `BeaconState` --- eth2/types/src/beacon_state.rs | 11 +++++++++++ eth2/types/src/beacon_state/committee_cache.rs | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index 00b756c1a0..be085c655d 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -343,6 +343,17 @@ impl BeaconState { get_active_validator_indices(&self.validator_registry, epoch) } + /// Return the cached active validator indices at some epoch. + /// + /// Note: the indices are shuffled (i.e., not in ascending order). + /// + /// Returns an error if that epoch is not cached, or the cache is not initialized. + pub fn get_shuffling(&self, relative_epoch: RelativeEpoch) -> Result<&[usize], Error> { + let cache = self.cache(relative_epoch)?; + + Ok(cache.shuffling()) + } + /// Returns the crosslink committees for some slot. /// /// Note: Utilizes the cache and will fail if the appropriate cache is not initialized. diff --git a/eth2/types/src/beacon_state/committee_cache.rs b/eth2/types/src/beacon_state/committee_cache.rs index afefaa6ba9..27374c3391 100644 --- a/eth2/types/src/beacon_state/committee_cache.rs +++ b/eth2/types/src/beacon_state/committee_cache.rs @@ -114,6 +114,15 @@ impl CommitteeCache { &self.shuffling } + /// Returns the shuffled list of active validator indices for the initialized epoch. + /// + /// Always returns `&[]` for a non-initialized epoch. + /// + /// Spec v0.6.1 + pub fn shuffling(&self) -> &[usize] { + &self.shuffling + } + /// Return `Some(CrosslinkCommittee)` if the given shard has a committee during the given /// `epoch`. ///