From 96bdc294190042028011d7aa2a0eabc4686bae58 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Tue, 25 Jan 2022 18:59:26 +1100 Subject: [PATCH] Start using tree vector --- consensus/fork_choice/src/fork_choice.rs | 2 +- .../state_processing/src/upgrade/merge.rs | 1 + consensus/types/src/beacon_block.rs | 2 +- consensus/types/src/beacon_state.rs | 109 +++++++++++++++--- consensus/types/src/deposit.rs | 2 +- consensus/types/src/execution_payload.rs | 3 + .../types/src/execution_payload_header.rs | 3 + consensus/types/src/historical_batch.rs | 6 +- consensus/types/src/lib.rs | 7 +- consensus/types/src/sync_committee.rs | 5 +- consensus/types/src/test_utils/test_random.rs | 2 +- 11 files changed, 113 insertions(+), 29 deletions(-) diff --git a/consensus/fork_choice/src/fork_choice.rs b/consensus/fork_choice/src/fork_choice.rs index 801b0f39df..a9cfd88967 100644 --- a/consensus/fork_choice/src/fork_choice.rs +++ b/consensus/fork_choice/src/fork_choice.rs @@ -408,7 +408,7 @@ where .find_head::( *store.justified_checkpoint(), *store.finalized_checkpoint(), - store.justified_balances(), + &justified_balances, store.proposer_boost_root(), spec, ) diff --git a/consensus/state_processing/src/upgrade/merge.rs b/consensus/state_processing/src/upgrade/merge.rs index 2e4ed441a4..b36f66c305 100644 --- a/consensus/state_processing/src/upgrade/merge.rs +++ b/consensus/state_processing/src/upgrade/merge.rs @@ -63,6 +63,7 @@ pub fn upgrade_to_bellatrix( committee_caches: mem::take(&mut pre.committee_caches), pubkey_cache: mem::take(&mut pre.pubkey_cache), exit_cache: mem::take(&mut pre.exit_cache), + #[cfg(not(feature = "milhouse"))] tree_hash_cache: mem::take(&mut pre.tree_hash_cache), }); diff --git a/consensus/types/src/beacon_block.rs b/consensus/types/src/beacon_block.rs index 0026db0ee7..b0c3094e4f 100644 --- a/consensus/types/src/beacon_block.rs +++ b/consensus/types/src/beacon_block.rs @@ -329,7 +329,7 @@ impl BeaconBlockBase { }; let deposit = Deposit { - proof: FixedVector::from_elem(Hash256::zero()), + proof: ssz_types::FixedVector::from_elem(Hash256::zero()), data: deposit_data, }; diff --git a/consensus/types/src/beacon_state.rs b/consensus/types/src/beacon_state.rs index cebc8fd94f..7918f05bce 100644 --- a/consensus/types/src/beacon_state.rs +++ b/consensus/types/src/beacon_state.rs @@ -30,7 +30,7 @@ pub use eth_spec::*; pub use iter::BlockRootsIter; #[cfg(feature = "milhouse")] -pub use milhouse::{interface::Interface, List as VList, List}; +pub use milhouse::{interface::Interface, List as VList, List, Vector as FixedVector}; #[cfg(not(feature = "milhouse"))] pub use { @@ -48,19 +48,26 @@ mod tests; mod tree_hash_cache; #[cfg(feature = "milhouse")] -pub type ListMut<'a, T, N> = Interface<'a, T, List>; -#[cfg(not(feature = "milhouse"))] -pub type ListMut<'a, T, N> = &'a mut VList; +mod type_aliases { + use super::*; -#[cfg(feature = "milhouse")] -pub type ValidatorsMut<'a, N> = ListMut<'a, Validator, N>; -#[cfg(not(feature = "milhouse"))] -pub type ValidatorsMut<'a, N> = &'a mut VList; + pub type ListMut<'a, T, N> = Interface<'a, T, List>; + pub type VectMut<'a, T, N> = Interface<'a, T, FixedVector>; + pub type ValidatorsMut<'a, N> = ListMut<'a, Validator, N>; + pub type BalancesMut<'a, N> = ListMut<'a, u64, N>; +} -#[cfg(feature = "milhouse")] -pub type BalancesMut<'a, N> = ListMut<'a, u64, N>; #[cfg(not(feature = "milhouse"))] -pub type BalancesMut<'a, N> = ListMut<'a, u64, N>; +mod type_aliases { + use super::*; + + pub type ListMut<'a, T, N> = &'a mut VList; + pub type VectMut<'a, T, N> = &'a mut FixedVector; + pub type ValidatorsMut<'a, N> = &'a mut VList; + pub type BalancesMut<'a, N> = ListMut<'a, u64, N>; +} + +pub use type_aliases::*; pub const CACHED_EPOCHS: usize = 3; const MAX_RANDOM_BYTE: u64 = (1 << 8) - 1; @@ -241,9 +248,11 @@ where // History pub latest_block_header: BeaconBlockHeader, - #[compare_fields(as_slice)] + #[superstruct(getter(rename = "block_roots_raw"))] + #[test_random(default)] pub block_roots: FixedVector, - #[compare_fields(as_slice)] + #[superstruct(getter(rename = "state_roots_raw"))] + #[test_random(default)] pub state_roots: FixedVector, #[test_random(default)] pub historical_roots: VList, @@ -267,10 +276,15 @@ where pub balances: VList, // Randomness + #[superstruct(getter(rename = "randao_mixes_raw"))] + #[test_random(default)] pub randao_mixes: FixedVector, // Slashings - #[serde(with = "ssz_types::serde_utils::quoted_u64_fixed_vec")] + #[superstruct(getter(rename = "slashings_raw"))] + #[test_random(default)] + // FIXME(sproul): serde quoting + // #[serde(with = "ssz_types::serde_utils::quoted_u64_fixed_vec")] pub slashings: FixedVector, // Attestations (genesis fork only) @@ -879,7 +893,7 @@ impl BeaconState { let aggregate_pubkey = AggregatePublicKey::aggregate(&decompressed_pubkeys)?; Ok(SyncCommittee { - pubkeys: FixedVector::new(pubkeys)?, + pubkeys: ssz_types::FixedVector::new(pubkeys)?, aggregate_pubkey: aggregate_pubkey.to_public_key().compress(), }) } @@ -974,7 +988,7 @@ impl BeaconState { /// Fill `randao_mixes` with pub fn fill_randao_mixes_with(&mut self, index_root: Hash256) { - *self.randao_mixes_mut() = FixedVector::from_elem(index_root); + *self.randao_mixes_raw_mut() = FixedVector::from_elem(index_root); } /// Safely obtains the index for `randao_mixes` @@ -1100,7 +1114,7 @@ impl BeaconState { } /// Get a reference to the entire `slashings` vector. - pub fn get_all_slashings(&self) -> &[u64] { + pub fn get_all_slashings(&self) -> &FixedVector { self.slashings() } @@ -1171,6 +1185,67 @@ impl BeaconState { match self { BeaconState::Base(state) => (state.validators.as_mut(), state.balances.as_mut()), BeaconState::Altair(state) => (state.validators.as_mut(), state.balances.as_mut()), + BeaconState::Merge(state) => (state.validators.as_mut(), state.balances.as_mut()), + } + } + + pub fn block_roots(&self) -> &FixedVector { + self.block_roots_raw() + } + + pub fn block_roots_mut(&mut self) -> VectMut { + #[cfg(not(feature = "milhouse"))] + { + self.block_roots_raw_mut() + } + #[cfg(feature = "milhouse")] + { + self.block_roots_raw_mut().as_mut() + } + } + + pub fn state_roots(&self) -> &FixedVector { + self.state_roots_raw() + } + + pub fn state_roots_mut(&mut self) -> VectMut { + #[cfg(not(feature = "milhouse"))] + { + self.state_roots_raw_mut() + } + #[cfg(feature = "milhouse")] + { + self.state_roots_raw_mut().as_mut() + } + } + + pub fn randao_mixes(&self) -> &FixedVector { + self.randao_mixes_raw() + } + + pub fn randao_mixes_mut(&mut self) -> VectMut { + #[cfg(not(feature = "milhouse"))] + { + self.randao_mixes_raw_mut() + } + #[cfg(feature = "milhouse")] + { + self.randao_mixes_raw_mut().as_mut() + } + } + + pub fn slashings(&self) -> &FixedVector { + self.slashings_raw() + } + + pub fn slashings_mut(&mut self) -> VectMut { + #[cfg(not(feature = "milhouse"))] + { + self.slashings_raw_mut() + } + #[cfg(feature = "milhouse")] + { + self.slashings_raw_mut().as_mut() } } diff --git a/consensus/types/src/deposit.rs b/consensus/types/src/deposit.rs index a347cf675c..67003a2fea 100644 --- a/consensus/types/src/deposit.rs +++ b/consensus/types/src/deposit.rs @@ -2,7 +2,7 @@ use crate::test_utils::TestRandom; use crate::*; use serde_derive::{Deserialize, Serialize}; use ssz_derive::{Decode, Encode}; -use ssz_types::typenum::U33; +use ssz_types::{typenum::U33, FixedVector}; use test_random_derive::TestRandom; use tree_hash_derive::TreeHash; diff --git a/consensus/types/src/execution_payload.rs b/consensus/types/src/execution_payload.rs index 2fb253f12c..0bc37510cf 100644 --- a/consensus/types/src/execution_payload.rs +++ b/consensus/types/src/execution_payload.rs @@ -6,6 +6,9 @@ use ssz_derive::{Decode, Encode}; use test_random_derive::TestRandom; use tree_hash_derive::TreeHash; +// FIXME(sproul): tree-ify the payload types +use ssz_types::{FixedVector, VariableList}; + pub type Transaction = VariableList; #[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))] diff --git a/consensus/types/src/execution_payload_header.rs b/consensus/types/src/execution_payload_header.rs index 6cb76a6465..4b43a527df 100644 --- a/consensus/types/src/execution_payload_header.rs +++ b/consensus/types/src/execution_payload_header.rs @@ -4,6 +4,9 @@ use ssz_derive::{Decode, Encode}; use test_random_derive::TestRandom; use tree_hash_derive::TreeHash; +// FIXME(sproul): tree-ify the payload types +use ssz_types::{FixedVector, VariableList}; + #[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))] #[derive( Default, Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom, diff --git a/consensus/types/src/historical_batch.rs b/consensus/types/src/historical_batch.rs index 325f5f8537..bf8c2c851b 100644 --- a/consensus/types/src/historical_batch.rs +++ b/consensus/types/src/historical_batch.rs @@ -1,17 +1,13 @@ -use crate::test_utils::TestRandom; use crate::*; - use serde_derive::{Deserialize, Serialize}; use ssz_derive::{Decode, Encode}; -use ssz_types::FixedVector; -use test_random_derive::TestRandom; use tree_hash_derive::TreeHash; /// Historical block and state roots. /// /// Spec v0.12.1 #[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))] -#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom)] +#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash)] pub struct HistoricalBatch { pub block_roots: FixedVector, pub state_roots: FixedVector, diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index 4a52f28b86..695ecbc101 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -166,8 +166,11 @@ pub use bls::{ AggregatePublicKey, AggregateSignature, Keypair, PublicKey, PublicKeyBytes, SecretKey, Signature, SignatureBytes, }; -pub use ssz_types::{typenum, typenum::Unsigned, BitList, BitVector, FixedVector, VariableList}; +pub use ssz_types::{typenum, typenum::Unsigned, BitList, BitVector, VariableList}; pub use superstruct::superstruct; #[cfg(feature = "milhouse")] -pub use milhouse; +pub use milhouse::{self, Vector as FixedVector}; + +#[cfg(not(feature = "milhouse"))] +pub use ssz_types::FixedVector; diff --git a/consensus/types/src/sync_committee.rs b/consensus/types/src/sync_committee.rs index 598d5fc16f..8bddb0b458 100644 --- a/consensus/types/src/sync_committee.rs +++ b/consensus/types/src/sync_committee.rs @@ -1,6 +1,6 @@ use crate::test_utils::TestRandom; use crate::typenum::Unsigned; -use crate::{EthSpec, FixedVector, SyncSubnetId}; +use crate::{EthSpec, SyncSubnetId}; use bls::PublicKeyBytes; use safe_arith::{ArithError, SafeArith}; use serde_derive::{Deserialize, Serialize}; @@ -9,6 +9,9 @@ use std::collections::HashMap; use test_random_derive::TestRandom; use tree_hash_derive::TreeHash; +// Use flat `FixedVector` regardless of whether or not tree states are used. +use ssz_types::FixedVector; + #[derive(Debug, PartialEq)] pub enum Error { ArithError(ArithError), diff --git a/consensus/types/src/test_utils/test_random.rs b/consensus/types/src/test_utils/test_random.rs index 064b57f428..5528558d25 100644 --- a/consensus/types/src/test_utils/test_random.rs +++ b/consensus/types/src/test_utils/test_random.rs @@ -79,7 +79,7 @@ where } } -impl TestRandom for FixedVector +impl TestRandom for ssz_types::FixedVector where T: TestRandom, {