@@ -360,7 +360,7 @@ where
360360 const MIN_PEER_CHANNELS : usize = 3 ;
361361
362362 let network_graph = self . network_graph . deref ( ) . read_only ( ) ;
363- let paths = peers. iter ( )
363+ let peer_channels = peers. iter ( )
364364 . map ( |pubkey| ( pubkey, NodeId :: from_pubkey ( & pubkey) ) )
365365 // Limit to peers with announced channels
366366 . filter_map ( |( pubkey, node_id) |
@@ -369,7 +369,9 @@ where
369369 . map ( |info| & info. channels [ ..] )
370370 . and_then ( |channels| ( channels. len ( ) >= MIN_PEER_CHANNELS ) . then ( || channels) )
371371 . map ( |channels| ( pubkey, node_id, channels) )
372- )
372+ ) ;
373+
374+ let three_hop_paths = peer_channels. clone ( )
373375 // Pair peers with their other peers
374376 . flat_map ( |( pubkey, node_id, channels) |
375377 channels
@@ -391,20 +393,25 @@ where
391393 )
392394 . map ( |( source_pubkey, pubkey) | vec ! [ source_pubkey, pubkey, recipient] )
393395 . 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 ( ( ) )
408415 }
409416}
410417
0 commit comments