diff --git a/eth2/types/src/beacon_state.rs b/eth2/types/src/beacon_state.rs index aac95d37d2..f709bc68fa 100644 --- a/eth2/types/src/beacon_state.rs +++ b/eth2/types/src/beacon_state.rs @@ -51,9 +51,9 @@ pub enum Error { registry_len: usize, }, PreviousEpochCacheUninitialized, - CurrentEpochCacheUnintialized, + CurrentEpochCacheUninitialized, RelativeEpochError(RelativeEpochError), - EpochCacheUnintialized(RelativeEpoch), + EpochCacheUninitialized(RelativeEpoch), EpochCacheError(EpochCacheError), TreeHashCacheError(TreeHashCacheError), } @@ -706,8 +706,9 @@ impl BeaconState { pub fn get_attestation_duties( &self, validator_index: usize, + relative_epoch: RelativeEpoch, ) -> Result<&Option, Error> { - let cache = self.cache(RelativeEpoch::Current)?; + let cache = self.cache(relative_epoch)?; Ok(cache .attestation_duties @@ -799,10 +800,15 @@ impl BeaconState { if cache.is_initialized_at(relative_epoch.into_epoch(self.current_epoch())) { Ok(cache) } else { - Err(Error::EpochCacheUnintialized(relative_epoch)) + Err(Error::EpochCacheUninitialized(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(); + } + // FIXME(sproul): drop_previous/current_epoch_cache /// Updates the pubkey cache, if required. diff --git a/eth2/types/src/beacon_state/epoch_cache/tests.rs b/eth2/types/src/beacon_state/epoch_cache/tests.rs index c1187e8ff9..df8ed223b5 100644 --- a/eth2/types/src/beacon_state/epoch_cache/tests.rs +++ b/eth2/types/src/beacon_state/epoch_cache/tests.rs @@ -111,6 +111,8 @@ fn can_start_on_any_shard() { } } +/// This spec has more shards than slots in an epoch, permitting epochs where not all shards are +/// included in the committee. #[derive(Clone, PartialEq, Debug, Default, Serialize, Deserialize)] pub struct ExcessShardsEthSpec; diff --git a/eth2/types/src/beacon_state/tests.rs b/eth2/types/src/beacon_state/tests.rs index 753f8bf8e1..35f0269b1f 100644 --- a/eth2/types/src/beacon_state/tests.rs +++ b/eth2/types/src/beacon_state/tests.rs @@ -72,7 +72,6 @@ fn get_active_index_root_index() { test_active_index::(slot); } -/* /// Test that /// /// 1. Using the cache before it's built fails. @@ -89,14 +88,12 @@ 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, spec), + state.get_attestation_duties(0, relative_epoch), Err(BeaconStateError::EpochCacheUninitialized(relative_epoch)) ); // Build the cache. - state - .build_current_epoch_cache(relative_epoch, spec) - .unwrap(); + state.build_epoch_cache(relative_epoch, spec).unwrap(); // Assert a call to a cache-using function passes. let _ = state @@ -125,10 +122,8 @@ fn cache_initialization() { test_cache_initialization(&mut state, RelativeEpoch::Previous, &spec); test_cache_initialization(&mut state, RelativeEpoch::Current, &spec); - test_cache_initialization(&mut state, RelativeEpoch::NextWithRegistryChange, &spec); - test_cache_initialization(&mut state, RelativeEpoch::NextWithoutRegistryChange, &spec); + test_cache_initialization(&mut state, RelativeEpoch::Next, &spec); } -*/ #[test] fn tree_hash_cache() { @@ -208,13 +203,23 @@ mod committees { ); // Loop through each validator in the committee. - for &i in cc.committee { + for (committee_i, validator_i) in cc.committee.iter().enumerate() { // Assert the validators are assigned contiguously across committees. assert_eq!( - i, + *validator_i, *expected_indices_iter.next().unwrap(), "Non-sequential validators." ); + // Assert a call to `get_attestation_duties` is consistent with a call to + // `get_crosslink_committees_at_slot` + let attestation_duty = state + .get_attestation_duties(*validator_i, relative_epoch) + .unwrap() + .unwrap(); + assert_eq!(attestation_duty.slot, slot); + assert_eq!(attestation_duty.shard, cc.shard); + assert_eq!(attestation_duty.committee_index, committee_i); + assert_eq!(attestation_duty.committee_len, cc.committee.len()); } } }