Implemented detailed state eqs in ef_tests

This commit is contained in:
Paul Hauner 2019-05-22 18:00:21 +10:00
parent 7a99654f89
commit b2666d700c
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF
4 changed files with 73 additions and 4 deletions

View File

@ -762,6 +762,16 @@ impl<T: EthSpec> BeaconState<T> {
Ok(()) Ok(())
} }
/// Drop all caches on the state.
pub fn drop_all_caches(&mut self) {
self.drop_committee_cache(RelativeEpoch::Previous);
self.drop_committee_cache(RelativeEpoch::Current);
self.drop_committee_cache(RelativeEpoch::Next);
self.drop_pubkey_cache();
self.drop_tree_hash_cache();
self.exit_cache = ExitCache::default();
}
/// Build an epoch cache, unless it is has already been built. /// Build an epoch cache, unless it is has already been built.
pub fn build_committee_cache( pub fn build_committee_cache(
&mut self, &mut self,
@ -824,7 +834,7 @@ impl<T: EthSpec> BeaconState<T> {
} }
/// Drops the cache, leaving it in an uninitialized state. /// Drops the cache, leaving it in an uninitialized state.
fn drop_cache(&mut self, relative_epoch: RelativeEpoch) { fn drop_committee_cache(&mut self, relative_epoch: RelativeEpoch) {
self.committee_caches[Self::cache_index(relative_epoch)] = CommitteeCache::default(); self.committee_caches[Self::cache_index(relative_epoch)] = CommitteeCache::default();
} }
@ -887,6 +897,11 @@ impl<T: EthSpec> BeaconState<T> {
.and_then(|b| Ok(Hash256::from_slice(b))) .and_then(|b| Ok(Hash256::from_slice(b)))
.map_err(Into::into) .map_err(Into::into)
} }
/// Completely drops the tree hash cache, replacing it with a new, empty cache.
pub fn drop_tree_hash_cache(&mut self) {
self.tree_hash_cache = TreeHashCache::default()
}
} }
impl From<RelativeEpochError> for Error { impl From<RelativeEpochError> for Error {

View File

@ -9,6 +9,7 @@ fake_crypto = ["bls/fake_crypto"]
[dependencies] [dependencies]
bls = { path = "../../eth2/utils/bls" } bls = { path = "../../eth2/utils/bls" }
compare_fields = { path = "../../eth2/utils/compare_fields" }
ethereum-types = "0.5" ethereum-types = "0.5"
hex = "0.3" hex = "0.3"
rayon = "1.0" rayon = "1.0"

View File

@ -1,5 +1,7 @@
use super::*; use super::*;
use compare_fields::CompareFields;
use std::fmt::Debug; use std::fmt::Debug;
use types::BeaconState;
pub const MAX_VALUE_STRING_LEN: usize = 500; pub const MAX_VALUE_STRING_LEN: usize = 500;
@ -20,6 +22,55 @@ impl CaseResult {
} }
} }
/// Same as `compare_result_detailed`, however it drops the caches on both states before
/// comparison.
pub fn compare_beacon_state_results_without_caches<T: EthSpec, E: Debug>(
result: &mut Result<BeaconState<T>, E>,
expected: &mut Option<BeaconState<T>>,
) -> Result<(), Error> {
match (result.as_mut(), expected.as_mut()) {
(Ok(ref mut result), Some(ref mut expected)) => {
result.drop_all_caches();
expected.drop_all_caches();
}
_ => (),
};
compare_result_detailed(&result, &expected)
}
/// Same as `compare_result`, however utilizes the `CompareFields` trait to give a list of
/// mismatching fields when `Ok(result) != Some(expected)`.
pub fn compare_result_detailed<T, E>(
result: &Result<T, E>,
expected: &Option<T>,
) -> Result<(), Error>
where
T: PartialEq<T> + Debug + CompareFields,
E: Debug,
{
match (result, expected) {
(Ok(result), Some(expected)) => {
let mismatching_fields: Vec<String> = expected
.compare_fields(result)
.into_iter()
.filter(|c| !c.equal)
.map(|c| c.field_name)
.collect();
if !mismatching_fields.is_empty() {
Err(Error::NotEqual(format!(
"Result mismatch. Fields not equal: {:?}",
mismatching_fields
)))
} else {
Ok(())
}
}
_ => compare_result(result, expected),
}
}
/// Compares `result` with `expected`. /// Compares `result` with `expected`.
/// ///
/// If `expected.is_none()` then `result` is expected to be `Err`. Otherwise, `T` in `result` and /// If `expected.is_none()` then `result` is expected to be `Err`. Otherwise, `T` in `result` and

View File

@ -1,5 +1,5 @@
use super::*; use super::*;
use crate::case_result::compare_result; use crate::case_result::compare_beacon_state_results_without_caches;
use serde_derive::Deserialize; use serde_derive::Deserialize;
use state_processing::per_block_processing::process_deposits; use state_processing::per_block_processing::process_deposits;
use types::{BeaconState, Deposit, EthSpec}; use types::{BeaconState, Deposit, EthSpec};
@ -24,9 +24,11 @@ impl<E: EthSpec> Case for OperationsDeposit<E> {
fn result(&self) -> Result<(), Error> { fn result(&self) -> Result<(), Error> {
let mut state = self.pre.clone(); let mut state = self.pre.clone();
let deposit = self.deposit.clone(); let deposit = self.deposit.clone();
let mut expected = self.post.clone();
let result = process_deposits(&mut state, &[deposit], &E::spec()).and_then(|_| Ok(state)); let mut result =
process_deposits(&mut state, &[deposit], &E::spec()).and_then(|_| Ok(state));
compare_result(&result, &self.post) compare_beacon_state_results_without_caches(&mut result, &mut expected)
} }
} }