Merge pull request #131 from tnull/2024-02-align-rustfmt
[ldk-sample] / src / main.rs
index 73a6ddf3356e7d2aabfa9c32ea9debfbef379250..add6990056b03bedd823492f841e9b302164a47e 100644 (file)
@@ -51,6 +51,7 @@ use std::fs;
 use std::fs::File;
 use std::io;
 use std::io::Write;
+use std::net::ToSocketAddrs;
 use std::path::Path;
 use std::sync::atomic::{AtomicBool, Ordering};
 use std::sync::{Arc, Mutex, RwLock};
@@ -172,11 +173,11 @@ pub(crate) type BumpTxEventHandler = BumpTransactionEventHandler<
 >;
 
 async fn handle_ldk_events(
-       channel_manager: &Arc<ChannelManager>, bitcoind_client: &BitcoindClient,
+       channel_manager: Arc<ChannelManager>, bitcoind_client: &BitcoindClient,
        network_graph: &NetworkGraph, keys_manager: &KeysManager,
-       bump_tx_event_handler: &BumpTxEventHandler,
+       bump_tx_event_handler: &BumpTxEventHandler, peer_manager: Arc<PeerManager>,
        inbound_payments: Arc<Mutex<InboundPaymentInfoStorage>>,
-       outbound_payments: Arc<Mutex<OutboundPaymentInfoStorage>>, fs_store: &Arc<FilesystemStore>,
+       outbound_payments: Arc<Mutex<OutboundPaymentInfoStorage>>, fs_store: Arc<FilesystemStore>,
        network: Network, event: Event,
 ) {
        match event {
@@ -227,7 +228,7 @@ async fn handle_ldk_events(
                                print!("> ");
                                io::stdout().flush().unwrap();
                        }
-               }
+               },
                Event::PaymentClaimable {
                        payment_hash,
                        purpose,
@@ -250,7 +251,7 @@ async fn handle_ldk_events(
                                PaymentPurpose::SpontaneousPayment(preimage) => Some(preimage),
                        };
                        channel_manager.claim_funds(payment_preimage.unwrap());
-               }
+               },
                Event::PaymentClaimed {
                        payment_hash,
                        purpose,
@@ -268,7 +269,7 @@ async fn handle_ldk_events(
                        let (payment_preimage, payment_secret) = match purpose {
                                PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
                                        (payment_preimage, Some(payment_secret))
-                               }
+                               },
                                PaymentPurpose::SpontaneousPayment(preimage) => (Some(preimage), None),
                        };
                        let mut inbound = inbound_payments.lock().unwrap();
@@ -278,7 +279,7 @@ async fn handle_ldk_events(
                                        payment.status = HTLCStatus::Succeeded;
                                        payment.preimage = payment_preimage;
                                        payment.secret = payment_secret;
-                               }
+                               },
                                Entry::Vacant(e) => {
                                        e.insert(PaymentInfo {
                                                preimage: payment_preimage,
@@ -286,10 +287,10 @@ async fn handle_ldk_events(
                                                status: HTLCStatus::Succeeded,
                                                amt_msat: MillisatAmount(Some(amount_msat)),
                                        });
-                               }
+                               },
                        }
                        fs_store.write("", "", INBOUND_PAYMENTS_FNAME, &inbound.encode()).unwrap();
-               }
+               },
                Event::PaymentSent {
                        payment_preimage, payment_hash, fee_paid_msat, payment_id, ..
                } => {
@@ -315,7 +316,7 @@ async fn handle_ldk_events(
                                }
                        }
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound.encode()).unwrap();
-               }
+               },
                Event::OpenChannelRequest {
                        ref temporary_channel_id, ref counterparty_node_id, ..
                } => {
@@ -344,11 +345,11 @@ async fn handle_ldk_events(
                        }
                        print!("> ");
                        io::stdout().flush().unwrap();
-               }
-               Event::PaymentPathSuccessful { .. } => {}
-               Event::PaymentPathFailed { .. } => {}
-               Event::ProbeSuccessful { .. } => {}
-               Event::ProbeFailed { .. } => {}
+               },
+               Event::PaymentPathSuccessful { .. } => {},
+               Event::PaymentPathFailed { .. } => {},
+               Event::ProbeSuccessful { .. } => {},
+               Event::ProbeFailed { .. } => {},
                Event::PaymentFailed { payment_hash, reason, payment_id, .. } => {
                        print!(
                                "\nEVENT: Failed to send payment to payment hash {}: {:?}",
@@ -364,7 +365,7 @@ async fn handle_ldk_events(
                                payment.status = HTLCStatus::Failed;
                        }
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound.encode()).unwrap();
-               }
+               },
                Event::InvoiceRequestFailed { payment_id } => {
                        print!("\nEVENT: Failed to request invoice to send payment with id {}", payment_id);
                        print!("> ");
@@ -376,7 +377,7 @@ async fn handle_ldk_events(
                                payment.status = HTLCStatus::Failed;
                        }
                        fs_store.write("", "", OUTBOUND_PAYMENTS_FNAME, &outbound.encode()).unwrap();
-               }
+               },
                Event::PaymentForwarded {
                        prev_channel_id,
                        next_channel_id,
@@ -399,10 +400,10 @@ async fn handle_ldk_events(
                                                                None => "unnamed node".to_string(),
                                                                Some(announcement) => {
                                                                        format!("node {}", announcement.alias)
-                                                               }
+                                                               },
                                                        },
                                                }
-                                       }
+                                       },
                                },
                        };
                        let channel_str = |channel_id: &Option<ChannelId>| {
@@ -438,8 +439,8 @@ async fn handle_ldk_events(
                        }
                        print!("> ");
                        io::stdout().flush().unwrap();
-               }
-               Event::HTLCHandlingFailed { .. } => {}
+               },
+               Event::HTLCHandlingFailed { .. } => {},
                Event::PendingHTLCsForwardable { time_forwardable } => {
                        let forwarding_channel_manager = channel_manager.clone();
                        let min = time_forwardable.as_millis() as u64;
@@ -448,7 +449,7 @@ async fn handle_ldk_events(
                                tokio::time::sleep(Duration::from_millis(millis_to_sleep)).await;
                                forwarding_channel_manager.process_pending_htlc_forwards();
                        });
-               }
+               },
                Event::SpendableOutputs { outputs, channel_id: _ } => {
                        // SpendableOutputDescriptors, of which outputs is a vec of, are critical to keep track
                        // of! While a `StaticOutput` descriptor is just an output to a static, well-known key,
@@ -465,7 +466,7 @@ async fn handle_ldk_events(
                                let output: SpendableOutputDescriptor = output;
                                fs_store.write(PENDING_SPENDABLE_OUTPUT_DIR, "", &key, &output.encode()).unwrap();
                        }
-               }
+               },
                Event::ChannelPending { channel_id, counterparty_node_id, .. } => {
                        println!(
                                "\nEVENT: Channel {} with peer {} is pending awaiting funding lock-in!",
@@ -474,7 +475,7 @@ async fn handle_ldk_events(
                        );
                        print!("> ");
                        io::stdout().flush().unwrap();
-               }
+               },
                Event::ChannelReady {
                        ref channel_id,
                        user_channel_id: _,
@@ -488,7 +489,7 @@ async fn handle_ldk_events(
                        );
                        print!("> ");
                        io::stdout().flush().unwrap();
-               }
+               },
                Event::ChannelClosed {
                        channel_id,
                        reason,
@@ -505,14 +506,27 @@ async fn handle_ldk_events(
                        );
                        print!("> ");
                        io::stdout().flush().unwrap();
-               }
+               },
                Event::DiscardFunding { .. } => {
                        // A "real" node should probably "lock" the UTXOs spent in funding transactions until
                        // the funding transaction either confirms, or this event is generated.
-               }
-               Event::HTLCIntercepted { .. } => {}
+               },
+               Event::HTLCIntercepted { .. } => {},
                Event::BumpTransaction(event) => bump_tx_event_handler.handle_event(&event),
-               Event::ConnectionNeeded { .. } => {}
+               Event::ConnectionNeeded { node_id, addresses } => {
+                       tokio::spawn(async move {
+                               for address in addresses {
+                                       if let Ok(sockaddrs) = address.to_socket_addrs() {
+                                               for addr in sockaddrs {
+                                                       let pm = Arc::clone(&peer_manager);
+                                                       if cli::connect_peer_if_necessary(node_id, addr, pm).await.is_ok() {
+                                                               return;
+                                                       }
+                                               }
+                                       }
+                               }
+                       });
+               },
        }
 }
 
@@ -546,7 +560,7 @@ async fn start_ldk() {
                Err(e) => {
                        println!("Failed to connect to bitcoind client: {}", e);
                        return;
-               }
+               },
        };
 
        // Check that the bitcoind we've connected to is running the network we expect
@@ -593,11 +607,11 @@ async fn start_ldk() {
                        Ok(mut f) => {
                                Write::write_all(&mut f, &key).expect("Failed to write node keys seed to disk");
                                f.sync_all().expect("Failed to sync node keys seed to disk");
-                       }
+                       },
                        Err(e) => {
                                println!("ERROR: Unable to create keys seed file {}: {}", keys_seed_path, e);
                                return;
-                       }
+                       },
                }
                key
        };
@@ -886,6 +900,7 @@ async fn start_ldk() {
        let inbound_payments_event_listener = Arc::clone(&inbound_payments);
        let outbound_payments_event_listener = Arc::clone(&outbound_payments);
        let fs_store_event_listener = Arc::clone(&fs_store);
+       let peer_manager_event_listener = Arc::clone(&peer_manager);
        let network = args.network;
        let event_handler = move |event: Event| {
                let channel_manager_event_listener = Arc::clone(&channel_manager_event_listener);
@@ -896,16 +911,18 @@ async fn start_ldk() {
                let inbound_payments_event_listener = Arc::clone(&inbound_payments_event_listener);
                let outbound_payments_event_listener = Arc::clone(&outbound_payments_event_listener);
                let fs_store_event_listener = Arc::clone(&fs_store_event_listener);
+               let peer_manager_event_listener = Arc::clone(&peer_manager_event_listener);
                async move {
                        handle_ldk_events(
-                               &channel_manager_event_listener,
+                               channel_manager_event_listener,
                                &bitcoind_client_event_listener,
                                &network_graph_event_listener,
                                &keys_manager_event_listener,
                                &bump_tx_event_handler,
+                               peer_manager_event_listener,
                                inbound_payments_event_listener,
                                outbound_payments_event_listener,
-                               &fs_store_event_listener,
+                               fs_store_event_listener,
                                network,
                                event,
                        )
@@ -973,7 +990,7 @@ async fn start_ldk() {
                                                        }
                                                }
                                        }
-                               }
+                               },
                                Err(e) => println!("ERROR: errored reading channel peer info from disk: {:?}", e),
                        }
                }