From 1b4dad0d767e6d72b130e25b1295104113888a95 Mon Sep 17 00:00:00 2001 From: Michael Sproul Date: Wed, 24 Nov 2021 17:21:01 +1100 Subject: [PATCH] Persistent beacon state (consensus/types) --- Cargo.lock | 218 ++++++++++++------ Cargo.toml | 5 - consensus/types/Cargo.toml | 4 +- consensus/types/src/beacon_state.rs | 90 +++++--- .../types/src/beacon_state/committee_cache.rs | 17 +- .../types/src/beacon_state/exit_cache.rs | 8 +- consensus/types/src/lib.rs | 2 +- 7 files changed, 237 insertions(+), 107 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3831fbcf46..ac14f6e231 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -298,9 +298,9 @@ dependencies = [ "eth1", "eth2", "eth2_hashing 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "eth2_ssz", - "eth2_ssz_derive", - "eth2_ssz_types", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_types 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "fork_choice", "futures", "genesis", @@ -331,7 +331,7 @@ dependencies = [ "task_executor", "tempfile", "tokio", - "tree_hash", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", ] @@ -450,14 +450,14 @@ dependencies = [ "blst", "eth2_hashing 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "eth2_serde_utils 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "eth2_ssz", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.12.1", "hex", "milagro_bls", "rand 0.7.3", "serde", "serde_derive", - "tree_hash", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "zeroize", ] @@ -480,7 +480,7 @@ dependencies = [ "beacon_node", "clap", "clap_utils", - "eth2_ssz", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "hex", "lighthouse_network", "log", @@ -581,14 +581,14 @@ name = "cached_tree_hash" version = "0.1.0" dependencies = [ "eth2_hashing 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "eth2_ssz", - "eth2_ssz_derive", - "eth2_ssz_types", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_types 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.12.1", "quickcheck", "quickcheck_macros", "smallvec", - "tree_hash", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -681,7 +681,7 @@ dependencies = [ "clap", "dirs", "eth2_network_config", - "eth2_ssz", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "hex", ] @@ -1072,13 +1072,13 @@ checksum = "b72465f46d518f6015d9cf07f7f3013a95dd6b9c2747c3d65ae0cce43929d14f" name = "deposit_contract" version = "0.2.0" dependencies = [ - "eth2_ssz", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethabi 12.0.0", "hex", "reqwest", "serde_json", "sha2", - "tree_hash", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", ] @@ -1270,8 +1270,8 @@ dependencies = [ "compare_fields", "compare_fields_derive", "derivative", - "eth2_ssz", - "eth2_ssz_derive", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.12.1", "fs2", "hex", @@ -1284,8 +1284,8 @@ dependencies = [ "state_processing", "store", "swap_or_not_shuffle", - "tree_hash", - "tree_hash_derive", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tree_hash_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", ] @@ -1413,8 +1413,8 @@ dependencies = [ "environment", "eth1_test_rig", "eth2", - "eth2_ssz", - "eth2_ssz_derive", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "fallback", "futures", "hex", @@ -1432,7 +1432,7 @@ dependencies = [ "task_executor", "tokio", "toml", - "tree_hash", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", "web3", ] @@ -1456,8 +1456,8 @@ dependencies = [ "bytes", "eth2_keystore", "eth2_serde_utils 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "eth2_ssz", - "eth2_ssz_derive", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures", "futures-util", "libsecp256k1 0.6.0", @@ -1561,7 +1561,7 @@ version = "0.2.0" dependencies = [ "enr", "eth2_config", - "eth2_ssz", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde_yaml", "tempfile", "types", @@ -1593,7 +1593,17 @@ dependencies = [ name = "eth2_ssz" version = "0.4.0" dependencies = [ - "eth2_ssz_derive", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethereum-types 0.12.1", + "smallvec", +] + +[[package]] +name = "eth2_ssz" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "948e343aa022785c07193f41ed37adfd9dd0350368060803b8302c7f798e8306" +dependencies = [ "ethereum-types 0.12.1", "smallvec", ] @@ -1608,18 +1618,45 @@ dependencies = [ "syn", ] +[[package]] +name = "eth2_ssz_derive" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "635b86d2c941bb71e7419a571e1763d65c93e51a1bafc400352e3bef6ff59fc9" +dependencies = [ + "darling 0.13.0", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "eth2_ssz_types" version = "0.2.1" dependencies = [ "arbitrary", "eth2_serde_utils 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "eth2_ssz", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde", "serde_derive", "serde_json", - "tree_hash", - "tree_hash_derive", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tree_hash_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum", +] + +[[package]] +name = "eth2_ssz_types" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9423ac7fb37037f828a32b724cdfa65ea62290055811731402a90fb8a5bcbb1" +dependencies = [ + "arbitrary", + "eth2_serde_utils 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "serde", + "serde_derive", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "typenum", ] @@ -1884,8 +1921,8 @@ name = "fork_choice" version = "0.1.0" dependencies = [ "beacon_chain", - "eth2_ssz", - "eth2_ssz_derive", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "proto_array", "store", "types", @@ -2047,7 +2084,7 @@ dependencies = [ "eth1", "eth1_test_rig", "eth2_hashing 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "eth2_ssz", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures", "int_to_bytes", "merkle_proof", @@ -2056,7 +2093,7 @@ dependencies = [ "slog", "state_processing", "tokio", - "tree_hash", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", ] @@ -2329,7 +2366,7 @@ dependencies = [ "environment", "eth1", "eth2", - "eth2_ssz", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures", "hex", "lazy_static", @@ -2345,7 +2382,7 @@ dependencies = [ "store", "tokio", "tokio-stream", - "tree_hash", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", "warp", "warp_utils", @@ -2670,7 +2707,7 @@ dependencies = [ "eth1_test_rig", "eth2", "eth2_network_config", - "eth2_ssz", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "eth2_wallet", "genesis", "lighthouse_network", @@ -2681,7 +2718,7 @@ dependencies = [ "serde_json", "serde_yaml", "state_processing", - "tree_hash", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", "validator_dir", "web3", @@ -3215,9 +3252,9 @@ dependencies = [ "dirs", "discv5", "error-chain", - "eth2_ssz", - "eth2_ssz_derive", - "eth2_ssz_types", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_types 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "exit-future", "fnv", "futures", @@ -3240,7 +3277,7 @@ dependencies = [ "smallvec", "snap", "strum", - "superstruct", + "superstruct 0.2.0", "task_executor", "tempfile", "tiny-keccak 2.0.2", @@ -3435,6 +3472,19 @@ dependencies = [ "zeroize", ] +[[package]] +name = "milhouse" +version = "0.1.0" +dependencies = [ + "derivative", + "eth2_hashing 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "parking_lot", + "serde", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "typenum", +] + [[package]] name = "mime" version = "0.3.16" @@ -3611,8 +3661,8 @@ dependencies = [ "beacon_chain", "environment", "error-chain", - "eth2_ssz", - "eth2_ssz_types", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_types 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "exit-future", "fnv", "futures", @@ -3876,8 +3926,8 @@ version = "0.2.0" dependencies = [ "beacon_chain", "derivative", - "eth2_ssz", - "eth2_ssz_derive", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools", "lazy_static", "lighthouse_metrics", @@ -4387,8 +4437,8 @@ dependencies = [ name = "proto_array" version = "0.2.0" dependencies = [ - "eth2_ssz", - "eth2_ssz_derive", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde", "serde_derive", "serde_yaml", @@ -5229,8 +5279,8 @@ version = "0.1.0" dependencies = [ "bincode", "byteorder", - "eth2_ssz", - "eth2_ssz_derive", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "filesystem", "flate2", "lazy_static", @@ -5248,8 +5298,8 @@ dependencies = [ "slog", "sloggers", "tempfile", - "tree_hash", - "tree_hash_derive", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tree_hash_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", ] @@ -5508,8 +5558,8 @@ dependencies = [ "bls", "env_logger 0.9.0", "eth2_hashing 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "eth2_ssz", - "eth2_ssz_types", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_types 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "int_to_bytes", "integer-sqrt", "itertools", @@ -5519,7 +5569,7 @@ dependencies = [ "rayon", "safe_arith", "smallvec", - "tree_hash", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", ] @@ -5528,7 +5578,7 @@ name = "state_transition_vectors" version = "0.1.0" dependencies = [ "beacon_chain", - "eth2_ssz", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static", "state_processing", "types", @@ -5547,8 +5597,8 @@ dependencies = [ "beacon_chain", "db-key", "directory", - "eth2_ssz", - "eth2_ssz_derive", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "itertools", "lazy_static", "leveldb", @@ -5603,6 +5653,17 @@ version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" +[[package]] +name = "superstruct" +version = "0.1.0" +dependencies = [ + "darling 0.13.0", + "itertools", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "superstruct" version = "0.2.0" @@ -6093,15 +6154,26 @@ version = "0.4.0" dependencies = [ "beacon_chain", "eth2_hashing 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", - "eth2_ssz", - "eth2_ssz_derive", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.12.1", "rand 0.7.3", "smallvec", - "tree_hash_derive", + "tree_hash_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", ] +[[package]] +name = "tree_hash" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9c8a86fad3169a65aad2265d3c6a8bc119d0b771046af3c1b2fb0e9b12182b" +dependencies = [ + "eth2_hashing 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "ethereum-types 0.12.1", + "smallvec", +] + [[package]] name = "tree_hash_derive" version = "0.4.0" @@ -6111,6 +6183,17 @@ dependencies = [ "syn", ] +[[package]] +name = "tree_hash_derive" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3cd22d128157837a4434bb51119aef11103f17bfe8c402ce688cf25aa1e608ad" +dependencies = [ + "darling 0.13.0", + "quote", + "syn", +] + [[package]] name = "trust-dns-proto" version = "0.20.3" @@ -6211,15 +6294,16 @@ dependencies = [ "eth2_hashing 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "eth2_interop_keypairs", "eth2_serde_utils 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "eth2_ssz", - "eth2_ssz_derive", - "eth2_ssz_types", + "eth2_ssz 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_derive 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", + "eth2_ssz_types 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "ethereum-types 0.12.1", "hex", "int_to_bytes", "itertools", "lazy_static", "log", + "milhouse", "parking_lot", "rand 0.7.3", "rand_xorshift", @@ -6232,12 +6316,12 @@ dependencies = [ "serde_yaml", "slog", "state_processing", - "superstruct", + "superstruct 0.1.0", "swap_or_not_shuffle", "tempfile", "test_random_derive", - "tree_hash", - "tree_hash_derive", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "tree_hash_derive 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -6426,7 +6510,7 @@ dependencies = [ "task_executor", "tempfile", "tokio", - "tree_hash", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", "url", "validator_dir", @@ -6447,7 +6531,7 @@ dependencies = [ "lockfile", "rand 0.7.3", "tempfile", - "tree_hash", + "tree_hash 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "types", ] diff --git a/Cargo.toml b/Cargo.toml index a7789fa0fd..b005ce1c19 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,10 +85,5 @@ members = [ [patch] [patch.crates-io] -eth2_ssz = { path = "consensus/ssz" } -eth2_ssz_types = { path = "consensus/ssz_types" } -eth2_ssz_derive = { path = "consensus/ssz_derive" } -tree_hash = { path = "consensus/tree_hash" } -tree_hash_derive = { path = "consensus/tree_hash_derive" } fixed-hash = { git = "https://github.com/paritytech/parity-common", rev="df638ab0885293d21d656dc300d39236b69ce57d" } warp = { git = "https://github.com/macladson/warp", rev ="7e75acc" } diff --git a/consensus/types/Cargo.toml b/consensus/types/Cargo.toml index 1f9ea10c64..eeeeb8568a 100644 --- a/consensus/types/Cargo.toml +++ b/consensus/types/Cargo.toml @@ -43,7 +43,9 @@ regex = "1.3.9" lazy_static = "1.4.0" parking_lot = "0.11.1" itertools = "0.10.0" -superstruct = "0.2.0" +# superstruct = "0.2.0" +superstruct = { path = "../../../superstruct" } +milhouse = { path = "../../../milhouse", optional = true } [dev-dependencies] criterion = "0.3.3" diff --git a/consensus/types/src/beacon_state.rs b/consensus/types/src/beacon_state.rs index a12f35143f..b8c7d9ac22 100644 --- a/consensus/types/src/beacon_state.rs +++ b/consensus/types/src/beacon_state.rs @@ -12,7 +12,7 @@ use safe_arith::{ArithError, SafeArith}; use serde_derive::{Deserialize, Serialize}; use ssz::{ssz_encode, Decode, DecodeError, Encode}; use ssz_derive::{Decode, Encode}; -use ssz_types::{typenum::Unsigned, BitVector, FixedVector}; +use ssz_types::{typenum::Unsigned, BitVector}; use std::convert::TryInto; use std::{fmt, mem, sync::Arc}; use superstruct::superstruct; @@ -28,7 +28,12 @@ pub use self::committee_cache::{ pub use clone_config::CloneConfig; pub use eth_spec::*; pub use iter::BlockRootsIter; -pub use tree_hash_cache::BeaconTreeHashCache; + +#[cfg(feature = "milhouse")] +use milhouse::prelude::{List as VList, *}; + +#[cfg(not(feature = "milhouse"))] +use {ssz_types::FixedVector, tree_hash_cache::BeaconTreeHashCache, VariableList as VList}; #[macro_use] mod committee_cache; @@ -37,8 +42,13 @@ mod exit_cache; mod iter; mod pubkey_cache; mod tests; +#[cfg(not(feature = "milhouse"))] mod tree_hash_cache; +#[cfg(feature = "milhouse")] +pub type ListMut<'a, T, N> = Interface>; +pub type ValidatorsMut<'a, N> = ListMut<'a, Validator, N>; + pub const CACHED_EPOCHS: usize = 3; const MAX_RANDOM_BYTE: u64 = (1 << 8) - 1; @@ -230,8 +240,8 @@ where pub eth1_deposit_index: u64, // Registry - #[compare_fields(as_slice)] - pub validators: VariableList, + #[test_random(default)] + pub validators: VList, #[compare_fields(as_slice)] #[serde(with = "ssz_types::serde_utils::quoted_u64_var_list")] pub balances: VariableList, @@ -306,6 +316,7 @@ where #[tree_hash(skip_hashing)] #[test_random(default)] #[derivative(Clone(clone_with = "clone_default"))] + #[cfg(not(feature = "milhouse"))] pub tree_hash_cache: BeaconTreeHashCache, } @@ -343,8 +354,8 @@ impl BeaconState { eth1_deposit_index: 0, // Validator registry - validators: VariableList::empty(), // Set later. - balances: VariableList::empty(), // Set later. + validators: VList::empty(), // Set later. + balances: VariableList::empty(), // Set later. // Randomness randao_mixes: FixedVector::from_elem(Hash256::zero()), @@ -371,6 +382,7 @@ impl BeaconState { ], pubkey_cache: PubkeyCache::default(), exit_cache: ExitCache::default(), + #[cfg(not(feature = "milhouse"))] tree_hash_cache: <_>::default(), }) } @@ -1085,6 +1097,7 @@ impl BeaconState { } /// Convenience accessor for validators and balances simultaneously. + #[cfg(not(feature = "milhouse"))] pub fn validators_and_balances_mut(&mut self) -> (&mut [Validator], &mut [u64]) { match self { BeaconState::Base(state) => (&mut state.validators, &mut state.balances), @@ -1092,6 +1105,16 @@ impl BeaconState { } } + #[cfg(feature = "milhouse")] + pub fn validators_and_balances_mut( + &mut self, + ) -> (ValidatorsMut, &mut [u64]) { + match self { + BeaconState::Base(state) => (state.validators.as_mut(), &mut state.balances), + BeaconState::Altair(state) => (state.validators.as_mut(), &mut state.balances), + } + } + /// Generate a seed for the given `epoch`. pub fn get_seed( &self, @@ -1134,12 +1157,14 @@ impl BeaconState { .ok_or(Error::UnknownValidator(validator_index)) } + /* FIXME(sproul): lens? /// Safe mutator for the `validators` list. pub fn get_validator_mut(&mut self, validator_index: usize) -> Result<&mut Validator, Error> { self.validators_mut() .get_mut(validator_index) .ok_or(Error::UnknownValidator(validator_index)) } + */ /// Return the effective balance for a validator with the given `validator_index`. pub fn get_effective_balance(&self, validator_index: usize) -> Result { @@ -1519,6 +1544,7 @@ impl BeaconState { /// Initialize but don't fill the tree hash cache, if it isn't already initialized. pub fn initialize_tree_hash_cache(&mut self) { + #[cfg(not(feature = "milhouse"))] if !self.tree_hash_cache().is_initialized() { *self.tree_hash_cache_mut() = BeaconTreeHashCache::new(self) } @@ -1528,42 +1554,53 @@ impl BeaconState { /// /// Initialize the tree hash cache if it isn't already initialized. pub fn update_tree_hash_cache(&mut self) -> Result { - self.initialize_tree_hash_cache(); + #[cfg(not(feature = "milhouse"))] + { + self.initialize_tree_hash_cache(); - let cache = self.tree_hash_cache_mut().take(); + let cache = self.tree_hash_cache_mut().take(); - if let Some(mut cache) = cache { - // Note: we return early if the tree hash fails, leaving `self.tree_hash_cache` as - // None. There's no need to keep a cache that fails. - let root = cache.recalculate_tree_hash_root(self)?; - self.tree_hash_cache_mut().restore(cache); - Ok(root) - } else { - Err(Error::TreeHashCacheNotInitialized) + if let Some(mut cache) = cache { + // Note: we return early if the tree hash fails, leaving `self.tree_hash_cache` as + // None. There's no need to keep a cache that fails. + let root = cache.recalculate_tree_hash_root(self)?; + self.tree_hash_cache_mut().restore(cache); + Ok(root) + } else { + Err(Error::TreeHashCacheNotInitialized) + } } + #[cfg(feature = "milhouse")] + Ok(self.tree_hash_root()) } /// Compute the tree hash root of the validators using the tree hash cache. /// /// Initialize the tree hash cache if it isn't already initialized. pub fn update_validators_tree_hash_cache(&mut self) -> Result { - self.initialize_tree_hash_cache(); + #[cfg(not(feature = "milhouse"))] + { + self.initialize_tree_hash_cache(); - let cache = self.tree_hash_cache_mut().take(); + let cache = self.tree_hash_cache_mut().take(); - if let Some(mut cache) = cache { - // Note: we return early if the tree hash fails, leaving `self.tree_hash_cache` as - // None. There's no need to keep a cache that fails. - let root = cache.recalculate_validators_tree_hash_root(self.validators())?; - self.tree_hash_cache_mut().restore(cache); - Ok(root) - } else { - Err(Error::TreeHashCacheNotInitialized) + if let Some(mut cache) = cache { + // Note: we return early if the tree hash fails, leaving `self.tree_hash_cache` as + // None. There's no need to keep a cache that fails. + let root = cache.recalculate_validators_tree_hash_root(self.validators())?; + self.tree_hash_cache_mut().restore(cache); + Ok(root) + } else { + Err(Error::TreeHashCacheNotInitialized) + } } + #[cfg(feature = "milhouse")] + Ok(self.validators().tree_hash_root()) } /// Completely drops the tree hash cache, replacing it with a new, empty cache. pub fn drop_tree_hash_cache(&mut self) { + #[cfg(not(feature = "milhouse"))] self.tree_hash_cache_mut().uninitialize(); } @@ -1583,6 +1620,7 @@ impl BeaconState { if config.exit_cache { *res.exit_cache_mut() = self.exit_cache().clone(); } + #[cfg(not(feature = "milhouse"))] if config.tree_hash_cache { *res.tree_hash_cache_mut() = self.tree_hash_cache().clone(); } diff --git a/consensus/types/src/beacon_state/committee_cache.rs b/consensus/types/src/beacon_state/committee_cache.rs index 8a87cddac8..3dce0103f0 100644 --- a/consensus/types/src/beacon_state/committee_cache.rs +++ b/consensus/types/src/beacon_state/committee_cache.rs @@ -10,6 +10,9 @@ use ssz_derive::{Decode, Encode}; use std::ops::Range; use swap_or_not_shuffle::shuffle_list; +#[cfg(feature = "milhouse")] +use milhouse::prelude::*; + mod tests; // Define "legacy" implementations of `Option`, `Option` which use four bytes @@ -312,17 +315,21 @@ pub fn epoch_committee_count(committees_per_slot: usize, slots_per_epoch: usize) /// `epoch`. /// /// Spec v0.12.1 -pub fn get_active_validator_indices(validators: &[Validator], epoch: Epoch) -> Vec { - let mut active = Vec::with_capacity(validators.len()); +pub fn get_active_validator_indices<'a, V, I>(validators: V, epoch: Epoch) -> Vec +where + V: IntoIterator, + I: ExactSizeIterator + Iterator, +{ + let iter = validators.into_iter(); - for (index, validator) in validators.iter().enumerate() { + let mut active = Vec::with_capacity(iter.len()); + + for (index, validator) in iter.enumerate() { if validator.is_active_at(epoch) { active.push(index) } } - active.shrink_to_fit(); - active } diff --git a/consensus/types/src/beacon_state/exit_cache.rs b/consensus/types/src/beacon_state/exit_cache.rs index 1c199c0475..c81b68752e 100644 --- a/consensus/types/src/beacon_state/exit_cache.rs +++ b/consensus/types/src/beacon_state/exit_cache.rs @@ -12,14 +12,18 @@ pub struct ExitCache { impl ExitCache { /// Initialize a new cache for the given list of validators. - pub fn new(validators: &[Validator], spec: &ChainSpec) -> Result { + pub fn new<'a, V, I>(validators: V, spec: &ChainSpec) -> Result + where + V: IntoIterator, + I: ExactSizeIterator + Iterator, + { let mut exit_cache = ExitCache { initialized: true, ..ExitCache::default() }; // Add all validators with a non-default exit epoch to the cache. validators - .iter() + .into_iter() .filter(|validator| validator.exit_epoch != spec.far_future_epoch) .try_for_each(|validator| exit_cache.record_validator_exit(validator.exit_epoch))?; Ok(exit_cache) diff --git a/consensus/types/src/lib.rs b/consensus/types/src/lib.rs index 403544f007..4e78ffd7bb 100644 --- a/consensus/types/src/lib.rs +++ b/consensus/types/src/lib.rs @@ -98,7 +98,7 @@ pub use crate::beacon_block_body::{ }; pub use crate::beacon_block_header::BeaconBlockHeader; pub use crate::beacon_committee::{BeaconCommittee, OwnedBeaconCommittee}; -pub use crate::beacon_state::{BeaconTreeHashCache, Error as BeaconStateError, *}; +pub use crate::beacon_state::{Error as BeaconStateError, *}; pub use crate::chain_spec::{ChainSpec, Config, Domain}; pub use crate::checkpoint::Checkpoint; pub use crate::config_and_preset::ConfigAndPreset;