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