Skip to content

Commit

Permalink
Use clap to parse args in executor binary
Browse files Browse the repository at this point in the history
  • Loading branch information
slowli committed Oct 27, 2023
1 parent 24d7091 commit 34e4da3
Show file tree
Hide file tree
Showing 7 changed files with 175 additions and 197 deletions.
2 changes: 1 addition & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ assert_matches = "1.5.0"
async-trait = "0.1.71"
bit-vec = "0.6"
blst = "0.3.10"
clap = { version = "4.3.3", features = ["derive"] }
clap = { version = "4.3.3", features = ["derive", "env"] }
ed25519-dalek = { version = "2.0.0", features = ["serde", "rand_core"] }
futures = "0.3.28"
hex = "0.4.3"
Expand Down
2 changes: 1 addition & 1 deletion node/tools/src/bin/localnet_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn with_unspecified_ip(addr: SocketAddr) -> SocketAddr {
}

/// Command line arguments.
#[derive(Parser, Debug)]
#[derive(Debug, Parser)]
struct Args {
/// Path to a file with newline separated IP:port addrs of the nodes to configure.
/// Binary will generate a config for each IP in this file.
Expand Down
125 changes: 125 additions & 0 deletions node/tools/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//! Node configuration.
use anyhow::Context as _;
use crypto::{read_optional_text, Text, TextFmt};
use executor::{ConsensusConfig, ExecutorConfig};
use roles::{node, validator};
use schema::{proto::executor::config as proto, read_optional, read_required, ProtoFmt};
use std::{fs, net, path::Path};

/// This struct holds the file path to each of the config files.
#[derive(Debug)]
pub struct ConfigPaths<'a> {
/// Path to a JSON file with node configuration.
pub config: &'a Path,
/// Path to a validator key file.
pub validator_key: Option<&'a Path>,
/// Path to a node key file.
pub node_key: &'a Path,
}

/// Node configuration including executor configuration, optional validator configuration,
/// and application-specific settings (e.g. metrics scraping).
#[derive(Debug)]
pub struct NodeConfig {
/// Executor configuration.
pub executor: ExecutorConfig,
/// IP:port to serve metrics data for scraping.
pub metrics_server_addr: Option<net::SocketAddr>,
/// Consensus network config.
pub consensus: Option<ConsensusConfig>,
}

impl ProtoFmt for NodeConfig {
type Proto = proto::NodeConfig;

fn read(r: &Self::Proto) -> anyhow::Result<Self> {
Ok(Self {
executor: read_required(&r.executor).context("executor")?,
metrics_server_addr: read_optional_text(&r.metrics_server_addr)
.context("metrics_server_addr")?,
consensus: read_optional(&r.consensus).context("consensus")?,
})
}

fn build(&self) -> Self::Proto {
Self::Proto {
executor: Some(self.executor.build()),
metrics_server_addr: self.metrics_server_addr.as_ref().map(TextFmt::encode),
consensus: self.consensus.as_ref().map(ProtoFmt::build),
}
}
}

/// Main struct that holds the config options for the node.
#[derive(Debug)]
pub struct Configs {
/// Executor configuration of the node.
pub executor: ExecutorConfig,
/// IP:port to serve metrics data for scraping.
pub metrics_server_addr: Option<net::SocketAddr>,
/// Consensus-specific config extensions. Only set for validators.
pub consensus: Option<(ConsensusConfig, validator::SecretKey)>,
/// The validator secret key for this node.
/// The node secret key. This key is used by both full nodes and validators to identify themselves
/// in the P2P network.
pub node_key: node::SecretKey,
}

impl Configs {
/// Method to fetch the node config.
#[tracing::instrument(level = "trace", ret)]
pub fn read(args: ConfigPaths<'_>) -> anyhow::Result<Self> {
let node_config = fs::read_to_string(args.config).with_context(|| {
format!(
"failed reading node config from `{}`",
args.config.display()
)
})?;
let node_config: NodeConfig = schema::decode_json(&node_config).with_context(|| {
format!(
"failed decoding JSON node config at `{}`",
args.config.display()
)
})?;

let validator_key: Option<validator::SecretKey> = args
.validator_key
.as_ref()
.map(|validator_key| {
let read_key = fs::read_to_string(validator_key).with_context(|| {
format!(
"failed reading validator key from `{}`",
validator_key.display()
)
})?;
Text::new(&read_key).decode().with_context(|| {
format!(
"failed decoding validator key at `{}`",
validator_key.display()
)
})
})
.transpose()?;
let read_key = fs::read_to_string(args.node_key).with_context(|| {
format!("failed reading node key from `{}`", args.node_key.display())
})?;
let node_key = Text::new(&read_key).decode().with_context(|| {
format!("failed decoding node key at `{}`", args.node_key.display())
})?;

anyhow::ensure!(
validator_key.is_some() == node_config.consensus.is_some(),
"Validator key and consensus config must be specified at the same time"
);
let consensus = validator_key.and_then(|key| Some((node_config.consensus?, key)));

let cfg = Configs {
executor: node_config.executor,
metrics_server_addr: node_config.metrics_server_addr,
consensus,
node_key,
};
Ok(cfg)
}
}
111 changes: 0 additions & 111 deletions node/tools/src/config/config_paths.rs

This file was deleted.

67 changes: 0 additions & 67 deletions node/tools/src/config/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion node/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
mod config;

pub use self::config::{Configs, NodeConfig};
pub use self::config::{ConfigPaths, Configs, NodeConfig};
Loading

0 comments on commit 34e4da3

Please sign in to comment.