Refactor BeaconStateBuilder
Made it a production-only struct. All the testing stuff can be done with BeaconStateBencher
This commit is contained in:
parent
63743a962c
commit
a77d1885a1
@ -1,6 +1,7 @@
|
|||||||
use super::ValidatorHarness;
|
use super::ValidatorHarness;
|
||||||
use beacon_chain::{BeaconChain, BlockProcessingOutcome};
|
use beacon_chain::{BeaconChain, BlockProcessingOutcome};
|
||||||
pub use beacon_chain::{BeaconChainError, CheckPoint};
|
pub use beacon_chain::{BeaconChainError, CheckPoint};
|
||||||
|
use bls::get_withdrawal_credentials;
|
||||||
use db::{
|
use db::{
|
||||||
stores::{BeaconBlockStore, BeaconStateStore},
|
stores::{BeaconBlockStore, BeaconStateStore},
|
||||||
MemoryDB,
|
MemoryDB,
|
||||||
@ -43,7 +44,12 @@ impl BeaconChainHarness {
|
|||||||
///
|
///
|
||||||
/// - A keypair, `BlockProducer` and `Attester` for each validator.
|
/// - A keypair, `BlockProducer` and `Attester` for each validator.
|
||||||
/// - A new BeaconChain struct where the given validators are in the genesis.
|
/// - A new BeaconChain struct where the given validators are in the genesis.
|
||||||
pub fn new(spec: ChainSpec, validator_count: usize, validators_dir: Option<&Path>) -> Self {
|
pub fn new(
|
||||||
|
spec: ChainSpec,
|
||||||
|
validator_count: usize,
|
||||||
|
validators_dir: Option<&Path>,
|
||||||
|
skip_deposit_verification: bool,
|
||||||
|
) -> Self {
|
||||||
let db = Arc::new(MemoryDB::open());
|
let db = Arc::new(MemoryDB::open());
|
||||||
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
|
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
|
||||||
let state_store = Arc::new(BeaconStateStore::new(db.clone()));
|
let state_store = Arc::new(BeaconStateStore::new(db.clone()));
|
||||||
@ -57,22 +63,47 @@ impl BeaconChainHarness {
|
|||||||
|
|
||||||
let mut state_builder = BeaconStateBuilder::new(genesis_time, latest_eth1_data, &spec);
|
let mut state_builder = BeaconStateBuilder::new(genesis_time, latest_eth1_data, &spec);
|
||||||
|
|
||||||
// If a `validators_dir` is specified, load the keypairs and validators from YAML files.
|
// If a `validators_dir` is specified, load the keypairs a YAML file.
|
||||||
//
|
//
|
||||||
// Otherwise, build all the keypairs and initial validator deposits manually.
|
// Otherwise, generate them deterministically where the first validator has a secret key of
|
||||||
//
|
// `1`, etc.
|
||||||
// It is _much_ faster to load from YAML, however it does skip all the initial processing
|
|
||||||
// and verification of `Deposits`, so it is a slightly less comprehensive test.
|
|
||||||
let keypairs = if let Some(path) = validators_dir {
|
let keypairs = if let Some(path) = validators_dir {
|
||||||
debug!("Loading validator keypairs from file...");
|
debug!("Loading validator keypairs from file...");
|
||||||
let keypairs_file = File::open(path.join("keypairs.yaml")).unwrap();
|
let keypairs_file = File::open(path.join("keypairs.yaml")).unwrap();
|
||||||
let mut keypairs: Vec<Keypair> = serde_yaml::from_reader(&keypairs_file).unwrap();
|
let mut keypairs: Vec<Keypair> = serde_yaml::from_reader(&keypairs_file).unwrap();
|
||||||
keypairs.truncate(validator_count);
|
keypairs.truncate(validator_count);
|
||||||
|
keypairs
|
||||||
|
} else {
|
||||||
|
debug!("Generating validator keypairs...");
|
||||||
|
generate_deterministic_keypairs(validator_count)
|
||||||
|
};
|
||||||
|
|
||||||
debug!("Loading validators from file...");
|
// Skipping deposit verification means directly generating `Validator` records, instead
|
||||||
let validators_file = File::open(path.join("validators.yaml")).unwrap();
|
// of generating `Deposit` objects, verifying them and converting them into `Validator`
|
||||||
let mut validators: Vec<Validator> = serde_yaml::from_reader(&validators_file).unwrap();
|
// records.
|
||||||
validators.truncate(validator_count);
|
//
|
||||||
|
// It is much faster to skip deposit verification, however it does not test the initial
|
||||||
|
// validator induction part of beacon chain genesis.
|
||||||
|
if skip_deposit_verification {
|
||||||
|
let validators = keypairs
|
||||||
|
.iter()
|
||||||
|
.map(|keypair| {
|
||||||
|
let withdrawal_credentials = Hash256::from_slice(&get_withdrawal_credentials(
|
||||||
|
&keypair.pk,
|
||||||
|
spec.bls_withdrawal_prefix_byte,
|
||||||
|
));
|
||||||
|
|
||||||
|
Validator {
|
||||||
|
pubkey: keypair.pk.clone(),
|
||||||
|
withdrawal_credentials,
|
||||||
|
activation_epoch: spec.far_future_epoch,
|
||||||
|
exit_epoch: spec.far_future_epoch,
|
||||||
|
withdrawable_epoch: spec.far_future_epoch,
|
||||||
|
initiated_exit: false,
|
||||||
|
slashed: false,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
let balances = vec![32_000_000_000; validator_count];
|
let balances = vec![32_000_000_000; validator_count];
|
||||||
|
|
||||||
@ -82,15 +113,10 @@ impl BeaconChainHarness {
|
|||||||
validator_count as u64,
|
validator_count as u64,
|
||||||
&spec,
|
&spec,
|
||||||
);
|
);
|
||||||
|
|
||||||
keypairs
|
|
||||||
} else {
|
} else {
|
||||||
debug!("Generating validator keypairs...");
|
|
||||||
let keypairs = generate_deterministic_keypairs(validator_count);
|
|
||||||
debug!("Generating initial validator deposits...");
|
debug!("Generating initial validator deposits...");
|
||||||
let deposits = generate_deposits_from_keypairs(&keypairs, genesis_time, &spec);
|
let deposits = generate_deposits_from_keypairs(&keypairs, genesis_time, &spec);
|
||||||
state_builder.process_initial_deposits(&deposits, &spec);
|
state_builder.process_initial_deposits(&deposits, &spec);
|
||||||
keypairs
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let genesis_state = state_builder.build(&spec).unwrap();
|
let genesis_state = state_builder.build(&spec).unwrap();
|
||||||
|
@ -81,7 +81,7 @@ impl TestCase {
|
|||||||
validator_count
|
validator_count
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut harness = BeaconChainHarness::new(spec, validator_count, validators_dir);
|
let mut harness = BeaconChainHarness::new(spec, validator_count, validators_dir, true);
|
||||||
|
|
||||||
info!("Starting simulation across {} slots...", slots);
|
info!("Starting simulation across {} slots...", slots);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user