@@ -359,7 +359,7 @@ where
359359 const MIN_PEER_CHANNELS : usize = 3 ;
360360
361361 let network_graph = self . network_graph . deref ( ) . read_only ( ) ;
362- let paths = peers. iter ( )
362+ let peer_channels = peers. iter ( )
363363 . map ( |pubkey| ( pubkey, NodeId :: from_pubkey ( & pubkey) ) )
364364 // Limit to peers with announced channels
365365 . filter_map ( |( pubkey, node_id) |
@@ -368,7 +368,9 @@ where
368368 . map ( |info| & info. channels [ ..] )
369369 . and_then ( |channels| ( channels. len ( ) >= MIN_PEER_CHANNELS ) . then ( || channels) )
370370 . map ( |channels| ( pubkey, node_id, channels) )
371- )
371+ ) ;
372+
373+ let three_hop_paths = peer_channels. clone ( )
372374 // Pair peers with their other peers
373375 . flat_map ( |( pubkey, node_id, channels) |
374376 channels
@@ -390,20 +392,25 @@ where
390392 )
391393 . map ( |( source_pubkey, pubkey) | vec ! [ source_pubkey, pubkey, recipient] )
392394 . 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 ( ( ) )
407414 }
408415}
409416
0 commit comments