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

Add config option to set maximum total routing fee #2417

Merged
merged 8 commits into from
Sep 26, 2023

Conversation

tnull
Copy link
Contributor

@tnull tnull commented Jul 14, 2023

Fixes #2339.
Closes #2596.
Based on #2555.

Currently, users have no means to upper-bound the total fees accruing when finding a route. Here, we add a corresponding field to PaymentParameters which is used to limit the candidate set during path finding.

To this end, we exclude any candidate hops if we find that using them would let the aggregated path routing fees exceed max_total_routeing_fee_msat. Moreover, we return an error if the aggregated fees over all paths of the selected route would surpass max_total_routeing_fee_msat.

@tnull tnull marked this pull request as draft July 14, 2023 15:08
@tnull
Copy link
Contributor Author

tnull commented Jul 14, 2023

Drafting for now as I'm not super happy that we only filter on a per-path basis and then fail entirely if the aggregate route surpasses the routing fee limit. So far experimented with the approach we took for the minimal_value_contribution, i.e., having static per-path quotas, but discarded this as it would for example limit the per-path total fee to max_total_routing_fee_msat/max_path_count, i.e., to only 10% of the given value by default.

I think it's not super bad currently as we already pre-sort and filter routes by their cost/value ratio, which however also includes the penalty. Considering whether it would make sense to, whenever max_total_routing_fee_msat is set, add yet another pre-filtering step that only takes total fees into account to decrease the chances of unnecessarily returning an empty route.

This also currently doesn't account for retries. We might need to track the 'used' fees in a structure similar to InFlightHtlcs.

@codecov-commenter
Copy link

codecov-commenter commented Jul 14, 2023

Codecov Report

Attention: 19 lines in your changes are missing coverage. Please review.

Comparison is base (7e7e7a0) 88.83% compared to head (77afeac) 89.68%.
Report is 35 commits behind head on main.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2417      +/-   ##
==========================================
+ Coverage   88.83%   89.68%   +0.85%     
==========================================
  Files         113      113              
  Lines       84489    89980    +5491     
  Branches    84489    89980    +5491     
==========================================
+ Hits        75052    80696    +5644     
+ Misses       7205     7055     -150     
+ Partials     2232     2229       -3     
Files Coverage Δ
lightning/src/ln/blinded_payment_tests.rs 100.00% <100.00%> (ø)
lightning/src/ln/functional_test_utils.rs 93.62% <ø> (+2.45%) ⬆️
lightning/src/ln/channelmanager.rs 87.68% <50.00%> (+6.55%) ⬆️
lightning-invoice/src/payment.rs 77.30% <33.33%> (-0.26%) ⬇️
lightning/src/ln/payment_tests.rs 98.06% <98.60%> (+0.01%) ⬆️
lightning/src/ln/outbound_payment.rs 90.03% <92.94%> (+1.23%) ⬆️
lightning/src/routing/router.rs 93.73% <93.22%> (+0.10%) ⬆️

... and 17 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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.

This is fine, I think. I agree we should clean it up later to more aggressively find paths even though our first choice was fee-limited out, but this is certainly better than not having fee limiting.

lightning/src/routing/router.rs Outdated Show resolved Hide resolved
lightning/src/routing/router.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@valentinewallace valentinewallace left a comment

Choose a reason for hiding this comment

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

Looks good to me as a first step, given where we're at with the release!

lightning/src/routing/router.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@alecchendev alecchendev left a comment

Choose a reason for hiding this comment

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

Nice

lightning/src/routing/router.rs Outdated Show resolved Hide resolved
@tnull tnull force-pushed the 2023-07-max-total-fee branch 3 times, most recently from ed0ade8 to da559d3 Compare July 18, 2023 12:28
@tnull tnull marked this pull request as ready for review July 18, 2023 12:42
@tnull tnull force-pushed the 2023-07-max-total-fee branch 3 times, most recently from c34a4a9 to be27e7f Compare July 18, 2023 14:07
lightning/src/ln/outbound_payment.rs Outdated Show resolved Hide resolved
lightning/src/routing/router.rs Outdated Show resolved Hide resolved
lightning/src/routing/router.rs Outdated Show resolved Hide resolved
lightning/src/routing/router.rs Outdated Show resolved Hide resolved
lightning/src/routing/router.rs Show resolved Hide resolved
@valentinewallace
Copy link
Contributor

LGTM after squash

@tnull
Copy link
Contributor Author

tnull commented Jul 18, 2023

LGTM after squash

Squashed fixups without further changes.

lightning/src/routing/router.rs Outdated Show resolved Hide resolved
lightning/src/routing/router.rs Outdated Show resolved Hide resolved
lightning/src/routing/router.rs Outdated Show resolved Hide resolved
@tnull
Copy link
Contributor Author

tnull commented Jul 20, 2023

Rebased on main to get rid of the CI failure.

@tnull tnull removed this from the 0.0.116 milestone Jul 21, 2023
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.

We should land this to get the API in place for a second alpha, but we should get some of these comments fixed (at least the total-fee final check one) for 0.0.117.

// subtracting the used fee from the total fee budget.
route_params.max_total_routing_fee_msat = route_params
.max_total_routing_fee_msat.map(|m| m.saturating_sub(total_ok_fees_msat));
route_params.final_value_msat = pending_amt_unsent;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Not a regression, but this is wrong - this includes any overpayment amount, ensuring we overpay again when we go to retry, rather than just recalculating the amount sent against the intended amount. Lets definitely fix this ASAP.

Copy link
Contributor Author

@tnull tnull Sep 27, 2023

Choose a reason for hiding this comment

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

(nevermind my previous reply): Tacked a corresponding commit onto #2575.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, thinking about this once more, I think my previous comment was kinda correct: if we find a route and need to overpay for some of the paths, we may reduce the value of the others to meet the final value, i.e., the route's split is essentially determined at this time. If now some of the overpaid paths fail and we start to only retry the net. value (excluding overpaid amounts), the sum of all paths might not meet the expected final value. I suspect this is no issue if we have infinite auto-retries as we'd retry until the expected value is met, but if we're limited we probably will run out of retry attempts sooner than expected/necessary. Not sure how we'd want to resolve this?

Copy link
Collaborator

Choose a reason for hiding this comment

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

My point here is we don't need to think about paths in this specific case - we tried to send a payment for route.route_params.final_value_msat and we now have route.get_total_amount() - pending_amt_unsent pending. We should retry pending_amt_unsent - (route.get_total_amount() - route.route_params.final_value_msat).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My point here is we don't need to think about paths in this specific case - we tried to send a payment for route.route_params.final_value_msat and we now have route.get_total_amount() - pending_amt_unsent pending. We should retry pending_amt_unsent - (route.get_total_amount() - route.route_params.final_value_msat).

Right, then we seem to be on the same page that per-path accounting doesn't make sense, but only to account for the overpayment for the whole route.

I think what you propose unfortunately doesn't work as is though as for the immediate/legacy retry case we'd recursively reduce final_value_msat while we don't do this in the newer auto-retry case where we'd leave final_value_msat static, but account for for the pending amounts differently. In d7ab0ef I solved this by taking the min, but this seems to be pretty hacky.

lightning/src/routing/router.rs Show resolved Hide resolved
lightning/src/routing/router.rs Show resolved Hide resolved
lightning/src/ln/payment_tests.rs Show resolved Hide resolved
@@ -159,6 +166,142 @@ fn mpp_retry() {
claim_payment_along_route(&nodes[0], &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]], false, payment_preimage);
}

#[test]
fn mpp_retry_overpay() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm, this is nice but isn't what I was mostly interested about. I'm really curious to test overpaying the final recipient and ensuring the route_params' final_value_msat is correct, not just the remaining fees.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, had understood above request slightly differently. IIUC, this might now more or less be covered by the new test introduced in #2604?

Copy link
Collaborator

Choose a reason for hiding this comment

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

That test tests the router end of it, but I'm really worried about the outbound_payment part overpaying.

@TheBlueMatt TheBlueMatt merged commit 1ac53ed into lightningdevkit:main Sep 26, 2023
14 of 15 checks passed
@TheBlueMatt TheBlueMatt mentioned this pull request Sep 26, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 27, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 27, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 27, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 27, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 27, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 27, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 27, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 27, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 28, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 28, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 28, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 28, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 28, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 28, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 28, 2023
tnull added a commit to tnull/rust-lightning that referenced this pull request Sep 28, 2023
TheBlueMatt pushed a commit to TheBlueMatt/rust-lightning that referenced this pull request Sep 28, 2023
TheBlueMatt pushed a commit to TheBlueMatt/rust-lightning that referenced this pull request Sep 28, 2023
TheBlueMatt added a commit that referenced this pull request Sep 28, 2023
slanesuke pushed a commit to slanesuke/rust-lightning that referenced this pull request Oct 6, 2023
slanesuke pushed a commit to slanesuke/rust-lightning that referenced this pull request Oct 6, 2023
optout21 pushed a commit to optout21/rust-lightning that referenced this pull request Nov 4, 2023
optout21 pushed a commit to optout21/rust-lightning that referenced this pull request Nov 4, 2023
benthecarman added a commit to benthecarman/rust-lightning that referenced this pull request Nov 8, 2023
In lightningdevkit#2417 support for setting a max fee was added but it is not exposed
when using the `pay_invoice` utility functions. This makes it so the
user can pass in that parameter. One downside is that this removes the
defualt value form before.
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.

Add another test for max_total_routing_fee_msat Allow to set maximum total fees during routing
6 participants