Skip to content

Commit d87be8a

Browse files
committed
fixup!: add help text to ArgsConfig & DRY up alias parsing code
1 parent fcbaca3 commit d87be8a

1 file changed

Lines changed: 67 additions & 31 deletions

File tree

ldk-server/src/util/config.rs

Lines changed: 67 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -219,16 +219,10 @@ impl ConfigBuilder {
219219
let alias = self
220220
.alias
221221
.map(|alias_str| {
222-
let mut bytes = [0u8; 32];
223-
let alias_bytes = alias_str.trim().as_bytes();
224-
if alias_bytes.len() > 32 {
225-
return Err(io::Error::new(
226-
io::ErrorKind::InvalidInput,
227-
"node.alias must be at most 32 bytes long.".to_string(),
228-
));
229-
}
230-
bytes[..alias_bytes.len()].copy_from_slice(alias_bytes);
231-
Ok(NodeAlias(bytes))
222+
let node_alias = parse_alias(alias_str.as_ref()).map_err(|e| {
223+
io::Error::new(e.kind(), format!("Failed to parse alias: {}", e))
224+
})?;
225+
Ok::<NodeAlias, io::Error>(node_alias)
232226
})
233227
.transpose()?;
234228

@@ -243,7 +237,7 @@ impl ConfigBuilder {
243237
.filter(|&&is_configured| is_configured)
244238
.count();
245239

246-
if configured_sources_count > 1 {
240+
if configured_sources_count != 1 {
247241
return Err(io::Error::new(
248242
io::ErrorKind::InvalidInput,
249243
"Must set a single chain source, multiple were configured".to_string(),
@@ -473,34 +467,70 @@ impl From<LSPS2ServiceTomlConfig> for LSPS2ServiceConfig {
473467
override_usage = "ldk-server [config_path]"
474468
)]
475469
pub struct ArgsConfig {
476-
#[arg(required = false)]
470+
#[arg(required = false, help = "The configuration file for running LDK Server.")]
477471
config_file: Option<String>,
478472

479-
#[arg(long, env = "LDK_SERVER_NODE_NETWORK")]
473+
#[arg(
474+
long,
475+
env = "LDK_SERVER_NODE_NETWORK",
476+
help = "The used Bitcoin network for the underlying Bitcoin node."
477+
)]
480478
node_network: Option<Network>,
481479

482-
#[arg(long, env = "LDK_SERVER_NODE_LISTENING_ADDRESSES")]
480+
#[arg(
481+
long,
482+
env = "LDK_SERVER_NODE_LISTENING_ADDRESSES",
483+
help = "The addresses on which the node will listen for incoming connections."
484+
)]
483485
node_listening_addresses: Option<Vec<String>>,
484486

485-
#[arg(long, env = "LDK_SERVER_NODE_ANNOUNCEMENT_ADDRESSES")]
487+
#[arg(
488+
long,
489+
env = "LDK_SERVER_NODE_ANNOUNCEMENT_ADDRESSES",
490+
help = "The addresses which the node will announce to the gossip network that it accepts connections on."
491+
)]
486492
node_announcement_addresses: Option<Vec<String>>,
487493

488-
#[arg(long, env = "LDK_SERVER_NODE_REST_SERVICE_ADDRESS")]
494+
#[arg(
495+
long,
496+
env = "LDK_SERVER_NODE_REST_SERVICE_ADDRESS",
497+
help = "The rest service address for the LDK Server API."
498+
)]
489499
node_rest_service_address: Option<String>,
490500

491-
#[arg(long, env = "LDK_SERVER_NODE_ALIAS")]
501+
#[arg(
502+
long,
503+
env = "LDK_SERVER_NODE_ALIAS",
504+
help = "The node alias that will be used when broadcasting announcements to the gossip network."
505+
)]
492506
node_alias: Option<String>,
493507

494-
#[arg(long, env = "LDK_SERVER_BITCOIND_RPC_ADDRESS")]
508+
#[arg(
509+
long,
510+
env = "LDK_SERVER_BITCOIND_RPC_ADDRESS",
511+
help = "The underlying Bitcoin node RPC address."
512+
)]
495513
bitcoind_rpc_address: Option<String>,
496514

497-
#[arg(long, env = "LDK_SERVER_BITCOIND_RPC_USER")]
515+
#[arg(
516+
long,
517+
env = "LDK_SERVER_BITCOIND_RPC_USER",
518+
help = "The underlying Bitcoin node RPC user."
519+
)]
498520
bitcoind_rpc_user: Option<String>,
499521

500-
#[arg(long, env = "LDK_SERVER_BITCOIND_RPC_PASSWORD")]
522+
#[arg(
523+
long,
524+
env = "LDK_SERVER_BITCOIND_RPC_PASSWORD",
525+
help = "The underlying Bitcoin node RPC password."
526+
)]
501527
bitcoind_rpc_password: Option<String>,
502528

503-
#[arg(long, env = "LDK_SERVER_STORAGE_DIR_PATH")]
529+
#[arg(
530+
long,
531+
env = "LDK_SERVER_STORAGE_DIR_PATH",
532+
help = "The path where the underlying LDK and BDK persist their data."
533+
)]
504534
storage_dir_path: Option<String>,
505535
}
506536

@@ -542,6 +572,19 @@ fn missing_field_err(field: &str) -> io::Error {
542572
)
543573
}
544574

575+
fn parse_alias(alias_str: &str) -> Result<NodeAlias, io::Error> {
576+
let mut bytes = [0u8; 32];
577+
let alias_bytes = alias_str.trim().as_bytes();
578+
if alias_bytes.len() > 32 {
579+
return Err(io::Error::new(
580+
io::ErrorKind::InvalidInput,
581+
"node.alias must be at most 32 bytes long.".to_string(),
582+
));
583+
}
584+
bytes[..alias_bytes.len()].copy_from_slice(alias_bytes);
585+
Ok(NodeAlias(bytes))
586+
}
587+
545588
#[cfg(test)]
546589
mod tests {
547590
use std::str::FromStr;
@@ -614,13 +657,6 @@ mod tests {
614657
)
615658
}
616659

617-
fn parse_alias(alias_str: &str) -> NodeAlias {
618-
let mut bytes = [0u8; 32];
619-
let alias_bytes = alias_str.trim().as_bytes();
620-
bytes[..alias_bytes.len()].copy_from_slice(alias_bytes);
621-
NodeAlias(bytes)
622-
}
623-
624660
#[test]
625661
fn test_config_from_file() {
626662
let storage_path = std::env::temp_dir();
@@ -654,7 +690,7 @@ mod tests {
654690
let expected = Config {
655691
listening_addrs: Some(vec![SocketAddress::from_str("localhost:3001").unwrap()]),
656692
announcement_addrs: Some(vec![SocketAddress::from_str("54.3.7.81:3001").unwrap()]),
657-
alias: Some(parse_alias(alias)),
693+
alias: Some(parse_alias(alias).unwrap()),
658694
network: Network::Regtest,
659695
rest_service_addr: SocketAddr::from_str("127.0.0.1:3002").unwrap(),
660696
storage_dir_path: Some("/tmp".to_string()),
@@ -939,7 +975,7 @@ mod tests {
939975
args_config.node_rest_service_address.as_deref().unwrap(),
940976
)
941977
.unwrap(),
942-
alias: Some(parse_alias(args_config.node_alias.as_deref().unwrap())),
978+
alias: Some(parse_alias(args_config.node_alias.as_deref().unwrap()).unwrap()),
943979
storage_dir_path: Some(args_config.storage_dir_path.unwrap()),
944980
tls_config: None,
945981
chain_source: ChainSource::Rpc {
@@ -1025,7 +1061,7 @@ mod tests {
10251061
args_config.node_rest_service_address.as_deref().unwrap(),
10261062
)
10271063
.unwrap(),
1028-
alias: Some(parse_alias(args_config.node_alias.as_deref().unwrap())),
1064+
alias: Some(parse_alias(args_config.node_alias.as_deref().unwrap()).unwrap()),
10291065
storage_dir_path: Some(args_config.storage_dir_path.unwrap()),
10301066
tls_config: Some(TlsConfig {
10311067
cert_path: Some("/path/to/tls.crt".to_string()),

0 commit comments

Comments
 (0)