@@ -487,20 +487,23 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
487
487
node_info. features . supports_basic_mpp ( )
488
488
} else { false }
489
489
} else { false } ;
490
+ log_trace ! ( logger, "Searching for a route from payer {} to payee {} {} MPP" , our_node_id, payee,
491
+ if allow_mpp { "with" } else { "without" } ) ;
490
492
491
493
// Step (1).
492
494
// Prepare the data we'll use for payee-to-payer search by
493
495
// inserting first hops suggested by the caller as targets.
494
496
// Our search will then attempt to reach them while traversing from the payee node.
495
- let mut first_hop_targets: HashMap < _ , ( _ , ChannelFeatures , _ , NodeFeatures ) > =
497
+ let mut first_hop_targets: HashMap < _ , Vec < ( _ , ChannelFeatures , _ , NodeFeatures ) > > =
496
498
HashMap :: with_capacity ( if first_hops. is_some ( ) { first_hops. as_ref ( ) . unwrap ( ) . len ( ) } else { 0 } ) ;
497
499
if let Some ( hops) = first_hops {
498
500
for chan in hops {
499
501
let short_channel_id = chan. short_channel_id . expect ( "first_hops should be filled in with usable channels, not pending ones" ) ;
500
502
if chan. counterparty . node_id == * our_node_id {
501
503
return Err ( LightningError { err : "First hop cannot have our_node_id as a destination." . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
502
504
}
503
- first_hop_targets. insert ( chan. counterparty . node_id , ( short_channel_id, chan. counterparty . features . to_context ( ) , chan. outbound_capacity_msat , chan. counterparty . features . to_context ( ) ) ) ;
505
+ first_hop_targets. entry ( chan. counterparty . node_id ) . or_insert ( Vec :: new ( ) )
506
+ . push ( ( short_channel_id, chan. counterparty . features . to_context ( ) , chan. outbound_capacity_msat , chan. counterparty . features . to_context ( ) ) ) ;
504
507
}
505
508
if first_hop_targets. is_empty ( ) {
506
509
return Err ( LightningError { err : "Cannot route when there are no outbound routes away from us" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
@@ -824,8 +827,8 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
824
827
} ;
825
828
826
829
if !skip_node {
827
- if first_hops . is_some ( ) {
828
- if let Some ( & ( ref first_hop, ref features, ref outbound_capacity_msat, _) ) = first_hop_targets . get ( & $node_id ) {
830
+ if let Some ( first_channels ) = first_hop_targets . get ( & $node_id ) {
831
+ for ( ref first_hop, ref features, ref outbound_capacity_msat, _) in first_channels {
829
832
add_entry!( first_hop, * our_node_id, $node_id, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features, $fee_to_target_msat, $next_hops_value_contribution, $next_hops_path_htlc_minimum_msat) ;
830
833
}
831
834
}
@@ -878,9 +881,10 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
878
881
879
882
// If first hop is a private channel and the only way to reach the payee, this is the only
880
883
// place where it could be added.
881
- if first_hops. is_some ( ) {
882
- if let Some ( & ( ref first_hop, ref features, ref outbound_capacity_msat, _) ) = first_hop_targets. get ( & payee) {
883
- add_entry ! ( first_hop, * our_node_id, payee, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features, 0 , path_value_msat, 0 ) ;
884
+ if let Some ( first_channels) = first_hop_targets. get ( & payee) {
885
+ for ( ref first_hop, ref features, ref outbound_capacity_msat, _) in first_channels {
886
+ let added = add_entry ! ( first_hop, * our_node_id, payee, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features, 0 , path_value_msat, 0 ) ;
887
+ log_trace ! ( logger, "{} direct route to payee via SCID {}" , if added { "Added" } else { "Skipped" } , first_hop) ;
884
888
}
885
889
}
886
890
@@ -897,7 +901,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
897
901
} ,
898
902
}
899
903
900
- // Step (1 ).
904
+ // Step (2 ).
901
905
// If a caller provided us with last hops, add them to routing targets. Since this happens
902
906
// earlier than general path finding, they will be somewhat prioritized, although currently
903
907
// it matters only if the fees are exactly the same.
@@ -949,8 +953,10 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
949
953
}
950
954
951
955
// Searching for a direct channel between last checked hop and first_hop_targets
952
- if let Some ( & ( ref first_hop, ref features, ref outbound_capacity_msat, _) ) = first_hop_targets. get ( & prev_hop_id) {
953
- add_entry ! ( first_hop, * our_node_id , prev_hop_id, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features, aggregate_next_hops_fee_msat, path_value_msat, aggregate_next_hops_path_htlc_minimum_msat) ;
956
+ if let Some ( first_channels) = first_hop_targets. get ( & prev_hop_id) {
957
+ for ( ref first_hop, ref features, ref outbound_capacity_msat, _) in first_channels {
958
+ add_entry ! ( first_hop, * our_node_id , prev_hop_id, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features, aggregate_next_hops_fee_msat, path_value_msat, aggregate_next_hops_path_htlc_minimum_msat) ;
959
+ }
954
960
}
955
961
956
962
if !hop_used {
@@ -981,8 +987,10 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
981
987
// Note that we *must* check if the last hop was added as `add_entry`
982
988
// always assumes that the third argument is a node to which we have a
983
989
// path.
984
- if let Some ( & ( ref first_hop, ref features, ref outbound_capacity_msat, _) ) = first_hop_targets. get ( & hop. src_node_id ) {
985
- add_entry ! ( first_hop, * our_node_id , hop. src_node_id, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features, aggregate_next_hops_fee_msat, path_value_msat, aggregate_next_hops_path_htlc_minimum_msat) ;
990
+ if let Some ( first_channels) = first_hop_targets. get ( & hop. src_node_id ) {
991
+ for ( ref first_hop, ref features, ref outbound_capacity_msat, _) in first_channels {
992
+ add_entry ! ( first_hop, * our_node_id , hop. src_node_id, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features, aggregate_next_hops_fee_msat, path_value_msat, aggregate_next_hops_path_htlc_minimum_msat) ;
993
+ }
986
994
}
987
995
}
988
996
}
@@ -995,7 +1003,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
995
1003
// last hops communicated by the caller, and the payment receiver.
996
1004
let mut found_new_path = false ;
997
1005
998
- // Step (2 ).
1006
+ // Step (3 ).
999
1007
// If this loop terminates due the exhaustion of targets, two situations are possible:
1000
1008
// - not enough outgoing liquidity:
1001
1009
// 0 < already_collected_value_msat < final_value_msat
@@ -1013,20 +1021,30 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
1013
1021
let mut ordered_hops = vec ! ( ( new_entry. clone( ) , NodeFeatures :: empty( ) ) ) ;
1014
1022
1015
1023
' path_walk: loop {
1016
- if let Some ( & ( _, _, _, ref features) ) = first_hop_targets. get ( & ordered_hops. last ( ) . unwrap ( ) . 0 . pubkey ) {
1017
- ordered_hops. last_mut ( ) . unwrap ( ) . 1 = features. clone ( ) ;
1018
- } else if let Some ( node) = network_nodes. get ( & ordered_hops. last ( ) . unwrap ( ) . 0 . pubkey ) {
1019
- if let Some ( node_info) = node. announcement_info . as_ref ( ) {
1020
- ordered_hops. last_mut ( ) . unwrap ( ) . 1 = node_info. features . clone ( ) ;
1024
+ let mut features_set = false ;
1025
+ if let Some ( first_channels) = first_hop_targets. get ( & ordered_hops. last ( ) . unwrap ( ) . 0 . pubkey ) {
1026
+ for ( scid, _, _, ref features) in first_channels {
1027
+ if * scid == ordered_hops. last ( ) . unwrap ( ) . 0 . short_channel_id {
1028
+ ordered_hops. last_mut ( ) . unwrap ( ) . 1 = features. clone ( ) ;
1029
+ features_set = true ;
1030
+ break ;
1031
+ }
1032
+ }
1033
+ }
1034
+ if !features_set {
1035
+ if let Some ( node) = network_nodes. get ( & ordered_hops. last ( ) . unwrap ( ) . 0 . pubkey ) {
1036
+ if let Some ( node_info) = node. announcement_info . as_ref ( ) {
1037
+ ordered_hops. last_mut ( ) . unwrap ( ) . 1 = node_info. features . clone ( ) ;
1038
+ } else {
1039
+ ordered_hops. last_mut ( ) . unwrap ( ) . 1 = NodeFeatures :: empty ( ) ;
1040
+ }
1021
1041
} else {
1022
- ordered_hops. last_mut ( ) . unwrap ( ) . 1 = NodeFeatures :: empty ( ) ;
1042
+ // We should be able to fill in features for everything except the last
1043
+ // hop, if the last hop was provided via a BOLT 11 invoice (though we
1044
+ // should be able to extend it further as BOLT 11 does have feature
1045
+ // flags for the last hop node itself).
1046
+ assert ! ( ordered_hops. last( ) . unwrap( ) . 0 . pubkey == * payee) ;
1023
1047
}
1024
- } else {
1025
- // We should be able to fill in features for everything except the last
1026
- // hop, if the last hop was provided via a BOLT 11 invoice (though we
1027
- // should be able to extend it further as BOLT 11 does have feature
1028
- // flags for the last hop node itself).
1029
- assert ! ( ordered_hops. last( ) . unwrap( ) . 0 . pubkey == * payee) ;
1030
1048
}
1031
1049
1032
1050
// Means we succesfully traversed from the payer to the payee, now
@@ -1130,7 +1148,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
1130
1148
break ' paths_collection;
1131
1149
}
1132
1150
1133
- // Step (3 ).
1151
+ // Step (4 ).
1134
1152
// Stop either when the recommended value is reached or if no new path was found in this
1135
1153
// iteration.
1136
1154
// In the latter case, making another path finding attempt won't help,
@@ -1154,7 +1172,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
1154
1172
}
1155
1173
}
1156
1174
1157
- // Step (4 ).
1175
+ // Step (5 ).
1158
1176
if payment_paths. len ( ) == 0 {
1159
1177
return Err ( LightningError { err : "Failed to find a path to the given destination" . to_owned ( ) , action : ErrorAction :: IgnoreError } ) ;
1160
1178
}
@@ -1175,12 +1193,12 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
1175
1193
let mut cur_route = Vec :: < PaymentPath > :: new ( ) ;
1176
1194
let mut aggregate_route_value_msat = 0 ;
1177
1195
1178
- // Step (5 ).
1196
+ // Step (6 ).
1179
1197
// TODO: real random shuffle
1180
1198
// Currently just starts with i_th and goes up to i-1_th in a looped way.
1181
1199
let cur_payment_paths = [ & payment_paths[ i..] , & payment_paths[ ..i] ] . concat ( ) ;
1182
1200
1183
- // Step (6 ).
1201
+ // Step (7 ).
1184
1202
for payment_path in cur_payment_paths {
1185
1203
cur_route. push ( payment_path. clone ( ) ) ;
1186
1204
aggregate_route_value_msat += payment_path. get_value_msat ( ) ;
@@ -1219,7 +1237,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
1219
1237
1220
1238
assert ! ( cur_route. len( ) > 0 ) ;
1221
1239
1222
- // Step (7 ).
1240
+ // Step (8 ).
1223
1241
// Now, substract the overpaid value from the most-expensive path.
1224
1242
// TODO: this could also be optimized by also sorting by feerate_per_sat_routed,
1225
1243
// so that the sender pays less fees overall. And also htlc_minimum_msat.
@@ -1236,7 +1254,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
1236
1254
drawn_routes. push ( cur_route) ;
1237
1255
}
1238
1256
1239
- // Step (8 ).
1257
+ // Step (9 ).
1240
1258
// Select the best route by lowest total fee.
1241
1259
drawn_routes. sort_by_key ( |paths| paths. iter ( ) . map ( |path| path. get_total_fee_paid_msat ( ) ) . sum :: < u64 > ( ) ) ;
1242
1260
let mut selected_paths = Vec :: < Vec < RouteHop > > :: new ( ) ;
@@ -4220,6 +4238,50 @@ mod tests {
4220
4238
}
4221
4239
}
4222
4240
4241
+ #[ test]
4242
+ fn multiple_direct_first_hops ( ) {
4243
+ // Previously we'd only ever considered one first hop path per counterparty.
4244
+ // However, as we don't restrict users to one channel per peer, we really need to support
4245
+ // looking at all first hop paths.
4246
+ // Here we test that we do not ignore all-but-the-last first hop paths per counterparty (as
4247
+ // we used to do by overwriting the `first_hop_targets` hashmap entry) and that we can MPP
4248
+ // route over multiple channels with the same first hop.
4249
+ let secp_ctx = Secp256k1 :: new ( ) ;
4250
+ let ( _, our_id, _, nodes) = get_nodes ( & secp_ctx) ;
4251
+ let logger = Arc :: new ( test_utils:: TestLogger :: new ( ) ) ;
4252
+ let network_graph = NetworkGraph :: new ( genesis_block ( Network :: Testnet ) . header . block_hash ( ) ) ;
4253
+
4254
+ {
4255
+ let route = get_route ( & our_id, & network_graph, & nodes[ 0 ] , Some ( InvoiceFeatures :: known ( ) ) , Some ( & [
4256
+ & get_channel_details ( Some ( 3 ) , nodes[ 0 ] , InitFeatures :: known ( ) , 200_000 ) ,
4257
+ & get_channel_details ( Some ( 2 ) , nodes[ 0 ] , InitFeatures :: known ( ) , 10_000 ) ,
4258
+ ] ) , & [ ] , 100_000 , 42 , Arc :: clone ( & logger) ) . unwrap ( ) ;
4259
+ assert_eq ! ( route. paths. len( ) , 1 ) ;
4260
+ assert_eq ! ( route. paths[ 0 ] . len( ) , 1 ) ;
4261
+
4262
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . pubkey, nodes[ 0 ] ) ;
4263
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . short_channel_id, 3 ) ;
4264
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . fee_msat, 100_000 ) ;
4265
+ }
4266
+ {
4267
+ let route = get_route ( & our_id, & network_graph, & nodes[ 0 ] , Some ( InvoiceFeatures :: known ( ) ) , Some ( & [
4268
+ & get_channel_details ( Some ( 3 ) , nodes[ 0 ] , InitFeatures :: known ( ) , 50_000 ) ,
4269
+ & get_channel_details ( Some ( 2 ) , nodes[ 0 ] , InitFeatures :: known ( ) , 50_000 ) ,
4270
+ ] ) , & [ ] , 100_000 , 42 , Arc :: clone ( & logger) ) . unwrap ( ) ;
4271
+ assert_eq ! ( route. paths. len( ) , 2 ) ;
4272
+ assert_eq ! ( route. paths[ 0 ] . len( ) , 1 ) ;
4273
+ assert_eq ! ( route. paths[ 1 ] . len( ) , 1 ) ;
4274
+
4275
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . pubkey, nodes[ 0 ] ) ;
4276
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . short_channel_id, 3 ) ;
4277
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . fee_msat, 50_000 ) ;
4278
+
4279
+ assert_eq ! ( route. paths[ 1 ] [ 0 ] . pubkey, nodes[ 0 ] ) ;
4280
+ assert_eq ! ( route. paths[ 1 ] [ 0 ] . short_channel_id, 2 ) ;
4281
+ assert_eq ! ( route. paths[ 1 ] [ 0 ] . fee_msat, 50_000 ) ;
4282
+ }
4283
+ }
4284
+
4223
4285
#[ test]
4224
4286
fn total_fees_single_path ( ) {
4225
4287
let route = Route {
0 commit comments