A first go at persisting validator keys and handling configuration. Addresses issue #253.

- Creates a keystore directory in the config
 - Fetches serialized keys from the keystore directory
 - If no keys, generates keys randomly, saves serialized keys to keystore dir.
This commit is contained in:
Luke Anderson
2019-03-12 21:56:45 +11:00
parent b2926b4ed0
commit e942d7533b
4 changed files with 73 additions and 9 deletions

View File

@@ -6,7 +6,7 @@ use bls::Keypair;
use clap::{App, Arg};
use grpcio::{ChannelBuilder, EnvBuilder};
use protos::services_grpc::{BeaconBlockServiceClient, ValidatorServiceClient};
use slog::{error, info, o, Drain};
use slog::{debug, error, info, o, Drain};
use slot_clock::SystemTimeSlotClock;
use std::path::PathBuf;
use std::sync::Arc;
@@ -17,6 +17,8 @@ mod block_producer_service;
mod config;
mod duties;
const NUMBER_OF_VALIDATOR_TEST_KEYS: u16 = 3;
fn main() {
// Logging
let decorator = slog_term::TermDecorator::new().build();
@@ -55,7 +57,7 @@ fn main() {
)
.get_matches();
let mut config = ClientConfig::default();
let mut config = ClientConfig::default().expect("Unable to create a default configuration.");
// Custom datadir
if let Some(dir) = matches.value_of("datadir") {
@@ -123,9 +125,27 @@ fn main() {
* Start threads.
*/
let mut threads = vec![];
// TODO: keypairs are randomly generated; they should be loaded from a file or generated.
// https://github.com/sigp/lighthouse/issues/160
let keypairs = vec![Keypair::random()];
let keypairs = config
.fetch_keys()
.expect("Encountered an error while fetching saved keys.")
.unwrap_or_else(|| {
// TODO: Key generation should occur in a separate binary
let mut k = Vec::new();
info!(
log,
"No key pairs found, generating and saving 3 random key pairs."
);
for _n in 0..NUMBER_OF_VALIDATOR_TEST_KEYS {
let keypair = Keypair::random();
config
.save_key(&keypair)
.expect("Unable to save newly generated private key.");
debug!(log, "Keypair generated {:?}", keypair.identifier());
k.push(keypair);
}
k
});
for keypair in keypairs {
info!(log, "Starting validator services"; "validator" => keypair.pk.concatenated_hex_id());