mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-20 13:24:44 +00:00
Allow test_harness to load validators from file.
This commit is contained in:
@@ -11,6 +11,7 @@ use rayon::prelude::*;
|
||||
use slot_clock::TestingSlotClock;
|
||||
use ssz::TreeHash;
|
||||
use std::collections::HashSet;
|
||||
use std::fs::File;
|
||||
use std::iter::FromIterator;
|
||||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
@@ -54,22 +55,44 @@ impl BeaconChainHarness {
|
||||
block_hash: Hash256::zero(),
|
||||
};
|
||||
|
||||
let (keypairs, initial_validator_deposits) = if let Some(path) = validators_dir {
|
||||
let keypairs_path = path.join("keypairs.yaml");
|
||||
let deposits_path = path.join("deposits.yaml");
|
||||
load_deposits_from_file(
|
||||
validator_count,
|
||||
&keypairs_path.as_path(),
|
||||
&deposits_path.as_path(),
|
||||
)
|
||||
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.
|
||||
//
|
||||
// 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.
|
||||
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);
|
||||
|
||||
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);
|
||||
|
||||
let balances = vec![32_000_000_000; validator_count];
|
||||
|
||||
state_builder.import_existing_validators(
|
||||
validators,
|
||||
balances,
|
||||
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);
|
||||
(keypairs, deposits)
|
||||
state_builder.process_initial_deposits(&deposits, &spec);
|
||||
keypairs
|
||||
};
|
||||
|
||||
let mut state_builder = BeaconStateBuilder::new(genesis_time, latest_eth1_data, &spec);
|
||||
state_builder.process_initial_deposits(&initial_validator_deposits, &spec);
|
||||
let genesis_state = state_builder.build(&spec).unwrap();
|
||||
let state_root = Hash256::from_slice(&genesis_state.hash_tree_root());
|
||||
let genesis_block = BeaconBlock::genesis(state_root, &spec);
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
use crate::beacon_chain_harness::{
|
||||
generate_deposits_from_keypairs, generate_deterministic_keypairs,
|
||||
};
|
||||
use crate::beacon_chain_harness::generate_deterministic_keypairs;
|
||||
use bls::get_withdrawal_credentials;
|
||||
use clap::{value_t, ArgMatches};
|
||||
use log::debug;
|
||||
use serde_yaml;
|
||||
@@ -9,33 +8,52 @@ use std::{fs, fs::File};
|
||||
use types::*;
|
||||
|
||||
const KEYPAIRS_FILE: &str = "keypairs.yaml";
|
||||
const DEPOSITS_FILE: &str = "deposits.yaml";
|
||||
const VALIDATORS_FILE: &str = "validators.yaml";
|
||||
|
||||
pub fn prepare(matches: &ArgMatches, spec: &ChainSpec) {
|
||||
let validator_count = value_t!(matches.value_of("validator_count"), usize)
|
||||
.expect("Validator count is required argument");
|
||||
let genesis_time =
|
||||
value_t!(matches.value_of("genesis_time"), u64).expect("Genesis time is required argument");
|
||||
let output_dir = matches
|
||||
.value_of("output_dir")
|
||||
.expect("Output dir has a default value.");
|
||||
|
||||
debug!("Created keypairs and deposits, writing to file...");
|
||||
debug!("Created keypairs and validators, writing to file...");
|
||||
|
||||
fs::create_dir_all(Path::new(output_dir)).unwrap();
|
||||
|
||||
// Ensure that keypairs is dropped before writing deposits, this provides a big memory saving
|
||||
// Ensure that keypairs is dropped before writing validators, this provides a big memory saving
|
||||
// for large validator_counts.
|
||||
let deposits = {
|
||||
let validators: Vec<Validator> = {
|
||||
debug!("Creating {} keypairs...", validator_count);
|
||||
let keypairs = generate_deterministic_keypairs(validator_count);
|
||||
debug!("Writing {} keypairs to file...", validator_count);
|
||||
write_keypairs(output_dir, &keypairs);
|
||||
debug!("Creating {} deposits to file...", validator_count);
|
||||
generate_deposits_from_keypairs(&keypairs, genesis_time, &spec)
|
||||
debug!("Creating {} validators...", validator_count);
|
||||
keypairs
|
||||
.iter()
|
||||
.map(|keypair| generate_validator(&keypair, spec))
|
||||
.collect()
|
||||
};
|
||||
debug!("Writing {} deposits to file...", validator_count);
|
||||
write_deposits(output_dir, &deposits);
|
||||
|
||||
debug!("Writing {} validators to file...", validator_count);
|
||||
write_validators(output_dir, &validators);
|
||||
}
|
||||
|
||||
fn generate_validator(keypair: &Keypair, spec: &ChainSpec) -> Validator {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
fn write_keypairs(output_dir: &str, keypairs: &[Keypair]) {
|
||||
@@ -44,8 +62,8 @@ fn write_keypairs(output_dir: &str, keypairs: &[Keypair]) {
|
||||
serde_yaml::to_writer(keypairs_file, &keypairs).unwrap();
|
||||
}
|
||||
|
||||
fn write_deposits(output_dir: &str, deposits: &[Deposit]) {
|
||||
let deposits_path = Path::new(output_dir).join(DEPOSITS_FILE);
|
||||
let deposits_file = File::create(deposits_path).unwrap();
|
||||
serde_yaml::to_writer(deposits_file, &deposits).unwrap();
|
||||
fn write_validators(output_dir: &str, validators: &[Validator]) {
|
||||
let validators_path = Path::new(output_dir).join(VALIDATORS_FILE);
|
||||
let validators_file = File::create(validators_path).unwrap();
|
||||
serde_yaml::to_writer(validators_file, &validators).unwrap();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user