From 73c7ad73b820a1ba6da2a7460c2ebf0897d11208 Mon Sep 17 00:00:00 2001 From: Emilia Hane Date: Tue, 14 Feb 2023 13:33:38 +0100 Subject: [PATCH] Disable use of system time in tests --- beacon_node/beacon_chain/src/builder.rs | 4 +- .../beacon_chain/src/shuffling_cache.rs | 4 +- .../beacon_chain/src/snapshot_cache.rs | 4 +- beacon_node/beacon_chain/src/test_utils.rs | 819 +++++++++--------- .../src/validator_pubkey_cache.rs | 4 +- .../tests/attestation_verification.rs | 9 +- .../beacon_chain/tests/block_verification.rs | 15 +- .../tests/payload_invalidation.rs | 6 +- beacon_node/beacon_chain/tests/rewards.rs | 5 +- .../tests/sync_committee_verification.rs | 14 +- beacon_node/beacon_chain/tests/tests.rs | 10 +- .../client/src/address_change_broadcast.rs | 6 +- beacon_node/http_api/tests/common.rs | 9 +- beacon_node/http_api/tests/tests.rs | 9 +- beacon_node/http_metrics/tests/tests.rs | 4 +- .../network/src/beacon_processor/tests.rs | 4 +- beacon_node/network/src/service/tests.rs | 6 +- .../network/src/sync/block_lookups/tests.rs | 6 +- .../network/src/sync/range_sync/range.rs | 6 +- beacon_node/operation_pool/src/lib.rs | 20 +- consensus/fork_choice/tests/tests.rs | 6 +- .../src/per_block_processing/tests.rs | 8 +- .../examples/flamegraph_beacon_state.rs | 4 +- .../src/beacon_state/committee_cache/tests.rs | 6 +- consensus/types/src/beacon_state/tests.rs | 6 +- lcli/src/transition_blocks.rs | 4 +- testing/ef_tests/src/cases/fork_choice.rs | 32 +- testing/state_transition_vectors/src/exit.rs | 10 +- testing/state_transition_vectors/src/main.rs | 4 +- 29 files changed, 485 insertions(+), 559 deletions(-) diff --git a/beacon_node/beacon_chain/src/builder.rs b/beacon_node/beacon_chain/src/builder.rs index 0f29c9e09a..bd78b2a3e7 100644 --- a/beacon_node/beacon_chain/src/builder.rs +++ b/beacon_node/beacon_chain/src/builder.rs @@ -1017,7 +1017,7 @@ fn descriptive_db_error(item: &str, error: &StoreError) -> String { #[cfg(test)] mod test { use super::*; - use crate::test_utils::EphemeralTestingSlotClockHarnessType; + use crate::test_utils::EphemeralHarnessType; use crate::validator_monitor::DEFAULT_INDIVIDUAL_TRACKING_THRESHOLD; use eth2_hashing::hash; use genesis::{ @@ -1032,7 +1032,7 @@ mod test { use types::{EthSpec, MinimalEthSpec, Slot}; type TestEthSpec = MinimalEthSpec; - type Builder = BeaconChainBuilder>; + type Builder = BeaconChainBuilder>; fn get_logger() -> Logger { let builder = NullLoggerBuilder; diff --git a/beacon_node/beacon_chain/src/shuffling_cache.rs b/beacon_node/beacon_chain/src/shuffling_cache.rs index 15b9a33cb1..a01847a0e1 100644 --- a/beacon_node/beacon_chain/src/shuffling_cache.rs +++ b/beacon_node/beacon_chain/src/shuffling_cache.rs @@ -208,11 +208,11 @@ impl BlockShufflingIds { #[cfg(test)] mod test { use super::*; - use crate::test_utils::EphemeralTestingSlotClockHarnessType; + use crate::test_utils::EphemeralHarnessType; use types::*; type BeaconChainHarness = - crate::test_utils::BeaconChainHarness>; + crate::test_utils::BeaconChainHarness>; /// Returns two different committee caches for testing. fn committee_caches() -> (Arc, Arc) { diff --git a/beacon_node/beacon_chain/src/snapshot_cache.rs b/beacon_node/beacon_chain/src/snapshot_cache.rs index 91d20dbd37..d2846c0856 100644 --- a/beacon_node/beacon_chain/src/snapshot_cache.rs +++ b/beacon_node/beacon_chain/src/snapshot_cache.rs @@ -365,13 +365,13 @@ impl SnapshotCache { #[cfg(test)] mod test { use super::*; - use crate::test_utils::{BeaconChainHarness, EphemeralTestingSlotClockHarnessType}; + use crate::test_utils::{BeaconChainHarness, EphemeralHarnessType}; use types::{ test_utils::generate_deterministic_keypair, BeaconBlock, Epoch, MainnetEthSpec, SignedBeaconBlock, Slot, }; - fn get_harness() -> BeaconChainHarness> { + fn get_harness() -> BeaconChainHarness> { let harness = BeaconChainHarness::builder(MainnetEthSpec) .default_spec() .deterministic_keypairs(1) diff --git a/beacon_node/beacon_chain/src/test_utils.rs b/beacon_node/beacon_chain/src/test_utils.rs index d2abcadd02..a523e55bc2 100644 --- a/beacon_node/beacon_chain/src/test_utils.rs +++ b/beacon_node/beacon_chain/src/test_utils.rs @@ -37,7 +37,7 @@ use sensitive_url::SensitiveUrl; use slog::{o, Drain, Logger}; use slog_async::Async; use slog_term::{FullFormat, TermDecorator}; -use slot_clock::{SlotClock, SystemTimeSlotClock, TestingSlotClock}; +use slot_clock::{SlotClock, TestingSlotClock}; use state_processing::per_block_processing::compute_timestamp_at_slot; use state_processing::{ state_advance::{complete_state_advance, partial_state_advance}, @@ -67,25 +67,16 @@ const FORK_NAME_ENV_VAR: &str = "FORK_NAME"; // a different value. pub const DEFAULT_TARGET_AGGREGATORS: u64 = u64::max_value(); -pub type BaseHarnessType = - Witness, TEthSpec, THotStore, TColdStore>; +pub type BaseHarnessType = + Witness, TEthSpec, THotStore, TColdStore>; -pub type DiskHarnessType = BaseHarnessType, LevelDB, TSlotClock>; +pub type DiskHarnessType = BaseHarnessType, LevelDB>; +pub type EphemeralHarnessType = BaseHarnessType, MemoryStore>; -pub type EphemeralHarnessType = - BaseHarnessType, MemoryStore, TSlotClock>; -pub type EphemeralTestingSlotClockHarnessType = - BaseHarnessType, MemoryStore, TestingSlotClock>; -pub type EphemeralSystemTimeSlotClockHarnessType = - BaseHarnessType, MemoryStore, SystemTimeSlotClock>; - -pub type TestingSlotClockHarnessType = - BaseHarnessType; - -pub type BoxedMutator = Box< +pub type BoxedMutator = Box< dyn FnOnce( - BeaconChainBuilder>, - ) -> BeaconChainBuilder>, + BeaconChainBuilder>, + ) -> BeaconChainBuilder>, >; pub type AddBlocksResult = ( @@ -157,7 +148,7 @@ pub fn test_spec() -> ChainSpec { spec } -pub struct Builder { +pub struct Builder { eth_spec_instance: T::EthSpec, spec: Option, validator_keypairs: Option>, @@ -166,19 +157,17 @@ pub struct Builder { store_config: Option, #[allow(clippy::type_complexity)] store: Option>>, - #[allow(clippy::type_complexity)] - initial_mutator: Option>, - #[allow(clippy::type_complexity)] - store_mutator: Option>, + initial_mutator: Option>, + store_mutator: Option>, execution_layer: Option>, mock_execution_layer: Option>, mock_builder: Option>, - testing_slot_clock: Option, + testing_slot_clock: Option, runtime: TestRuntime, log: Logger, } -impl Builder, S> { +impl Builder> { pub fn fresh_ephemeral_store(mut self) -> Self { let spec = self.spec.as_ref().expect("cannot build without spec"); let validator_keypairs = self @@ -247,7 +236,7 @@ impl Builder, S> { } } -impl Builder, S> { +impl Builder> { /// Disk store, start from genesis. pub fn fresh_disk_store(mut self, store: Arc, LevelDB>>) -> Self { let validator_keypairs = self @@ -284,24 +273,11 @@ impl Builder, S> { } } -impl Builder, TestingSlotClock> +impl Builder> where E: EthSpec, Hot: ItemStore, Cold: ItemStore, -{ - pub fn testing_slot_clock(mut self, slot_clock: TestingSlotClock) -> Self { - self.testing_slot_clock = Some(slot_clock); - self - } -} - -impl Builder, S> -where - E: EthSpec, - Hot: ItemStore, - Cold: ItemStore, - S: SlotClock + 'static, { pub fn new(eth_spec_instance: E) -> Self { let runtime = TestRuntime::default(); @@ -377,7 +353,7 @@ where } /// This mutator will be run before the `store_mutator`. - pub fn initial_mutator(mut self, mutator: BoxedMutator) -> Self { + pub fn initial_mutator(mut self, mutator: BoxedMutator) -> Self { assert!( self.initial_mutator.is_none(), "initial mutator already set" @@ -387,14 +363,14 @@ where } /// This mutator will be run after the `initial_mutator`. - pub fn store_mutator(mut self, mutator: BoxedMutator) -> Self { + pub fn store_mutator(mut self, mutator: BoxedMutator) -> Self { assert!(self.store_mutator.is_none(), "store mutator already set"); self.store_mutator = Some(mutator); self } /// Purposefully replace the `store_mutator`. - pub fn override_store_mutator(mut self, mutator: BoxedMutator) -> Self { + pub fn override_store_mutator(mut self, mutator: BoxedMutator) -> Self { assert!(self.store_mutator.is_some(), "store mutator not set"); self.store_mutator = Some(mutator); self @@ -532,7 +508,12 @@ where self } - pub fn build(self) -> BeaconChainHarness> { + pub fn testing_slot_clock(mut self, slot_clock: TestingSlotClock) -> Self { + self.testing_slot_clock = Some(slot_clock); + self + } + + pub fn build(self) -> BeaconChainHarness> { let (shutdown_tx, shutdown_receiver) = futures::channel::mpsc::channel(1); let log = self.log; @@ -636,369 +617,13 @@ pub type HarnessSyncContributions = Vec<( Option>, )>; -impl BeaconChainHarness> +impl BeaconChainHarness> where E: EthSpec, Hot: ItemStore, Cold: ItemStore, { - pub fn set_current_slot(&self, slot: Slot) { - let current_slot = self.chain.slot().unwrap(); - let current_epoch = current_slot.epoch(E::slots_per_epoch()); - let epoch = slot.epoch(E::slots_per_epoch()); - assert!( - epoch >= current_epoch, - "Jumping backwards to an earlier epoch isn't well defined. \ - Please generate test blocks epoch-by-epoch instead." - ); - self.chain.slot_clock.set_slot(slot.into()); - } - - pub async fn process_block( - &self, - slot: Slot, - block_root: Hash256, - block: SignedBeaconBlock, - ) -> Result> { - self.set_current_slot(slot); - let block_hash: SignedBeaconBlockHash = self - .chain - .process_block( - block_root, - Arc::new(block), - CountUnrealized::True, - NotifyExecutionLayer::Yes, - ) - .await? - .into(); - self.chain.recompute_head_at_current_slot().await; - Ok(block_hash) - } - - pub async fn add_block_at_slot( - &self, - slot: Slot, - state: BeaconState, - ) -> Result<(SignedBeaconBlockHash, SignedBeaconBlock, BeaconState), BlockError> { - self.set_current_slot(slot); - let (block, new_state) = self.make_block(state, slot).await; - let block_hash = self - .process_block(slot, block.canonical_root(), block.clone()) - .await?; - Ok((block_hash, block, new_state)) - } - - pub async fn add_attested_block_at_slot( - &self, - slot: Slot, - state: BeaconState, - state_root: Hash256, - validators: &[usize], - ) -> Result<(SignedBeaconBlockHash, BeaconState), BlockError> { - let (block_hash, block, state) = self.add_block_at_slot(slot, state).await?; - self.attest_block(&state, state_root, block_hash, &block, validators); - Ok((block_hash, state)) - } - - async fn add_attested_blocks_at_slots_given_lbh( - &self, - mut state: BeaconState, - state_root: Hash256, - slots: &[Slot], - validators: &[usize], - mut latest_block_hash: Option, - ) -> AddBlocksResult { - assert!( - slots.windows(2).all(|w| w[0] <= w[1]), - "Slots have to be sorted" - ); // slice.is_sorted() isn't stabilized at the moment of writing this - let mut block_hash_from_slot: HashMap = HashMap::new(); - let mut state_hash_from_slot: HashMap = HashMap::new(); - for slot in slots { - let (block_hash, new_state) = self - .add_attested_block_at_slot(*slot, state, state_root, validators) - .await - .unwrap(); - state = new_state; - block_hash_from_slot.insert(*slot, block_hash); - state_hash_from_slot.insert(*slot, state.tree_hash_root().into()); - latest_block_hash = Some(block_hash); - } - ( - block_hash_from_slot, - state_hash_from_slot, - latest_block_hash.unwrap(), - state, - ) - } - - pub async fn add_attested_blocks_at_slots( - &self, - state: BeaconState, - state_root: Hash256, - slots: &[Slot], - validators: &[usize], - ) -> AddBlocksResult { - assert!(!slots.is_empty()); - self.add_attested_blocks_at_slots_given_lbh(state, state_root, slots, validators, None) - .await - } - - /// A monstrosity of great usefulness. - /// - /// Calls `add_attested_blocks_at_slots` for each of the chains in `chains`, - /// taking care to batch blocks by epoch so that the slot clock gets advanced one - /// epoch at a time. - /// - /// Chains is a vec of `(state, slots, validators)` tuples. - pub async fn add_blocks_on_multiple_chains( - &self, - chains: Vec<(BeaconState, Vec, Vec)>, - ) -> Vec> { - let slots_per_epoch = E::slots_per_epoch(); - - let min_epoch = chains - .iter() - .map(|(_, slots, _)| slots.iter().min().unwrap()) - .min() - .unwrap() - .epoch(slots_per_epoch); - let max_epoch = chains - .iter() - .map(|(_, slots, _)| slots.iter().max().unwrap()) - .max() - .unwrap() - .epoch(slots_per_epoch); - - let mut chains = chains - .into_iter() - .map(|(state, slots, validators)| { - ( - state, - slots, - validators, - HashMap::new(), - HashMap::new(), - SignedBeaconBlockHash::from(Hash256::zero()), - ) - }) - .collect::>(); - - for epoch in min_epoch.as_u64()..=max_epoch.as_u64() { - let mut new_chains = vec![]; - - for ( - mut head_state, - slots, - validators, - mut block_hashes, - mut state_hashes, - head_block, - ) in chains - { - let epoch_slots = slots - .iter() - .filter(|s| s.epoch(slots_per_epoch).as_u64() == epoch) - .copied() - .collect::>(); - - let head_state_root = head_state.update_tree_hash_cache().unwrap(); - let (new_block_hashes, new_state_hashes, new_head_block, new_head_state) = self - .add_attested_blocks_at_slots_given_lbh( - head_state, - head_state_root, - &epoch_slots, - &validators, - Some(head_block), - ) - .await; - - block_hashes.extend(new_block_hashes); - state_hashes.extend(new_state_hashes); - - new_chains.push(( - new_head_state, - slots, - validators, - block_hashes, - state_hashes, - new_head_block, - )); - } - - chains = new_chains; - } - - chains - .into_iter() - .map(|(state, _, _, block_hashes, state_hashes, head_block)| { - (block_hashes, state_hashes, head_block, state) - }) - .collect() - } - - /// Deprecated: Do not modify the slot clock manually; rely on add_attested_blocks_at_slots() - /// instead - /// - /// Advance the slot of the `BeaconChain`. - /// - /// Does not produce blocks or attestations. - pub fn advance_slot(&self) { - self.chain.slot_clock.advance_slot(); - } - - /// Advance the clock to `lookahead` before the start of `slot`. - pub fn advance_to_slot_lookahead(&self, slot: Slot, lookahead: Duration) { - let time = self.chain.slot_clock.start_of(slot).unwrap() - lookahead; - self.chain.slot_clock.set_current_time(time); - } - - /// Uses `Self::extend_chain` to build the chain out to the `target_slot`. - pub async fn extend_to_slot(&self, target_slot: Slot) -> Hash256 { - if self.chain.slot().unwrap() == self.chain.canonical_head.cached_head().head_slot() { - self.advance_slot(); - } - - let num_slots = target_slot - .as_usize() - .checked_sub(self.chain.slot().unwrap().as_usize()) - .expect("target_slot must be >= current_slot") - .checked_add(1) - .unwrap(); - - self.extend_slots(num_slots).await - } - - /// Uses `Self::extend_chain` to `num_slots` blocks. - /// - /// Utilizes: - /// - /// - BlockStrategy::OnCanonicalHead, - /// - AttestationStrategy::AllValidators, - pub async fn extend_slots(&self, num_slots: usize) -> Hash256 { - if self.chain.slot().unwrap() == self.chain.canonical_head.cached_head().head_slot() { - self.advance_slot(); - } - - self.extend_chain( - num_slots, - BlockStrategy::OnCanonicalHead, - AttestationStrategy::AllValidators, - ) - .await - } - - /// Deprecated: Use add_attested_blocks_at_slots() instead - /// - /// Extend the `BeaconChain` with some blocks and attestations. Returns the root of the - /// last-produced block (the head of the chain). - /// - /// Chain will be extended by `num_blocks` blocks. - /// - /// The `block_strategy` dictates where the new blocks will be placed. - /// - /// The `attestation_strategy` dictates which validators will attest to the newly created - /// blocks. - pub async fn extend_chain( - &self, - num_blocks: usize, - block_strategy: BlockStrategy, - attestation_strategy: AttestationStrategy, - ) -> Hash256 { - let (mut state, slots) = match block_strategy { - BlockStrategy::OnCanonicalHead => { - let current_slot: u64 = self.get_current_slot().into(); - let slots: Vec = (current_slot..(current_slot + (num_blocks as u64))) - .map(Slot::new) - .collect(); - let state = self.get_current_state(); - (state, slots) - } - BlockStrategy::ForkCanonicalChainAt { - previous_slot, - first_slot, - } => { - let first_slot_: u64 = first_slot.into(); - let slots: Vec = (first_slot_..(first_slot_ + (num_blocks as u64))) - .map(Slot::new) - .collect(); - let state = self - .chain - .state_at_slot(previous_slot, StateSkipConfig::WithStateRoots) - .unwrap(); - (state, slots) - } - }; - let validators = match attestation_strategy { - AttestationStrategy::AllValidators => self.get_all_validators(), - AttestationStrategy::SomeValidators(vals) => vals, - }; - let state_root = state.update_tree_hash_cache().unwrap(); - let (_, _, last_produced_block_hash, _) = self - .add_attested_blocks_at_slots(state, state_root, &slots, &validators) - .await; - last_produced_block_hash.into() - } - - /// Deprecated: Use add_attested_blocks_at_slots() instead - /// - /// Creates two forks: - /// - /// - The "honest" fork: created by the `honest_validators` who have built `honest_fork_blocks` - /// on the head - /// - The "faulty" fork: created by the `faulty_validators` who skipped a slot and - /// then built `faulty_fork_blocks`. - /// - /// Returns `(honest_head, faulty_head)`, the roots of the blocks at the top of each chain. - pub async fn generate_two_forks_by_skipping_a_block( - &self, - honest_validators: &[usize], - faulty_validators: &[usize], - honest_fork_blocks: usize, - faulty_fork_blocks: usize, - ) -> (Hash256, Hash256) { - let initial_head_slot = self.chain.head_snapshot().beacon_block.slot(); - - // Move to the next slot so we may produce some more blocks on the head. - self.advance_slot(); - - // Extend the chain with blocks where only honest validators agree. - let honest_head = self - .extend_chain( - honest_fork_blocks, - BlockStrategy::OnCanonicalHead, - AttestationStrategy::SomeValidators(honest_validators.to_vec()), - ) - .await; - - // Go back to the last block where all agreed, and build blocks upon it where only faulty nodes - // agree. - let faulty_head = self - .extend_chain( - faulty_fork_blocks, - BlockStrategy::ForkCanonicalChainAt { - previous_slot: initial_head_slot, - // `initial_head_slot + 2` means one slot is skipped. - first_slot: initial_head_slot + 2, - }, - AttestationStrategy::SomeValidators(faulty_validators.to_vec()), - ) - .await; - - assert_ne!(honest_head, faulty_head, "forks should be distinct"); - - (honest_head, faulty_head) - } -} - -impl BeaconChainHarness> -where - E: EthSpec, - Hot: ItemStore, - Cold: ItemStore, - S: SlotClock + 'static, -{ - pub fn builder(eth_spec_instance: E) -> Builder, S> { + pub fn builder(eth_spec_instance: E) -> Builder> { Builder::new(eth_spec_instance) } @@ -1064,16 +689,6 @@ where self.chain.canonical_head.cached_head().head_block_root() } - pub fn get_finalized_checkpoints(&self) -> HashSet { - let chain_dump = self.chain.chain_dump().unwrap(); - chain_dump - .iter() - .cloned() - .map(|checkpoint| checkpoint.beacon_state.finalized_checkpoint().root.into()) - .filter(|block_hash| *block_hash != Hash256::zero().into()) - .collect() - } - pub fn finalized_checkpoint(&self) -> Checkpoint { self.chain .canonical_head @@ -1129,18 +744,6 @@ where state.get_block_root(slot).unwrap() == state.get_block_root(slot - 1).unwrap() } - /// Deprecated: Use make_block() instead - /// - /// Returns a newly created block, signed by the proposer for the given slot. - pub async fn build_block( - &self, - state: BeaconState, - slot: Slot, - _block_strategy: BlockStrategy, - ) -> (SignedBeaconBlock, BeaconState) { - self.make_block(state, slot).await - } - pub async fn make_block( &self, mut state: BeaconState, @@ -2053,6 +1656,27 @@ where (deposits, state) } + pub async fn process_block( + &self, + slot: Slot, + block_root: Hash256, + block: SignedBeaconBlock, + ) -> Result> { + self.set_current_slot(slot); + let block_hash: SignedBeaconBlockHash = self + .chain + .process_block( + block_root, + Arc::new(block), + CountUnrealized::True, + NotifyExecutionLayer::Yes, + ) + .await? + .into(); + self.chain.recompute_head_at_current_slot().await; + Ok(block_hash) + } + pub async fn process_block_result( &self, block: SignedBeaconBlock, @@ -2110,6 +1734,31 @@ where } } + pub fn set_current_slot(&self, slot: Slot) { + let current_slot = self.chain.slot().unwrap(); + let current_epoch = current_slot.epoch(E::slots_per_epoch()); + let epoch = slot.epoch(E::slots_per_epoch()); + assert!( + epoch >= current_epoch, + "Jumping backwards to an earlier epoch isn't well defined. \ + Please generate test blocks epoch-by-epoch instead." + ); + self.chain.slot_clock.set_slot(slot.into()); + } + + pub async fn add_block_at_slot( + &self, + slot: Slot, + state: BeaconState, + ) -> Result<(SignedBeaconBlockHash, SignedBeaconBlock, BeaconState), BlockError> { + self.set_current_slot(slot); + let (block, new_state) = self.make_block(state, slot).await; + let block_hash = self + .process_block(slot, block.canonical_root(), block.clone()) + .await?; + Ok((block_hash, block, new_state)) + } + pub fn attest_block( &self, state: &BeaconState, @@ -2123,6 +1772,330 @@ where self.process_attestations(attestations); } + pub async fn add_attested_block_at_slot( + &self, + slot: Slot, + state: BeaconState, + state_root: Hash256, + validators: &[usize], + ) -> Result<(SignedBeaconBlockHash, BeaconState), BlockError> { + let (block_hash, block, state) = self.add_block_at_slot(slot, state).await?; + self.attest_block(&state, state_root, block_hash, &block, validators); + Ok((block_hash, state)) + } + + pub async fn add_attested_blocks_at_slots( + &self, + state: BeaconState, + state_root: Hash256, + slots: &[Slot], + validators: &[usize], + ) -> AddBlocksResult { + assert!(!slots.is_empty()); + self.add_attested_blocks_at_slots_given_lbh(state, state_root, slots, validators, None) + .await + } + + async fn add_attested_blocks_at_slots_given_lbh( + &self, + mut state: BeaconState, + state_root: Hash256, + slots: &[Slot], + validators: &[usize], + mut latest_block_hash: Option, + ) -> AddBlocksResult { + assert!( + slots.windows(2).all(|w| w[0] <= w[1]), + "Slots have to be sorted" + ); // slice.is_sorted() isn't stabilized at the moment of writing this + let mut block_hash_from_slot: HashMap = HashMap::new(); + let mut state_hash_from_slot: HashMap = HashMap::new(); + for slot in slots { + let (block_hash, new_state) = self + .add_attested_block_at_slot(*slot, state, state_root, validators) + .await + .unwrap(); + state = new_state; + block_hash_from_slot.insert(*slot, block_hash); + state_hash_from_slot.insert(*slot, state.tree_hash_root().into()); + latest_block_hash = Some(block_hash); + } + ( + block_hash_from_slot, + state_hash_from_slot, + latest_block_hash.unwrap(), + state, + ) + } + + /// A monstrosity of great usefulness. + /// + /// Calls `add_attested_blocks_at_slots` for each of the chains in `chains`, + /// taking care to batch blocks by epoch so that the slot clock gets advanced one + /// epoch at a time. + /// + /// Chains is a vec of `(state, slots, validators)` tuples. + pub async fn add_blocks_on_multiple_chains( + &self, + chains: Vec<(BeaconState, Vec, Vec)>, + ) -> Vec> { + let slots_per_epoch = E::slots_per_epoch(); + + let min_epoch = chains + .iter() + .map(|(_, slots, _)| slots.iter().min().unwrap()) + .min() + .unwrap() + .epoch(slots_per_epoch); + let max_epoch = chains + .iter() + .map(|(_, slots, _)| slots.iter().max().unwrap()) + .max() + .unwrap() + .epoch(slots_per_epoch); + + let mut chains = chains + .into_iter() + .map(|(state, slots, validators)| { + ( + state, + slots, + validators, + HashMap::new(), + HashMap::new(), + SignedBeaconBlockHash::from(Hash256::zero()), + ) + }) + .collect::>(); + + for epoch in min_epoch.as_u64()..=max_epoch.as_u64() { + let mut new_chains = vec![]; + + for ( + mut head_state, + slots, + validators, + mut block_hashes, + mut state_hashes, + head_block, + ) in chains + { + let epoch_slots = slots + .iter() + .filter(|s| s.epoch(slots_per_epoch).as_u64() == epoch) + .copied() + .collect::>(); + + let head_state_root = head_state.update_tree_hash_cache().unwrap(); + let (new_block_hashes, new_state_hashes, new_head_block, new_head_state) = self + .add_attested_blocks_at_slots_given_lbh( + head_state, + head_state_root, + &epoch_slots, + &validators, + Some(head_block), + ) + .await; + + block_hashes.extend(new_block_hashes); + state_hashes.extend(new_state_hashes); + + new_chains.push(( + new_head_state, + slots, + validators, + block_hashes, + state_hashes, + new_head_block, + )); + } + + chains = new_chains; + } + + chains + .into_iter() + .map(|(state, _, _, block_hashes, state_hashes, head_block)| { + (block_hashes, state_hashes, head_block, state) + }) + .collect() + } + + pub fn get_finalized_checkpoints(&self) -> HashSet { + let chain_dump = self.chain.chain_dump().unwrap(); + chain_dump + .iter() + .cloned() + .map(|checkpoint| checkpoint.beacon_state.finalized_checkpoint().root.into()) + .filter(|block_hash| *block_hash != Hash256::zero().into()) + .collect() + } + + /// Deprecated: Do not modify the slot clock manually; rely on add_attested_blocks_at_slots() + /// instead + /// + /// Advance the slot of the `BeaconChain`. + /// + /// Does not produce blocks or attestations. + pub fn advance_slot(&self) { + self.chain.slot_clock.advance_slot(); + } + + /// Advance the clock to `lookahead` before the start of `slot`. + pub fn advance_to_slot_lookahead(&self, slot: Slot, lookahead: Duration) { + let time = self.chain.slot_clock.start_of(slot).unwrap() - lookahead; + self.chain.slot_clock.set_current_time(time); + } + + /// Deprecated: Use make_block() instead + /// + /// Returns a newly created block, signed by the proposer for the given slot. + pub async fn build_block( + &self, + state: BeaconState, + slot: Slot, + _block_strategy: BlockStrategy, + ) -> (SignedBeaconBlock, BeaconState) { + self.make_block(state, slot).await + } + + /// Uses `Self::extend_chain` to build the chain out to the `target_slot`. + pub async fn extend_to_slot(&self, target_slot: Slot) -> Hash256 { + if self.chain.slot().unwrap() == self.chain.canonical_head.cached_head().head_slot() { + self.advance_slot(); + } + + let num_slots = target_slot + .as_usize() + .checked_sub(self.chain.slot().unwrap().as_usize()) + .expect("target_slot must be >= current_slot") + .checked_add(1) + .unwrap(); + + self.extend_slots(num_slots).await + } + + /// Uses `Self::extend_chain` to `num_slots` blocks. + /// + /// Utilizes: + /// + /// - BlockStrategy::OnCanonicalHead, + /// - AttestationStrategy::AllValidators, + pub async fn extend_slots(&self, num_slots: usize) -> Hash256 { + if self.chain.slot().unwrap() == self.chain.canonical_head.cached_head().head_slot() { + self.advance_slot(); + } + + self.extend_chain( + num_slots, + BlockStrategy::OnCanonicalHead, + AttestationStrategy::AllValidators, + ) + .await + } + + /// Deprecated: Use add_attested_blocks_at_slots() instead + /// + /// Extend the `BeaconChain` with some blocks and attestations. Returns the root of the + /// last-produced block (the head of the chain). + /// + /// Chain will be extended by `num_blocks` blocks. + /// + /// The `block_strategy` dictates where the new blocks will be placed. + /// + /// The `attestation_strategy` dictates which validators will attest to the newly created + /// blocks. + pub async fn extend_chain( + &self, + num_blocks: usize, + block_strategy: BlockStrategy, + attestation_strategy: AttestationStrategy, + ) -> Hash256 { + let (mut state, slots) = match block_strategy { + BlockStrategy::OnCanonicalHead => { + let current_slot: u64 = self.get_current_slot().into(); + let slots: Vec = (current_slot..(current_slot + (num_blocks as u64))) + .map(Slot::new) + .collect(); + let state = self.get_current_state(); + (state, slots) + } + BlockStrategy::ForkCanonicalChainAt { + previous_slot, + first_slot, + } => { + let first_slot_: u64 = first_slot.into(); + let slots: Vec = (first_slot_..(first_slot_ + (num_blocks as u64))) + .map(Slot::new) + .collect(); + let state = self + .chain + .state_at_slot(previous_slot, StateSkipConfig::WithStateRoots) + .unwrap(); + (state, slots) + } + }; + let validators = match attestation_strategy { + AttestationStrategy::AllValidators => self.get_all_validators(), + AttestationStrategy::SomeValidators(vals) => vals, + }; + let state_root = state.update_tree_hash_cache().unwrap(); + let (_, _, last_produced_block_hash, _) = self + .add_attested_blocks_at_slots(state, state_root, &slots, &validators) + .await; + last_produced_block_hash.into() + } + + /// Deprecated: Use add_attested_blocks_at_slots() instead + /// + /// Creates two forks: + /// + /// - The "honest" fork: created by the `honest_validators` who have built `honest_fork_blocks` + /// on the head + /// - The "faulty" fork: created by the `faulty_validators` who skipped a slot and + /// then built `faulty_fork_blocks`. + /// + /// Returns `(honest_head, faulty_head)`, the roots of the blocks at the top of each chain. + pub async fn generate_two_forks_by_skipping_a_block( + &self, + honest_validators: &[usize], + faulty_validators: &[usize], + honest_fork_blocks: usize, + faulty_fork_blocks: usize, + ) -> (Hash256, Hash256) { + let initial_head_slot = self.chain.head_snapshot().beacon_block.slot(); + + // Move to the next slot so we may produce some more blocks on the head. + self.advance_slot(); + + // Extend the chain with blocks where only honest validators agree. + let honest_head = self + .extend_chain( + honest_fork_blocks, + BlockStrategy::OnCanonicalHead, + AttestationStrategy::SomeValidators(honest_validators.to_vec()), + ) + .await; + + // Go back to the last block where all agreed, and build blocks upon it where only faulty nodes + // agree. + let faulty_head = self + .extend_chain( + faulty_fork_blocks, + BlockStrategy::ForkCanonicalChainAt { + previous_slot: initial_head_slot, + // `initial_head_slot + 2` means one slot is skipped. + first_slot: initial_head_slot + 2, + }, + AttestationStrategy::SomeValidators(faulty_validators.to_vec()), + ) + .await; + + assert_ne!(honest_head, faulty_head, "forks should be distinct"); + + (honest_head, faulty_head) + } + pub fn process_sync_contributions( &self, sync_contributions: HarnessSyncContributions, diff --git a/beacon_node/beacon_chain/src/validator_pubkey_cache.rs b/beacon_node/beacon_chain/src/validator_pubkey_cache.rs index c36b9059be..00140dd6ec 100644 --- a/beacon_node/beacon_chain/src/validator_pubkey_cache.rs +++ b/beacon_node/beacon_chain/src/validator_pubkey_cache.rs @@ -191,14 +191,14 @@ impl DatabasePubkey { #[cfg(test)] mod test { use super::*; - use crate::test_utils::{BeaconChainHarness, EphemeralTestingSlotClockHarnessType}; + use crate::test_utils::{BeaconChainHarness, EphemeralHarnessType}; use logging::test_logger; use std::sync::Arc; use store::HotColdDB; use types::{BeaconState, EthSpec, Keypair, MainnetEthSpec}; type E = MainnetEthSpec; - type T = EphemeralTestingSlotClockHarnessType; + type T = EphemeralHarnessType; fn get_state(validator_count: usize) -> (BeaconState, Vec) { let harness = BeaconChainHarness::builder(MainnetEthSpec) diff --git a/beacon_node/beacon_chain/tests/attestation_verification.rs b/beacon_node/beacon_chain/tests/attestation_verification.rs index f2bd40bf8f..6a9e604793 100644 --- a/beacon_node/beacon_chain/tests/attestation_verification.rs +++ b/beacon_node/beacon_chain/tests/attestation_verification.rs @@ -3,8 +3,7 @@ use beacon_chain::{ attestation_verification::Error as AttnError, test_utils::{ - test_spec, AttestationStrategy, BeaconChainHarness, BlockStrategy, - EphemeralTestingSlotClockHarnessType, + test_spec, AttestationStrategy, BeaconChainHarness, BlockStrategy, EphemeralHarnessType, }, BeaconChain, BeaconChainError, BeaconChainTypes, WhenSlotSkipped, }; @@ -32,9 +31,7 @@ lazy_static! { } /// Returns a beacon chain harness. -fn get_harness( - validator_count: usize, -) -> BeaconChainHarness> { +fn get_harness(validator_count: usize) -> BeaconChainHarness> { let mut spec = test_spec::(); // A kind-of arbitrary number that ensures that _some_ validators are aggregators, but @@ -192,7 +189,7 @@ fn get_non_aggregator( } struct GossipTester { - harness: BeaconChainHarness>, + harness: BeaconChainHarness>, /* * Valid unaggregated attestation */ diff --git a/beacon_node/beacon_chain/tests/block_verification.rs b/beacon_node/beacon_chain/tests/block_verification.rs index a19f7a2a1a..8de906afd9 100644 --- a/beacon_node/beacon_chain/tests/block_verification.rs +++ b/beacon_node/beacon_chain/tests/block_verification.rs @@ -2,10 +2,7 @@ use beacon_chain::{ blob_verification::{AsBlock, BlockWrapper}, - test_utils::{ - AttestationStrategy, BeaconChainHarness, BlockStrategy, - EphemeralTestingSlotClockHarnessType, - }, + test_utils::{AttestationStrategy, BeaconChainHarness, BlockStrategy, EphemeralHarnessType}, }; use beacon_chain::{BeaconSnapshot, BlockError, ChainSegmentResult, NotifyExecutionLayer}; use fork_choice::CountUnrealized; @@ -68,9 +65,7 @@ async fn get_chain_segment() -> Vec> { segment } -fn get_harness( - validator_count: usize, -) -> BeaconChainHarness> { +fn get_harness(validator_count: usize) -> BeaconChainHarness> { let harness = BeaconChainHarness::builder(MainnetEthSpec) .default_spec() .keypairs(KEYPAIRS[0..validator_count].to_vec()) @@ -104,7 +99,7 @@ fn junk_aggregate_signature() -> AggregateSignature { fn update_proposal_signatures( snapshots: &mut [BeaconSnapshot], - harness: &BeaconChainHarness>, + harness: &BeaconChainHarness>, ) { for snapshot in snapshots { let spec = &harness.chain.spec; @@ -335,7 +330,7 @@ async fn chain_segment_non_linear_slots() { async fn assert_invalid_signature( chain_segment: &[BeaconSnapshot], - harness: &BeaconChainHarness>, + harness: &BeaconChainHarness>, block_index: usize, snapshots: &[BeaconSnapshot], item: &str, @@ -406,7 +401,7 @@ async fn assert_invalid_signature( async fn get_invalid_sigs_harness( chain_segment: &[BeaconSnapshot], -) -> BeaconChainHarness> { +) -> BeaconChainHarness> { let harness = get_harness(VALIDATOR_COUNT); harness .chain diff --git a/beacon_node/beacon_chain/tests/payload_invalidation.rs b/beacon_node/beacon_chain/tests/payload_invalidation.rs index b79ee960a7..d0f81652bb 100644 --- a/beacon_node/beacon_chain/tests/payload_invalidation.rs +++ b/beacon_node/beacon_chain/tests/payload_invalidation.rs @@ -6,7 +6,7 @@ use beacon_chain::otb_verification_service::{ }; use beacon_chain::{ canonical_head::{CachedHead, CanonicalHead}, - test_utils::{BeaconChainHarness, EphemeralTestingSlotClockHarnessType}, + test_utils::{BeaconChainHarness, EphemeralHarnessType}, BeaconChainError, BlockError, ExecutionPayloadError, NotifyExecutionLayer, OverrideForkchoiceUpdate, StateSkipConfig, WhenSlotSkipped, INVALID_FINALIZED_MERGE_TRANSITION_BLOCK_SHUTDOWN_REASON, @@ -45,7 +45,7 @@ enum Payload { } struct InvalidPayloadRig { - harness: BeaconChainHarness>, + harness: BeaconChainHarness>, enable_attestations: bool, } @@ -116,7 +116,7 @@ impl InvalidPayloadRig { self.harness.chain.canonical_head.cached_head() } - fn canonical_head(&self) -> &CanonicalHead> { + fn canonical_head(&self) -> &CanonicalHead> { &self.harness.chain.canonical_head } diff --git a/beacon_node/beacon_chain/tests/rewards.rs b/beacon_node/beacon_chain/tests/rewards.rs index a10368cbdd..b61bea1242 100644 --- a/beacon_node/beacon_chain/tests/rewards.rs +++ b/beacon_node/beacon_chain/tests/rewards.rs @@ -3,8 +3,7 @@ use std::collections::HashMap; use beacon_chain::test_utils::{ - generate_deterministic_keypairs, BeaconChainHarness, - EphemeralTestingSlotClockHarnessType as HarnessType, + generate_deterministic_keypairs, BeaconChainHarness, EphemeralHarnessType, }; use beacon_chain::{ test_utils::{AttestationStrategy, BlockStrategy, RelativeSyncCommittee}, @@ -18,7 +17,7 @@ lazy_static! { static ref KEYPAIRS: Vec = generate_deterministic_keypairs(VALIDATOR_COUNT); } -fn get_harness() -> BeaconChainHarness> { +fn get_harness() -> BeaconChainHarness> { let mut spec = E::default_spec(); spec.altair_fork_epoch = Some(Epoch::new(0)); // We use altair for all tests diff --git a/beacon_node/beacon_chain/tests/sync_committee_verification.rs b/beacon_node/beacon_chain/tests/sync_committee_verification.rs index 3f63296a85..239f55e7d3 100644 --- a/beacon_node/beacon_chain/tests/sync_committee_verification.rs +++ b/beacon_node/beacon_chain/tests/sync_committee_verification.rs @@ -1,9 +1,7 @@ #![cfg(not(debug_assertions))] use beacon_chain::sync_committee_verification::Error as SyncCommitteeError; -use beacon_chain::test_utils::{ - BeaconChainHarness, EphemeralTestingSlotClockHarnessType, RelativeSyncCommittee, -}; +use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType, RelativeSyncCommittee}; use int_to_bytes::int_to_bytes32; use lazy_static::lazy_static; use safe_arith::SafeArith; @@ -25,9 +23,7 @@ lazy_static! { } /// Returns a beacon chain harness. -fn get_harness( - validator_count: usize, -) -> BeaconChainHarness> { +fn get_harness(validator_count: usize) -> BeaconChainHarness> { let mut spec = E::default_spec(); spec.altair_fork_epoch = Some(Epoch::new(0)); let harness = BeaconChainHarness::builder(MainnetEthSpec) @@ -46,7 +42,7 @@ fn get_harness( /// /// Also returns some info about who created it. fn get_valid_sync_committee_message( - harness: &BeaconChainHarness>, + harness: &BeaconChainHarness>, slot: Slot, relative_sync_committee: RelativeSyncCommittee, message_index: usize, @@ -72,7 +68,7 @@ fn get_valid_sync_committee_message( } fn get_valid_sync_contribution( - harness: &BeaconChainHarness>, + harness: &BeaconChainHarness>, relative_sync_committee: RelativeSyncCommittee, ) -> (SignedContributionAndProof, usize, SecretKey) { let head_state = harness.chain.head_beacon_state_cloned(); @@ -104,7 +100,7 @@ fn get_valid_sync_contribution( /// Returns a proof and index for a validator that is **not** an aggregator for the current sync period. fn get_non_aggregator( - harness: &BeaconChainHarness>, + harness: &BeaconChainHarness>, slot: Slot, ) -> (usize, SecretKey) { let state = &harness.chain.head_snapshot().beacon_state; diff --git a/beacon_node/beacon_chain/tests/tests.rs b/beacon_node/beacon_chain/tests/tests.rs index 60c2e86980..384fcbe5db 100644 --- a/beacon_node/beacon_chain/tests/tests.rs +++ b/beacon_node/beacon_chain/tests/tests.rs @@ -3,8 +3,8 @@ use beacon_chain::{ attestation_verification::Error as AttnError, test_utils::{ - AttestationStrategy, BeaconChainHarness, BlockStrategy, - EphemeralTestingSlotClockHarnessType, OP_POOL_DB_KEY, + AttestationStrategy, BeaconChainHarness, BlockStrategy, EphemeralHarnessType, + OP_POOL_DB_KEY, }, BeaconChain, NotifyExecutionLayer, StateSkipConfig, WhenSlotSkipped, }; @@ -26,9 +26,7 @@ lazy_static! { static ref KEYPAIRS: Vec = types::test_utils::generate_deterministic_keypairs(VALIDATOR_COUNT); } -fn get_harness( - validator_count: usize, -) -> BeaconChainHarness> { +fn get_harness(validator_count: usize) -> BeaconChainHarness> { let harness = BeaconChainHarness::builder(MinimalEthSpec) .default_spec() .keypairs(KEYPAIRS[0..validator_count].to_vec()) @@ -145,7 +143,7 @@ async fn iterators() { } fn find_reorg_slot( - chain: &BeaconChain>, + chain: &BeaconChain>, new_state: &BeaconState, new_block_root: Hash256, ) -> Slot { diff --git a/beacon_node/client/src/address_change_broadcast.rs b/beacon_node/client/src/address_change_broadcast.rs index ccc34de16b..272ee908fb 100644 --- a/beacon_node/client/src/address_change_broadcast.rs +++ b/beacon_node/client/src/address_change_broadcast.rs @@ -153,9 +153,7 @@ pub async fn broadcast_address_changes( #[cfg(test)] mod tests { use super::*; - use beacon_chain::test_utils::{ - BeaconChainHarness, EphemeralTestingSlotClockHarnessType as HarnessType, - }; + use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType}; use operation_pool::ReceivedPreCapella; use state_processing::{SigVerifiedOp, VerifyOperation}; use std::collections::HashSet; @@ -168,7 +166,7 @@ mod tests { pub const EXECUTION_ADDRESS: Address = Address::repeat_byte(42); struct Tester { - harness: BeaconChainHarness>, + harness: BeaconChainHarness>, /// Changes which should be broadcast at the Capella fork. received_pre_capella_changes: Vec>, /// Changes which should *not* be broadcast at the Capella fork. diff --git a/beacon_node/http_api/tests/common.rs b/beacon_node/http_api/tests/common.rs index fba1671c62..5cc9ab2ef8 100644 --- a/beacon_node/http_api/tests/common.rs +++ b/beacon_node/http_api/tests/common.rs @@ -1,8 +1,5 @@ use beacon_chain::{ - test_utils::{ - BeaconChainHarness, BoxedMutator, Builder, - EphemeralTestingSlotClockHarnessType as HarnessType, - }, + test_utils::{BeaconChainHarness, BoxedMutator, Builder, EphemeralHarnessType}, BeaconChain, BeaconChainTypes, }; use directory::DEFAULT_ROOT_DIR; @@ -41,7 +38,7 @@ pub const EXTERNAL_ADDR: &str = "/ip4/0.0.0.0/tcp/9000"; /// HTTP API tester that allows interaction with the underlying beacon chain harness. pub struct InteractiveTester { - pub harness: BeaconChainHarness>, + pub harness: BeaconChainHarness>, pub client: BeaconNodeHttpClient, pub network_rx: NetworkReceivers, _server_shutdown: oneshot::Sender<()>, @@ -59,7 +56,7 @@ pub struct ApiServer> { pub external_peer_id: PeerId, } -type HarnessBuilder = Builder, TestingSlotClock>; +type HarnessBuilder = Builder, TestingSlotClock>; type Initializer = Box) -> HarnessBuilder>; type Mutator = BoxedMutator, MemoryStore, TestingSlotClock>; diff --git a/beacon_node/http_api/tests/tests.rs b/beacon_node/http_api/tests/tests.rs index 3a955ad034..43099c7a91 100644 --- a/beacon_node/http_api/tests/tests.rs +++ b/beacon_node/http_api/tests/tests.rs @@ -1,10 +1,7 @@ use crate::common::{create_api_server, create_api_server_on_port, ApiServer}; use beacon_chain::test_utils::RelativeSyncCommittee; use beacon_chain::{ - test_utils::{ - AttestationStrategy, BeaconChainHarness, BlockStrategy, - EphemeralTestingSlotClockHarnessType, - }, + test_utils::{AttestationStrategy, BeaconChainHarness, BlockStrategy, EphemeralHarnessType}, BeaconChain, StateSkipConfig, WhenSlotSkipped, MAXIMUM_GOSSIP_CLOCK_DISPARITY, }; use environment::null_logger; @@ -60,8 +57,8 @@ const SKIPPED_SLOTS: &[u64] = &[ ]; struct ApiTester { - harness: Arc>>, - chain: Arc>>, + harness: Arc>>, + chain: Arc>>, client: BeaconNodeHttpClient, next_block: SignedBeaconBlock, reorg_block: SignedBeaconBlock, diff --git a/beacon_node/http_metrics/tests/tests.rs b/beacon_node/http_metrics/tests/tests.rs index f64c11163a..b3e02d4cb6 100644 --- a/beacon_node/http_metrics/tests/tests.rs +++ b/beacon_node/http_metrics/tests/tests.rs @@ -1,4 +1,4 @@ -use beacon_chain::test_utils::EphemeralTestingSlotClockHarnessType; +use beacon_chain::test_utils::EphemeralHarnessType; use environment::null_logger; use http_metrics::Config; use reqwest::StatusCode; @@ -7,7 +7,7 @@ use std::sync::Arc; use tokio::sync::oneshot; use types::MainnetEthSpec; -type Context = http_metrics::Context>; +type Context = http_metrics::Context>; #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn returns_200_ok() { diff --git a/beacon_node/network/src/beacon_processor/tests.rs b/beacon_node/network/src/beacon_processor/tests.rs index ea5c586e95..e2b96fb476 100644 --- a/beacon_node/network/src/beacon_processor/tests.rs +++ b/beacon_node/network/src/beacon_processor/tests.rs @@ -7,7 +7,7 @@ use crate::beacon_processor::work_reprocessing_queue::{ use crate::beacon_processor::*; use crate::{service::NetworkMessage, sync::SyncMessage}; use beacon_chain::test_utils::{ - AttestationStrategy, BeaconChainHarness, BlockStrategy, EphemeralTestingSlotClockHarnessType, + AttestationStrategy, BeaconChainHarness, BlockStrategy, EphemeralHarnessType, }; use beacon_chain::{BeaconChain, MAXIMUM_GOSSIP_CLOCK_DISPARITY}; use lighthouse_network::{ @@ -28,7 +28,7 @@ use types::{ }; type E = MainnetEthSpec; -type T = EphemeralTestingSlotClockHarnessType; +type T = EphemeralHarnessType; const SLOTS_PER_EPOCH: u64 = 32; const VALIDATOR_COUNT: usize = SLOTS_PER_EPOCH as usize; diff --git a/beacon_node/network/src/service/tests.rs b/beacon_node/network/src/service/tests.rs index 66d23234dc..57be233d5d 100644 --- a/beacon_node/network/src/service/tests.rs +++ b/beacon_node/network/src/service/tests.rs @@ -3,9 +3,7 @@ mod tests { use crate::persisted_dht::load_dht; use crate::{NetworkConfig, NetworkService}; - use beacon_chain::test_utils::{ - BeaconChainHarness, EphemeralSystemTimeSlotClockHarnessType as HarnessType, - }; + use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType}; use lighthouse_network::Enr; use slog::{o, Drain, Level, Logger}; use sloggers::{null::NullLoggerBuilder, Build}; @@ -36,7 +34,7 @@ mod tests { fn test_dht_persistence() { let log = get_logger(false); - let beacon_chain = BeaconChainHarness::>::builder(E) + let beacon_chain = BeaconChainHarness::EphemeralHarnessType::::builder(E) .default_spec() .deterministic_keypairs(8) .fresh_ephemeral_store() diff --git a/beacon_node/network/src/sync/block_lookups/tests.rs b/beacon_node/network/src/sync/block_lookups/tests.rs index c310d61b44..9556be80ca 100644 --- a/beacon_node/network/src/sync/block_lookups/tests.rs +++ b/beacon_node/network/src/sync/block_lookups/tests.rs @@ -9,9 +9,7 @@ use super::*; use beacon_chain::{ builder::Witness, eth1_chain::CachingEth1Backend, - test_utils::{ - build_log, BeaconChainHarness, EphemeralSystemTimeSlotClockHarnessType as HarnessType, - }, + test_utils::{build_log, BeaconChainHarness, EphemeralHarnessType}, }; pub use genesis::{interop_genesis_state, DEFAULT_ETH1_BLOCK_HASH}; use lighthouse_network::{NetworkGlobals, Request}; @@ -39,7 +37,7 @@ impl TestRig { let log = build_log(slog::Level::Debug, enable_log); // Initialise a new beacon chain - let harness = BeaconChainHarness::>::builder(E::default()) + let harness = BeaconChainHarness::>::builder(E::default()) .default_spec() .logger(log.clone()) .deterministic_keypairs(1) diff --git a/beacon_node/network/src/sync/range_sync/range.rs b/beacon_node/network/src/sync/range_sync/range.rs index afa42371bf..a947b70166 100644 --- a/beacon_node/network/src/sync/range_sync/range.rs +++ b/beacon_node/network/src/sync/range_sync/range.rs @@ -381,9 +381,7 @@ mod tests { builder::Witness, eth1_chain::CachingEth1Backend, parking_lot::RwLock, - test_utils::{ - build_log, BeaconChainHarness, EphemeralSystemTimeSlotClockHarnessType as HarnessType, - }, + test_utils::{build_log, BeaconChainHarness, EphemeralHarnessType}, EngineState, }; use lighthouse_network::{ @@ -586,7 +584,7 @@ mod tests { fn range(log_enabled: bool) -> (TestRig, RangeSync) { let log = build_log(slog::Level::Trace, log_enabled); // Initialise a new beacon chain - let harness = BeaconChainHarness::>::builder(E::default()) + let harness = BeaconChainHarness::>::builder(E::default()) .default_spec() .logger(log.clone()) .deterministic_keypairs(1) diff --git a/beacon_node/operation_pool/src/lib.rs b/beacon_node/operation_pool/src/lib.rs index 82e287e0e3..d401deb896 100644 --- a/beacon_node/operation_pool/src/lib.rs +++ b/beacon_node/operation_pool/src/lib.rs @@ -768,7 +768,7 @@ mod release_tests { use super::attestation::earliest_attestation_validators; use super::*; use beacon_chain::test_utils::{ - test_spec, BeaconChainHarness, EphemeralTestingSlotClockHarnessType, RelativeSyncCommittee, + test_spec, BeaconChainHarness, EphemeralHarnessType, RelativeSyncCommittee, }; use lazy_static::lazy_static; use maplit::hashset; @@ -787,7 +787,7 @@ mod release_tests { fn get_harness( validator_count: usize, spec: Option, - ) -> BeaconChainHarness> { + ) -> BeaconChainHarness> { let harness = BeaconChainHarness::builder(E::default()) .spec_or_default(spec) .keypairs(KEYPAIRS[0..validator_count].to_vec()) @@ -803,10 +803,7 @@ mod release_tests { /// Test state for attestation-related tests. fn attestation_test_state( num_committees: usize, - ) -> ( - BeaconChainHarness>, - ChainSpec, - ) { + ) -> (BeaconChainHarness>, ChainSpec) { let spec = test_spec::(); let num_validators = @@ -819,10 +816,7 @@ mod release_tests { /// Test state for sync contribution-related tests. async fn sync_contribution_test_state( num_committees: usize, - ) -> ( - BeaconChainHarness>, - ChainSpec, - ) { + ) -> (BeaconChainHarness>, ChainSpec) { let mut spec = E::default_spec(); spec.altair_fork_epoch = Some(Epoch::new(0)); @@ -1792,10 +1786,8 @@ mod release_tests { ); } - fn cross_fork_harness() -> ( - BeaconChainHarness>, - ChainSpec, - ) { + fn cross_fork_harness() -> (BeaconChainHarness>, ChainSpec) + { let mut spec = E::default_spec(); // Give some room to sign surround slashings. diff --git a/consensus/fork_choice/tests/tests.rs b/consensus/fork_choice/tests/tests.rs index 21eb656232..00bd1f763d 100644 --- a/consensus/fork_choice/tests/tests.rs +++ b/consensus/fork_choice/tests/tests.rs @@ -5,7 +5,7 @@ use std::sync::Mutex; use std::time::Duration; use beacon_chain::test_utils::{ - AttestationStrategy, BeaconChainHarness, BlockStrategy, EphemeralTestingSlotClockHarnessType, + AttestationStrategy, BeaconChainHarness, BlockStrategy, EphemeralHarnessType, }; use beacon_chain::{ BeaconChain, BeaconChainError, BeaconForkChoiceStore, ChainConfig, ForkChoiceError, @@ -35,7 +35,7 @@ pub enum MutationDelay { /// A helper struct to make testing fork choice more ergonomic and less repetitive. struct ForkChoiceTest { - harness: BeaconChainHarness>, + harness: BeaconChainHarness>, } /// Allows us to use `unwrap` in some cases. @@ -397,7 +397,7 @@ impl ForkChoiceTest { mut comparison_func: G, ) -> Self where - F: FnMut(&mut IndexedAttestation, &BeaconChain>), + F: FnMut(&mut IndexedAttestation, &BeaconChain>), G: FnMut(Result<(), BeaconChainError>), { let head = self.harness.chain.head_snapshot(); diff --git a/consensus/state_processing/src/per_block_processing/tests.rs b/consensus/state_processing/src/per_block_processing/tests.rs index ba0e8f07c2..db0b91d03c 100644 --- a/consensus/state_processing/src/per_block_processing/tests.rs +++ b/consensus/state_processing/src/per_block_processing/tests.rs @@ -10,9 +10,7 @@ use crate::{ per_block_processing::{process_operations, verify_exit::verify_exit}, BlockSignatureStrategy, ConsensusContext, VerifyBlockRoot, VerifySignatures, }; -use beacon_chain::test_utils::{ - BeaconChainHarness, EphemeralTestingSlotClockHarnessType as HarnessType, -}; +use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType}; use lazy_static::lazy_static; use ssz_types::Bitfield; use test_utils::generate_deterministic_keypairs; @@ -32,11 +30,11 @@ lazy_static! { async fn get_harness( epoch_offset: u64, num_validators: usize, -) -> BeaconChainHarness> { +) -> BeaconChainHarness> { // Set the state and block to be in the last slot of the `epoch_offset`th epoch. let last_slot_of_epoch = (MainnetEthSpec::genesis_epoch() + epoch_offset).end_slot(E::slots_per_epoch()); - let harness = BeaconChainHarness::>::builder(E::default()) + let harness = BeaconChainHarness::>::builder(E::default()) .default_spec() .keypairs(KEYPAIRS[0..num_validators].to_vec()) .fresh_ephemeral_store() diff --git a/consensus/tree_hash/examples/flamegraph_beacon_state.rs b/consensus/tree_hash/examples/flamegraph_beacon_state.rs index f7fb7b9320..e5b505bb91 100644 --- a/consensus/tree_hash/examples/flamegraph_beacon_state.rs +++ b/consensus/tree_hash/examples/flamegraph_beacon_state.rs @@ -1,10 +1,10 @@ -use beacon_chain::test_utils::{BeaconChainHarness, EphemeralTestingSlotClockHarnessType}; +use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType}; use types::{BeaconState, EthSpec, MainnetEthSpec}; const TREE_HASH_LOOPS: usize = 1_000; const VALIDATOR_COUNT: usize = 1_000; -fn get_harness() -> BeaconChainHarness> { +fn get_harness() -> BeaconChainHarness> { let harness = BeaconChainHarness::builder(T::default()) .default_spec() .deterministic_keypairs(VALIDATOR_COUNT) diff --git a/consensus/types/src/beacon_state/committee_cache/tests.rs b/consensus/types/src/beacon_state/committee_cache/tests.rs index e39f5e2796..11cc6095da 100644 --- a/consensus/types/src/beacon_state/committee_cache/tests.rs +++ b/consensus/types/src/beacon_state/committee_cache/tests.rs @@ -1,8 +1,6 @@ #![cfg(test)] use crate::test_utils::*; -use beacon_chain::test_utils::{ - BeaconChainHarness, EphemeralTestingSlotClockHarnessType as HarnessType, -}; +use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType}; use beacon_chain::types::*; use swap_or_not_shuffle::shuffle_list; @@ -13,7 +11,7 @@ lazy_static! { static ref KEYPAIRS: Vec = generate_deterministic_keypairs(VALIDATOR_COUNT); } -fn get_harness(validator_count: usize) -> BeaconChainHarness> { +fn get_harness(validator_count: usize) -> BeaconChainHarness> { let harness = BeaconChainHarness::builder(E::default()) .default_spec() .keypairs(KEYPAIRS[0..validator_count].to_vec()) diff --git a/consensus/types/src/beacon_state/tests.rs b/consensus/types/src/beacon_state/tests.rs index 333caf4eda..d63eaafc4b 100644 --- a/consensus/types/src/beacon_state/tests.rs +++ b/consensus/types/src/beacon_state/tests.rs @@ -2,8 +2,8 @@ use crate::test_utils::*; use crate::test_utils::{SeedableRng, XorShiftRng}; use beacon_chain::test_utils::{ - interop_genesis_state_with_eth1, test_spec, BeaconChainHarness, - EphemeralTestingSlotClockHarnessType as HarnessType, DEFAULT_ETH1_BLOCK_HASH, + interop_genesis_state_with_eth1, test_spec, BeaconChainHarness, EphemeralHarnessType, + DEFAULT_ETH1_BLOCK_HASH, }; use beacon_chain::types::{ test_utils::TestRandom, BeaconState, BeaconStateAltair, BeaconStateBase, BeaconStateError, @@ -28,7 +28,7 @@ lazy_static! { async fn get_harness( validator_count: usize, slot: Slot, -) -> BeaconChainHarness> { +) -> BeaconChainHarness> { let harness = BeaconChainHarness::builder(E::default()) .default_spec() .keypairs(KEYPAIRS[0..validator_count].to_vec()) diff --git a/lcli/src/transition_blocks.rs b/lcli/src/transition_blocks.rs index f92bbcb493..44a1772ccd 100644 --- a/lcli/src/transition_blocks.rs +++ b/lcli/src/transition_blocks.rs @@ -62,7 +62,7 @@ //! --exclude-post-block-thc //! ``` use beacon_chain::{ - test_utils::EphemeralTestingSlotClockHarnessType, validator_pubkey_cache::ValidatorPubkeyCache, + test_utils::EphemeralHarnessType, validator_pubkey_cache::ValidatorPubkeyCache, }; use clap::ArgMatches; use clap_utils::{parse_optional, parse_required}; @@ -297,7 +297,7 @@ fn do_transition( block: SignedBeaconBlock, mut state_root_opt: Option, config: &Config, - validator_pubkey_cache: &ValidatorPubkeyCache>, + validator_pubkey_cache: &ValidatorPubkeyCache>, spec: &ChainSpec, ) -> Result, String> { if !config.exclude_cache_builds { diff --git a/testing/ef_tests/src/cases/fork_choice.rs b/testing/ef_tests/src/cases/fork_choice.rs index 584c99a1de..8b4d0caf57 100644 --- a/testing/ef_tests/src/cases/fork_choice.rs +++ b/testing/ef_tests/src/cases/fork_choice.rs @@ -6,7 +6,7 @@ use beacon_chain::{ attestation_verification::{ obtain_indexed_attestation_and_committees_per_slot, VerifiedAttestation, }, - test_utils::{BeaconChainHarness, EphemeralTestingSlotClockHarnessType}, + test_utils::{BeaconChainHarness, EphemeralHarnessType}, BeaconChainTypes, CachedHead, CountUnrealized, NotifyExecutionLayer, }; use execution_layer::{json_structures::JsonPayloadStatusV1Status, PayloadStatusV1}; @@ -286,7 +286,7 @@ impl Case for ForkChoiceTest { /// A testing rig used to execute a test case. struct Tester { - harness: BeaconChainHarness>, + harness: BeaconChainHarness>, spec: ChainSpec, } @@ -306,15 +306,14 @@ impl Tester { )); } - let harness = - BeaconChainHarness::>::builder(E::default()) - .spec(spec.clone()) - .keypairs(vec![]) - .genesis_state_ephemeral_store(case.anchor_state.clone()) - .mock_execution_layer() - .recalculate_fork_times_with_genesis(0) - .mock_execution_layer_all_payloads_valid() - .build(); + let harness = BeaconChainHarness::>::builder(E::default()) + .spec(spec.clone()) + .keypairs(vec![]) + .genesis_state_ephemeral_store(case.anchor_state.clone()) + .mock_execution_layer() + .recalculate_fork_times_with_genesis(0) + .mock_execution_layer_all_payloads_valid() + .build(); if harness.chain.genesis_block_root != case.anchor_block.canonical_root() { // This check will need to be removed if/when the fork-choice tests use a non-genesis @@ -470,12 +469,11 @@ impl Tester { .map_err(|e| { Error::InternalError(format!("attestation indexing failed with {:?}", e)) })?; - let verified_attestation: ManuallyVerifiedAttestation< - EphemeralTestingSlotClockHarnessType, - > = ManuallyVerifiedAttestation { - attestation, - indexed_attestation, - }; + let verified_attestation: ManuallyVerifiedAttestation> = + ManuallyVerifiedAttestation { + attestation, + indexed_attestation, + }; self.harness .chain diff --git a/testing/state_transition_vectors/src/exit.rs b/testing/state_transition_vectors/src/exit.rs index 837ac03536..d581eba965 100644 --- a/testing/state_transition_vectors/src/exit.rs +++ b/testing/state_transition_vectors/src/exit.rs @@ -1,5 +1,5 @@ use super::*; -use beacon_chain::test_utils::{BeaconChainHarness, EphemeralTestingSlotClockHarnessType}; +use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType}; use state_processing::{ per_block_processing, per_block_processing::errors::ExitInvalid, BlockProcessingError, BlockSignatureStrategy, ConsensusContext, VerifyBlockRoot, @@ -18,12 +18,8 @@ struct ExitTest { #[allow(clippy::type_complexity)] state_modifier: Box)>, #[allow(clippy::type_complexity)] - block_modifier: Box< - dyn FnOnce( - &BeaconChainHarness>, - &mut BeaconBlock, - ), - >, + block_modifier: + Box>, &mut BeaconBlock)>, #[allow(dead_code)] expected: Result<(), BlockProcessingError>, } diff --git a/testing/state_transition_vectors/src/main.rs b/testing/state_transition_vectors/src/main.rs index a1ce3be3de..3e7c37af54 100644 --- a/testing/state_transition_vectors/src/main.rs +++ b/testing/state_transition_vectors/src/main.rs @@ -2,7 +2,7 @@ mod macros; mod exit; -use beacon_chain::test_utils::{BeaconChainHarness, EphemeralTestingSlotClockHarnessType}; +use beacon_chain::test_utils::{BeaconChainHarness, EphemeralHarnessType}; use lazy_static::lazy_static; use ssz::Encode; use std::env; @@ -53,7 +53,7 @@ lazy_static! { async fn get_harness( slot: Slot, validator_count: usize, -) -> BeaconChainHarness> { +) -> BeaconChainHarness> { let harness = BeaconChainHarness::builder(E::default()) .default_spec() .keypairs(KEYPAIRS[0..validator_count].to_vec())