]> git.bitcoin.ninja Git - ldk-sample/commitdiff
Merge pull request #22 from TheBlueMatt/main
authorvalentinewallace <valentinewallace@users.noreply.github.com>
Wed, 28 Jul 2021 21:22:37 +0000 (14:22 -0700)
committerGitHub <noreply@github.com>
Wed, 28 Jul 2021 21:22:37 +0000 (14:22 -0700)
Allow multiple publicly-announced addresses and avoid funding panic

1  2 
src/cli.rs
src/main.rs

diff --combined src/cli.rs
index 47106a0dce0193bb6fad4b7285b0299958f08a0b,bfea9d25072b1ec053c70369aa7eb0c26f6ba867..b45de4221b95da49c5e9021b37f94728ded5ea57
@@@ -34,14 -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<NetAddress>,
+       pub(crate) ldk_announced_listen_addr: Vec<NetAddress>,
        pub(crate) ldk_announced_node_name: [u8; 32],
        pub(crate) network: Network,
  }
  
  pub(crate) fn parse_startup_args() -> Result<LdkUserInfo, ()> {
        if env::args().len() < 3 {
-               println!("ldk-tutorial-node requires 3 arguments: `cargo run <bitcoind-rpc-username>:<bitcoind-rpc-password>@<bitcoind-rpc-host>:<bitcoind-rpc-port> ldk_storage_directory_path [<ldk-incoming-peer-listening-port>] [bitcoin-network] [announced-listen-addr announced-node-name]`");
+               println!("ldk-tutorial-node requires 3 arguments: `cargo run <bitcoind-rpc-username>:<bitcoind-rpc-password>@<bitcoind-rpc-host>:<bitcoind-rpc-port> ldk_storage_directory_path [<ldk-incoming-peer-listening-port>] [bitcoin-network] [announced-node-name announced-listen-addr*]`");
                return Err(());
        }
        let bitcoind_rpc_info = env::args().skip(1).next().unwrap();
@@@ -77,7 -77,7 +77,7 @@@
                }
        };
  
-       let arg_idx = match ldk_peer_port_set {
+       let mut arg_idx = match ldk_peer_port_set {
                true => 4,
                false => 3,
        };
                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
                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,
@@@ -401,7 -409,7 +409,7 @@@ fn list_channels(channel_manager: Arc<C
                }
                println!(
                        "\t\tpeer_pubkey: {},",
 -                      hex_utils::hex_str(&chan_info.remote_network_id.serialize())
 +                      hex_utils::hex_str(&chan_info.counterparty.node_id.serialize())
                );
                if let Some(id) = chan_info.short_channel_id {
                        println!("\t\tshort_channel_id: {},", id);
diff --combined src/main.rs
index d24fcad81177ac75e66016a1b55d9e2f72807c0a,b7d74aa0ffb57c0ed427ead353f162b3acd20dbd..4073be9ea73faa4395955d00b8f1693f3a002405
@@@ -19,10 -19,11 +19,10 @@@ use lightning::chain
  use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
  use lightning::chain::chainmonitor;
  use lightning::chain::keysinterface::{InMemorySigner, KeysInterface, KeysManager};
 -use lightning::chain::Filter;
 -use lightning::chain::Watch;
 +use lightning::chain::{BestBlock, Filter, Watch};
  use lightning::ln::channelmanager;
  use lightning::ln::channelmanager::{
 -      BestBlock, ChainParameters, ChannelManagerReadArgs, SimpleArcChannelManager,
 +      ChainParameters, ChannelManagerReadArgs, SimpleArcChannelManager,
  };
  use lightning::ln::peer_handler::{MessageHandler, SimpleArcPeerManager};
  use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
@@@ -138,7 -139,15 +138,15 @@@ async fn handle_ldk_events
                        let final_tx: Transaction =
                                encode::deserialize(&hex_utils::to_vec(&signed_tx.hex).unwrap()).unwrap();
                        // Give the funding transaction back to LDK for opening the channel.
-                       channel_manager.funding_transaction_generated(&temporary_channel_id, final_tx).unwrap();
+                       if channel_manager
+                               .funding_transaction_generated(&temporary_channel_id, final_tx)
+                               .is_err()
+                       {
+                               println!(
+                                       "\nERROR: Channel went away before we could fund it. The peer disconnected or refused the channel.");
+                               print!("> ");
+                               io::stdout().flush().unwrap();
+                       }
                }
                Event::PaymentReceived { payment_hash, payment_preimage, payment_secret, amt, .. } => {
                        let mut payments = inbound_payments.lock().unwrap();
@@@ -551,7 -560,7 +559,7 @@@ async fn start_ldk() 
                Ok(mut info) => {
                        for (pubkey, peer_addr) in info.drain() {
                                for chan_info in channel_manager.list_channels() {
 -                                      if pubkey == chan_info.remote_network_id {
 +                                      if pubkey == chan_info.counterparty.node_id {
                                                let _ =
                                                        cli::connect_peer_if_necessary(pubkey, peer_addr, peer_manager.clone())
                                                                .await;
        // 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 {
                                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(),
                                );
                        }
                });