Skip to content

Commit

Permalink
Support keystore files with no extension (#21).
Browse files Browse the repository at this point in the history
Other changes:

  - Improve user messages regarding validator key loading.
  • Loading branch information
Tumas committed Mar 20, 2024
1 parent 6fe2be2 commit 7d94290
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 18 deletions.
50 changes: 37 additions & 13 deletions grandine/src/validators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use anyhow::{Error, Result};
use bls::{PublicKeyBytes, SecretKey};
use educe::Educe;
use eip_2335::Keystore;
use log::warn;
use log::{info, warn};
use rayon::iter::{IntoParallelIterator as _, ParallelIterator as _};
use signer::KeyOrigin;
use std_ext::ArcExt;
Expand All @@ -30,6 +30,11 @@ pub enum Validators {
},
}

enum KeystoreExtension {
Json,
None,
}

impl Validators {
fn keymap_from_paths(
keystore_dir: impl AsRef<Path>,
Expand All @@ -38,7 +43,7 @@ impl Validators {
let keystore_dir = keystore_dir.as_ref();
let keystore_password_file = keystore_password_file.as_ref();
let individual_passwords = keystore_password_file.is_dir();
let keystore_glob = "*.json";
let keystore_glob = "*";

let old_working_directory = std::env::current_dir()?;

Expand All @@ -47,20 +52,35 @@ impl Validators {
let keystores = glob::glob(keystore_glob)
.expect("glob pattern should be valid")
.flatten()
.map(|path| {
let keystore_file = keystore_dir.join(path.as_path());
.filter_map(|path| {
// None is a supported extension
let supported_extension = match path.extension() {
None => Some(KeystoreExtension::None),
Some(extension) => (extension == "json").then_some(KeystoreExtension::Json),
};

let password_file = if individual_passwords {
let file_stem = path
.file_stem()
.expect("glob patterns above only match paths that have file names");
if let Some(extension) = supported_extension {
let keystore_file = keystore_dir.join(path.as_path());

keystore_password_file.join(file_stem).with_extension("txt")
} else {
keystore_password_file.to_path_buf()
};
let password_file = if individual_passwords {
let file_stem = path
.file_stem()
.expect("glob patterns above only match paths that have file names");

(keystore_file, password_file)
let password_file = keystore_password_file.join(file_stem);

match extension {
KeystoreExtension::Json => password_file.with_extension("txt"),
KeystoreExtension::None => password_file,
}
} else {
keystore_password_file.to_path_buf()
};

Some((keystore_file, password_file))
} else {
None
}
})
.collect();

Expand All @@ -74,6 +94,8 @@ impl Validators {
mut validator_key_cache: Option<&mut ValidatorKeyCache>,
keystore_storage: &ValidatorKeyCache,
) -> Result<Vec<(PublicKeyBytes, Arc<SecretKey>, KeyOrigin)>> {
info!("started loading keystore and password files");

// Collect all passwords and keystores first.
// They may be used to load secret keys from the cache.
// Secret keys are decrypted later.
Expand Down Expand Up @@ -129,6 +151,8 @@ impl Validators {

let public_key = secret_key.to_public_key().into();

info!("decrypted validator key {public_key:?}");

Ok((public_key, secret_key))
})?;

Expand Down
8 changes: 3 additions & 5 deletions runtime/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use genesis::GenesisProvider;
use http_api::{Channels as HttpApiChannels, HttpApi, HttpApiConfig};
use keymanager::KeyManager;
use liveness_tracker::LivenessTracker;
use log::info;
use log::{info, warn};
use metrics::{run_metrics_server, MetricsChannels, MetricsService};
use operation_pools::{AttestationAggPool, BlsToExecutionChangePool, SyncCommitteeAggPool};
use p2p::{
Expand Down Expand Up @@ -82,12 +82,10 @@ pub async fn run_after_genesis<P: Preset>(
..
} = storage_config;

for pubkey in signer.keys() {
info!("loaded validator key {pubkey:?}");
}

if !signer.is_empty() {
info!("loaded {} validator key(s)", signer.keys().len());
} else if validator_config.keystore_storage_password_file.is_some() {
warn!("failed to load validator keys");
}

let (execution_service_tx, execution_service_rx) = mpsc::unbounded();
Expand Down

0 comments on commit 7d94290

Please sign in to comment.