diff --git a/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs index f220619ce..95899e23b 100644 --- a/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs +++ b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness.rs @@ -1,12 +1,12 @@ use super::ValidatorHarness; use beacon_chain::{BeaconChain, BlockProcessingOutcome}; pub use beacon_chain::{BeaconChainError, CheckPoint}; -use bls::{create_proof_of_possession, get_withdrawal_credentials}; use db::{ stores::{BeaconBlockStore, BeaconStateStore}, MemoryDB, }; use fork_choice::BitwiseLMDGhost; +use generate_deposits::generate_deposits_with_random_keypairs; use log::debug; use rayon::prelude::*; use slot_clock::TestingSlotClock; @@ -15,6 +15,8 @@ use std::iter::FromIterator; use std::sync::Arc; use types::*; +mod generate_deposits; + /// The beacon chain harness simulates a single beacon node with `validator_count` validators connected /// to it. Each validator is provided a borrow to the beacon chain, where it may read /// information and submit blocks/attestations for processing. @@ -47,40 +49,8 @@ impl BeaconChainHarness { block_hash: Hash256::zero(), }; - debug!("Generating validator keypairs..."); - - let keypairs: Vec = (0..validator_count) - .collect::>() - .par_iter() - .map(|_| Keypair::random()) - .collect(); - - debug!("Creating validator deposits..."); - - let initial_validator_deposits = keypairs - .par_iter() - .map(|keypair| Deposit { - branch: vec![], // branch verification is not specified. - index: 0, // index verification is not specified. - deposit_data: DepositData { - amount: 32_000_000_000, // 32 ETH (in Gwei) - timestamp: genesis_time - 1, - deposit_input: DepositInput { - pubkey: keypair.pk.clone(), - // Validator can withdraw using their main keypair. - withdrawal_credentials: Hash256::from_slice( - &get_withdrawal_credentials( - &keypair.pk, - spec.bls_withdrawal_prefix_byte, - )[..], - ), - proof_of_possession: create_proof_of_possession(&keypair), - }, - }, - }) - .collect(); - - debug!("Creating the BeaconChain..."); + let (keypairs, initial_validator_deposits) = + generate_deposits_with_random_keypairs(validator_count, genesis_time, &spec); // Create the Beacon Chain let beacon_chain = Arc::new( diff --git a/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness/generate_deposits.rs b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness/generate_deposits.rs new file mode 100644 index 000000000..39924fb67 --- /dev/null +++ b/beacon_node/beacon_chain/test_harness/src/beacon_chain_harness/generate_deposits.rs @@ -0,0 +1,54 @@ +use bls::{create_proof_of_possession, get_withdrawal_credentials}; +use log::debug; +use rayon::prelude::*; +use types::*; + +/// Generates `validator_count` deposits using randomly generated keypairs and some default specs +/// for the deposits. +pub fn generate_deposits_with_random_keypairs( + validator_count: usize, + genesis_time: u64, + spec: &ChainSpec, +) -> (Vec, Vec) { + debug!( + "Generating {} random validator keypairs...", + validator_count + ); + + let keypairs: Vec = (0..validator_count) + .collect::>() + .par_iter() + .map(|_| Keypair::random()) + .collect(); + + debug!( + "Generating {} validator deposits from random keypairs...", + validator_count + ); + + let initial_validator_deposits = + keypairs + .par_iter() + .map(|keypair| Deposit { + branch: vec![], // branch verification is not specified. + index: 0, // index verification is not specified. + deposit_data: DepositData { + amount: 32_000_000_000, // 32 ETH (in Gwei) + timestamp: genesis_time - 1, + deposit_input: DepositInput { + pubkey: keypair.pk.clone(), + // Validator can withdraw using their main keypair. + withdrawal_credentials: Hash256::from_slice( + &get_withdrawal_credentials( + &keypair.pk, + spec.bls_withdrawal_prefix_byte, + )[..], + ), + proof_of_possession: create_proof_of_possession(&keypair), + }, + }, + }) + .collect(); + + (keypairs, initial_validator_deposits) +}