@@ -359,7 +359,7 @@ where
359
359
const MIN_PEER_CHANNELS : usize = 3 ;
360
360
361
361
let network_graph = self . network_graph . deref ( ) . read_only ( ) ;
362
- let paths = peers. iter ( )
362
+ let peer_channels = peers. iter ( )
363
363
. map ( |pubkey| ( pubkey, NodeId :: from_pubkey ( & pubkey) ) )
364
364
// Limit to peers with announced channels
365
365
. filter_map ( |( pubkey, node_id) |
@@ -368,7 +368,9 @@ where
368
368
. map ( |info| & info. channels [ ..] )
369
369
. and_then ( |channels| ( channels. len ( ) >= MIN_PEER_CHANNELS ) . then ( || channels) )
370
370
. map ( |channels| ( pubkey, node_id, channels) )
371
- )
371
+ ) ;
372
+
373
+ let three_hop_paths = peer_channels. clone ( )
372
374
// Pair peers with their other peers
373
375
. flat_map ( |( pubkey, node_id, channels) |
374
376
channels
@@ -390,20 +392,25 @@ where
390
392
)
391
393
. map ( |( source_pubkey, pubkey) | vec ! [ source_pubkey, pubkey, recipient] )
392
394
. map ( |node_pks| BlindedPath :: new_for_message ( & node_pks, entropy_source, secp_ctx) )
393
- . take ( MAX_PATHS )
394
- . collect :: < Result < Vec < _ > , _ > > ( ) ;
395
-
396
- match paths {
397
- Ok ( paths) if !paths. is_empty ( ) => Ok ( paths) ,
398
- _ => {
399
- if network_graph. nodes ( ) . contains_key ( & NodeId :: from_pubkey ( & recipient) ) {
400
- BlindedPath :: one_hop_for_message ( recipient, entropy_source, secp_ctx)
401
- . map ( |path| vec ! [ path] )
402
- } else {
403
- Err ( ( ) )
404
- }
405
- } ,
406
- }
395
+ . take ( MAX_PATHS ) ;
396
+
397
+ let two_hop_paths = peer_channels
398
+ . map ( |( pubkey, _, _) | vec ! [ * pubkey, recipient] )
399
+ . map ( |node_pks| BlindedPath :: new_for_message ( & node_pks, entropy_source, secp_ctx) )
400
+ . take ( MAX_PATHS ) ;
401
+
402
+ three_hop_paths
403
+ . collect :: < Result < Vec < _ > , _ > > ( ) . ok ( )
404
+ . and_then ( |paths| ( !paths. is_empty ( ) ) . then ( || paths) )
405
+ . or_else ( || two_hop_paths. collect :: < Result < Vec < _ > , _ > > ( ) . ok ( ) )
406
+ . and_then ( |paths| ( !paths. is_empty ( ) ) . then ( || paths) )
407
+ . or_else ( || network_graph
408
+ . node ( & NodeId :: from_pubkey ( & recipient) ) . ok_or ( ( ) )
409
+ . and_then ( |_| BlindedPath :: one_hop_for_message ( recipient, entropy_source, secp_ctx) )
410
+ . map ( |path| vec ! [ path] )
411
+ . ok ( )
412
+ )
413
+ . ok_or ( ( ) )
407
414
}
408
415
}
409
416
0 commit comments