Add BeaconChainHarness::builder (#2707)

## Issue Addressed

NA

## Proposed Changes

This PR is near-identical to https://github.com/sigp/lighthouse/pull/2652, however it is to be merged into `unstable` instead of `merge-f2f`. Please see that PR for reasoning.

I'm making this duplicate PR to merge to `unstable` in an effort to shrink the diff between `unstable` and `merge-f2f` by doing smaller, lead-up PRs.

## Additional Info

NA
This commit is contained in:
Paul Hauner
2021-10-14 02:58:10 +00:00
parent 0a77d783a4
commit e2d09bb8ac
23 changed files with 449 additions and 438 deletions

View File

@@ -5,7 +5,6 @@ extern crate lazy_static;
use beacon_chain::test_utils::{AttestationStrategy, BeaconChainHarness, BlockStrategy};
use beacon_chain::{StateSkipConfig, WhenSlotSkipped};
use store::config::StoreConfig;
use tree_hash::TreeHash;
use types::{AggregateSignature, EthSpec, Keypair, MainnetEthSpec, RelativeEpoch, Slot};
@@ -25,12 +24,11 @@ fn produces_attestations() {
let num_blocks_produced = MainnetEthSpec::slots_per_epoch() * 4;
let additional_slots_tested = MainnetEthSpec::slots_per_epoch() * 3;
let harness = BeaconChainHarness::new_with_store_config(
MainnetEthSpec,
None,
KEYPAIRS[..].to_vec(),
StoreConfig::default(),
);
let harness = BeaconChainHarness::builder(MainnetEthSpec)
.default_spec()
.keypairs(KEYPAIRS[..].to_vec())
.fresh_ephemeral_store()
.build();
let chain = &harness.chain;

View File

@@ -14,7 +14,6 @@ use int_to_bytes::int_to_bytes32;
use state_processing::{
per_block_processing::errors::AttestationValidationError, per_slot_processing,
};
use store::config::StoreConfig;
use tree_hash::TreeHash;
use types::{
test_utils::generate_deterministic_keypair, AggregateSignature, Attestation, BeaconStateError,
@@ -41,12 +40,11 @@ fn get_harness(validator_count: usize) -> BeaconChainHarness<EphemeralHarnessTyp
// not all.
spec.target_aggregators_per_committee = 4;
let harness = BeaconChainHarness::new_with_store_config(
MainnetEthSpec,
Some(spec),
KEYPAIRS[0..validator_count].to_vec(),
StoreConfig::default(),
);
let harness = BeaconChainHarness::builder(MainnetEthSpec)
.spec(spec)
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.fresh_ephemeral_store()
.build();
harness.advance_slot();

View File

@@ -6,7 +6,7 @@ extern crate lazy_static;
use beacon_chain::test_utils::{
AttestationStrategy, BeaconChainHarness, BlockStrategy, EphemeralHarnessType,
};
use beacon_chain::{BeaconSnapshot, BlockError, ChainConfig, ChainSegmentResult};
use beacon_chain::{BeaconSnapshot, BlockError, ChainSegmentResult};
use logging::test_logger;
use slasher::{Config as SlasherConfig, Slasher};
use state_processing::{
@@ -15,7 +15,6 @@ use state_processing::{
per_slot_processing, BlockProcessingError,
};
use std::sync::Arc;
use store::config::StoreConfig;
use tempfile::tempdir;
use types::{test_utils::generate_deterministic_keypair, *};
@@ -53,12 +52,11 @@ fn get_chain_segment() -> Vec<BeaconSnapshot<E>> {
}
fn get_harness(validator_count: usize) -> BeaconChainHarness<EphemeralHarnessType<E>> {
let harness = BeaconChainHarness::new_with_store_config(
MainnetEthSpec,
None,
KEYPAIRS[0..validator_count].to_vec(),
StoreConfig::default(),
);
let harness = BeaconChainHarness::builder(MainnetEthSpec)
.default_spec()
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.fresh_ephemeral_store()
.build();
harness.advance_slot();
@@ -841,14 +839,13 @@ fn verify_block_for_gossip_slashing_detection() {
.unwrap(),
);
let harness = BeaconChainHarness::ephemeral_with_mutator(
MainnetEthSpec,
None,
KEYPAIRS.to_vec(),
StoreConfig::default(),
ChainConfig::default(),
|builder| builder.slasher(slasher.clone()),
);
let inner_slasher = slasher.clone();
let harness = BeaconChainHarness::builder(MainnetEthSpec)
.default_spec()
.keypairs(KEYPAIRS.to_vec())
.fresh_ephemeral_store()
.initial_mutator(Box::new(move |builder| builder.slasher(inner_slasher)))
.build();
harness.advance_slot();
let state = harness.get_current_state();
@@ -923,13 +920,11 @@ fn add_base_block_to_altair_chain() {
// The Altair fork happens at epoch 1.
spec.altair_fork_epoch = Some(Epoch::new(1));
let harness = BeaconChainHarness::new_with_chain_config(
MainnetEthSpec,
Some(spec),
KEYPAIRS[..].to_vec(),
StoreConfig::default(),
ChainConfig::default(),
);
let harness = BeaconChainHarness::builder(MainnetEthSpec)
.spec(spec)
.keypairs(KEYPAIRS[..].to_vec())
.fresh_ephemeral_store()
.build();
// Move out of the genesis slot.
harness.advance_slot();
@@ -1042,13 +1037,11 @@ fn add_altair_block_to_base_chain() {
// Altair never happens.
spec.altair_fork_epoch = None;
let harness = BeaconChainHarness::new_with_chain_config(
MainnetEthSpec,
Some(spec),
KEYPAIRS[..].to_vec(),
StoreConfig::default(),
ChainConfig::default(),
);
let harness = BeaconChainHarness::builder(MainnetEthSpec)
.spec(spec)
.keypairs(KEYPAIRS[..].to_vec())
.fresh_ephemeral_store()
.build();
// Move out of the genesis slot.
harness.advance_slot();

View File

@@ -38,12 +38,11 @@ fn get_store(db_path: &TempDir) -> Arc<HotColdDB> {
}
fn get_harness(store: Arc<HotColdDB>, validator_count: usize) -> TestHarness {
let harness = BeaconChainHarness::new_with_disk_store(
MinimalEthSpec,
None,
store,
KEYPAIRS[0..validator_count].to_vec(),
);
let harness = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.fresh_disk_store(store)
.build();
harness.advance_slot();
harness
}

View File

@@ -61,12 +61,11 @@ fn get_harness(
store: Arc<HotColdDB<E, LevelDB<E>, LevelDB<E>>>,
validator_count: usize,
) -> TestHarness {
let harness = BeaconChainHarness::new_with_disk_store(
MinimalEthSpec,
None,
store,
KEYPAIRS[0..validator_count].to_vec(),
);
let harness = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.fresh_disk_store(store)
.build();
harness.advance_slot();
harness
}
@@ -365,12 +364,11 @@ fn delete_blocks_and_states() {
let store = get_store(&db_path);
let validators_keypairs =
types::test_utils::generate_deterministic_keypairs(LOW_VALIDATOR_COUNT);
let harness = BeaconChainHarness::new_with_disk_store(
MinimalEthSpec,
None,
store.clone(),
validators_keypairs,
);
let harness = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(validators_keypairs)
.fresh_disk_store(store.clone())
.build();
let unforked_blocks: u64 = 4 * E::slots_per_epoch();
@@ -492,8 +490,11 @@ fn multi_epoch_fork_valid_blocks_test(
let store = get_store(&db_path);
let validators_keypairs =
types::test_utils::generate_deterministic_keypairs(LOW_VALIDATOR_COUNT);
let harness =
BeaconChainHarness::new_with_disk_store(MinimalEthSpec, None, store, validators_keypairs);
let harness = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(validators_keypairs)
.fresh_disk_store(store)
.build();
let num_fork1_blocks: u64 = num_fork1_blocks_.try_into().unwrap();
let num_fork2_blocks: u64 = num_fork2_blocks_.try_into().unwrap();
@@ -783,7 +784,11 @@ fn prunes_abandoned_fork_between_two_finalized_checkpoints() {
let validators_keypairs = types::test_utils::generate_deterministic_keypairs(VALIDATOR_COUNT);
let honest_validators: Vec<usize> = (0..HONEST_VALIDATOR_COUNT).collect();
let adversarial_validators: Vec<usize> = (HONEST_VALIDATOR_COUNT..VALIDATOR_COUNT).collect();
let rig = BeaconChainHarness::new(MinimalEthSpec, None, validators_keypairs);
let rig = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(validators_keypairs)
.fresh_ephemeral_store()
.build();
let slots_per_epoch = rig.slots_per_epoch();
let (mut state, state_root) = rig.get_current_state_and_root();
@@ -888,7 +893,11 @@ fn pruning_does_not_touch_abandoned_block_shared_with_canonical_chain() {
let validators_keypairs = types::test_utils::generate_deterministic_keypairs(VALIDATOR_COUNT);
let honest_validators: Vec<usize> = (0..HONEST_VALIDATOR_COUNT).collect();
let adversarial_validators: Vec<usize> = (HONEST_VALIDATOR_COUNT..VALIDATOR_COUNT).collect();
let rig = BeaconChainHarness::new(MinimalEthSpec, None, validators_keypairs);
let rig = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(validators_keypairs)
.fresh_ephemeral_store()
.build();
let slots_per_epoch = rig.slots_per_epoch();
let (state, state_root) = rig.get_current_state_and_root();
@@ -1013,7 +1022,11 @@ fn pruning_does_not_touch_blocks_prior_to_finalization() {
let validators_keypairs = types::test_utils::generate_deterministic_keypairs(VALIDATOR_COUNT);
let honest_validators: Vec<usize> = (0..HONEST_VALIDATOR_COUNT).collect();
let adversarial_validators: Vec<usize> = (HONEST_VALIDATOR_COUNT..VALIDATOR_COUNT).collect();
let rig = BeaconChainHarness::new(MinimalEthSpec, None, validators_keypairs);
let rig = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(validators_keypairs)
.fresh_ephemeral_store()
.build();
let slots_per_epoch = rig.slots_per_epoch();
let (mut state, state_root) = rig.get_current_state_and_root();
@@ -1103,7 +1116,11 @@ fn prunes_fork_growing_past_youngest_finalized_checkpoint() {
let validators_keypairs = types::test_utils::generate_deterministic_keypairs(VALIDATOR_COUNT);
let honest_validators: Vec<usize> = (0..HONEST_VALIDATOR_COUNT).collect();
let adversarial_validators: Vec<usize> = (HONEST_VALIDATOR_COUNT..VALIDATOR_COUNT).collect();
let rig = BeaconChainHarness::new(MinimalEthSpec, None, validators_keypairs);
let rig = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(validators_keypairs)
.fresh_ephemeral_store()
.build();
let (state, state_root) = rig.get_current_state_and_root();
// Fill up 0th epoch with canonical chain blocks
@@ -1241,7 +1258,11 @@ fn prunes_skipped_slots_states() {
let validators_keypairs = types::test_utils::generate_deterministic_keypairs(VALIDATOR_COUNT);
let honest_validators: Vec<usize> = (0..HONEST_VALIDATOR_COUNT).collect();
let adversarial_validators: Vec<usize> = (HONEST_VALIDATOR_COUNT..VALIDATOR_COUNT).collect();
let rig = BeaconChainHarness::new(MinimalEthSpec, None, validators_keypairs);
let rig = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(validators_keypairs)
.fresh_ephemeral_store()
.build();
let (state, state_root) = rig.get_current_state_and_root();
let canonical_slots_zeroth_epoch: Vec<Slot> =
@@ -1360,7 +1381,11 @@ fn finalizes_non_epoch_start_slot() {
let validators_keypairs = types::test_utils::generate_deterministic_keypairs(VALIDATOR_COUNT);
let honest_validators: Vec<usize> = (0..HONEST_VALIDATOR_COUNT).collect();
let adversarial_validators: Vec<usize> = (HONEST_VALIDATOR_COUNT..VALIDATOR_COUNT).collect();
let rig = BeaconChainHarness::new(MinimalEthSpec, None, validators_keypairs);
let rig = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(validators_keypairs)
.fresh_ephemeral_store()
.build();
let (state, state_root) = rig.get_current_state_and_root();
let canonical_slots_zeroth_epoch: Vec<Slot> =
@@ -1923,12 +1948,11 @@ fn finalizes_after_resuming_from_db() {
let db_path = tempdir().unwrap();
let store = get_store(&db_path);
let harness = BeaconChainHarness::new_with_disk_store(
MinimalEthSpec,
None,
store.clone(),
KEYPAIRS[0..validator_count].to_vec(),
);
let harness = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.fresh_disk_store(store.clone())
.build();
harness.advance_slot();
@@ -1967,12 +1991,11 @@ fn finalizes_after_resuming_from_db() {
let original_chain = harness.chain;
let resumed_harness = BeaconChainHarness::resume_from_disk_store(
MinimalEthSpec,
None,
store,
KEYPAIRS[0..validator_count].to_vec(),
);
let resumed_harness = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.resumed_disk_store(store)
.build();
assert_chains_pretty_much_the_same(&original_chain, &resumed_harness.chain);
@@ -2037,22 +2060,20 @@ fn revert_minority_fork_on_resume() {
// Chain with no fork epoch configured.
let db_path1 = tempdir().unwrap();
let store1 = get_store_with_spec(&db_path1, spec1.clone());
let harness1 = BeaconChainHarness::new_with_disk_store(
MinimalEthSpec,
Some(spec1),
store1,
KEYPAIRS[0..validator_count].to_vec(),
);
let harness1 = BeaconChainHarness::builder(MinimalEthSpec)
.spec(spec1)
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.fresh_disk_store(store1)
.build();
// Chain with fork epoch configured.
let db_path2 = tempdir().unwrap();
let store2 = get_store_with_spec(&db_path2, spec2.clone());
let harness2 = BeaconChainHarness::new_with_disk_store(
MinimalEthSpec,
Some(spec2.clone()),
store2,
KEYPAIRS[0..validator_count].to_vec(),
);
let harness2 = BeaconChainHarness::builder(MinimalEthSpec)
.spec(spec2.clone())
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.fresh_disk_store(store2)
.build();
// Apply the same blocks to both chains initially.
let mut state = harness1.get_current_state();
@@ -2130,13 +2151,12 @@ fn revert_minority_fork_on_resume() {
// the beacon chain builder loads the head block.
drop(harness1);
let resume_store = get_store_with_spec(&db_path1, spec2.clone());
let resumed_harness = BeaconChainHarness::new_with_mutator(
MinimalEthSpec,
spec2,
resume_store,
KEYPAIRS[0..validator_count].to_vec(),
ChainConfig::default(),
|mut builder| {
let resumed_harness = BeaconChainHarness::builder(MinimalEthSpec)
.spec(spec2)
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.resumed_disk_store(resume_store)
.override_store_mutator(Box::new(move |mut builder| {
builder = builder
.resume_from_db()
.unwrap()
@@ -2147,8 +2167,8 @@ fn revert_minority_fork_on_resume() {
.unwrap()
.set_slot(end_slot.as_u64());
builder
},
);
}))
.build();
// Head should now be just before the fork.
resumed_harness.chain.fork_choice().unwrap();

View File

@@ -28,11 +28,11 @@ lazy_static! {
fn get_harness(validator_count: usize) -> BeaconChainHarness<EphemeralHarnessType<E>> {
let mut spec = E::default_spec();
spec.altair_fork_epoch = Some(Epoch::new(0));
let harness = BeaconChainHarness::new(
MainnetEthSpec,
Some(spec),
KEYPAIRS[0..validator_count].to_vec(),
);
let harness = BeaconChainHarness::builder(MainnetEthSpec)
.spec(spec)
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.fresh_ephemeral_store()
.build();
harness.advance_slot();

View File

@@ -15,7 +15,6 @@ use operation_pool::PersistedOperationPool;
use state_processing::{
per_slot_processing, per_slot_processing::Error as SlotProcessingError, EpochProcessingError,
};
use store::config::StoreConfig;
use types::{BeaconStateError, EthSpec, Hash256, Keypair, MinimalEthSpec, RelativeEpoch, Slot};
// Should ideally be divisible by 3.
@@ -27,12 +26,11 @@ lazy_static! {
}
fn get_harness(validator_count: usize) -> BeaconChainHarness<EphemeralHarnessType<MinimalEthSpec>> {
let harness = BeaconChainHarness::new_with_store_config(
MinimalEthSpec,
None,
KEYPAIRS[0..validator_count].to_vec(),
StoreConfig::default(),
);
let harness = BeaconChainHarness::builder(MinimalEthSpec)
.default_spec()
.keypairs(KEYPAIRS[0..validator_count].to_vec())
.fresh_ephemeral_store()
.build();
harness.advance_slot();