Skip to content

Commit

Permalink
Get max HTLC from CandidateRouteHop instead of EffectiveCapacity
Browse files Browse the repository at this point in the history
With the addition of blinded route hints, we can't assume that any provided
route hint has enough capacity for the entire payment anymore. Blinded route
hints provided a max_htlc_msat, so use that in routing.
  • Loading branch information
valentinewallace committed May 16, 2023
1 parent 1d0e55e commit 7a801a3
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions lightning/src/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -957,16 +957,23 @@ impl<'a> CandidateRouteHop<'a> {
}

#[inline]
fn max_htlc_from_capacity(capacity: EffectiveCapacity, max_channel_saturation_power_of_half: u8) -> u64 {
fn max_htlc_from_route_candidate(candidate: &CandidateRouteHop, max_channel_saturation_power_of_half: u8) -> u64 {
let saturation_shift: u32 = max_channel_saturation_power_of_half as u32;
match capacity {
EffectiveCapacity::ExactLiquidity { liquidity_msat } => liquidity_msat,
EffectiveCapacity::Infinite => u64::max_value(),
EffectiveCapacity::Unknown => EffectiveCapacity::Unknown.as_msat(),
EffectiveCapacity::MaximumHTLC { amount_msat } =>
amount_msat.checked_shr(saturation_shift).unwrap_or(0),
EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat } =>
cmp::min(capacity_msat.checked_shr(saturation_shift).unwrap_or(0), htlc_maximum_msat),
match candidate {
CandidateRouteHop::FirstHop { details } => details.next_outbound_htlc_limit_msat,
CandidateRouteHop::PublicHop { info, .. } => {
match info.effective_capacity() {
EffectiveCapacity::MaximumHTLC { amount_msat } =>
amount_msat.checked_shr(saturation_shift).unwrap_or(0),
EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat } =>
cmp::min(capacity_msat.checked_shr(saturation_shift).unwrap_or(0), htlc_maximum_msat),
_ => {
debug_assert!(false);
EffectiveCapacity::Unknown.as_msat()
}
}
},
CandidateRouteHop::PrivateHop { hint } => hint.htlc_maximum_msat.unwrap_or(u64::max_value()),
}
}

Expand Down Expand Up @@ -1479,7 +1486,7 @@ where L::Target: Logger {
if $src_node_id != $dest_node_id {
let short_channel_id = $candidate.short_channel_id();
let effective_capacity = $candidate.effective_capacity();
let htlc_maximum_msat = max_htlc_from_capacity(effective_capacity, channel_saturation_pow_half);
let htlc_maximum_msat = max_htlc_from_route_candidate(&$candidate, channel_saturation_pow_half);

// It is tricky to subtract $next_hops_fee_msat from available liquidity here.
// It may be misleading because we might later choose to reduce the value transferred
Expand Down Expand Up @@ -2023,8 +2030,7 @@ where L::Target: Logger {
.entry((hop.candidate.short_channel_id(), *prev_hop < hop.node_id))
.and_modify(|used_liquidity_msat| *used_liquidity_msat += spent_on_hop_msat)
.or_insert(spent_on_hop_msat);
let hop_capacity = hop.candidate.effective_capacity();
let hop_max_msat = max_htlc_from_capacity(hop_capacity, channel_saturation_pow_half);
let hop_max_msat = max_htlc_from_route_candidate(&hop.candidate, channel_saturation_pow_half);
if *used_liquidity_msat == hop_max_msat {
// If this path used all of this channel's available liquidity, we know
// this path will not be selected again in the next loop iteration.
Expand Down

0 comments on commit 7a801a3

Please sign in to comment.