Refactor BeaconStateBuilder

Made it a production-only struct. All the testing stuff can be done with
BeaconStateBencher
This commit is contained in:
Paul Hauner 2019-03-09 10:39:05 +11:00
parent 63743a962c
commit a77d1885a1
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
2 changed files with 42 additions and 16 deletions

View File

@ -1,6 +1,7 @@
use super::ValidatorHarness;
use beacon_chain::{BeaconChain, BlockProcessingOutcome};
pub use beacon_chain::{BeaconChainError, CheckPoint};
use bls::get_withdrawal_credentials;
use db::{
stores::{BeaconBlockStore, BeaconStateStore},
MemoryDB,
@ -43,7 +44,12 @@ impl BeaconChainHarness {
///
/// - A keypair, `BlockProducer` and `Attester` for each validator.
/// - 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 block_store = Arc::new(BeaconBlockStore::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);
// 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.
//
// 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.
// Otherwise, generate them deterministically where the first validator has a secret key of
// `1`, etc.
let keypairs = if let Some(path) = validators_dir {
debug!("Loading validator keypairs from file...");
let keypairs_file = File::open(path.join("keypairs.yaml")).unwrap();
let mut keypairs: Vec<Keypair> = serde_yaml::from_reader(&keypairs_file).unwrap();
keypairs.truncate(validator_count);
keypairs
} else {
debug!("Generating validator keypairs...");
generate_deterministic_keypairs(validator_count)
};
debug!("Loading validators from file...");
let validators_file = File::open(path.join("validators.yaml")).unwrap();
let mut validators: Vec<Validator> = serde_yaml::from_reader(&validators_file).unwrap();
validators.truncate(validator_count);
// Skipping deposit verification means directly generating `Validator` records, instead
// of generating `Deposit` objects, verifying them and converting them into `Validator`
// records.
//
// 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];
@ -82,15 +113,10 @@ impl BeaconChainHarness {
validator_count as u64,
&spec,
);
keypairs
} else {
debug!("Generating validator keypairs...");
let keypairs = generate_deterministic_keypairs(validator_count);
debug!("Generating initial validator deposits...");
let deposits = generate_deposits_from_keypairs(&keypairs, genesis_time, &spec);
state_builder.process_initial_deposits(&deposits, &spec);
keypairs
};
let genesis_state = state_builder.build(&spec).unwrap();

View File

@ -81,7 +81,7 @@ impl TestCase {
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);