Start using tree vector

This commit is contained in:
Michael Sproul
2022-01-25 18:59:26 +11:00
parent 6714edf95b
commit 96bdc29419
11 changed files with 113 additions and 29 deletions

View File

@@ -408,7 +408,7 @@ where
.find_head::<E>( .find_head::<E>(
*store.justified_checkpoint(), *store.justified_checkpoint(),
*store.finalized_checkpoint(), *store.finalized_checkpoint(),
store.justified_balances(), &justified_balances,
store.proposer_boost_root(), store.proposer_boost_root(),
spec, spec,
) )

View File

@@ -63,6 +63,7 @@ pub fn upgrade_to_bellatrix<E: EthSpec>(
committee_caches: mem::take(&mut pre.committee_caches), committee_caches: mem::take(&mut pre.committee_caches),
pubkey_cache: mem::take(&mut pre.pubkey_cache), pubkey_cache: mem::take(&mut pre.pubkey_cache),
exit_cache: mem::take(&mut pre.exit_cache), exit_cache: mem::take(&mut pre.exit_cache),
#[cfg(not(feature = "milhouse"))]
tree_hash_cache: mem::take(&mut pre.tree_hash_cache), tree_hash_cache: mem::take(&mut pre.tree_hash_cache),
}); });

View File

@@ -329,7 +329,7 @@ impl<T: EthSpec> BeaconBlockBase<T> {
}; };
let deposit = Deposit { let deposit = Deposit {
proof: FixedVector::from_elem(Hash256::zero()), proof: ssz_types::FixedVector::from_elem(Hash256::zero()),
data: deposit_data, data: deposit_data,
}; };

View File

@@ -30,7 +30,7 @@ pub use eth_spec::*;
pub use iter::BlockRootsIter; pub use iter::BlockRootsIter;
#[cfg(feature = "milhouse")] #[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"))] #[cfg(not(feature = "milhouse"))]
pub use { pub use {
@@ -48,19 +48,26 @@ mod tests;
mod tree_hash_cache; mod tree_hash_cache;
#[cfg(feature = "milhouse")] #[cfg(feature = "milhouse")]
pub type ListMut<'a, T, N> = Interface<'a, T, List<T, N>>; mod type_aliases {
#[cfg(not(feature = "milhouse"))] use super::*;
pub type ListMut<'a, T, N> = &'a mut VList<T, N>;
#[cfg(feature = "milhouse")] pub type ListMut<'a, T, N> = Interface<'a, T, List<T, N>>;
pub type ValidatorsMut<'a, N> = ListMut<'a, Validator, N>; pub type VectMut<'a, T, N> = Interface<'a, T, FixedVector<T, N>>;
#[cfg(not(feature = "milhouse"))] pub type ValidatorsMut<'a, N> = ListMut<'a, Validator, N>;
pub type ValidatorsMut<'a, N> = &'a mut VList<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"))] #[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<T, N>;
pub type VectMut<'a, T, N> = &'a mut FixedVector<T, N>;
pub type ValidatorsMut<'a, N> = &'a mut VList<Validator, N>;
pub type BalancesMut<'a, N> = ListMut<'a, u64, N>;
}
pub use type_aliases::*;
pub const CACHED_EPOCHS: usize = 3; pub const CACHED_EPOCHS: usize = 3;
const MAX_RANDOM_BYTE: u64 = (1 << 8) - 1; const MAX_RANDOM_BYTE: u64 = (1 << 8) - 1;
@@ -241,9 +248,11 @@ where
// History // History
pub latest_block_header: BeaconBlockHeader, pub latest_block_header: BeaconBlockHeader,
#[compare_fields(as_slice)] #[superstruct(getter(rename = "block_roots_raw"))]
#[test_random(default)]
pub block_roots: FixedVector<Hash256, T::SlotsPerHistoricalRoot>, pub block_roots: FixedVector<Hash256, T::SlotsPerHistoricalRoot>,
#[compare_fields(as_slice)] #[superstruct(getter(rename = "state_roots_raw"))]
#[test_random(default)]
pub state_roots: FixedVector<Hash256, T::SlotsPerHistoricalRoot>, pub state_roots: FixedVector<Hash256, T::SlotsPerHistoricalRoot>,
#[test_random(default)] #[test_random(default)]
pub historical_roots: VList<Hash256, T::HistoricalRootsLimit>, pub historical_roots: VList<Hash256, T::HistoricalRootsLimit>,
@@ -267,10 +276,15 @@ where
pub balances: VList<u64, T::ValidatorRegistryLimit>, pub balances: VList<u64, T::ValidatorRegistryLimit>,
// Randomness // Randomness
#[superstruct(getter(rename = "randao_mixes_raw"))]
#[test_random(default)]
pub randao_mixes: FixedVector<Hash256, T::EpochsPerHistoricalVector>, pub randao_mixes: FixedVector<Hash256, T::EpochsPerHistoricalVector>,
// Slashings // 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<u64, T::EpochsPerSlashingsVector>, pub slashings: FixedVector<u64, T::EpochsPerSlashingsVector>,
// Attestations (genesis fork only) // Attestations (genesis fork only)
@@ -879,7 +893,7 @@ impl<T: EthSpec> BeaconState<T> {
let aggregate_pubkey = AggregatePublicKey::aggregate(&decompressed_pubkeys)?; let aggregate_pubkey = AggregatePublicKey::aggregate(&decompressed_pubkeys)?;
Ok(SyncCommittee { Ok(SyncCommittee {
pubkeys: FixedVector::new(pubkeys)?, pubkeys: ssz_types::FixedVector::new(pubkeys)?,
aggregate_pubkey: aggregate_pubkey.to_public_key().compress(), aggregate_pubkey: aggregate_pubkey.to_public_key().compress(),
}) })
} }
@@ -974,7 +988,7 @@ impl<T: EthSpec> BeaconState<T> {
/// Fill `randao_mixes` with /// Fill `randao_mixes` with
pub fn fill_randao_mixes_with(&mut self, index_root: Hash256) { 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` /// Safely obtains the index for `randao_mixes`
@@ -1100,7 +1114,7 @@ impl<T: EthSpec> BeaconState<T> {
} }
/// Get a reference to the entire `slashings` vector. /// Get a reference to the entire `slashings` vector.
pub fn get_all_slashings(&self) -> &[u64] { pub fn get_all_slashings(&self) -> &FixedVector<u64, T::EpochsPerSlashingsVector> {
self.slashings() self.slashings()
} }
@@ -1171,6 +1185,67 @@ impl<T: EthSpec> BeaconState<T> {
match self { match self {
BeaconState::Base(state) => (state.validators.as_mut(), state.balances.as_mut()), BeaconState::Base(state) => (state.validators.as_mut(), state.balances.as_mut()),
BeaconState::Altair(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<Hash256, T::SlotsPerHistoricalRoot> {
self.block_roots_raw()
}
pub fn block_roots_mut(&mut self) -> VectMut<Hash256, T::SlotsPerHistoricalRoot> {
#[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<Hash256, T::SlotsPerHistoricalRoot> {
self.state_roots_raw()
}
pub fn state_roots_mut(&mut self) -> VectMut<Hash256, T::SlotsPerHistoricalRoot> {
#[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<Hash256, T::EpochsPerHistoricalVector> {
self.randao_mixes_raw()
}
pub fn randao_mixes_mut(&mut self) -> VectMut<Hash256, T::EpochsPerHistoricalVector> {
#[cfg(not(feature = "milhouse"))]
{
self.randao_mixes_raw_mut()
}
#[cfg(feature = "milhouse")]
{
self.randao_mixes_raw_mut().as_mut()
}
}
pub fn slashings(&self) -> &FixedVector<u64, T::EpochsPerSlashingsVector> {
self.slashings_raw()
}
pub fn slashings_mut(&mut self) -> VectMut<u64, T::EpochsPerSlashingsVector> {
#[cfg(not(feature = "milhouse"))]
{
self.slashings_raw_mut()
}
#[cfg(feature = "milhouse")]
{
self.slashings_raw_mut().as_mut()
} }
} }

View File

@@ -2,7 +2,7 @@ use crate::test_utils::TestRandom;
use crate::*; use crate::*;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode}; use ssz_derive::{Decode, Encode};
use ssz_types::typenum::U33; use ssz_types::{typenum::U33, FixedVector};
use test_random_derive::TestRandom; use test_random_derive::TestRandom;
use tree_hash_derive::TreeHash; use tree_hash_derive::TreeHash;

View File

@@ -6,6 +6,9 @@ use ssz_derive::{Decode, Encode};
use test_random_derive::TestRandom; use test_random_derive::TestRandom;
use tree_hash_derive::TreeHash; use tree_hash_derive::TreeHash;
// FIXME(sproul): tree-ify the payload types
use ssz_types::{FixedVector, VariableList};
pub type Transaction<T> = VariableList<u8, T>; pub type Transaction<T> = VariableList<u8, T>;
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))] #[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]

View File

@@ -4,6 +4,9 @@ use ssz_derive::{Decode, Encode};
use test_random_derive::TestRandom; use test_random_derive::TestRandom;
use tree_hash_derive::TreeHash; 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))] #[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))]
#[derive( #[derive(
Default, Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom, Default, Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, TreeHash, TestRandom,

View File

@@ -1,17 +1,13 @@
use crate::test_utils::TestRandom;
use crate::*; use crate::*;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode}; use ssz_derive::{Decode, Encode};
use ssz_types::FixedVector;
use test_random_derive::TestRandom;
use tree_hash_derive::TreeHash; use tree_hash_derive::TreeHash;
/// Historical block and state roots. /// Historical block and state roots.
/// ///
/// Spec v0.12.1 /// Spec v0.12.1
#[cfg_attr(feature = "arbitrary-fuzz", derive(arbitrary::Arbitrary))] #[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<T: EthSpec> { pub struct HistoricalBatch<T: EthSpec> {
pub block_roots: FixedVector<Hash256, T::SlotsPerHistoricalRoot>, pub block_roots: FixedVector<Hash256, T::SlotsPerHistoricalRoot>,
pub state_roots: FixedVector<Hash256, T::SlotsPerHistoricalRoot>, pub state_roots: FixedVector<Hash256, T::SlotsPerHistoricalRoot>,

View File

@@ -166,8 +166,11 @@ pub use bls::{
AggregatePublicKey, AggregateSignature, Keypair, PublicKey, PublicKeyBytes, SecretKey, AggregatePublicKey, AggregateSignature, Keypair, PublicKey, PublicKeyBytes, SecretKey,
Signature, SignatureBytes, 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; pub use superstruct::superstruct;
#[cfg(feature = "milhouse")] #[cfg(feature = "milhouse")]
pub use milhouse; pub use milhouse::{self, Vector as FixedVector};
#[cfg(not(feature = "milhouse"))]
pub use ssz_types::FixedVector;

View File

@@ -1,6 +1,6 @@
use crate::test_utils::TestRandom; use crate::test_utils::TestRandom;
use crate::typenum::Unsigned; use crate::typenum::Unsigned;
use crate::{EthSpec, FixedVector, SyncSubnetId}; use crate::{EthSpec, SyncSubnetId};
use bls::PublicKeyBytes; use bls::PublicKeyBytes;
use safe_arith::{ArithError, SafeArith}; use safe_arith::{ArithError, SafeArith};
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
@@ -9,6 +9,9 @@ use std::collections::HashMap;
use test_random_derive::TestRandom; use test_random_derive::TestRandom;
use tree_hash_derive::TreeHash; use tree_hash_derive::TreeHash;
// Use flat `FixedVector` regardless of whether or not tree states are used.
use ssz_types::FixedVector;
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq)]
pub enum Error { pub enum Error {
ArithError(ArithError), ArithError(ArithError),

View File

@@ -79,7 +79,7 @@ where
} }
} }
impl<T, N: Unsigned> TestRandom for FixedVector<T, N> impl<T, N: Unsigned> TestRandom for ssz_types::FixedVector<T, N>
where where
T: TestRandom, T: TestRandom,
{ {