11use crate :: types:: { DynStore , Sweeper , Wallet } ;
2+
23use crate :: {
34 hex_utils, ChannelManager , Config , Error , NetworkGraph , PeerInfo , PeerStore , UserChannelId ,
45} ;
56
7+ use crate :: connection:: ConnectionManager ;
8+
69use crate :: payment:: store:: {
710 PaymentDetails , PaymentDetailsUpdate , PaymentDirection , PaymentKind , PaymentStatus ,
811 PaymentStore ,
@@ -315,6 +318,7 @@ where
315318 event_queue : Arc < EventQueue < L > > ,
316319 wallet : Arc < Wallet > ,
317320 channel_manager : Arc < ChannelManager > ,
321+ connection_manager : Arc < ConnectionManager < L > > ,
318322 output_sweeper : Arc < Sweeper > ,
319323 network_graph : Arc < NetworkGraph > ,
320324 payment_store : Arc < PaymentStore < L > > ,
@@ -330,14 +334,16 @@ where
330334{
331335 pub fn new (
332336 event_queue : Arc < EventQueue < L > > , wallet : Arc < Wallet > , channel_manager : Arc < ChannelManager > ,
333- output_sweeper : Arc < Sweeper > , network_graph : Arc < NetworkGraph > ,
334- payment_store : Arc < PaymentStore < L > > , peer_store : Arc < PeerStore < L > > ,
335- runtime : Arc < RwLock < Option < tokio:: runtime:: Runtime > > > , logger : L , config : Arc < Config > ,
337+ connection_manager : Arc < ConnectionManager < L > > , output_sweeper : Arc < Sweeper > ,
338+ network_graph : Arc < NetworkGraph > , payment_store : Arc < PaymentStore < L > > ,
339+ peer_store : Arc < PeerStore < L > > , runtime : Arc < RwLock < Option < tokio:: runtime:: Runtime > > > ,
340+ logger : L , config : Arc < Config > ,
336341 ) -> Self {
337342 Self {
338343 event_queue,
339344 wallet,
340345 channel_manager,
346+ connection_manager,
341347 output_sweeper,
342348 network_graph,
343349 payment_store,
@@ -978,7 +984,33 @@ where
978984 LdkEvent :: HTLCIntercepted { .. } => { } ,
979985 LdkEvent :: BumpTransaction ( _) => { } ,
980986 LdkEvent :: InvoiceRequestFailed { .. } => { } ,
981- LdkEvent :: ConnectionNeeded { .. } => { } ,
987+ LdkEvent :: ConnectionNeeded { node_id, addresses } => {
988+ let runtime_lock = self . runtime . read ( ) . unwrap ( ) ;
989+ debug_assert ! ( runtime_lock. is_some( ) ) ;
990+
991+ if let Some ( runtime) = runtime_lock. as_ref ( ) {
992+ let spawn_logger = self . logger . clone ( ) ;
993+ let spawn_cm = Arc :: clone ( & self . connection_manager ) ;
994+ runtime. spawn ( async move {
995+ for addr in & addresses {
996+ match spawn_cm. connect_peer_if_necessary ( node_id, addr. clone ( ) ) . await {
997+ Ok ( ( ) ) => {
998+ return ;
999+ } ,
1000+ Err ( e) => {
1001+ log_error ! (
1002+ spawn_logger,
1003+ "Failed to establish connection to peer {}@{}: {}" ,
1004+ node_id,
1005+ addr,
1006+ e
1007+ ) ;
1008+ } ,
1009+ }
1010+ }
1011+ } ) ;
1012+ }
1013+ } ,
9821014 }
9831015 }
9841016}
0 commit comments