Skip to content

Commit

Permalink
Making file config and cli config mutually exclusive
Browse files Browse the repository at this point in the history
  • Loading branch information
ElFantasma committed Mar 18, 2024
1 parent 9be2f43 commit a61822d
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 124 deletions.
24 changes: 15 additions & 9 deletions node/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,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.5.3", features = ["derive"] }
criterion = "0.5.1"
ed25519-dalek = { version = "2.0.0", features = ["rand_core"] }
ff_ce = "0.14.3"
Expand Down
49 changes: 49 additions & 0 deletions node/tools/src/bin/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//! asdfasdf
use clap::{command, Args, Parser};
use std::path::PathBuf;

/// Command-line application launching a node executor.
#[derive(Debug, Parser)]
/// asdfasdf
struct Cli {
/// Node configuration.
/// Command line config
/// Full json config
#[arg(long, requires="node_key", conflicts_with_all=["config_file", "validator_key_file", "node_key_file"])]
config: Option<String>,
/// Plain node key
#[arg(long, requires="config", conflicts_with_all=["config_file", "validator_key_file", "node_key_file"])]
node_key: Option<String>,
/// Plain validator key
#[arg(long, conflicts_with_all=["config_file", "validator_key_file", "node_key_file"])]
validator_key: Option<String>,

/// Path to a validator key file. If set to an empty string, validator key will not be read
/// (i.e., a node will be initialized as a non-validator node).
#[arg(long, default_value = "./validator_key")]
validator_key_file: PathBuf,
/// Path to a JSON file with node configuration.
#[arg(long, default_value = "./config.json")]
config_file: PathBuf,
/// Path to a node key file.
#[arg(long, default_value = "./node_key")]
node_key_file: PathBuf,

/// Path to the rocksdb database of the node.
#[arg(long, default_value = "./database")]
database: PathBuf,
/// Port for the RPC server.
#[arg(long)]
rpc_port: Option<u16>,
}

/// adasdsa
#[tokio::main]
/// adasdsa
async fn main() -> anyhow::Result<()> {
let args: Cli = Cli::parse();
dbg!(args);
Ok(())
}
110 changes: 57 additions & 53 deletions node/tools/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,32 @@ impl ProtoFmt for AppConfig {
/// Configuration information.
#[derive(Debug)]
pub struct ConfigArgs<'a> {
/// Node configuration from command line.
pub config: Option<AppConfig>,
/// Path to a JSON file with node configuration.
pub config_file: &'a Path,
/// Validator key as a string.
pub validator_key: Option<validator::SecretKey>,
/// Path to a validator key file.
pub validator_key_file: &'a Path,
/// Node key as a string.
pub node_key: Option<node::SecretKey>,
/// Path to a node key file.
pub node_key_file: &'a Path,
/// Node configuration.
pub config_args: ConfigSource<'a>,
/// Path to the rocksdb database.
pub database: &'a Path,
}

#[derive(Debug)]
pub enum ConfigSource<'a> {
CliConfig {
/// Node configuration from command line.
config: AppConfig,
/// Node key as a string.
node_key: node::SecretKey,
/// Validator key as a string.
validator_key: Option<validator::SecretKey>,
},
PathConfig {
/// Path to a JSON file with node configuration.
config_file: &'a Path,
/// Path to a validator key file.
validator_key_file: &'a Path,
/// Path to a node key file.
node_key_file: &'a Path,
},
}

pub struct Configs {
pub app: AppConfig,
pub validator_key: Option<validator::SecretKey>,
Expand All @@ -179,50 +189,44 @@ pub struct Configs {
impl<'a> ConfigArgs<'a> {
// Loads configs from the file system.
pub fn load(self) -> anyhow::Result<Configs> {
Ok(Configs {
app: (|| {
let config = match self.config {
Some(app) => app,
None => {
let app = fs::read_to_string(self.config_file).context(format!(
"failed reading file: {}",
self.config_file.display()
))?;
decode_json::<Serde<AppConfig>>(&app)
.context("failed decoding JSON")?
.0
}
};
Ok::<AppConfig, anyhow::Error>(config)
})()
.context("config")?,

validator_key: self.validator_key.or({
fs::read_to_string(self.validator_key_file)
.context(format!(
"failed reading file: {}",
self.validator_key_file.display()
))
match self.config_args {
ConfigSource::CliConfig {
config,
node_key,
validator_key,
} => Ok(Configs {
app: config.clone(),
validator_key: validator_key.clone(),
node_key: node_key.clone(),
database: self.database.into(),
}),
ConfigSource::PathConfig {
config_file,
validator_key_file,
node_key_file,
} => Ok(Configs {
app: (|| {
let app = fs::read_to_string(config_file).context("failed reading file")?;
decode_json::<Serde<AppConfig>>(&app).context("failed decoding JSON")
})()
.with_context(|| config_file.display().to_string())?
.0,

validator_key: fs::read_to_string(validator_key_file)
.ok()
.map(|value| Text::new(&value).decode().context("failed decoding key"))
}
.transpose()
.context("validator key")?),

node_key: self.node_key.or({
fs::read_to_string(self.node_key_file)
.context(format!(
"failed reading file: {}",
self.node_key_file.display()
))
.ok()
.map( |value| Text::new(&value).decode().context("failed decoding key"))
}
.transpose()
.context("node key")?).expect("Missing node key: Should provide --node-key, --node-key-file, or place a `node_key` file at root directory"),
.transpose()
.with_context(|| validator_key_file.display().to_string())?,

database: self.database.into(),
})
node_key: (|| {
let key = fs::read_to_string(node_key_file).context("failed reading file")?;
Text::new(&key).decode().context("failed decoding key")
})()
.with_context(|| node_key_file.display().to_string())?,

database: self.database.into(),
}),
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion node/tools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ mod store;
#[cfg(test)]
mod tests;

pub use config::{decode_json, AppConfig, ConfigArgs, NodeAddr, NODES_PORT};
pub use config::{decode_json, AppConfig, ConfigArgs, ConfigSource, NodeAddr, NODES_PORT};
pub use rpc::server::RPCServer;
Loading

0 comments on commit a61822d

Please sign in to comment.