Add progress on epoch cache testing

This commit is contained in:
Paul Hauner 2019-05-16 17:54:45 +10:00
parent 944ac73ef9
commit 86c3dad3e7
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
3 changed files with 45 additions and 64 deletions

View File

@ -331,17 +331,14 @@ impl<T: EthSpec> BeaconState<T> {
+ offset / (committee_count / spec.slots_per_epoch))
}
// FIXME(sproul): get_cached_current_active_validator_indices
/*
pub fn get_cached_active_validator_indices(
&self,
spec: &ChainSpec,
) -> Result<&[usize], Error> {
let cache = self.cache(relative_epoch, spec)?;
/// Return the cached active validator indices at some epoch.
///
/// Returns an error if that epoch is not cached, or the cache is not initialized.
pub fn get_cached_active_validator_indices(&self, epoch: Epoch) -> Result<&[usize], Error> {
let cache = self.cache(epoch)?;
Ok(&cache.active_validator_indices)
}
*/
/// Returns the active validator indices for the given epoch.
///
@ -359,8 +356,8 @@ impl<T: EthSpec> BeaconState<T> {
/// Spec v0.5.1
pub fn get_crosslink_committees_at_slot(
&self,
slot: Slot,
spec: &ChainSpec,
_slot: Slot,
_spec: &ChainSpec,
) -> Result<&Vec<CrosslinkCommittee>, Error> {
unimplemented!("FIXME(sproul)")
}
@ -385,7 +382,7 @@ impl<T: EthSpec> BeaconState<T> {
/// If the state does not contain an index for a beacon proposer at the requested `slot`, then `None` is returned.
///
/// Spec v0.5.1
pub fn get_beacon_proposer_index(&self, spec: &ChainSpec) -> Result<usize, Error> {
pub fn get_beacon_proposer_index(&self, _spec: &ChainSpec) -> Result<usize, Error> {
unimplemented!("FIXME(sproul)")
/*
let cache = self.cache(relative_epoch, spec)?;
@ -474,12 +471,7 @@ impl<T: EthSpec> BeaconState<T> {
/// See `Self::get_randao_mix`.
///
/// Spec v0.5.1
pub fn update_randao_mix(
&mut self,
epoch: Epoch,
signature: &Signature,
spec: &ChainSpec,
) -> Result<(), Error> {
pub fn update_randao_mix(&mut self, epoch: Epoch, signature: &Signature) -> Result<(), Error> {
let i = epoch.as_usize() % T::LatestRandaoMixesLength::to_usize();
let signature_hash = Hash256::from_slice(&hash(&ssz_encode(signature)));
@ -618,7 +610,6 @@ impl<T: EthSpec> BeaconState<T> {
pub fn get_matching_source_attestations(
&self,
epoch: Epoch,
spec: &ChainSpec,
) -> Result<&[PendingAttestation], Error> {
if epoch == self.current_epoch() {
Ok(&self.current_epoch_attestations)
@ -692,7 +683,7 @@ impl<T: EthSpec> BeaconState<T> {
pub fn get_churn_limit(&self, spec: &ChainSpec) -> Result<u64, Error> {
Ok(std::cmp::max(
spec.min_per_epoch_churn_limit,
self.cache(self.current_epoch(), spec)?
self.cache(self.current_epoch())?
.active_validator_indices
.len() as u64
/ spec.churn_limit_quotient,
@ -710,9 +701,8 @@ impl<T: EthSpec> BeaconState<T> {
pub fn get_attestation_duties(
&self,
validator_index: usize,
spec: &ChainSpec,
) -> Result<&Option<AttestationDuty>, Error> {
let cache = self.cache(self.current_epoch(), spec)?;
let cache = self.cache(self.current_epoch())?;
Ok(cache
.attestation_duties
@ -770,13 +760,8 @@ impl<T: EthSpec> BeaconState<T> {
) -> Result<(), Error> {
let epoch = relative_epoch.into_epoch(self.current_epoch());
self.epoch_caches[Self::cache_index(relative_epoch)] = EpochCache::initialized(
&self,
epoch,
self.generate_seed(epoch, spec)?,
self.get_epoch_start_shard(epoch, spec)?,
spec,
)?;
self.epoch_caches[Self::cache_index(relative_epoch)] =
EpochCache::initialized(&self, epoch, spec)?;
Ok(())
}
@ -785,9 +770,7 @@ impl<T: EthSpec> BeaconState<T> {
/// This should be used if the `slot` of this state is advanced beyond an epoch boundary.
///
/// Note: whilst this function will preserve already-built caches, it will not build any.
pub fn advance_caches(&mut self, spec: &ChainSpec) {
let previous = Self::cache_index(RelativeEpoch::Previous);
let current = Self::cache_index(RelativeEpoch::Previous);
pub fn advance_caches(&mut self) {
let next = Self::cache_index(RelativeEpoch::Previous);
let caches = &mut self.epoch_caches[..];
@ -805,9 +788,9 @@ impl<T: EthSpec> BeaconState<T> {
/// Returns the cache for some `RelativeEpoch`. Returns an error if the cache has not been
/// initialized.
fn cache(&self, epoch: Epoch, spec: &ChainSpec) -> Result<&EpochCache, Error> {
fn cache(&self, epoch: Epoch) -> Result<&EpochCache, Error> {
let relative_epoch = RelativeEpoch::from_epoch(self.current_epoch(), epoch)
.map_err(|e| Error::EpochOutOfBounds)?;
.map_err(|_| Error::EpochOutOfBounds)?;
let cache = &self.epoch_caches[Self::cache_index(relative_epoch)];

View File

@ -33,8 +33,6 @@ impl EpochCache {
pub fn initialized<T: EthSpec>(
state: &BeaconState<T>,
epoch: Epoch,
seed: Hash256,
epoch_start_shard: u64,
spec: &ChainSpec,
) -> Result<EpochCache, BeaconStateError> {
if epoch != state.previous_epoch() && epoch != state.current_epoch() {
@ -92,11 +90,7 @@ impl EpochCache {
/// Return `Some(CrosslinkCommittee)` if the given shard has a committee during the given
/// `epoch`.
pub fn get_crosslink_committee_for_shard(
&self,
shard: Shard,
spec: &ChainSpec,
) -> Option<&CrosslinkCommittee> {
pub fn get_crosslink_committee_for_shard(&self, shard: Shard) -> Option<&CrosslinkCommittee> {
if shard > self.shard_crosslink_committees.len() as u64 {
None
} else {

View File

@ -10,27 +10,20 @@ fn do_sane_cache_test<T: EthSpec>(
epoch: Epoch,
relative_epoch: RelativeEpoch,
validator_count: usize,
expected_seed: Hash256,
expected_shuffling_start: u64,
spec: &ChainSpec,
) {
let active_indices: Vec<usize> = (0..validator_count).collect();
let seed = state.generate_seed(epoch, spec).unwrap();
let expected_shuffling_start = state.get_epoch_start_shard(epoch, spec).unwrap();
assert_eq!(
&active_indices[..],
state
.get_cached_active_validator_indices(relative_epoch, &spec)
.unwrap(),
state.get_cached_active_validator_indices(epoch).unwrap(),
"Validator indices mismatch"
);
let shuffling = shuffle_list(
active_indices,
spec.shuffle_round_count,
&expected_seed[..],
false,
)
.unwrap();
let shuffling =
shuffle_list(active_indices, spec.shuffle_round_count, &seed[..], false).unwrap();
let committees_per_epoch = spec.get_epoch_committee_count(shuffling.len());
let committees_per_slot = committees_per_epoch / spec.slots_per_epoch;
@ -75,28 +68,38 @@ fn setup_sane_cache_test<T: EthSpec>(validator_count: usize, spec: &ChainSpec) -
let (mut state, _keypairs) = builder.build();
state.current_shuffling_start_shard = 0;
state.current_shuffling_seed = Hash256::from_slice(&[1; 32]);
state.previous_shuffling_start_shard = spec.shard_count - 1;
state.previous_shuffling_seed = Hash256::from_slice(&[2; 32]);
state
.build_epoch_cache(RelativeEpoch::Previous, spec)
.unwrap();
state
.build_epoch_cache(RelativeEpoch::Current, spec)
.unwrap();
state
.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, spec)
.unwrap();
state
.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, spec)
.unwrap();
state.build_epoch_cache(RelativeEpoch::Next, spec).unwrap();
state
}
#[test]
fn builds_sane_current_epoch_cache() {
let mut spec = FewValidatorsEthSpec::spec();
let validator_count = (spec.shard_count * spec.target_committee_size) + 1;
let state: BeaconState<FewValidatorsEthSpec> =
setup_sane_cache_test(validator_count as usize, &spec);
let epoch = state.current_epoch();
do_sane_cache_test(
state,
epoch,
RelativeEpoch::Current,
validator_count as usize,
&spec,
);
}
/*
#[test]
fn builds_sane_current_epoch_cache() {
let mut spec = FewValidatorsEthSpec::spec();
@ -157,3 +160,4 @@ fn builds_sane_next_without_update_epoch_cache() {
&spec,
);
}
*/