@@ -1832,89 +1832,96 @@ where L::Target: Logger {
18321832 total_fee_msat = total_fee_msat. saturating_add( hop_use_fee_msat) ;
18331833 }
18341834
1835- let channel_usage = ChannelUsage {
1836- amount_msat: amount_to_transfer_over_msat,
1837- inflight_htlc_msat: used_liquidity_msat,
1838- effective_capacity,
1839- } ;
1840- let channel_penalty_msat = scid_opt. map_or( 0 ,
1841- |scid| scorer. channel_penalty_msat( scid, & $src_node_id, & $dest_node_id,
1842- channel_usage, score_params) ) ;
1843- let path_penalty_msat = $next_hops_path_penalty_msat
1844- . saturating_add( channel_penalty_msat) ;
1845- let new_graph_node = RouteGraphNode {
1846- node_id: $src_node_id,
1847- lowest_fee_to_node: total_fee_msat,
1848- total_cltv_delta: hop_total_cltv_delta,
1849- value_contribution_msat,
1850- path_htlc_minimum_msat,
1851- path_penalty_msat,
1852- path_length_to_node,
1853- } ;
1854-
1855- // Update the way of reaching $src_node_id with the given short_channel_id (from $dest_node_id),
1856- // if this way is cheaper than the already known
1857- // (considering the cost to "reach" this channel from the route destination,
1858- // the cost of using this channel,
1859- // and the cost of routing to the source node of this channel).
1860- // Also, consider that htlc_minimum_msat_difference, because we might end up
1861- // paying it. Consider the following exploit:
1862- // we use 2 paths to transfer 1.5 BTC. One of them is 0-fee normal 1 BTC path,
1863- // and for the other one we picked a 1sat-fee path with htlc_minimum_msat of
1864- // 1 BTC. Now, since the latter is more expensive, we gonna try to cut it
1865- // by 0.5 BTC, but then match htlc_minimum_msat by paying a fee of 0.5 BTC
1866- // to this channel.
1867- // Ideally the scoring could be smarter (e.g. 0.5*htlc_minimum_msat here),
1868- // but it may require additional tracking - we don't want to double-count
1869- // the fees included in $next_hops_path_htlc_minimum_msat, but also
1870- // can't use something that may decrease on future hops.
1871- let old_cost = cmp:: max( old_entry. total_fee_msat, old_entry. path_htlc_minimum_msat)
1872- . saturating_add( old_entry. path_penalty_msat) ;
1873- let new_cost = cmp:: max( total_fee_msat, path_htlc_minimum_msat)
1874- . saturating_add( path_penalty_msat) ;
1875-
1876- if !old_entry. was_processed && new_cost < old_cost {
1877- targets. push( new_graph_node) ;
1878- old_entry. next_hops_fee_msat = $next_hops_fee_msat;
1879- old_entry. hop_use_fee_msat = hop_use_fee_msat;
1880- old_entry. total_fee_msat = total_fee_msat;
1881- old_entry. node_id = $dest_node_id. clone( ) ;
1882- old_entry. candidate = $candidate. clone( ) ;
1883- old_entry. fee_msat = 0 ; // This value will be later filled with hop_use_fee_msat of the following channel
1884- old_entry. path_htlc_minimum_msat = path_htlc_minimum_msat;
1885- old_entry. path_penalty_msat = path_penalty_msat;
1886- #[ cfg( all( not( ldk_bench) , any( test, fuzzing) ) ) ]
1887- {
1888- old_entry. value_contribution_msat = value_contribution_msat;
1889- }
1890- did_add_update_path_to_src_node = Some ( value_contribution_msat) ;
1891- } else if old_entry. was_processed && new_cost < old_cost {
1892- #[ cfg( all( not( ldk_bench) , any( test, fuzzing) ) ) ]
1893- {
1894- // If we're skipping processing a node which was previously
1895- // processed even though we found another path to it with a
1896- // cheaper fee, check that it was because the second path we
1897- // found (which we are processing now) has a lower value
1898- // contribution due to an HTLC minimum limit.
1899- //
1900- // e.g. take a graph with two paths from node 1 to node 2, one
1901- // through channel A, and one through channel B. Channel A and
1902- // B are both in the to-process heap, with their scores set by
1903- // a higher htlc_minimum than fee.
1904- // Channel A is processed first, and the channels onwards from
1905- // node 1 are added to the to-process heap. Thereafter, we pop
1906- // Channel B off of the heap, note that it has a much more
1907- // restrictive htlc_maximum_msat, and recalculate the fees for
1908- // all of node 1's channels using the new, reduced, amount.
1909- //
1910- // This would be bogus - we'd be selecting a higher-fee path
1911- // with a lower htlc_maximum_msat instead of the one we'd
1912- // already decided to use.
1913- debug_assert!( path_htlc_minimum_msat < old_entry. path_htlc_minimum_msat) ;
1914- debug_assert!(
1915- value_contribution_msat + path_penalty_msat <
1916- old_entry. value_contribution_msat + old_entry. path_penalty_msat
1917- ) ;
1835+ // Ignore hops if they by themselves would already put us over `max_total_routing_fee_msat`
1836+ let max_total_routing_fee_msat = payment_params. max_total_routing_fee_msat. unwrap_or( u64 :: max_value( ) ) ;
1837+ if total_fee_msat > max_total_routing_fee_msat {
1838+ log_trace!( logger, "Ignoring candidate hop {} as the path's total fee {} would exceed the maximum total routing fee limit {}" ,
1839+ LoggedCandidateHop ( & $candidate) , total_fee_msat, max_total_routing_fee_msat) ;
1840+ } else {
1841+ let channel_usage = ChannelUsage {
1842+ amount_msat: amount_to_transfer_over_msat,
1843+ inflight_htlc_msat: used_liquidity_msat,
1844+ effective_capacity,
1845+ } ;
1846+ let channel_penalty_msat = scid_opt. map_or( 0 ,
1847+ |scid| scorer. channel_penalty_msat( scid, & $src_node_id, & $dest_node_id,
1848+ channel_usage, score_params) ) ;
1849+ let path_penalty_msat = $next_hops_path_penalty_msat
1850+ . saturating_add( channel_penalty_msat) ;
1851+ let new_graph_node = RouteGraphNode {
1852+ node_id: $src_node_id,
1853+ lowest_fee_to_node: total_fee_msat,
1854+ total_cltv_delta: hop_total_cltv_delta,
1855+ value_contribution_msat,
1856+ path_htlc_minimum_msat,
1857+ path_penalty_msat,
1858+ path_length_to_node,
1859+ } ;
1860+
1861+ // Update the way of reaching $src_node_id with the given short_channel_id (from $dest_node_id),
1862+ // if this way is cheaper than the already known
1863+ // (considering the cost to "reach" this channel from the route destination,
1864+ // the cost of using this channel,
1865+ // and the cost of routing to the source node of this channel).
1866+ // Also, consider that htlc_minimum_msat_difference, because we might end up
1867+ // paying it. Consider the following exploit:
1868+ // we use 2 paths to transfer 1.5 BTC. One of them is 0-fee normal 1 BTC path,
1869+ // and for the other one we picked a 1sat-fee path with htlc_minimum_msat of
1870+ // 1 BTC. Now, since the latter is more expensive, we gonna try to cut it
1871+ // by 0.5 BTC, but then match htlc_minimum_msat by paying a fee of 0.5 BTC
1872+ // to this channel.
1873+ // Ideally the scoring could be smarter (e.g. 0.5*htlc_minimum_msat here),
1874+ // but it may require additional tracking - we don't want to double-count
1875+ // the fees included in $next_hops_path_htlc_minimum_msat, but also
1876+ // can't use something that may decrease on future hops.
1877+ let old_cost = cmp:: max( old_entry. total_fee_msat, old_entry. path_htlc_minimum_msat)
1878+ . saturating_add( old_entry. path_penalty_msat) ;
1879+ let new_cost = cmp:: max( total_fee_msat, path_htlc_minimum_msat)
1880+ . saturating_add( path_penalty_msat) ;
1881+
1882+ if !old_entry. was_processed && new_cost < old_cost {
1883+ targets. push( new_graph_node) ;
1884+ old_entry. next_hops_fee_msat = $next_hops_fee_msat;
1885+ old_entry. hop_use_fee_msat = hop_use_fee_msat;
1886+ old_entry. total_fee_msat = total_fee_msat;
1887+ old_entry. node_id = $dest_node_id. clone( ) ;
1888+ old_entry. candidate = $candidate. clone( ) ;
1889+ old_entry. fee_msat = 0 ; // This value will be later filled with hop_use_fee_msat of the following channel
1890+ old_entry. path_htlc_minimum_msat = path_htlc_minimum_msat;
1891+ old_entry. path_penalty_msat = path_penalty_msat;
1892+ #[ cfg( all( not( ldk_bench) , any( test, fuzzing) ) ) ]
1893+ {
1894+ old_entry. value_contribution_msat = value_contribution_msat;
1895+ }
1896+ did_add_update_path_to_src_node = Some ( value_contribution_msat) ;
1897+ } else if old_entry. was_processed && new_cost < old_cost {
1898+ #[ cfg( all( not( ldk_bench) , any( test, fuzzing) ) ) ]
1899+ {
1900+ // If we're skipping processing a node which was previously
1901+ // processed even though we found another path to it with a
1902+ // cheaper fee, check that it was because the second path we
1903+ // found (which we are processing now) has a lower value
1904+ // contribution due to an HTLC minimum limit.
1905+ //
1906+ // e.g. take a graph with two paths from node 1 to node 2, one
1907+ // through channel A, and one through channel B. Channel A and
1908+ // B are both in the to-process heap, with their scores set by
1909+ // a higher htlc_minimum than fee.
1910+ // Channel A is processed first, and the channels onwards from
1911+ // node 1 are added to the to-process heap. Thereafter, we pop
1912+ // Channel B off of the heap, note that it has a much more
1913+ // restrictive htlc_maximum_msat, and recalculate the fees for
1914+ // all of node 1's channels using the new, reduced, amount.
1915+ //
1916+ // This would be bogus - we'd be selecting a higher-fee path
1917+ // with a lower htlc_maximum_msat instead of the one we'd
1918+ // already decided to use.
1919+ debug_assert!( path_htlc_minimum_msat < old_entry. path_htlc_minimum_msat) ;
1920+ debug_assert!(
1921+ value_contribution_msat + path_penalty_msat <
1922+ old_entry. value_contribution_msat + old_entry. path_penalty_msat
1923+ ) ;
1924+ }
19181925 }
19191926 }
19201927 }
@@ -2502,6 +2509,14 @@ where L::Target: Logger {
25022509 // Make sure we would never create a route with more paths than we allow.
25032510 debug_assert ! ( paths. len( ) <= payment_params. max_path_count. into( ) ) ;
25042511
2512+ // Make sure we would never create a route whose total fees exceed max_total_routing_fee_msat.
2513+ if let Some ( max_total_routing_fee_msat) = payment_params. max_total_routing_fee_msat {
2514+ if paths. iter ( ) . map ( |p| p. fee_msat ( ) ) . sum :: < u64 > ( ) > max_total_routing_fee_msat {
2515+ return Err ( LightningError { err : format ! ( "Failed to find route that adheres to the maximum total fee limit of {}msat" ,
2516+ max_total_routing_fee_msat) , action : ErrorAction :: IgnoreError } ) ;
2517+ }
2518+ }
2519+
25052520 if let Some ( node_features) = payment_params. payee . node_features ( ) {
25062521 for path in paths. iter_mut ( ) {
25072522 path. hops . last_mut ( ) . unwrap ( ) . node_features = node_features. clone ( ) ;
0 commit comments