From 994021ca6d591c018c798862adee8ff4145198ad Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 21 Feb 2024 11:08:44 +0100 Subject: [PATCH] Handle `ConnectionNeeded` events We spawn a background task that will try to connect to any of the provided socket addresses and return as soon as it suceeds. --- src/event.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++----- src/lib.rs | 1 + 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/event.rs b/src/event.rs index 36048e4d6..4bbb4153d 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,8 +1,11 @@ -use crate::types::{DynStore, Sweeper, Wallet}; +use crate::types::{DynStore, PeerManager, Sweeper, Wallet}; + use crate::{ hex_utils, ChannelManager, Config, Error, NetworkGraph, PeerInfo, PeerStore, UserChannelId, }; +use crate::connection::connect_peer_if_necessary; + use crate::payment_store::{ PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentStatus, PaymentStore, }; @@ -295,6 +298,7 @@ where event_queue: Arc>, wallet: Arc, channel_manager: Arc, + peer_manager: Arc, output_sweeper: Arc, network_graph: Arc, payment_store: Arc>, @@ -310,14 +314,16 @@ where { pub fn new( event_queue: Arc>, wallet: Arc, channel_manager: Arc, - output_sweeper: Arc, network_graph: Arc, - payment_store: Arc>, peer_store: Arc>, - runtime: Arc>>, logger: L, config: Arc, + peer_manager: Arc, output_sweeper: Arc, + network_graph: Arc, payment_store: Arc>, + peer_store: Arc>, runtime: Arc>>, + logger: L, config: Arc, ) -> Self { Self { event_queue, wallet, channel_manager, + peer_manager, output_sweeper, network_graph, payment_store, @@ -872,7 +878,41 @@ where LdkEvent::HTLCIntercepted { .. } => {}, LdkEvent::BumpTransaction(_) => {}, LdkEvent::InvoiceRequestFailed { .. } => {}, - LdkEvent::ConnectionNeeded { .. } => {}, + LdkEvent::ConnectionNeeded { node_id, addresses } => { + let runtime_lock = self.runtime.read().unwrap(); + debug_assert!(runtime_lock.is_some()); + + if let Some(runtime) = runtime_lock.as_ref() { + let spawn_logger = self.logger.clone(); + let spawn_pm = Arc::clone(&self.peer_manager); + let addresses = addresses.clone(); + runtime.spawn(async move { + for addr in &addresses { + match connect_peer_if_necessary( + node_id, + addr.clone(), + Arc::clone(&spawn_pm), + spawn_logger.clone(), + ) + .await + { + Ok(()) => { + return; + }, + Err(e) => { + log_error!( + spawn_logger, + "Failed to establish connection to peer {}@{}: {}", + node_id, + addr, + e + ); + }, + } + } + }); + } + }, } } } diff --git a/src/lib.rs b/src/lib.rs index d24b45f9a..0a7e9199c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -586,6 +586,7 @@ impl Node { Arc::clone(&self.event_queue), Arc::clone(&self.wallet), Arc::clone(&self.channel_manager), + Arc::clone(&self.peer_manager), Arc::clone(&self.output_sweeper), Arc::clone(&self.network_graph), Arc::clone(&self.payment_store),