From: Matt Corallo Date: Mon, 5 Jul 2021 00:00:01 +0000 (+0000) Subject: Allow multiple publicly-announced addresses X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=93d3b7942b2a0a4990e5a93fda262f9edb671364;p=ldk-sample Allow multiple publicly-announced addresses --- diff --git a/src/cli.rs b/src/cli.rs index b69f9af..bfea9d2 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -34,14 +34,14 @@ pub(crate) struct LdkUserInfo { pub(crate) bitcoind_rpc_host: String, pub(crate) ldk_storage_dir_path: String, pub(crate) ldk_peer_listening_port: u16, - pub(crate) ldk_announced_listen_addr: Option, + pub(crate) ldk_announced_listen_addr: Vec, pub(crate) ldk_announced_node_name: [u8; 32], pub(crate) network: Network, } pub(crate) fn parse_startup_args() -> Result { if env::args().len() < 3 { - println!("ldk-tutorial-node requires 3 arguments: `cargo run :@: ldk_storage_directory_path [] [bitcoin-network] [announced-listen-addr announced-node-name]`"); + println!("ldk-tutorial-node requires 3 arguments: `cargo run :@: ldk_storage_directory_path [] [bitcoin-network] [announced-node-name announced-listen-addr*]`"); return Err(()); } let bitcoind_rpc_info = env::args().skip(1).next().unwrap(); @@ -77,7 +77,7 @@ pub(crate) fn parse_startup_args() -> Result { } }; - let arg_idx = match ldk_peer_port_set { + let mut arg_idx = match ldk_peer_port_set { true => 4, false => 3, }; @@ -88,24 +88,12 @@ pub(crate) fn parse_startup_args() -> Result { None => Network::Testnet, }; - let ldk_announced_listen_addr = match env::args().skip(arg_idx + 1).next().as_ref() { - Some(s) => match IpAddr::from_str(s) { - Ok(IpAddr::V4(a)) => { - Some(NetAddress::IPv4 { addr: a.octets(), port: ldk_peer_listening_port }) - } - Ok(IpAddr::V6(a)) => { - Some(NetAddress::IPv6 { addr: a.octets(), port: ldk_peer_listening_port }) - } - Err(_) => panic!("Failed to parse announced-listen-addr into an IP address"), - }, - None => None, - }; - - let ldk_announced_node_name = match env::args().skip(arg_idx + 2).next().as_ref() { + let ldk_announced_node_name = match env::args().skip(arg_idx + 1).next().as_ref() { Some(s) => { if s.len() > 32 { panic!("Node Alias can not be longer than 32 bytes"); } + arg_idx += 1; let mut bytes = [0; 32]; bytes[..s.len()].copy_from_slice(s.as_bytes()); bytes @@ -113,6 +101,26 @@ pub(crate) fn parse_startup_args() -> Result { None => [0; 32], }; + let mut ldk_announced_listen_addr = Vec::new(); + loop { + match env::args().skip(arg_idx + 1).next().as_ref() { + Some(s) => match IpAddr::from_str(s) { + Ok(IpAddr::V4(a)) => { + ldk_announced_listen_addr + .push(NetAddress::IPv4 { addr: a.octets(), port: ldk_peer_listening_port }); + arg_idx += 1; + } + Ok(IpAddr::V6(a)) => { + ldk_announced_listen_addr + .push(NetAddress::IPv6 { addr: a.octets(), port: ldk_peer_listening_port }); + arg_idx += 1; + } + Err(_) => panic!("Failed to parse announced-listen-addr into an IP address"), + }, + None => break, + } + } + Ok(LdkUserInfo { bitcoind_rpc_username, bitcoind_rpc_password, diff --git a/src/main.rs b/src/main.rs index cba93f6..3932a58 100644 --- a/src/main.rs +++ b/src/main.rs @@ -569,7 +569,7 @@ async fn start_ldk() { // to avoid churn in the global network graph. let chan_manager = Arc::clone(&channel_manager); let network = args.network; - if args.ldk_announced_listen_addr.is_some() { + if !args.ldk_announced_listen_addr.is_empty() { tokio::spawn(async move { let mut interval = tokio::time::interval(Duration::from_secs(60)); loop { @@ -577,7 +577,7 @@ async fn start_ldk() { chan_manager.broadcast_node_announcement( [0; 3], args.ldk_announced_node_name, - vec![args.ldk_announced_listen_addr.as_ref().unwrap().clone()], + args.ldk_announced_listen_addr.clone(), ); } });