Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't over-penalize channels with inflight HTLCs #3356

Merged
merged 4 commits into from
Oct 19, 2024

Conversation

jkczyz
Copy link
Contributor

@jkczyz jkczyz commented Oct 10, 2024

Commit df52da7 modified ProbabilisticScorer to apply some penalty amount multipliers (e.g., liquidity_penalty_amount_multiplier_msat) to the total amount flowing over the channel (i.e., including inflight HTLCs), not just the payment in question. This led to over-penalizing in-use channels. Instead, only apply the total amount when calculating success probability.

Fixes #3271

@jkczyz
Copy link
Contributor Author

jkczyz commented Oct 10, 2024

Unclear why this change causes routing::router::tests::avoids_saturating_channels to fail.

Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code changes lgtm except the one comment.

lightning/src/routing/scoring.rs Show resolved Hide resolved
@jkczyz jkczyz force-pushed the 2024-10-inflight-scoring branch from 0677c21 to 69594e9 Compare October 11, 2024 22:42
@TheBlueMatt
Copy link
Collaborator

avoids_saturating_channels may actually be buggy in structure, not, sure. Probably needs someone to sit down and carefully look at the graph structure we build.

@jkczyz
Copy link
Contributor Author

jkczyz commented Oct 16, 2024

avoids_saturating_channels may actually be buggy in structure, not, sure. Probably needs someone to sit down and carefully look at the graph structure we build.

AFAICT, this has to due with using unstable sort with selected_route, which contains paths both with and without channel_saturation_pow_half applied. Prior to my change, the paths using channel_saturation_pow_half came after those not using it after being sorted. But this is the opposite with my change.

selected_route.sort_unstable_by(|a, b|
(((b.get_cost_msat() as u128) << 64) / (b.get_value_msat() as u128))
.cmp(&(((a.get_cost_msat() as u128) << 64) / (a.get_value_msat() as u128)))
);

Next, the retain only keeps the path with channel_saturation_pow_half applied. But with my change when the sort order is different, only the path without using channel_saturation_pow_half is kept.

let mut paths_left = selected_route.len();
selected_route.retain(|path| {
if paths_left == 1 {
return true
}
let path_value_msat = path.get_value_msat();
if path_value_msat <= overpaid_value_msat {
overpaid_value_msat -= path_value_msat;
paths_left -= 1;
return false;
}
true
});

Thus, the test fails because it wants two paths but it only gets one (i.e., the one not applying channel_saturation_pow_half). Haven't looked further yet to see what the fix would be.

@jkczyz
Copy link
Contributor Author

jkczyz commented Oct 17, 2024

Ok, I took a closer look. Unstable sort isn't actually the problem. Since the the penalty has decreased, the cost per value on those paths where channel_saturation_pow_half was applied is no longer less than the cost for the paths where it wasn't applied.

@jkczyz jkczyz force-pushed the 2024-10-inflight-scoring branch from 69594e9 to 9ac1737 Compare October 17, 2024 16:04
@jkczyz jkczyz marked this pull request as ready for review October 17, 2024 16:05
@jkczyz
Copy link
Contributor Author

jkczyz commented Oct 17, 2024

Changing the amount used in the test from 100_000_000 to 75_000_000 msats fixed the test. PTAL.

Copy link
Contributor

@G8XSU G8XSU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does what it says in commit description.

  • We now send inflight_htlc_amount and htlc_amount separately to calculate penalty.
  • Uses htlc_amount to calculate penalty multiplers(negative).
  • Uses total_amount (inflight_htlc+htlc_amount) to calculate success probability(positive) and bounds.
  • All instances of htlc_amount and total_amount (inflight_htlc+htlc_amount) checked.

lightning/src/routing/scoring.rs Show resolved Hide resolved
let (numerator, denominator) = success_probability(
total_inflight_amount_msat, 0, available_capacity, available_capacity,
score_params, true,
);
let negative_log10_times_2048 =
log_approx::negative_log10_times_2048(numerator, denominator);
res = res.saturating_add(Self::combined_penalty_msat(amount_msat, negative_log10_times_2048,
Copy link
Contributor

@G8XSU G8XSU Oct 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doubt: Can you explain why liquidity_penalty only needs to be applied over htlc_amount_msat and not total_inflight?
Previously, this penalty would increase with each new htlc being routed through the same channel, but now, every htlc(whether the 1st or the 10th) will be treated equal for liquidity penalty iiuc.

I understand it might not be working as designed/intended at first but how was it wrong?
Is it because we don't want to penalize twice(once for liquidity_penalty and next for success_probability), since success_probability is already non-linear for increasing amounts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, we want to use the total amount when computing the success probability since in-flight HTLCs will affect this. However, when computing the actual penalties we use the payment amount (i.e., amount_msat) and the success probability. So each successive HTLC in-flight would cause the penalty to increase since the success probability would decrease (assuming the total amount is above the minimum liquidity estimate).

That said, as written, our docs may need to be clearer about what amount is used to compute the success probability (total amount) and what amount is used for the penalty (payment amount w/ success probability).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That said, as written, our docs may need to be clearer about what amount is used to compute the success probability (total amount) and what amount is used for the penalty (payment amount w/ success probability).

Updated the docs in the latest push, BTW.

Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, feel free to squash fixups.

Commit df52da7 modified
ProbabilisticScorer to apply some penalty amount multipliers (e.g.,
liquidity_penalty_amount_multiplier_msat) to the total amount flowing
over the channel (i.e., including inflight HTLCs), not just the payment
in question. This led to over-penalizing in-use channels. Instead, only
apply the total amount when calculating success probability.
Commit df52da7 modified
ProbabilisticScorer to apply some penalty amount multipliers to the
total amount flowing over the channel. However, the commit updated the
docs for base_penalty_amount_multiplier_msat even though that behavior
didn't change. This commit reverts those docs.
Rename parameters used when calculating success probability to make it
clear that the total mount in-flight should be used rather than the
payment amount.
@jkczyz jkczyz force-pushed the 2024-10-inflight-scoring branch from 9ac1737 to 0305000 Compare October 18, 2024 23:28
Copy link
Contributor

@G8XSU G8XSU left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm!

@@ -1074,26 +1074,30 @@ fn three_f64_pow_3(a: f64, b: f64, c: f64) -> (f64, f64, f64) {
///
/// Must not return a numerator or denominator greater than 2^31 for arguments less than 2^31.
///
/// `total_inflight_amount_msat` includes the amount of the HTLC and any HTLCs in flight over the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: there is a typo in last commit's description,
mount -> amount.

Copy link
Collaborator

@TheBlueMatt TheBlueMatt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not gonna hold this up on a commit message typo.

@TheBlueMatt TheBlueMatt merged commit 1e0f43f into lightningdevkit:main Oct 19, 2024
19 of 21 checks passed
@G8XSU
Copy link
Contributor

G8XSU commented Oct 21, 2024

Not gonna hold this up on a commit message typo.

Yes, was just waiting for your review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

In-flight amounts get calculated as a part of the per-hop-per-amount penalty when scoring a channel
3 participants