@@ -951,7 +951,10 @@ impl<'a> CandidateRouteHop<'a> {
951
951
liquidity_msat : details. next_outbound_htlc_limit_msat ,
952
952
} ,
953
953
CandidateRouteHop :: PublicHop { info, .. } => info. effective_capacity ( ) ,
954
- CandidateRouteHop :: PrivateHop { .. } => EffectiveCapacity :: Infinite ,
954
+ CandidateRouteHop :: PrivateHop { hint : RouteHintHop { htlc_maximum_msat : Some ( max) , .. } } =>
955
+ EffectiveCapacity :: HintMaxHTLC { amount_msat : * max } ,
956
+ CandidateRouteHop :: PrivateHop { hint : RouteHintHop { htlc_maximum_msat : None , .. } } =>
957
+ EffectiveCapacity :: Infinite ,
955
958
}
956
959
}
957
960
}
@@ -965,6 +968,9 @@ fn max_htlc_from_capacity(capacity: EffectiveCapacity, max_channel_saturation_po
965
968
EffectiveCapacity :: Unknown => EffectiveCapacity :: Unknown . as_msat ( ) ,
966
969
EffectiveCapacity :: AdvertisedMaxHTLC { amount_msat } =>
967
970
amount_msat. checked_shr ( saturation_shift) . unwrap_or ( 0 ) ,
971
+ // Treat htlc_maximum_msat from a route hint as an exact liquidity amount, since the invoice is
972
+ // expected to have been generated from up-to-date capacity information.
973
+ EffectiveCapacity :: HintMaxHTLC { amount_msat } => amount_msat,
968
974
EffectiveCapacity :: Total { capacity_msat, htlc_maximum_msat } =>
969
975
cmp:: min ( capacity_msat. checked_shr ( saturation_shift) . unwrap_or ( 0 ) , htlc_maximum_msat) ,
970
976
}
@@ -1835,8 +1841,8 @@ where L::Target: Logger {
1835
1841
1836
1842
if !add_entry ! ( candidate, source, target, aggregate_next_hops_fee_msat,
1837
1843
path_value_msat, aggregate_next_hops_path_htlc_minimum_msat,
1838
- aggregate_next_hops_path_penalty_msat,
1839
- aggregate_next_hops_cltv_delta , aggregate_next_hops_path_length) {
1844
+ aggregate_next_hops_path_penalty_msat, aggregate_next_hops_cltv_delta ,
1845
+ aggregate_next_hops_path_length) {
1840
1846
// If this hop was not used then there is no use checking the preceding
1841
1847
// hops in the RouteHint. We can break by just searching for a direct
1842
1848
// channel between last checked hop and first_hop_targets.
@@ -1863,14 +1869,15 @@ where L::Target: Logger {
1863
1869
. saturating_add ( 1 ) ;
1864
1870
1865
1871
// Searching for a direct channel between last checked hop and first_hop_targets
1872
+ let hint_candidate_contribution_msat = cmp:: min ( path_value_msat,
1873
+ candidate. effective_capacity ( ) . as_msat ( ) . saturating_sub ( used_liquidity_msat) ) ;
1866
1874
if let Some ( first_channels) = first_hop_targets. get ( & NodeId :: from_pubkey ( & prev_hop_id) ) {
1867
1875
for details in first_channels {
1868
- let candidate = CandidateRouteHop :: FirstHop { details } ;
1869
- add_entry ! ( candidate, our_node_id, NodeId :: from_pubkey( & prev_hop_id) ,
1870
- aggregate_next_hops_fee_msat, path_value_msat,
1871
- aggregate_next_hops_path_htlc_minimum_msat,
1872
- aggregate_next_hops_path_penalty_msat, aggregate_next_hops_cltv_delta,
1873
- aggregate_next_hops_path_length) ;
1876
+ let first_hop_candidate = CandidateRouteHop :: FirstHop { details } ;
1877
+ add_entry ! ( first_hop_candidate, our_node_id, NodeId :: from_pubkey( & prev_hop_id) ,
1878
+ aggregate_next_hops_fee_msat, hint_candidate_contribution_msat,
1879
+ aggregate_next_hops_path_htlc_minimum_msat, aggregate_next_hops_path_penalty_msat,
1880
+ aggregate_next_hops_cltv_delta, aggregate_next_hops_path_length) ;
1874
1881
}
1875
1882
}
1876
1883
@@ -1905,10 +1912,11 @@ where L::Target: Logger {
1905
1912
// path.
1906
1913
if let Some ( first_channels) = first_hop_targets. get ( & NodeId :: from_pubkey ( & hop. src_node_id ) ) {
1907
1914
for details in first_channels {
1908
- let candidate = CandidateRouteHop :: FirstHop { details } ;
1909
- add_entry ! ( candidate , our_node_id,
1915
+ let first_hop_candidate = CandidateRouteHop :: FirstHop { details } ;
1916
+ add_entry ! ( first_hop_candidate , our_node_id,
1910
1917
NodeId :: from_pubkey( & hop. src_node_id) ,
1911
- aggregate_next_hops_fee_msat, path_value_msat,
1918
+ aggregate_next_hops_fee_msat,
1919
+ hint_candidate_contribution_msat,
1912
1920
aggregate_next_hops_path_htlc_minimum_msat,
1913
1921
aggregate_next_hops_path_penalty_msat,
1914
1922
aggregate_next_hops_cltv_delta,
@@ -5906,6 +5914,119 @@ mod tests {
5906
5914
assert ! ( route. is_ok( ) ) ;
5907
5915
}
5908
5916
5917
+ #[ test]
5918
+ fn abide_by_route_hint_max_htlc ( ) {
5919
+ // Check that we abide by any htlc_maximum_msat provided in the route hints of the payment
5920
+ // params in the final route.
5921
+ let ( secp_ctx, network_graph, _, _, logger) = build_graph ( ) ;
5922
+ let netgraph = network_graph. read_only ( ) ;
5923
+ let ( _, our_id, _, nodes) = get_nodes ( & secp_ctx) ;
5924
+ let scorer = ln_test_utils:: TestScorer :: new ( ) ;
5925
+ let keys_manager = ln_test_utils:: TestKeysInterface :: new ( & [ 0u8 ; 32 ] , Network :: Testnet ) ;
5926
+ let random_seed_bytes = keys_manager. get_secure_random_bytes ( ) ;
5927
+ let config = UserConfig :: default ( ) ;
5928
+
5929
+ let max_htlc_msat = 50_000 ;
5930
+ let route_hint_1 = RouteHint ( vec ! [ RouteHintHop {
5931
+ src_node_id: nodes[ 2 ] ,
5932
+ short_channel_id: 42 ,
5933
+ fees: RoutingFees {
5934
+ base_msat: 100 ,
5935
+ proportional_millionths: 0 ,
5936
+ } ,
5937
+ cltv_expiry_delta: 10 ,
5938
+ htlc_minimum_msat: None ,
5939
+ htlc_maximum_msat: Some ( max_htlc_msat) ,
5940
+ } ] ) ;
5941
+ let dest_node_id = ln_test_utils:: pubkey ( 42 ) ;
5942
+ let payment_params = PaymentParameters :: from_node_id ( dest_node_id, 42 )
5943
+ . with_route_hints ( vec ! [ route_hint_1. clone( ) ] ) . unwrap ( )
5944
+ . with_bolt11_features ( channelmanager:: provided_invoice_features ( & config) ) . unwrap ( ) ;
5945
+
5946
+ // Make sure we'll error if our route hints don't have enough liquidity according to their
5947
+ // htlc_maximum_msat.
5948
+ if let Err ( LightningError { err, action : ErrorAction :: IgnoreError } ) = get_route ( & our_id,
5949
+ & payment_params, & netgraph, None , max_htlc_msat + 1 , Arc :: clone ( & logger) , & scorer, & ( ) ,
5950
+ & random_seed_bytes)
5951
+ {
5952
+ assert_eq ! ( err, "Failed to find a sufficient route to the given destination" ) ;
5953
+ } else { panic ! ( ) ; }
5954
+
5955
+ // Make sure we'll split an MPP payment across route hints if their htlc_maximum_msat warrants.
5956
+ let mut route_hint_2 = route_hint_1. clone ( ) ;
5957
+ route_hint_2. 0 [ 0 ] . short_channel_id = 43 ;
5958
+ let payment_params = PaymentParameters :: from_node_id ( dest_node_id, 42 )
5959
+ . with_route_hints ( vec ! [ route_hint_1, route_hint_2] ) . unwrap ( )
5960
+ . with_bolt11_features ( channelmanager:: provided_invoice_features ( & config) ) . unwrap ( ) ;
5961
+ let route = get_route ( & our_id, & payment_params, & netgraph, None , max_htlc_msat + 1 ,
5962
+ Arc :: clone ( & logger) , & scorer, & ( ) , & random_seed_bytes) . unwrap ( ) ;
5963
+ assert_eq ! ( route. paths. len( ) , 2 ) ;
5964
+ assert ! ( route. paths[ 0 ] . hops. last( ) . unwrap( ) . fee_msat <= max_htlc_msat) ;
5965
+ assert ! ( route. paths[ 1 ] . hops. last( ) . unwrap( ) . fee_msat <= max_htlc_msat) ;
5966
+ }
5967
+
5968
+ #[ test]
5969
+ fn direct_channel_to_hints_with_max_htlc ( ) {
5970
+ // Check that if we have a first hop channel peer that's connected to multiple provided route
5971
+ // hints, that we properly split the payment between the route hints if needed.
5972
+ let logger = Arc :: new ( ln_test_utils:: TestLogger :: new ( ) ) ;
5973
+ let network_graph = Arc :: new ( NetworkGraph :: new ( Network :: Testnet , Arc :: clone ( & logger) ) ) ;
5974
+ let scorer = ln_test_utils:: TestScorer :: new ( ) ;
5975
+ let keys_manager = ln_test_utils:: TestKeysInterface :: new ( & [ 0u8 ; 32 ] , Network :: Testnet ) ;
5976
+ let random_seed_bytes = keys_manager. get_secure_random_bytes ( ) ;
5977
+ let config = UserConfig :: default ( ) ;
5978
+
5979
+ let our_node_id = ln_test_utils:: pubkey ( 42 ) ;
5980
+ let intermed_node_id = ln_test_utils:: pubkey ( 43 ) ;
5981
+ let first_hop = vec ! [ get_channel_details( Some ( 42 ) , intermed_node_id, InitFeatures :: from_le_bytes( vec![ 0b11 ] ) , 10_000_000 ) ] ;
5982
+
5983
+ let amt_msat = 900_000 ;
5984
+ let max_htlc_msat = 500_000 ;
5985
+ let route_hint_1 = RouteHint ( vec ! [ RouteHintHop {
5986
+ src_node_id: intermed_node_id,
5987
+ short_channel_id: 44 ,
5988
+ fees: RoutingFees {
5989
+ base_msat: 100 ,
5990
+ proportional_millionths: 0 ,
5991
+ } ,
5992
+ cltv_expiry_delta: 10 ,
5993
+ htlc_minimum_msat: None ,
5994
+ htlc_maximum_msat: Some ( max_htlc_msat) ,
5995
+ } ] ) ;
5996
+ let mut route_hint_2 = route_hint_1. clone ( ) ;
5997
+ route_hint_2. 0 [ 0 ] . short_channel_id = 45 ;
5998
+ route_hint_2. 0 [ 0 ] . htlc_maximum_msat = Some ( max_htlc_msat) ;
5999
+ let dest_node_id = ln_test_utils:: pubkey ( 44 ) ;
6000
+ let payment_params = PaymentParameters :: from_node_id ( dest_node_id, 42 )
6001
+ . with_route_hints ( vec ! [ route_hint_1, route_hint_2] ) . unwrap ( )
6002
+ . with_bolt11_features ( channelmanager:: provided_invoice_features ( & config) ) . unwrap ( ) ;
6003
+
6004
+ let route = get_route ( & our_node_id, & payment_params, & network_graph. read_only ( ) ,
6005
+ Some ( & first_hop. iter ( ) . collect :: < Vec < _ > > ( ) ) , amt_msat, Arc :: clone ( & logger) , & scorer, & ( ) ,
6006
+ & random_seed_bytes) . unwrap ( ) ;
6007
+ assert_eq ! ( route. paths. len( ) , 2 ) ;
6008
+ assert ! ( route. paths[ 0 ] . hops. last( ) . unwrap( ) . fee_msat <= max_htlc_msat) ;
6009
+ assert ! ( route. paths[ 1 ] . hops. last( ) . unwrap( ) . fee_msat <= max_htlc_msat) ;
6010
+ assert_eq ! ( route. get_total_amount( ) , amt_msat) ;
6011
+
6012
+ // Re-run but with two first hop channels connected to the same route hint peers that must be
6013
+ // split between.
6014
+ let first_hops = vec ! [
6015
+ get_channel_details( Some ( 42 ) , intermed_node_id, InitFeatures :: from_le_bytes( vec![ 0b11 ] ) , amt_msat - 10 ) ,
6016
+ get_channel_details( Some ( 43 ) , intermed_node_id, InitFeatures :: from_le_bytes( vec![ 0b11 ] ) , amt_msat - 10 ) ,
6017
+ ] ;
6018
+ let route = get_route ( & our_node_id, & payment_params, & network_graph. read_only ( ) ,
6019
+ Some ( & first_hops. iter ( ) . collect :: < Vec < _ > > ( ) ) , amt_msat, Arc :: clone ( & logger) , & scorer, & ( ) ,
6020
+ & random_seed_bytes) . unwrap ( ) ;
6021
+ // TODO: `get_route` returns a suboptimal route here because first hop channels are not
6022
+ // resorted on the fly when processing route hints.
6023
+ assert_eq ! ( route. paths. len( ) , 3 ) ;
6024
+ assert ! ( route. paths[ 0 ] . hops. last( ) . unwrap( ) . fee_msat <= max_htlc_msat) ;
6025
+ assert ! ( route. paths[ 1 ] . hops. last( ) . unwrap( ) . fee_msat <= max_htlc_msat) ;
6026
+ assert ! ( route. paths[ 2 ] . hops. last( ) . unwrap( ) . fee_msat <= max_htlc_msat) ;
6027
+ assert_eq ! ( route. get_total_amount( ) , amt_msat) ;
6028
+ }
6029
+
5909
6030
#[ test]
5910
6031
fn blinded_route_ser ( ) {
5911
6032
let blinded_path_1 = BlindedPath {
0 commit comments