Re-add old cache tests

This commit is contained in:
Paul Hauner 2019-05-20 14:25:38 +10:00
parent 89df2b173e
commit cb74187cfc
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
3 changed files with 27 additions and 14 deletions

View File

@ -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<T: EthSpec> BeaconState<T> {
pub fn get_attestation_duties(
&self,
validator_index: usize,
relative_epoch: RelativeEpoch,
) -> Result<&Option<AttestationDuty>, Error> {
let cache = self.cache(RelativeEpoch::Current)?;
let cache = self.cache(relative_epoch)?;
Ok(cache
.attestation_duties
@ -799,10 +800,15 @@ impl<T: EthSpec> BeaconState<T> {
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.

View File

@ -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;

View File

@ -72,7 +72,6 @@ fn get_active_index_root_index() {
test_active_index::<FoundationEthSpec>(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());
}
}
}