Add TestingBeaconStateBuilder fn for cloned kps

Allows for faster test setups.

Implemented method for fork choice tests.
This commit is contained in:
Paul Hauner 2019-03-14 15:10:20 +11:00
parent ac6dc81ebf
commit f4959fc03c
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
2 changed files with 19 additions and 3 deletions

View File

@ -24,7 +24,7 @@ use std::collections::HashMap;
use std::sync::Arc;
use std::{fs::File, io::prelude::*, path::PathBuf};
use types::test_utils::TestingBeaconStateBuilder;
use types::{BeaconBlock, BeaconBlockBody, ChainSpec, Eth1Data, Hash256, Slot};
use types::{BeaconBlock, BeaconBlockBody, ChainSpec, Eth1Data, Hash256, Keypair, Slot};
use yaml_rust::yaml;
// Note: We Assume the block Id's are hex-encoded.
@ -218,7 +218,7 @@ fn load_test_cases_from_yaml(file_path: &str) -> Vec<yaml_rust::Yaml> {
// initialise a single validator and state. All blocks will reference this state root.
fn setup_inital_state(
fork_choice_algo: &ForkChoiceAlgorithm,
no_validators: usize,
num_validators: usize,
) -> (Box<ForkChoice>, Arc<BeaconBlockStore<MemoryDB>>, Hash256) {
let db = Arc::new(MemoryDB::open());
let block_store = Arc::new(BeaconBlockStore::new(db.clone()));
@ -243,7 +243,7 @@ fn setup_inital_state(
let spec = ChainSpec::foundation();
let state_builder =
TestingBeaconStateBuilder::from_deterministic_keypairs(no_validators, &spec);
TestingBeaconStateBuilder::from_single_keypair(num_validators, &Keypair::random(), &spec);
let (state, _keypairs) = state_builder.build();
let state_root = state.canonical_root();

View File

@ -74,6 +74,22 @@ impl TestingBeaconStateBuilder {
TestingBeaconStateBuilder::from_keypairs(keypairs, spec)
}
/// Uses the given keypair for all validators.
pub fn from_single_keypair(
validator_count: usize,
keypair: &Keypair,
spec: &ChainSpec,
) -> Self {
debug!("Generating {} cloned keypairs...", validator_count);
let mut keypairs = Vec::with_capacity(validator_count);
for _ in 0..validator_count {
keypairs.push(keypair.clone())
}
TestingBeaconStateBuilder::from_keypairs(keypairs, spec)
}
/// Creates the builder from an existing set of keypairs.
pub fn from_keypairs(keypairs: Vec<Keypair>, spec: &ChainSpec) -> Self {
let validator_count = keypairs.len();