Implement tree states & hierarchical state DB

This commit is contained in:
Michael Sproul
2023-06-19 10:14:47 +10:00
parent 2bb62b7f7d
commit 23db089a7a
193 changed files with 6093 additions and 5925 deletions

View File

@@ -1,7 +1,7 @@
use super::*;
use crate::case_result::compare_result;
use crate::impl_bls_load_case;
use bls::{PublicKeyBytes, Signature, SignatureBytes};
use bls::{PublicKey, PublicKeyBytes, Signature, SignatureBytes};
use serde_derive::Deserialize;
use std::convert::TryInto;
use types::Hash256;
@@ -30,6 +30,13 @@ impl Case for BlsVerify {
.try_into()
.and_then(|signature: Signature| {
let pk = self.input.pubkey.decompress()?;
// Check serialization roundtrip.
let pk_uncompressed = pk.serialize_uncompressed();
let pk_from_uncompressed = PublicKey::deserialize_uncompressed(&pk_uncompressed)
.expect("uncompressed serialization should round-trip");
assert_eq!(pk_from_uncompressed, pk);
Ok(signature.verify(&pk, Hash256::from_slice(&message)))
})
.unwrap_or(false);

View File

@@ -1,6 +1,6 @@
use super::*;
use crate::bls_setting::BlsSetting;
use crate::case_result::compare_beacon_state_results_without_caches;
use crate::case_result::{check_state_diff, compare_beacon_state_results_without_caches};
use crate::decode::{ssz_decode_state, yaml_decode_file};
use crate::type_name;
use crate::type_name::TypeName;
@@ -147,16 +147,17 @@ impl<E: EthSpec> EpochTransition<E> for Slashings {
validator_statuses.process_attestations(state)?;
process_slashings(
state,
None,
validator_statuses.total_balances.current_epoch(),
spec,
)?;
}
BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Capella(_) => {
let mut cache = altair::ParticipationCache::new(state, spec).unwrap();
process_slashings(
state,
altair::ParticipationCache::new(state, spec)
.unwrap()
.current_epoch_total_active_balance(),
Some(cache.process_slashings_indices()),
cache.current_epoch_total_active_balance(),
spec,
)?;
}
@@ -237,7 +238,7 @@ impl<E: EthSpec> EpochTransition<E> for InactivityUpdates {
BeaconState::Altair(_) | BeaconState::Merge(_) | BeaconState::Capella(_) => {
altair::process_inactivity_updates(
state,
&altair::ParticipationCache::new(state, spec).unwrap(),
&mut altair::ParticipationCache::new(state, spec).unwrap(),
spec,
)
}
@@ -312,18 +313,22 @@ impl<E: EthSpec, T: EpochTransition<E>> Case for EpochProcessing<E, T> {
fn result(&self, _case_index: usize, fork_name: ForkName) -> Result<(), Error> {
self.metadata.bls_setting.unwrap_or_default().check()?;
let mut state = self.pre.clone();
let spec = &testing_spec::<E>(fork_name);
let mut pre_state = self.pre.clone();
// Processing requires the committee caches.
pre_state.build_all_committee_caches(spec).unwrap();
let mut state = pre_state.clone();
let mut expected = self.post.clone();
let spec = &testing_spec::<E>(fork_name);
if let Some(post_state) = expected.as_mut() {
post_state.build_all_committee_caches(spec).unwrap();
}
let mut result = (|| {
// Processing requires the committee caches.
state.build_all_committee_caches(spec)?;
T::run(&mut state, spec).map(|_| state)
})();
let mut result = T::run(&mut state, spec).map(|_| state);
check_state_diff(&pre_state, &expected)?;
compare_beacon_state_results_without_caches(&mut result, &mut expected)
}
}

View File

@@ -50,7 +50,7 @@ impl<E: EthSpec> LoadCase for MerkleProofValidity<E> {
impl<E: EthSpec> Case for MerkleProofValidity<E> {
fn result(&self, _case_index: usize, _fork_name: ForkName) -> Result<(), Error> {
let mut state = self.state.clone();
state.initialize_tree_hash_cache();
state.update_tree_hash_cache().unwrap();
let proof = match state.compute_merkle_proof(self.merkle_proof.leaf_index) {
Ok(proof) => proof,
Err(_) => {
@@ -79,9 +79,6 @@ impl<E: EthSpec> Case for MerkleProofValidity<E> {
}
}
// Tree hash cache should still be initialized (not dropped).
assert!(state.tree_hash_cache().is_initialized());
Ok(())
}
}

View File

@@ -1,6 +1,6 @@
use super::*;
use crate::bls_setting::BlsSetting;
use crate::case_result::compare_beacon_state_results_without_caches;
use crate::case_result::{check_state_diff, compare_beacon_state_results_without_caches};
use crate::decode::{ssz_decode_file, ssz_decode_file_with, ssz_decode_state, yaml_decode_file};
use crate::testing_spec;
use serde_derive::Deserialize;
@@ -452,9 +452,8 @@ impl<E: EthSpec, O: Operation<E>> Case for Operations<E, O> {
fn result(&self, _case_index: usize, fork_name: ForkName) -> Result<(), Error> {
let spec = &testing_spec::<E>(fork_name);
let mut state = self.pre.clone();
let mut expected = self.post.clone();
let mut pre_state = self.pre.clone();
// Processing requires the committee caches.
// NOTE: some of the withdrawals tests have 0 active validators, do not try
// to build the commitee cache in this case.
@@ -462,6 +461,13 @@ impl<E: EthSpec, O: Operation<E>> Case for Operations<E, O> {
state.build_all_committee_caches(spec).unwrap();
}
let mut state = pre_state.clone();
let mut expected = self.post.clone();
if let Some(post_state) = expected.as_mut() {
post_state.build_all_committee_caches(spec).unwrap();
}
let mut result = self
.operation
.as_ref()
@@ -469,6 +475,7 @@ impl<E: EthSpec, O: Operation<E>> Case for Operations<E, O> {
.apply_to(&mut state, spec, self)
.map(|()| state);
check_state_diff(&pre_state, &expected)?;
compare_beacon_state_results_without_caches(&mut result, &mut expected)
}
}

View File

@@ -1,6 +1,6 @@
use super::*;
use crate::bls_setting::BlsSetting;
use crate::case_result::compare_beacon_state_results_without_caches;
use crate::case_result::{check_state_diff, compare_beacon_state_results_without_caches};
use crate::decode::{ssz_decode_file_with, ssz_decode_state, yaml_decode_file};
use serde_derive::Deserialize;
use state_processing::{
@@ -128,6 +128,16 @@ impl<E: EthSpec> Case for SanityBlocks<E> {
Ok(res) => (Ok(res.0), Ok(res.1)),
};
compare_beacon_state_results_without_caches(&mut indiv_result, &mut expected)?;
compare_beacon_state_results_without_caches(&mut bulk_result, &mut expected)
compare_beacon_state_results_without_caches(&mut bulk_result, &mut expected)?;
// Check state diff (requires fully built committee caches).
let mut pre = self.pre.clone();
pre.build_all_committee_caches(spec).unwrap();
let post = self.post.clone().map(|mut post| {
post.build_all_committee_caches(spec).unwrap();
post
});
check_state_diff(&pre, &post)?;
Ok(())
}
}

View File

@@ -1,6 +1,6 @@
use super::*;
use crate::bls_setting::BlsSetting;
use crate::case_result::compare_beacon_state_results_without_caches;
use crate::case_result::{check_state_diff, compare_beacon_state_results_without_caches};
use crate::decode::{ssz_decode_state, yaml_decode_file};
use serde_derive::Deserialize;
use state_processing::per_slot_processing;
@@ -67,6 +67,15 @@ impl<E: EthSpec> Case for SanitySlots<E> {
.try_for_each(|_| per_slot_processing(&mut state, None, spec).map(|_| ()))
.map(|_| state);
compare_beacon_state_results_without_caches(&mut result, &mut expected)
compare_beacon_state_results_without_caches(&mut result, &mut expected)?;
// Check state diff (requires fully built committee caches).
let mut pre = self.pre.clone();
pre.build_all_committee_caches(spec).unwrap();
let post = self.post.clone().map(|mut post| {
post.build_all_committee_caches(spec).unwrap();
post
});
check_state_diff(&pre, &post)
}
}

View File

@@ -42,7 +42,7 @@ fn load_from_dir<T: SszStaticType>(path: &Path) -> Result<(SszStaticRoots, Vec<u
let roots = yaml_decode_file(&path.join("roots.yaml"))?;
let serialized = snappy_decode_file(&path.join("serialized.ssz_snappy"))
.expect("serialized.ssz_snappy exists");
let value = yaml_decode_file(&path.join("value.yaml"))?;
let value = yaml_decode_file(&path.join("value.yaml")).unwrap();
Ok((roots, serialized, value))
}
@@ -119,7 +119,6 @@ impl<E: EthSpec> Case for SszStaticTHC<BeaconState<E>> {
check_tree_hash(&self.roots.root, self.value.tree_hash_root().as_bytes())?;
let mut state = self.value.clone();
state.initialize_tree_hash_cache();
let cached_tree_hash_root = state.update_tree_hash_cache().unwrap();
check_tree_hash(&self.roots.root, cached_tree_hash_root.as_bytes())?;