Skip to content

Commit

Permalink
Handle ConnectionNeeded events
Browse files Browse the repository at this point in the history
We spawn a background task that will try to connect to any of the
provided socket addresses and return as soon as it suceeds.
  • Loading branch information
tnull committed Mar 5, 2024
1 parent 2c663c7 commit f2bbafc
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::types::{DynStore, Sweeper, Wallet};

use crate::{
hex_utils, ChannelManager, Config, Error, NetworkGraph, PeerInfo, PeerStore, UserChannelId,
};

use crate::connection::ConnectionManager;

use crate::payment::payment_store::{
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentStatus, PaymentStore,
};
Expand Down Expand Up @@ -295,6 +298,7 @@ where
event_queue: Arc<EventQueue<L>>,
wallet: Arc<Wallet>,
channel_manager: Arc<ChannelManager>,
connection_manager: Arc<ConnectionManager<L>>,
output_sweeper: Arc<Sweeper>,
network_graph: Arc<NetworkGraph>,
payment_store: Arc<PaymentStore<L>>,
Expand All @@ -310,14 +314,16 @@ where
{
pub fn new(
event_queue: Arc<EventQueue<L>>, wallet: Arc<Wallet>, channel_manager: Arc<ChannelManager>,
output_sweeper: Arc<Sweeper>, network_graph: Arc<NetworkGraph>,
payment_store: Arc<PaymentStore<L>>, peer_store: Arc<PeerStore<L>>,
runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>, logger: L, config: Arc<Config>,
connection_manager: Arc<ConnectionManager<L>>, output_sweeper: Arc<Sweeper>,
network_graph: Arc<NetworkGraph>, payment_store: Arc<PaymentStore<L>>,
peer_store: Arc<PeerStore<L>>, runtime: Arc<RwLock<Option<tokio::runtime::Runtime>>>,
logger: L, config: Arc<Config>,
) -> Self {
Self {
event_queue,
wallet,
channel_manager,
connection_manager,
output_sweeper,
network_graph,
payment_store,
Expand Down Expand Up @@ -872,7 +878,34 @@ 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_cm = Arc::clone(&self.connection_manager);
let addresses = addresses.clone();
runtime.spawn(async move {
for addr in &addresses {
match spawn_cm.connect_peer_if_necessary(node_id, addr.clone()).await {
Ok(()) => {
return;
},
Err(e) => {
log_error!(
spawn_logger,
"Failed to establish connection to peer {}@{}: {}",
node_id,
addr,
e
);
},
}
}
});
}
},
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ impl Node {
Arc::clone(&self.event_queue),
Arc::clone(&self.wallet),
Arc::clone(&self.channel_manager),
Arc::clone(&self.connection_manager),
Arc::clone(&self.output_sweeper),
Arc::clone(&self.network_graph),
Arc::clone(&self.payment_store),
Expand Down

0 comments on commit f2bbafc

Please sign in to comment.