@@ -885,18 +885,20 @@ impl<'a> PaymentPath<'a> {
885
885
}
886
886
}
887
887
888
+ #[ inline( always) ]
889
+ /// Calculate the fees required to route the given amount over a channel with the given fees.
888
890
fn compute_fees ( amount_msat : u64 , channel_fees : RoutingFees ) -> Option < u64 > {
889
- let proportional_fee_millions =
890
- amount_msat. checked_mul ( channel_fees. proportional_millionths as u64 ) ;
891
- if let Some ( new_fee) = proportional_fee_millions. and_then ( |part| {
892
- ( channel_fees. base_msat as u64 ) . checked_add ( part / 1_000_000 ) } ) {
891
+ amount_msat. checked_mul ( channel_fees. proportional_millionths as u64 )
892
+ . and_then ( |part| ( channel_fees. base_msat as u64 ) . checked_add ( part / 1_000_000 ) )
893
+ }
893
894
894
- Some ( new_fee)
895
- } else {
896
- // This function may be (indirectly) called without any verification,
897
- // with channel_fees provided by a caller. We should handle it gracefully.
898
- None
899
- }
895
+ #[ inline( always) ]
896
+ /// Calculate the fees required to route the given amount over a channel with the given fees,
897
+ /// saturating to [`u64::max_value`].
898
+ fn compute_fees_saturating ( amount_msat : u64 , channel_fees : RoutingFees ) -> u64 {
899
+ amount_msat. checked_mul ( channel_fees. proportional_millionths as u64 )
900
+ . map ( |prop| prop / 1_000_000 ) . unwrap_or ( u64:: max_value ( ) )
901
+ . saturating_add ( channel_fees. base_msat as u64 )
900
902
}
901
903
902
904
/// The default `features` we assume for a node in a route, when no `features` are known about that
@@ -1254,10 +1256,10 @@ where L::Target: Logger {
1254
1256
// might violate htlc_minimum_msat on the hops which are next along the
1255
1257
// payment path (upstream to the payee). To avoid that, we recompute
1256
1258
// path fees knowing the final path contribution after constructing it.
1257
- let path_htlc_minimum_msat = compute_fees ( $next_hops_path_htlc_minimum_msat , $candidate . fees ( ) )
1258
- . and_then ( |fee_msat| fee_msat . checked_add ( $next_hops_path_htlc_minimum_msat ) )
1259
- . map ( |fee_msat| cmp :: max ( fee_msat , $candidate . htlc_minimum_msat ( ) ) )
1260
- . unwrap_or_else ( || u64 :: max_value ( ) ) ;
1259
+ let path_htlc_minimum_msat = cmp :: max (
1260
+ compute_fees_saturating ( $next_hops_path_htlc_minimum_msat , $candidate . fees ( ) )
1261
+ . saturating_add ( $next_hops_path_htlc_minimum_msat ) ,
1262
+ $candidate . htlc_minimum_msat ( ) ) ;
1261
1263
let hm_entry = dist. entry( $src_node_id) ;
1262
1264
let old_entry = hm_entry. or_insert_with( || {
1263
1265
// If there was previously no known way to access the source node
@@ -1291,20 +1293,15 @@ where L::Target: Logger {
1291
1293
1292
1294
if should_process {
1293
1295
let mut hop_use_fee_msat = 0 ;
1294
- let mut total_fee_msat = $next_hops_fee_msat;
1296
+ let mut total_fee_msat: u64 = $next_hops_fee_msat;
1295
1297
1296
1298
// Ignore hop_use_fee_msat for channel-from-us as we assume all channels-from-us
1297
1299
// will have the same effective-fee
1298
1300
if $src_node_id != our_node_id {
1299
- match compute_fees( amount_to_transfer_over_msat, $candidate. fees( ) ) {
1300
- // max_value means we'll always fail
1301
- // the old_entry.total_fee_msat > total_fee_msat check
1302
- None => total_fee_msat = u64 :: max_value( ) ,
1303
- Some ( fee_msat) => {
1304
- hop_use_fee_msat = fee_msat;
1305
- total_fee_msat += hop_use_fee_msat;
1306
- }
1307
- }
1301
+ // Note that `u64::max_value` means we'll always fail the
1302
+ // `old_entry.total_fee_msat > total_fee_msat` check below
1303
+ hop_use_fee_msat = compute_fees_saturating( amount_to_transfer_over_msat, $candidate. fees( ) ) ;
1304
+ total_fee_msat = total_fee_msat. saturating_add( hop_use_fee_msat) ;
1308
1305
}
1309
1306
1310
1307
let channel_usage = ChannelUsage {
0 commit comments