Skip to content

Commit bde841e

Browse files
committed
Clean up compute_fees and add a saturating variant
Often when we call `compute_fees` we really just want it to saturate and we deal with `u64::max_value` later. In that case, we're much better off doing the saturating in the `compute_fees` as it can use CMOVs rather than branching at each step and then `unwrap_or`ing at the callsite.
1 parent e64b5d9 commit bde841e

File tree

1 file changed

+21
-24
lines changed

1 file changed

+21
-24
lines changed

lightning/src/routing/router.rs

+21-24
Original file line numberDiff line numberDiff line change
@@ -885,18 +885,20 @@ 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.
888890
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+
}
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.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)
900902
}
901903

902904
/// 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 {
12541256
// might violate htlc_minimum_msat on the hops which are next along the
12551257
// payment path (upstream to the payee). To avoid that, we recompute
12561258
// 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());
12611263
let hm_entry = dist.entry($src_node_id);
12621264
let old_entry = hm_entry.or_insert_with(|| {
12631265
// If there was previously no known way to access the source node
@@ -1291,20 +1293,15 @@ where L::Target: Logger {
12911293

12921294
if should_process {
12931295
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;
12951297

12961298
// Ignore hop_use_fee_msat for channel-from-us as we assume all channels-from-us
12971299
// will have the same effective-fee
12981300
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);
13081305
}
13091306

13101307
let channel_usage = ChannelUsage {

0 commit comments

Comments
 (0)