Skip to content

Commit

Permalink
Fix ContextualNetAddress parsing (kaspanet#433)
Browse files Browse the repository at this point in the history
* Fix ContextualNetAddress parsing

* fix kaspanet#434

* Fix handling for all addresses + arrays

* Fix log-level taking default instead of config file

* Align Args deserialize names to CLI flag names

* More renames to align deserialize struct like cli

* update README
  • Loading branch information
orgr authored and D-Stacks committed Jul 10, 2024
1 parent 1bb7edb commit 4d4a391
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 14 deletions.
23 changes: 14 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,25 +251,30 @@ cargo run --release --bin kaspad -- --testnet
<details>
<summary>
Use a configuration file
Using a configuration file
</summary>
```bash
cargo run --release --bin kaspad -- --configfile /path/to/configfile.toml
# or
cargo run --release --bin kaspad -- -C /path/to/configfile.toml
```
The config file should be a list of \<CLI argument\> = \<value\> separated by newlines.
- The config file should be a list of \<CLI argument\> = \<value\> separated by newlines.
- Whitespace around the `=` is fine, `arg=value` and `arg = value` are both parsed correctly.
- Values with special characters like `.` or `=` will require quoting the value i.e \<CLI argument\> = "\<value\>".
- Arguments with multiple values should be surrounded with brackets like `addpeer = ["10.0.0.1", "1.2.3.4"]`.
For example:
```
testnet=true
utxoindex=false
disable-upnp=true
perf-metrics=true
appdir="some-dir"
testnet-suffix=11
testnet = true
utxoindex = false
disable-upnp = true
perf-metrics = true
appdir = "some-dir"
netsuffix = 11
addpeer = ["10.0.0.1", "1.2.3.4"]
```
note that some arguments have no space between words and some use kebab-case, pass the --help flag to view all possible arguments
Pass the `--help` flag to view all possible arguments
```bash
cargo run --release --bin kaspad -- --help
Expand Down
1 change: 1 addition & 0 deletions kaspad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ thiserror.workspace = true
tokio = { workspace = true, features = ["rt", "macros", "rt-multi-thread"] }
workflow-log.workspace = true
toml = "0.8.10"
serde_with = "3.7.0"

[features]
heap = ["dhat", "kaspa-alloc/heap"]
Expand Down
38 changes: 33 additions & 5 deletions kaspad/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,59 @@ use kaspa_core::kaspad_env::version;

use kaspa_utils::networking::ContextualNetAddress;
use kaspa_wrpc_server::address::WrpcNetAddress;
use serde_with::{serde_as, DisplayFromStr};

#[serde_as]
#[derive(Debug, Clone, Deserialize)]
#[serde(default, rename_all = "kebab-case", deny_unknown_fields)]
pub struct Args {
// NOTE: it is best if property names match config file fields
pub appdir: Option<String>,
pub logdir: Option<String>,
#[serde(rename = "nologfiles")]
pub no_log_files: bool,
#[serde_as(as = "Option<DisplayFromStr>")]
pub rpclisten: Option<ContextualNetAddress>,
#[serde_as(as = "Option<DisplayFromStr>")]
pub rpclisten_borsh: Option<WrpcNetAddress>,
#[serde_as(as = "Option<DisplayFromStr>")]
pub rpclisten_json: Option<WrpcNetAddress>,
#[serde(rename = "unsaferpc")]
pub unsafe_rpc: bool,
pub wrpc_verbose: bool,
#[serde(rename = "loglevel")]
pub log_level: String,
pub async_threads: usize,
#[serde(rename = "connect")]
#[serde_as(as = "Vec<DisplayFromStr>")]
pub connect_peers: Vec<ContextualNetAddress>,
#[serde(rename = "addpeer")]
#[serde_as(as = "Vec<DisplayFromStr>")]
pub add_peers: Vec<ContextualNetAddress>,
#[serde_as(as = "Option<DisplayFromStr>")]
pub listen: Option<ContextualNetAddress>,
#[serde(rename = "uacomment")]
pub user_agent_comments: Vec<String>,
pub utxoindex: bool,
pub txindex: bool,
pub reset_db: bool,
#[serde(rename = "outpeers")]
pub outbound_target: usize,
#[serde(rename = "maxinpeers")]
pub inbound_limit: usize,
#[serde(rename = "rpcmaxclients")]
pub rpc_max_clients: usize,
pub enable_unsynced_mining: bool,
pub enable_mainnet_mining: bool,
pub testnet: bool,
#[serde(rename = "netsuffix")]
pub testnet_suffix: u32,
pub devnet: bool,
pub simnet: bool,
pub archival: bool,
pub sanity: bool,
pub yes: bool,
#[serde_as(as = "Option<DisplayFromStr>")]
pub externalip: Option<ContextualNetAddress>,
pub perf_metrics: bool,
pub perf_metrics_interval_sec: u64,
Expand All @@ -69,7 +88,9 @@ pub struct Args {
pub prealloc_amount: u64,

pub disable_upnp: bool,
#[serde(rename = "nodnsseed")]
pub disable_dns_seeding: bool,
#[serde(rename = "nogrpc")]
pub disable_grpc: bool,
pub ram_scale: f64,
}
Expand Down Expand Up @@ -97,7 +118,7 @@ impl Default for Args {
simnet: false,
archival: false,
sanity: false,
logdir: Some("".into()),
logdir: None,
rpclisten: None,
wrpc_verbose: false,
log_level: "INFO".into(),
Expand Down Expand Up @@ -393,10 +414,10 @@ impl Args {
rpclisten_json: m.get_one::<WrpcNetAddress>("rpclisten-json").cloned().or(defaults.rpclisten_json),
unsafe_rpc: arg_match_unwrap_or::<bool>(&m, "unsaferpc", defaults.unsafe_rpc),
wrpc_verbose: false,
log_level: m.get_one::<String>("log_level").cloned().unwrap(),
log_level: arg_match_unwrap_or::<String>(&m, "log_level", defaults.log_level),
async_threads: arg_match_unwrap_or::<usize>(&m, "async_threads", defaults.async_threads),
connect_peers: m.get_many::<ContextualNetAddress>("connect-peers").unwrap_or_default().copied().collect(),
add_peers: m.get_many::<ContextualNetAddress>("add-peers").unwrap_or_default().copied().collect(),
connect_peers: arg_match_many_unwrap_or::<ContextualNetAddress>(&m, "connect-peers", defaults.connect_peers),
add_peers: arg_match_many_unwrap_or::<ContextualNetAddress>(&m, "add-peers", defaults.add_peers),
listen: m.get_one::<ContextualNetAddress>("listen").cloned().or(defaults.listen),
outbound_target: arg_match_unwrap_or::<usize>(&m, "outpeers", defaults.outbound_target),
inbound_limit: arg_match_unwrap_or::<usize>(&m, "maxinpeers", defaults.inbound_limit),
Expand All @@ -413,7 +434,7 @@ impl Args {
archival: arg_match_unwrap_or::<bool>(&m, "archival", defaults.archival),
sanity: arg_match_unwrap_or::<bool>(&m, "sanity", defaults.sanity),
yes: arg_match_unwrap_or::<bool>(&m, "yes", defaults.yes),
user_agent_comments: m.get_many::<String>("user_agent_comments").unwrap_or_default().cloned().collect(),
user_agent_comments: arg_match_many_unwrap_or::<String>(&m, "user_agent_comments", defaults.user_agent_comments),
externalip: m.get_one::<ContextualNetAddress>("externalip").cloned(),
perf_metrics: arg_match_unwrap_or::<bool>(&m, "perf-metrics", defaults.perf_metrics),
perf_metrics_interval_sec: arg_match_unwrap_or::<u64>(&m, "perf-metrics-interval-sec", defaults.perf_metrics_interval_sec),
Expand Down Expand Up @@ -441,6 +462,13 @@ fn arg_match_unwrap_or<T: Clone + Send + Sync + 'static>(m: &clap::ArgMatches, a
m.get_one::<T>(arg_id).cloned().filter(|_| m.value_source(arg_id) != Some(DefaultValue)).unwrap_or(default)
}

fn arg_match_many_unwrap_or<T: Clone + Send + Sync + 'static>(m: &clap::ArgMatches, arg_id: &str, default: Vec<T>) -> Vec<T> {
match m.get_many::<T>(arg_id) {
Some(val_ref) => val_ref.cloned().collect(),
None => default,
}
}

/*
-V, --version Display version information and exit
Expand Down

0 comments on commit 4d4a391

Please sign in to comment.