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

View File

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

View File

@ -10,27 +10,20 @@ fn do_sane_cache_test<T: EthSpec>(
epoch: Epoch, epoch: Epoch,
relative_epoch: RelativeEpoch, relative_epoch: RelativeEpoch,
validator_count: usize, validator_count: usize,
expected_seed: Hash256,
expected_shuffling_start: u64,
spec: &ChainSpec, spec: &ChainSpec,
) { ) {
let active_indices: Vec<usize> = (0..validator_count).collect(); 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!( assert_eq!(
&active_indices[..], &active_indices[..],
state state.get_cached_active_validator_indices(epoch).unwrap(),
.get_cached_active_validator_indices(relative_epoch, &spec)
.unwrap(),
"Validator indices mismatch" "Validator indices mismatch"
); );
let shuffling = shuffle_list( let shuffling =
active_indices, shuffle_list(active_indices, spec.shuffle_round_count, &seed[..], false).unwrap();
spec.shuffle_round_count,
&expected_seed[..],
false,
)
.unwrap();
let committees_per_epoch = spec.get_epoch_committee_count(shuffling.len()); let committees_per_epoch = spec.get_epoch_committee_count(shuffling.len());
let committees_per_slot = committees_per_epoch / spec.slots_per_epoch; 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(); 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 state
.build_epoch_cache(RelativeEpoch::Previous, spec) .build_epoch_cache(RelativeEpoch::Previous, spec)
.unwrap(); .unwrap();
state state
.build_epoch_cache(RelativeEpoch::Current, spec) .build_epoch_cache(RelativeEpoch::Current, spec)
.unwrap(); .unwrap();
state state.build_epoch_cache(RelativeEpoch::Next, spec).unwrap();
.build_epoch_cache(RelativeEpoch::NextWithRegistryChange, spec)
.unwrap();
state
.build_epoch_cache(RelativeEpoch::NextWithoutRegistryChange, spec)
.unwrap();
state 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] #[test]
fn builds_sane_current_epoch_cache() { fn builds_sane_current_epoch_cache() {
let mut spec = FewValidatorsEthSpec::spec(); let mut spec = FewValidatorsEthSpec::spec();
@ -157,3 +160,4 @@ fn builds_sane_next_without_update_epoch_cache() {
&spec, &spec,
); );
} }
*/