diff --git a/beacon_node/beacon_chain/src/beacon_chain.rs b/beacon_node/beacon_chain/src/beacon_chain.rs index db5ea1cdb6..9a55851a49 100644 --- a/beacon_node/beacon_chain/src/beacon_chain.rs +++ b/beacon_node/beacon_chain/src/beacon_chain.rs @@ -332,8 +332,8 @@ where // If required, transition the new state to the present slot. for _ in state.slot.as_u64()..present_slot.as_u64() { // Ensure the next epoch state caches are built in case of an epoch transition. - state.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, &self.spec)?; - state.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, &self.spec)?; + state.build_committee_cache(RelativeEpoch::NextWithoutRegistryChange, &self.spec)?; + state.build_committee_cache(RelativeEpoch::NextWithRegistryChange, &self.spec)?; per_slot_processing(&mut *state, &self.spec)?; } @@ -439,7 +439,7 @@ where pub fn block_proposer(&self, slot: Slot) -> Result { self.state .write() - .build_epoch_cache(RelativeEpoch::Current, &self.spec)?; + .build_committee_cache(RelativeEpoch::Current, &self.spec)?; let index = self.state.read().get_beacon_proposer_index( slot, @@ -669,7 +669,7 @@ where let mut state = self.state.read().clone(); - state.build_epoch_cache(RelativeEpoch::Current, &self.spec)?; + state.build_committee_cache(RelativeEpoch::Current, &self.spec)?; trace!("Finding attestations for new block..."); diff --git a/eth2/state_processing/benches/bench_block_processing.rs b/eth2/state_processing/benches/bench_block_processing.rs index 80be1828ff..978d532f14 100644 --- a/eth2/state_processing/benches/bench_block_processing.rs +++ b/eth2/state_processing/benches/bench_block_processing.rs @@ -207,12 +207,12 @@ pub fn bench_block_processing( let spec = initial_spec.clone(); c.bench( &format!("{}/block_processing", desc), - Benchmark::new("build_previous_state_epoch_cache", move |b| { + Benchmark::new("build_previous_state_committee_cache", move |b| { b.iter_batched( || state.clone(), |mut state| { state - .build_epoch_cache(RelativeEpoch::Previous, &spec) + .build_committee_cache(RelativeEpoch::Previous, &spec) .unwrap(); state }, @@ -227,12 +227,12 @@ pub fn bench_block_processing( let spec = initial_spec.clone(); c.bench( &format!("{}/block_processing", desc), - Benchmark::new("build_current_state_epoch_cache", move |b| { + Benchmark::new("build_current_state_committee_cache", move |b| { b.iter_batched( || state.clone(), |mut state| { state - .build_epoch_cache(RelativeEpoch::Current, &spec) + .build_committee_cache(RelativeEpoch::Current, &spec) .unwrap(); state }, diff --git a/eth2/state_processing/src/get_genesis_state.rs b/eth2/state_processing/src/get_genesis_state.rs index 6638b5246f..210d1a0a7b 100644 --- a/eth2/state_processing/src/get_genesis_state.rs +++ b/eth2/state_processing/src/get_genesis_state.rs @@ -30,7 +30,7 @@ pub fn get_genesis_state( } // Ensure the current epoch cache is built. - state.build_epoch_cache(RelativeEpoch::Current, spec)?; + state.build_committee_cache(RelativeEpoch::Current, spec)?; // Set all the active index roots to be the genesis active index root. let active_validator_indices = state diff --git a/eth2/state_processing/src/per_block_processing.rs b/eth2/state_processing/src/per_block_processing.rs index 9f40bffd7a..db73c96187 100644 --- a/eth2/state_processing/src/per_block_processing.rs +++ b/eth2/state_processing/src/per_block_processing.rs @@ -79,8 +79,8 @@ fn per_block_processing_signature_optional( process_block_header(state, block, spec)?; // Ensure the current and previous epoch cache is built. - state.build_epoch_cache(RelativeEpoch::Previous, spec)?; - state.build_epoch_cache(RelativeEpoch::Current, spec)?; + state.build_committee_cache(RelativeEpoch::Previous, spec)?; + state.build_committee_cache(RelativeEpoch::Current, spec)?; if should_verify_block_signature { verify_block_signature(&state, &block, &spec)?; @@ -315,7 +315,7 @@ pub fn process_attestations( ); // Ensure the previous epoch cache exists. - state.build_epoch_cache(RelativeEpoch::Previous, spec)?; + state.build_committee_cache(RelativeEpoch::Previous, spec)?; // Verify attestations in parallel. attestations diff --git a/eth2/state_processing/src/per_epoch_processing.rs b/eth2/state_processing/src/per_epoch_processing.rs index f1ca42eda8..69ee565fb4 100644 --- a/eth2/state_processing/src/per_epoch_processing.rs +++ b/eth2/state_processing/src/per_epoch_processing.rs @@ -34,8 +34,8 @@ pub fn per_epoch_processing( spec: &ChainSpec, ) -> Result<(), Error> { // Ensure the previous and next epoch caches are built. - state.build_epoch_cache(RelativeEpoch::Previous, spec)?; - state.build_epoch_cache(RelativeEpoch::Current, spec)?; + state.build_committee_cache(RelativeEpoch::Previous, spec)?; + state.build_committee_cache(RelativeEpoch::Current, spec)?; // Load the struct we use to assign validators into sets based on their participation. // diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index f709bc68fa..2671257f8e 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -1,4 +1,6 @@ -use self::epoch_cache::{get_active_validator_indices, EpochCache, Error as EpochCacheError}; +use self::committee_cache::{ + get_active_validator_indices, CommitteeCache, Error as CommitteeCacheError, +}; use self::exit_cache::ExitCache; use crate::test_utils::TestRandom; use crate::*; @@ -18,7 +20,7 @@ use tree_hash_derive::{CachedTreeHash, TreeHash}; pub use beacon_state_types::*; mod beacon_state_types; -mod epoch_cache; +mod committee_cache; mod exit_cache; mod pubkey_cache; mod tests; @@ -50,11 +52,11 @@ pub enum Error { cache_len: usize, registry_len: usize, }, - PreviousEpochCacheUninitialized, - CurrentEpochCacheUninitialized, + PreviousCommitteeCacheUninitialized, + CurrentCommitteeCacheUninitialized, RelativeEpochError(RelativeEpochError), - EpochCacheUninitialized(RelativeEpoch), - EpochCacheError(EpochCacheError), + CommitteeCacheUninitialized(RelativeEpoch), + CommitteeCacheError(CommitteeCacheError), TreeHashCacheError(TreeHashCacheError), } @@ -122,7 +124,7 @@ where #[ssz(skip_deserializing)] #[tree_hash(skip_hashing)] #[test_random(default)] - pub epoch_caches: [EpochCache; CACHED_EPOCHS], + pub committee_caches: [CommitteeCache; CACHED_EPOCHS], #[serde(default)] #[ssz(skip_serializing)] #[ssz(skip_deserializing)] @@ -213,10 +215,10 @@ impl BeaconState { /* * Caching (not in spec) */ - epoch_caches: [ - EpochCache::default(), - EpochCache::default(), - EpochCache::default(), + committee_caches: [ + CommitteeCache::default(), + CommitteeCache::default(), + CommitteeCache::default(), ], pubkey_cache: PubkeyCache::default(), tree_hash_cache: TreeHashCache::default(), @@ -732,9 +734,9 @@ impl BeaconState { /// Build all the caches, if they need to be built. pub fn build_all_caches(&mut self, spec: &ChainSpec) -> Result<(), Error> { - self.build_epoch_cache(RelativeEpoch::Previous, spec)?; - self.build_epoch_cache(RelativeEpoch::Current, spec)?; - self.build_epoch_cache(RelativeEpoch::Next, spec)?; + self.build_committee_cache(RelativeEpoch::Previous, spec)?; + self.build_committee_cache(RelativeEpoch::Current, spec)?; + self.build_committee_cache(RelativeEpoch::Next, spec)?; self.update_pubkey_cache()?; self.update_tree_hash_cache()?; self.exit_cache @@ -744,30 +746,30 @@ impl BeaconState { } /// Build an epoch cache, unless it is has already been built. - pub fn build_epoch_cache( + pub fn build_committee_cache( &mut self, relative_epoch: RelativeEpoch, spec: &ChainSpec, ) -> Result<(), Error> { let i = Self::cache_index(relative_epoch); - if self.epoch_caches[i].is_initialized_at(self.previous_epoch()) { + if self.committee_caches[i].is_initialized_at(self.previous_epoch()) { Ok(()) } else { - self.force_build_epoch_cache(relative_epoch, spec) + self.force_build_committee_cache(relative_epoch, spec) } } /// Always builds the previous epoch cache, even if it is already initialized. - pub fn force_build_epoch_cache( + pub fn force_build_committee_cache( &mut self, relative_epoch: RelativeEpoch, spec: &ChainSpec, ) -> Result<(), Error> { let epoch = relative_epoch.into_epoch(self.current_epoch()); - self.epoch_caches[Self::cache_index(relative_epoch)] = - EpochCache::initialized(&self, epoch, spec)?; + self.committee_caches[Self::cache_index(relative_epoch)] = + CommitteeCache::initialized(&self, epoch, spec)?; Ok(()) } @@ -779,9 +781,9 @@ impl BeaconState { pub fn advance_caches(&mut self) { let next = Self::cache_index(RelativeEpoch::Previous); - let caches = &mut self.epoch_caches[..]; + let caches = &mut self.committee_caches[..]; caches.rotate_left(1); - caches[next] = EpochCache::default(); + caches[next] = CommitteeCache::default(); } fn cache_index(relative_epoch: RelativeEpoch) -> usize { @@ -794,22 +796,22 @@ impl BeaconState { /// Returns the cache for some `RelativeEpoch`. Returns an error if the cache has not been /// initialized. - fn cache(&self, relative_epoch: RelativeEpoch) -> Result<&EpochCache, Error> { - let cache = &self.epoch_caches[Self::cache_index(relative_epoch)]; + fn cache(&self, relative_epoch: RelativeEpoch) -> Result<&CommitteeCache, Error> { + let cache = &self.committee_caches[Self::cache_index(relative_epoch)]; if cache.is_initialized_at(relative_epoch.into_epoch(self.current_epoch())) { Ok(cache) } else { - Err(Error::EpochCacheUninitialized(relative_epoch)) + Err(Error::CommitteeCacheUninitialized(relative_epoch)) } } /// Drops the cache, leaving it in an uninitialized state. fn drop_cache(&mut self, relative_epoch: RelativeEpoch) { - self.epoch_caches[Self::cache_index(relative_epoch)] = EpochCache::default(); + self.committee_caches[Self::cache_index(relative_epoch)] = CommitteeCache::default(); } - // FIXME(sproul): drop_previous/current_epoch_cache + // FIXME(sproul): drop_previous/current_committee_cache /// Updates the pubkey cache, if required. /// @@ -870,9 +872,9 @@ impl BeaconState { } } -impl From for Error { - fn from(e: EpochCacheError) -> Error { - Error::EpochCacheError(e) +impl From for Error { + fn from(e: CommitteeCacheError) -> Error { + Error::CommitteeCacheError(e) } } diff --git a/eth2/types/src/beacon_state/epoch_cache.rs b/eth2/types/src/beacon_state/committee_cache.rs similarity index 98% rename from eth2/types/src/beacon_state/epoch_cache.rs rename to eth2/types/src/beacon_state/committee_cache.rs index 3c4cb6c054..10b2d2a996 100644 --- a/eth2/types/src/beacon_state/epoch_cache.rs +++ b/eth2/types/src/beacon_state/committee_cache.rs @@ -14,7 +14,7 @@ pub enum Error { mod tests; #[derive(Debug, Default, PartialEq, Clone, Serialize, Deserialize)] -pub struct EpochCache { +pub struct CommitteeCache { /// `Some(epoch)` if the cache is initialized, where `epoch` is the cache it holds. initialized_epoch: Option, shuffling_start_shard: u64, @@ -26,13 +26,13 @@ pub struct EpochCache { pub attestation_duties: Vec>, } -impl EpochCache { +impl CommitteeCache { /// Return a new, fully initialized cache. pub fn initialized( state: &BeaconState, epoch: Epoch, spec: &ChainSpec, - ) -> Result { + ) -> Result { let relative_epoch = RelativeEpoch::from_epoch(state.current_epoch(), epoch) .map_err(|_| BeaconStateError::EpochOutOfBounds)?; @@ -74,7 +74,7 @@ impl EpochCache { ) .ok_or_else(|| Error::UnableToShuffle)?; - let mut cache = EpochCache { + let mut cache = CommitteeCache { initialized_epoch: Some(epoch), shuffling_start_shard, shuffling, diff --git a/eth2/types/src/beacon_state/epoch_cache/tests.rs b/eth2/types/src/beacon_state/committee_cache/tests.rs similarity index 84% rename from eth2/types/src/beacon_state/epoch_cache/tests.rs rename to eth2/types/src/beacon_state/committee_cache/tests.rs index df8ed223b5..9ea863ada9 100644 --- a/eth2/types/src/beacon_state/epoch_cache/tests.rs +++ b/eth2/types/src/beacon_state/committee_cache/tests.rs @@ -23,7 +23,7 @@ fn fails_without_validators() { let spec = &FewValidatorsEthSpec::spec(); assert_eq!( - EpochCache::initialized(&state, state.current_epoch(), &spec), + CommitteeCache::initialized(&state, state.current_epoch(), &spec), Err(BeaconStateError::InsufficientValidators) ); } @@ -33,16 +33,16 @@ fn initializes_with_the_right_epoch() { let state = new_state::(16, Slot::new(0)); let spec = &FewValidatorsEthSpec::spec(); - let cache = EpochCache::default(); + let cache = CommitteeCache::default(); assert_eq!(cache.initialized_epoch, None); - let cache = EpochCache::initialized(&state, state.current_epoch(), &spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.current_epoch(), &spec).unwrap(); assert_eq!(cache.initialized_epoch, Some(state.current_epoch())); - let cache = EpochCache::initialized(&state, state.previous_epoch(), &spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.previous_epoch(), &spec).unwrap(); assert_eq!(cache.initialized_epoch, Some(state.previous_epoch())); - let cache = EpochCache::initialized(&state, state.next_epoch(), &spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.next_epoch(), &spec).unwrap(); assert_eq!(cache.initialized_epoch, Some(state.next_epoch())); } @@ -78,13 +78,13 @@ fn shuffles_for_the_right_epoch() { .unwrap() }; - let cache = EpochCache::initialized(&state, state.current_epoch(), spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.current_epoch(), spec).unwrap(); assert_eq!(cache.shuffling, shuffling_with_seed(current_seed)); - let cache = EpochCache::initialized(&state, state.previous_epoch(), spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.previous_epoch(), spec).unwrap(); assert_eq!(cache.shuffling, shuffling_with_seed(previous_seed)); - let cache = EpochCache::initialized(&state, state.next_epoch(), spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.next_epoch(), spec).unwrap(); assert_eq!(cache.shuffling, shuffling_with_seed(next_seed)); } @@ -100,13 +100,13 @@ fn can_start_on_any_shard() { for i in 0..FewValidatorsEthSpec::shard_count() as u64 { state.latest_start_shard = i; - let cache = EpochCache::initialized(&state, state.current_epoch(), spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.current_epoch(), spec).unwrap(); assert_eq!(cache.shuffling_start_shard, i); - let cache = EpochCache::initialized(&state, state.previous_epoch(), spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.previous_epoch(), spec).unwrap(); assert_eq!(cache.shuffling_start_shard, i); - let cache = EpochCache::initialized(&state, state.next_epoch(), spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.next_epoch(), spec).unwrap(); assert_eq!(cache.shuffling_start_shard, i); } } @@ -195,16 +195,16 @@ fn starts_on_the_correct_shard() { for i in 0..ExcessShardsEthSpec::shard_count() { state.latest_start_shard = i as u64; - let cache = EpochCache::initialized(&state, state.current_epoch(), spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.current_epoch(), spec).unwrap(); assert_eq!(cache.shuffling_start_shard as usize, i); - let cache = EpochCache::initialized(&state, state.previous_epoch(), spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.previous_epoch(), spec).unwrap(); assert_eq!( cache.shuffling_start_shard as usize, (i + shard_count - previous_shards) % shard_count ); - let cache = EpochCache::initialized(&state, state.next_epoch(), spec).unwrap(); + let cache = CommitteeCache::initialized(&state, state.next_epoch(), spec).unwrap(); assert_eq!( cache.shuffling_start_shard as usize, (i + current_shards) % shard_count diff --git a/eth2/types/src/beacon_state/tests.rs b/eth2/types/src/beacon_state/tests.rs index 35f0269b1f..a70ecb52fd 100644 --- a/eth2/types/src/beacon_state/tests.rs +++ b/eth2/types/src/beacon_state/tests.rs @@ -89,11 +89,13 @@ fn test_cache_initialization<'a, T: EthSpec>( // Assuming the cache isn't already built, assert that a call to a cache-using function fails. assert_eq!( state.get_attestation_duties(0, relative_epoch), - Err(BeaconStateError::EpochCacheUninitialized(relative_epoch)) + Err(BeaconStateError::CommitteeCacheUninitialized( + relative_epoch + )) ); // Build the cache. - state.build_epoch_cache(relative_epoch, spec).unwrap(); + state.build_committee_cache(relative_epoch, spec).unwrap(); // Assert a call to a cache-using function passes. let _ = state @@ -106,7 +108,9 @@ fn test_cache_initialization<'a, T: EthSpec>( // Assert a call to a cache-using function fail. assert_eq!( state.get_beacon_proposer_index(slot, relative_epoch, spec), - Err(BeaconStateError::EpochCacheUninitialized(relative_epoch)) + Err(BeaconStateError::CommitteeCacheUninitialized( + relative_epoch + )) ); } @@ -256,12 +260,14 @@ mod committees { state.latest_randao_mixes = FixedLenVec::from(distinct_hashes); state - .build_epoch_cache(RelativeEpoch::Previous, spec) + .build_committee_cache(RelativeEpoch::Previous, spec) .unwrap(); state - .build_epoch_cache(RelativeEpoch::Current, spec) + .build_committee_cache(RelativeEpoch::Current, spec) + .unwrap(); + state + .build_committee_cache(RelativeEpoch::Next, spec) .unwrap(); - state.build_epoch_cache(RelativeEpoch::Next, spec).unwrap(); let cache_epoch = cache_epoch.into_epoch(state_epoch); 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 90bddc4fd1..20ed8a893a 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 @@ -205,10 +205,10 @@ impl TestingBeaconStateBuilder { let state = &mut self.state; state - .build_epoch_cache(RelativeEpoch::Previous, spec) + .build_committee_cache(RelativeEpoch::Previous, spec) .unwrap(); state - .build_epoch_cache(RelativeEpoch::Current, spec) + .build_committee_cache(RelativeEpoch::Current, spec) .unwrap(); let current_epoch = state.current_epoch();