Skip to content

Avoid generating unpayable routes due to balance restrictions #2312

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

Merged

Conversation

TheBlueMatt
Copy link
Collaborator

@TheBlueMatt TheBlueMatt commented May 21, 2023

When calculating the amount available to send for the next HTLC, if
we over-count we may create routes which are not actually usable.

Historically this has been an issue, constructing routes that aren't payable and leading to spurious payment failures.

Here we fix that issue across several commits, ultimately removing the entire can-we-send complexity in send_htlc entirely.

Fixes #1126.
Fixes #2252.

TheBlueMatt and others added 4 commits May 21, 2023 19:05
While its nice to be able to push an HTLC which spends balance that
is removed in our local commitment transaction but awaiting an RAA
from our peer for final removal its by no means a critical feature.

Because peers should really be sending RAAs quickly after we send
a commitment, this should be an exceedingly rare case, and we
already don't expose this as available balance when routing, so
this isn't even made available when sending, only forwarding.

Note that `test_pending_claimed_htlc_no_balance_underflow` is
removed as it tested a case which was only possible because of this
and now is no longer possible.
In the coming commits we redo our next-HTLC-available logic which
requires some minor test changes for tests which relied on
calculating routes which were not usable.

Here we do a minor prefactor to simplify a test which now no longer
requires later changes.
When calculating the amount available to send for the next HTLC, if
we over-count we may create routes which are not actually usable.

Historically this has been an issue, which we resolve over a few
commits.

Here we include the cost of the commitment transaction fee in our
calculation, subtracting the commitment tx fee cost from the
available as we do in `send_payment`.

We also add some testing when sending to ensure that send failures
are accounted for in our balance calculations.

This commit is based on original work by
Gleb Naumenko <[email protected]> and modified by
Matt Corallo <[email protected]>.
When calculating the amount available to send for the next HTLC, if
we over-count we may create routes which are not actually usable.

Historically this has been an issue, which we resolve over a few
commits.

Here we consider the number of in-flight HTLCs which we are allowed
to push towards a counterparty at once, setting the available
balance to zero if we cannot push any further HTLCs.

We also add some testing when sending to ensure that send failures
are accounted for in our balance calculations.
@TheBlueMatt TheBlueMatt added this to the 0.0.116 milestone May 21, 2023
@TheBlueMatt TheBlueMatt force-pushed the 2023-05-next-htlc-min-max branch from 40f3f28 to a3246d1 Compare May 21, 2023 19:14
@tnull tnull self-requested a review May 21, 2023 19:29
@TheBlueMatt TheBlueMatt force-pushed the 2023-05-next-htlc-min-max branch from a3246d1 to cf8ce3b Compare May 23, 2023 01:20
@codecov-commenter
Copy link

codecov-commenter commented May 23, 2023

Codecov Report

Patch coverage: 100.00% and project coverage change: +0.64 🎉

Comparison is base (6775b95) 90.81% compared to head (4bc3a79) 91.45%.

❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2312      +/-   ##
==========================================
+ Coverage   90.81%   91.45%   +0.64%     
==========================================
  Files         104      104              
  Lines       53009    64073   +11064     
  Branches    53009    64073   +11064     
==========================================
+ Hits        48139    58600   +10461     
- Misses       4870     5473     +603     
Impacted Files Coverage Δ
lightning/src/ln/channel.rs 92.25% <100.00%> (+2.45%) ⬆️
lightning/src/ln/channelmanager.rs 89.92% <100.00%> (+2.82%) ⬆️
lightning/src/ln/functional_tests.rs 98.97% <100.00%> (+0.73%) ⬆️
lightning/src/ln/payment_tests.rs 99.01% <100.00%> (+1.44%) ⬆️
lightning/src/routing/router.rs 93.00% <100.00%> (-0.05%) ⬇️

... and 50 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Do you have feedback about the report comment? Let us know in this issue.

@wpaulino wpaulino self-requested a review May 23, 2023 03:09
@dunxen dunxen self-requested a review May 23, 2023 09:53
@TheBlueMatt TheBlueMatt force-pushed the 2023-05-next-htlc-min-max branch from 885af49 to 559e4be Compare June 5, 2023 17:36
@TheBlueMatt TheBlueMatt force-pushed the 2023-05-next-htlc-min-max branch from 559e4be to d297ca0 Compare June 6, 2023 23:10
Copy link
Contributor

@wpaulino wpaulino left a comment

Choose a reason for hiding this comment

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

LGTM after squash.

When calculating the amount available to send for the next HTLC, if
we over-count we may create routes which are not actually usable.

Historically this has been an issue, which we resolve over a few
commits.

Here we consider whether one additional HTLC's commitment tx fees
would result in the counterparty's commitment tx fees being greater
than the reserve we've picked for them and, if so, limit our next
HTLC value to only include dust HTLCs.

We also add some testing when sending to ensure that send failures
are accounted for in our balance calculations.

This, and the previous few commits, fixes lightningdevkit#1126.
In the coming commits, in order to ensure all routes we generate
are usable, we'll start calculating the next-HTLC minimum for our
channels and using it in the router. Here we set this up by adding
an always-0 field for it in `ChannelDetails` and use it when
routing.
When calculating the amount available to send for the next HTLC, if
we over-count we may create routes which are not actually usable.

Historically this has been an issue, which we resolve over a few
commits.

Here we consider how much adding one additional (dust) HTLC would
impact our total dust exposure, setting the new next-HTLC-minimum
field to require HTLCs be non-dust if required or set our next-HTLC
maximum if we cannot send a dust HTLC but do have some additional
exposure remaining.

We also add some testing when sending to ensure that send failures
are accounted for in our balance calculations.

Fixes lightningdevkit#2252.
Now that the value available to send is expected to match the
success or failure of sending exactly, we should assert this in the
`chanmon_consistency` fuzzer.

In the next commit we'll actually rip the checks out of `send_htlc`
which will make this a somewhat less useful test, however fuzzing
on this specific commit can help to reveal bugs.
Now that the `get_available_balances` min/max bounds are exact, we
can stop doing all the explicit checks in `send_htlc` entirely,
instead comparing against the `get_available_balances` bounds and
failing if the amount is out of those bounds.

This breaks support for sending amounts below the dust limit if
there is some amount of dust exposure remaining before we hit our
cap, however we will no longer generate such routes anyway.
@TheBlueMatt TheBlueMatt force-pushed the 2023-05-next-htlc-min-max branch from d297ca0 to 4bc3a79 Compare June 6, 2023 23:58
@TheBlueMatt
Copy link
Collaborator Author

Squashed without further changes:

$ git diff-tree -U1 d297ca0c 4bc3a793
$

@TheBlueMatt TheBlueMatt merged commit f068df0 into lightningdevkit:main Jun 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants