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 4, 2024
1 parent 1a55615 commit 994021c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
50 changes: 45 additions & 5 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::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,
};
Expand Down Expand Up @@ -295,6 +298,7 @@ where
event_queue: Arc<EventQueue<L>>,
wallet: Arc<Wallet>,
channel_manager: Arc<ChannelManager>,
peer_manager: Arc<PeerManager>,
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>,
peer_manager: Arc<PeerManager>, 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,
peer_manager,
output_sweeper,
network_graph,
payment_store,
Expand Down Expand Up @@ -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
);
},
}
}
});
}
},
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 994021c

Please sign in to comment.