@@ -20,6 +20,7 @@ use std::path::PathBuf;
2020use std:: sync:: Arc ;
2121use tokio:: sync:: Mutex ;
2222use tokio_util:: task:: TaskTracker ;
23+ use triggered:: Listener ;
2324
2425/// The default directory where the simulation files are stored and where the results will be written to.
2526pub const DEFAULT_DATA_DIR : & str = "." ;
@@ -359,8 +360,14 @@ pub async fn create_simulation(
359360 ldk_hub : _,
360361 } = sim_params;
361362
362- let ( clients, clients_info) = get_clients ( nodes. to_vec ( ) , sim_params. ldk_hub . clone ( ) ) . await ?;
363363 let ( shutdown_trigger, shutdown_listener) = triggered:: trigger ( ) ;
364+ let ( clients, clients_info) = get_clients (
365+ nodes. to_vec ( ) ,
366+ sim_params. ldk_hub . clone ( ) ,
367+ tasks. clone ( ) ,
368+ shutdown_listener. clone ( ) ,
369+ )
370+ . await ?;
364371
365372 let validated_activities =
366373 get_validated_activities ( & clients, clients_info, sim_params. activity . clone ( ) ) . await ?;
@@ -383,6 +390,8 @@ pub async fn create_simulation(
383390async fn get_clients (
384391 nodes : Vec < NodeConnection > ,
385392 ldk_hub : Option < ldk:: LdkHubConfig > ,
393+ tasks : TaskTracker ,
394+ shutdown : Listener ,
386395) -> Result <
387396 (
388397 HashMap < PublicKey , Arc < Mutex < dyn LightningNode > > > ,
@@ -393,24 +402,12 @@ async fn get_clients(
393402 let mut clients: HashMap < PublicKey , Arc < Mutex < dyn LightningNode > > > = HashMap :: new ( ) ;
394403 let mut clients_info: HashMap < PublicKey , NodeInfo > = HashMap :: new ( ) ;
395404
396- let ( ldk_connections, other_connections) : ( Vec < _ > , Vec < _ > ) =
397- nodes. into_iter ( ) . partition ( |n| matches ! ( n, NodeConnection :: Ldk ( _) ) ) ;
398-
399- let ldk_configs: Vec < ldk:: LdkNodeConfig > = ldk_connections
405+ let ( ldk_connections, other_connections) : ( Vec < _ > , Vec < _ > ) = nodes
400406 . into_iter ( )
401- . filter_map ( |n| match n {
402- NodeConnection :: Ldk ( cfg) => Some ( cfg) ,
403- _ => None ,
404- } )
405- . collect ( ) ;
406-
407- for ldk_node in ldk:: build_ldk_nodes ( ldk_configs, ldk_hub) . await ? {
408- let info = ldk_node. get_info ( ) . clone ( ) ;
409- let node: Arc < Mutex < dyn LightningNode > > = Arc :: new ( Mutex :: new ( ldk_node) ) ;
410- clients. insert ( info. pubkey , node) ;
411- clients_info. insert ( info. pubkey , info) ;
412- }
407+ . partition ( |n| matches ! ( n, NodeConnection :: Ldk ( _) ) ) ;
413408
409+ // Build non-LDK nodes first so we have their peer addresses before connecting LDK nodes.
410+ let mut non_ldk_peers: Vec < ( PublicKey , String ) > = Vec :: new ( ) ;
414411 for connection in other_connections {
415412 let node: Arc < Mutex < dyn LightningNode > > = match connection {
416413 NodeConnection :: Lnd ( c) => Arc :: new ( Mutex :: new ( LndNode :: new ( c) . await ?) ) ,
@@ -419,10 +416,55 @@ async fn get_clients(
419416 NodeConnection :: Ldk ( _) => unreachable ! ( ) ,
420417 } ;
421418 let node_info = node. lock ( ) . await . get_info ( ) . clone ( ) ;
419+ if let Some ( addr) = node_info. p2p_addr . clone ( ) {
420+ non_ldk_peers. push ( ( node_info. pubkey , addr) ) ;
421+ } else {
422+ log:: warn!(
423+ "No p2p_addr for {}: LDK nodes will not connect to it as a peer." ,
424+ node_info. alias
425+ ) ;
426+ }
422427 clients. insert ( node_info. pubkey , node) ;
423428 clients_info. insert ( node_info. pubkey , node_info) ;
424429 }
425430
431+ let ldk_configs: Vec < ldk:: LdkNodeConfig > = ldk_connections
432+ . into_iter ( )
433+ . filter_map ( |n| match n {
434+ NodeConnection :: Ldk ( cfg) => Some ( cfg) ,
435+ _ => None ,
436+ } )
437+ . collect ( ) ;
438+
439+ let ldk_nodes = ldk:: build_ldk_nodes ( ldk_configs, ldk_hub, tasks, shutdown) . await ?;
440+
441+ // Collect LDK peer info for all-to-all connections.
442+ let ldk_peers: Vec < ( PublicKey , String ) > = ldk_nodes
443+ . iter ( )
444+ . filter_map ( |n| {
445+ let info = n. get_info ( ) ;
446+ info. p2p_addr . as_ref ( ) . map ( |addr| ( info. pubkey , addr. clone ( ) ) )
447+ } )
448+ . collect ( ) ;
449+
450+ for ldk_node in ldk_nodes {
451+ let info = ldk_node. get_info ( ) . clone ( ) ;
452+
453+ // Connect to every other LDK node.
454+ for ( peer_pk, peer_addr) in & ldk_peers {
455+ if peer_pk != & info. pubkey {
456+ ldk_node. connect_peer ( * peer_pk, peer_addr) ;
457+ }
458+ }
459+ // Connect to non-LDK nodes.
460+ for ( peer_pk, peer_addr) in & non_ldk_peers {
461+ ldk_node. connect_peer ( * peer_pk, peer_addr) ;
462+ }
463+
464+ clients. insert ( info. pubkey , Arc :: new ( Mutex :: new ( ldk_node) ) ) ;
465+ clients_info. insert ( info. pubkey , info) ;
466+ }
467+
426468 Ok ( ( clients, clients_info) )
427469}
428470
0 commit comments