@@ -885,18 +885,19 @@ impl<'a> PaymentPath<'a> {
885885 }
886886}
887887
888+ #[ inline( always) ]
889+ /// Calculate the fees required to route the given amount over a channel with the given fees.
888890fn 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+ }
893894
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 . saturating_mul ( channel_fees . proportional_millionths as u64 ) / 1_000_000 )
900+ . saturating_add ( channel_fees . base_msat as u64 )
900901}
901902
902903/// The default `features` we assume for a node in a route, when no `features` are known about that
@@ -1254,10 +1255,10 @@ where L::Target: Logger {
12541255 // might violate htlc_minimum_msat on the hops which are next along the
12551256 // payment path (upstream to the payee). To avoid that, we recompute
12561257 // 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 ( ) ) ;
1258+ let path_htlc_minimum_msat = cmp :: max (
1259+ compute_fees_saturating ( $next_hops_path_htlc_minimum_msat , $candidate . fees ( ) )
1260+ . saturating_add ( $next_hops_path_htlc_minimum_msat ) ,
1261+ $candidate . htlc_minimum_msat ( ) ) ;
12611262 let hm_entry = dist. entry( $src_node_id) ;
12621263 let old_entry = hm_entry. or_insert_with( || {
12631264 // If there was previously no known way to access the source node
@@ -1291,20 +1292,15 @@ where L::Target: Logger {
12911292
12921293 if should_process {
12931294 let mut hop_use_fee_msat = 0 ;
1294- let mut total_fee_msat = $next_hops_fee_msat;
1295+ let mut total_fee_msat: u64 = $next_hops_fee_msat;
12951296
12961297 // Ignore hop_use_fee_msat for channel-from-us as we assume all channels-from-us
12971298 // will have the same effective-fee
12981299 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- }
1300+ // Note that `u64::max_value` means we'll always fail the
1301+ // `old_entry.total_fee_msat > total_fee_msat` check below
1302+ hop_use_fee_msat = compute_fees_saturating( amount_to_transfer_over_msat, $candidate. fees( ) ) ;
1303+ total_fee_msat = total_fee_msat. saturating_add( hop_use_fee_msat) ;
13081304 }
13091305
13101306 let channel_usage = ChannelUsage {
0 commit comments