@@ -143,7 +143,10 @@ struct RouteGraphNode {
143
143
// - how much is needed for a path being constructed
144
144
// - how much value can channels following this node (up to the destination) can contribute,
145
145
// considering their capacity and fees
146
- value_contribution_msat : u64
146
+ value_contribution_msat : u64 ,
147
+ /// The maximum htlc_minimum_msat along the path, taking into consideration the fees required
148
+ /// to meet the minimum over the hops required to get there.
149
+ path_htlc_minimum_msat : u64 ,
147
150
}
148
151
149
152
impl cmp:: Ord for RouteGraphNode {
@@ -473,7 +476,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
473
476
// $next_hops_fee_msat represents the fees paid for using all the channel *after* this one,
474
477
// since that value has to be transferred over this channel.
475
478
( $chan_id: expr, $src_node_id: expr, $dest_node_id: expr, $directional_info: expr, $capacity_sats: expr, $chan_features: expr, $next_hops_fee_msat: expr,
476
- $next_hops_value_contribution: expr ) => {
479
+ $next_hops_value_contribution: expr, $incl_fee_next_hops_htlc_minimum_msat : expr ) => {
477
480
// Channels to self should not be used. This is more of belt-and-suspenders, because in
478
481
// practice these cases should be caught earlier:
479
482
// - for regular channels at channel announcement (TODO)
@@ -535,19 +538,27 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
535
538
// Can't overflow due to how the values were computed right above.
536
539
None => unreachable!( ) ,
537
540
} ;
541
+ #[ allow( unused_comparisons) ] // $incl_fee_next_hops_htlc_minimum_msat is 0 in some calls so rustc complains
542
+ let over_path_minimum_msat = amount_to_transfer_over_msat >= $directional_info. htlc_minimum_msat &&
543
+ amount_to_transfer_over_msat >= $incl_fee_next_hops_htlc_minimum_msat;
538
544
539
545
// If HTLC minimum is larger than the amount we're going to transfer, we shouldn't
540
546
// bother considering this channel.
541
547
// Since we're choosing amount_to_transfer_over_msat as maximum possible, it can
542
548
// be only reduced later (not increased), so this channel should just be skipped
543
549
// as not sufficient.
544
- if amount_to_transfer_over_msat < $directional_info . htlc_minimum_msat {
550
+ if !over_path_minimum_msat {
545
551
hit_minimum_limit = true ;
546
552
} else if contributes_sufficient_value {
547
553
// Note that low contribution here (limited by available_liquidity_msat)
548
554
// might violate htlc_minimum_msat on the hops which are next along the
549
555
// payment path (upstream to the payee). To avoid that, we recompute path
550
556
// path fees knowing the final path contribution after constructing it.
557
+ let path_htlc_minimum_msat = match compute_fees( $incl_fee_next_hops_htlc_minimum_msat, $directional_info. fees)
558
+ . map( |fee_msat| fee_msat. checked_add( $incl_fee_next_hops_htlc_minimum_msat) ) {
559
+ Some ( Some ( value_msat) ) => cmp:: max( value_msat, $directional_info. htlc_minimum_msat) ,
560
+ _ => u64 :: max_value( )
561
+ } ;
551
562
let hm_entry = dist. entry( & $src_node_id) ;
552
563
let old_entry = hm_entry. or_insert_with( || {
553
564
// If there was previously no known way to access
@@ -619,6 +630,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
619
630
lowest_fee_to_peer_through_node: total_fee_msat,
620
631
lowest_fee_to_node: $next_hops_fee_msat as u64 + hop_use_fee_msat,
621
632
value_contribution_msat: value_contribution_msat,
633
+ path_htlc_minimum_msat,
622
634
} ;
623
635
624
636
// Update the way of reaching $src_node_id with the given $chan_id (from $dest_node_id),
@@ -678,10 +690,10 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
678
690
// meaning how much will be paid in fees after this node (to the best of our knowledge).
679
691
// This data can later be helpful to optimize routing (pay lower fees).
680
692
macro_rules! add_entries_to_cheapest_to_target_node {
681
- ( $node: expr, $node_id: expr, $fee_to_target_msat: expr, $next_hops_value_contribution: expr ) => {
693
+ ( $node: expr, $node_id: expr, $fee_to_target_msat: expr, $next_hops_value_contribution: expr, $incl_fee_next_hops_htlc_minimum_msat : expr ) => {
682
694
if first_hops. is_some( ) {
683
695
if let Some ( & ( ref first_hop, ref features, ref outbound_capacity_msat) ) = first_hop_targets. get( & $node_id) {
684
- add_entry!( first_hop, * our_node_id, $node_id, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features. to_context( ) , $fee_to_target_msat, $next_hops_value_contribution) ;
696
+ add_entry!( first_hop, * our_node_id, $node_id, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features. to_context( ) , $fee_to_target_msat, $next_hops_value_contribution, $incl_fee_next_hops_htlc_minimum_msat ) ;
685
697
}
686
698
}
687
699
@@ -701,15 +713,15 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
701
713
if first_hops. is_none( ) || chan. node_two != * our_node_id {
702
714
if let Some ( two_to_one) = chan. two_to_one. as_ref( ) {
703
715
if two_to_one. enabled {
704
- add_entry!( chan_id, chan. node_two, chan. node_one, two_to_one, chan. capacity_sats, chan. features, $fee_to_target_msat, $next_hops_value_contribution) ;
716
+ add_entry!( chan_id, chan. node_two, chan. node_one, two_to_one, chan. capacity_sats, chan. features, $fee_to_target_msat, $next_hops_value_contribution, $incl_fee_next_hops_htlc_minimum_msat ) ;
705
717
}
706
718
}
707
719
}
708
720
} else {
709
721
if first_hops. is_none( ) || chan. node_one != * our_node_id {
710
722
if let Some ( one_to_two) = chan. one_to_two. as_ref( ) {
711
723
if one_to_two. enabled {
712
- add_entry!( chan_id, chan. node_one, chan. node_two, one_to_two, chan. capacity_sats, chan. features, $fee_to_target_msat, $next_hops_value_contribution) ;
724
+ add_entry!( chan_id, chan. node_one, chan. node_two, one_to_two, chan. capacity_sats, chan. features, $fee_to_target_msat, $next_hops_value_contribution, $incl_fee_next_hops_htlc_minimum_msat ) ;
713
725
}
714
726
}
715
727
@@ -736,7 +748,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
736
748
// place where it could be added.
737
749
if first_hops. is_some ( ) {
738
750
if let Some ( & ( ref first_hop, ref features, ref outbound_capacity_msat) ) = first_hop_targets. get ( & payee) {
739
- add_entry ! ( first_hop, * our_node_id, payee, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features. to_context( ) , 0 , path_value_msat) ;
751
+ add_entry ! ( first_hop, * our_node_id, payee, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features. to_context( ) , 0 , path_value_msat, 0 ) ;
740
752
}
741
753
}
742
754
@@ -749,7 +761,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
749
761
// If not, targets.pop() will not even let us enter the loop in step 2.
750
762
None => { } ,
751
763
Some ( node) => {
752
- add_entries_to_cheapest_to_target_node ! ( node, payee, 0 , path_value_msat) ;
764
+ add_entries_to_cheapest_to_target_node ! ( node, payee, 0 , path_value_msat, 0 ) ;
753
765
} ,
754
766
}
755
767
@@ -768,7 +780,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
768
780
// bit lazy here. In the future, we should pull them out via our
769
781
// ChannelManager, but there's no reason to waste the space until we
770
782
// need them.
771
- add_entry ! ( first_hop, * our_node_id , hop. src_node_id, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features. to_context( ) , 0 , path_value_msat) ;
783
+ add_entry ! ( first_hop, * our_node_id , hop. src_node_id, dummy_directional_info, Some ( outbound_capacity_msat / 1000 ) , features. to_context( ) , 0 , path_value_msat, 0 ) ;
772
784
true
773
785
} else {
774
786
// In any other case, only add the hop if the source is in the regular network
@@ -778,17 +790,17 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
778
790
if have_hop_src_in_graph {
779
791
// BOLT 11 doesn't allow inclusion of features for the last hop hints, which
780
792
// really sucks, cause we're gonna need that eventually.
781
- let last_hop_htlc_minimum_msat : u64 = match hop. htlc_minimum_msat {
793
+ let last_path_htlc_minimum_msat : u64 = match hop. htlc_minimum_msat {
782
794
Some ( htlc_minimum_msat) => htlc_minimum_msat,
783
795
None => 0
784
796
} ;
785
797
let directional_info = DummyDirectionalChannelInfo {
786
798
cltv_expiry_delta : hop. cltv_expiry_delta as u32 ,
787
- htlc_minimum_msat : last_hop_htlc_minimum_msat ,
799
+ htlc_minimum_msat : last_path_htlc_minimum_msat ,
788
800
htlc_maximum_msat : hop. htlc_maximum_msat ,
789
801
fees : hop. fees ,
790
802
} ;
791
- add_entry ! ( hop. short_channel_id, hop. src_node_id, payee, directional_info, None :: <u64 >, ChannelFeatures :: empty( ) , 0 , path_value_msat) ;
803
+ add_entry ! ( hop. short_channel_id, hop. src_node_id, payee, directional_info, None :: <u64 >, ChannelFeatures :: empty( ) , 0 , path_value_msat, 0 ) ;
792
804
}
793
805
}
794
806
@@ -805,7 +817,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
805
817
// Both these cases (and other cases except reaching recommended_value_msat) mean that
806
818
// paths_collection will be stopped because found_new_path==false.
807
819
// This is not necessarily a routing failure.
808
- ' path_construction: while let Some ( RouteGraphNode { pubkey, lowest_fee_to_node, value_contribution_msat, .. } ) = targets. pop ( ) {
820
+ ' path_construction: while let Some ( RouteGraphNode { pubkey, lowest_fee_to_node, value_contribution_msat, path_htlc_minimum_msat , .. } ) = targets. pop ( ) {
809
821
810
822
// Since we're going payee-to-payer, hitting our node as a target means we should stop
811
823
// traversing the graph and arrange the path out of what we found.
@@ -902,7 +914,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
902
914
match network. get_nodes ( ) . get ( & pubkey) {
903
915
None => { } ,
904
916
Some ( node) => {
905
- add_entries_to_cheapest_to_target_node ! ( node, & pubkey, lowest_fee_to_node, value_contribution_msat) ;
917
+ add_entries_to_cheapest_to_target_node ! ( node, & pubkey, lowest_fee_to_node, value_contribution_msat, path_htlc_minimum_msat ) ;
906
918
} ,
907
919
}
908
920
}
@@ -3459,6 +3471,66 @@ mod tests {
3459
3471
assert_eq ! ( route. paths[ 0 ] [ 1 ] . channel_features. le_flags( ) , & id_to_feature_flags( 13 ) ) ;
3460
3472
}
3461
3473
}
3474
+
3475
+ #[ test]
3476
+ fn htlc_max_reduction_below_min ( ) {
3477
+ // Test that if, while walking the graph, we reduce the value being sent to meet an
3478
+ // htlc_maximum_msat, we don't end up undershooting a later htlc_minimum_msat. In the
3479
+ // initial version of MPP we'd accept such routes but reject them while recalculating fees,
3480
+ // resulting in us thinking there is no possible path, even if other paths exist.
3481
+ let ( secp_ctx, net_graph_msg_handler, _, logger) = build_graph ( ) ;
3482
+ let ( our_privkey, our_id, privkeys, nodes) = get_nodes ( & secp_ctx) ;
3483
+
3484
+ // We modify the graph to set the htlc_minimum of channel 2 and 4 as needed - channel 2
3485
+ // gets an htlc_minimum_msat of 80_000 and channel 4 an htlc_maximum_msat of 90_000. We
3486
+ // then try to send 90_000.
3487
+ update_channel ( & net_graph_msg_handler, & secp_ctx, & our_privkey, UnsignedChannelUpdate {
3488
+ chain_hash : genesis_block ( Network :: Testnet ) . header . block_hash ( ) ,
3489
+ short_channel_id : 2 ,
3490
+ timestamp : 2 ,
3491
+ flags : 0 ,
3492
+ cltv_expiry_delta : 0 ,
3493
+ htlc_minimum_msat : 0 ,
3494
+ htlc_maximum_msat : OptionalField :: Present ( 80_000 ) ,
3495
+ fee_base_msat : 0 ,
3496
+ fee_proportional_millionths : 0 ,
3497
+ excess_data : Vec :: new ( )
3498
+ } ) ;
3499
+ update_channel ( & net_graph_msg_handler, & secp_ctx, & privkeys[ 1 ] , UnsignedChannelUpdate {
3500
+ chain_hash : genesis_block ( Network :: Testnet ) . header . block_hash ( ) ,
3501
+ short_channel_id : 4 ,
3502
+ timestamp : 2 ,
3503
+ flags : 0 ,
3504
+ cltv_expiry_delta : ( 4 << 8 ) | 1 ,
3505
+ htlc_minimum_msat : 90_000 ,
3506
+ htlc_maximum_msat : OptionalField :: Absent ,
3507
+ fee_base_msat : 0 ,
3508
+ fee_proportional_millionths : 0 ,
3509
+ excess_data : Vec :: new ( )
3510
+ } ) ;
3511
+
3512
+ {
3513
+ // Now, attempt to route 125 sats (just a bit below the capacity of 3 channels).
3514
+ // Our algorithm should provide us with these 3 paths.
3515
+ let route = get_route ( & our_id, & net_graph_msg_handler. network_graph . read ( ) . unwrap ( ) , & nodes[ 2 ] , None , & Vec :: new ( ) , 90_000 , 42 , Arc :: clone ( & logger) ) . unwrap ( ) ;
3516
+ assert_eq ! ( route. paths. len( ) , 1 ) ;
3517
+ assert_eq ! ( route. paths[ 0 ] . len( ) , 2 ) ;
3518
+
3519
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . pubkey, nodes[ 7 ] ) ;
3520
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . short_channel_id, 12 ) ;
3521
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . fee_msat, 90_000 * 2 ) ;
3522
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . cltv_expiry_delta, ( 13 << 8 ) | 1 ) ;
3523
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . node_features. le_flags( ) , & id_to_feature_flags( 8 ) ) ;
3524
+ assert_eq ! ( route. paths[ 0 ] [ 0 ] . channel_features. le_flags( ) , & id_to_feature_flags( 12 ) ) ;
3525
+
3526
+ assert_eq ! ( route. paths[ 0 ] [ 1 ] . pubkey, nodes[ 2 ] ) ;
3527
+ assert_eq ! ( route. paths[ 0 ] [ 1 ] . short_channel_id, 13 ) ;
3528
+ assert_eq ! ( route. paths[ 0 ] [ 1 ] . fee_msat, 90_000 ) ;
3529
+ assert_eq ! ( route. paths[ 0 ] [ 1 ] . cltv_expiry_delta, 42 ) ;
3530
+ assert_eq ! ( route. paths[ 0 ] [ 1 ] . node_features. le_flags( ) , & id_to_feature_flags( 3 ) ) ;
3531
+ assert_eq ! ( route. paths[ 0 ] [ 1 ] . channel_features. le_flags( ) , & id_to_feature_flags( 13 ) ) ;
3532
+ }
3533
+ }
3462
3534
}
3463
3535
3464
3536
#[ cfg( all( test, feature = "unstable" ) ) ]
0 commit comments