Commit graph

741 commits

Author SHA1 Message Date
Valentine Wallace
682bb9b0ae
Parameterize Simple*ChannelManager with DefaultRouter and ProbScorer 2023-01-05 11:29:00 -05:00
Valentine Wallace
3a274e0415
Implement routing against the netgraph in tests 2023-01-05 11:23:45 -05:00
Valentine Wallace
2e06efe2ff
Parameterize ChannelManager by a Router trait
This will be used in upcoming work to fetch routes on-the-fly for payment
retries, which will no longer be the responsibility of InvoicePayer.
2023-01-03 15:34:14 -05:00
Valentine Wallace
8d5f7c87cb
Make add_new_pending_payment private to module
And expose it in testing only, for safety
2022-12-20 21:23:54 -05:00
Matt Corallo
f7211fbf79
Merge pull request #1910 from arik-so/2022-12-keys-interface-name-split
Split KeysInterface into EntropySource, NodeSigner, and SignerProvider
2022-12-20 22:19:43 +00:00
Arik Sosman
9d7bb73b59
Split out KeysInterface into EntropySource, NodeSigner, and SignerProvider. 2022-12-20 10:09:11 -08:00
Valentine Wallace
afdaa64b44
Rename send_payment and retry_payment for retries
Once ChannelManager supports payment retries, it will make more sense for its
current send_payment method to be named send_payment_with_route because
retrying should be the default. Here we get a head start on this by making the
rename in outbound_payment, but not changing the public interface yet.
2022-12-19 21:08:08 -05:00
Valentine Wallace
afd31507d9
Swap pending_outbound_payments for OutboundPayments struct
This allows us to move a lot of outbound payment logic out of ChannelManager
and into the new outbound_payment module, and helps avoid growing
ChannelManager when we add retry logic to it in upcoming work.
2022-12-19 21:04:53 -05:00
Valentine Wallace
278ebd208a
Move PaymentSendFailure into outbound_payment module
And re-export it in channelmanager.rs so it can remain public
2022-12-19 14:10:03 -05:00
Valentine Wallace
070832643f
Move PendingOutboundPayment to new outbound_payment module
We want to move all outbound payment-related things to this new module, to help
break up ChannelManager so future payment retries work doesn't increase the
size of ChannelManager.
2022-12-19 14:09:59 -05:00
Matt Corallo
2d6818376c Drop forwarded HTLCs which were still pending at persist-time
If, after forwarding an intercepted payment to our counterparty, we
restart with a ChannelMonitor update having been persisted, but the
corresponding ChannelManager update not having been persisted,
we'll still have the intercepted HTLC in the
`pending_intercepted_htlcs` map on start (and potentially a pending
`HTLCIntercepted` event). This will cause us to allow the user to
handle the forwarded HTLC twice, potentially double-forwarding it.

This builds on 0bb87ddad7, which
provided a preemptive fix for the general relay case (though it was
not an actual issue at the time). We simply check for the HTLCs
having been forwarded on startup and remove them from the map.

Fixes #1858
2022-12-13 19:33:58 +00:00
Matt Corallo
b291f4ab7a
Merge pull request #1907 from TheBlueMatt/2022-12-abandon-crash-reset
Note that abandon_payment does not persist the state update in docs
2022-12-12 22:16:43 +00:00
Matt Corallo
1969b48b7a Note that abandon_payment does not persist the state update in docs
If a user calls `abandon_payment`, then restarts without freshly
persisting the `ChannelManager`, the payment will still be pending
on restart. This was unclear from the docs (and the docs seemed to
imply otherwise). Because this doesn't materially impact the
usability of `abandon_payment` (users shouldn't be called
`retry_payment` on an abandoned one anyway), we simply document it.

Fixes #1804.
2022-12-12 19:59:19 +00:00
Matt Corallo
626c60600e
Merge pull request #1886 from TheBlueMatt/2022-11-claim-relock
Relock `channel_state` in for each HTLC in `claim_funds` and lay the groundwork for async event generation
2022-12-12 03:10:38 +00:00
Matt Corallo
616d3ac784 Add second TODO when claiming to mirror the existing TODO on claim fail 2022-12-08 21:24:26 +00:00
Matt Corallo
7c48151c22 Drop unused link in claim_funds 2022-12-08 21:24:26 +00:00
Matt Corallo
b331778e34 Drop now-unused ClaimFundsFromHop enum and replace with an Err 2022-12-08 21:24:26 +00:00
Matt Corallo
1feb459811 Handle claim result event generation in claim_funds_from_hop
Currently `claim_funds` and `claim_funds_internal` call
`claim_funds_from_hop` and then surface and `Event` to the user
informing them of the forwarded/claimed payment based on it's
result. In both places we assume that a claim "completed" even if
a monitor update is being done async.

Instead, here we push that event generation through a
`MonitorUpdateCompletionAction` and a call to
`handle_monitor_update_completion_action`. This will allow us to
hold the event(s) until async monitor updates complete in the
future.
2022-12-08 21:24:26 +00:00
Matt Corallo
def193d6bd Don't hold channel_state lock for entire duration of claim_funds
When `claim_funds` has to claim multiple HTLCs as a part of a
single MPP payment, it currently does so holding the
`channel_state` lock for the entire duration of the claim loop.
Here we swap that for taking the lock once for each HTLC. This
allows us to be more flexible with locks going forward, and
ultimately isn't a huge change - if our counterparty intends to
force-close a channel, us choosing to ignore it by holding the
`channel_state` lock for the duration of the claim isn't going to
result in a commitment update, it will just result in the preimage
already being in the `ChannelMonitor`.
2022-12-08 21:24:26 +00:00
Matt Corallo
7b77a016b5 Handle closed-chan HTLC claims in claim_funds_from_hop
Currently `claim_funds` does all HTLC claims in one `channel_state`
lock, ensuring that we always make claims from channels which are
open. It can thus avoid ever having to generate a
`ChannelMonitorUpdate` containing a preimage for a closed channel,
which we only do in `claim_funds_internal` (for forwarded payments).

In the next commit we'll change the locking of
`claim_funds_from_hop` so that `claim_funds` is no longer under a
single lock but takes a lock for each claim. This allows us to be
more flexible with locks going forward, and ultimately isn't a huge
change - if our counterparty intends to force-close a channel, us
choosing to ignore it by holding the `channel_state` lock for the
duration of the claim isn't going to result in a commitment update,
it will just result in the preimage already being in the
`ChannelMonitor`.
2022-12-08 21:24:26 +00:00
Matt Corallo
bffbd3784d Add support for handling "actions" after a monitor update completes
This adds a new enum, `MonitorUpdateCompletionAction` and a method
to execute the "actions". They are intended to be done once a
(potentially-async) `ChannelMonitorUpdate` persistence completes,
however this behavior will be implemented in a future PR. For now,
this adds the relevant infrastructure which will allow us to
prepare `claim_funds` for better monitor async handling.
2022-12-08 21:24:26 +00:00
Matt Corallo
27e59ef01a Store pending claims awaiting monitor update in a separate map
In the next commits we'll move to generating `PaymentClaimed`
events while handling `ChannelMonitorUpdate`s rather than directly
in line. Thus, as a prerequisite, here we move to storing the info
required to generate the `PaymentClaimed` event in a separate map.

Note that while this does introduce a new map which is written as
an even value which users cannot opt out of, the map is only filled
in when users use the asynchronous `ChannelMonitor` updates and
after a future PR. As these are still considered beta, breaking
downgrades for such users is considered acceptable in the future PR
(which will likely be one LDK version later).
2022-12-08 19:52:28 +00:00
Matt Corallo
d9d4611e65
Merge pull request #1863 from TheBlueMatt/2022-11-holding-cell-batch-update
Lean on the holding cell when batch-forwarding/failing HTLCs
2022-12-07 17:52:04 +00:00
Matt Corallo
2390dbcb22
Merge pull request #1895 from TheBlueMatt/2022-12-fix-missing-data
Fix some onion errors and assert their length is correct
2022-12-06 22:46:04 +00:00
Matt Corallo
c9fe69fa5f Correctly handle any UPDATE errors to phandom invoices
If we try to send any onion error with the `UPDATE` flag in
response to a phantom receipt, we should always swap it for
something generic that doesn't require a `channel_update` in it.
Here we use `temporary_node_failure`.

Test provided by Valentine Wallace <vwallace@protonmail.com>
2022-12-06 20:00:44 +00:00
Matt Corallo
01d299ecdb Replace build_first_hop_failure_packet with HTLCFailReason
This ensures we always hit our new debug assertions while building
failure packets in the immediately-fail pipeline while processing
an inbound HTLC.
2022-12-06 20:00:44 +00:00
Matt Corallo
6daf62fea3 Use temporary_node_failure for a phantom HTLC with bogus CLTV
When we receive a phantom HTLC with a bogus/modified CLTV, we
should fail back with `incorrect_cltv_expiry`, but that requires a
`channel_update`, which we cannot generate for a phantom HTLC which
has no corresponding channel. Thus, instead, we have to fall back
to `incorrect_cltv_expiry`.

Fixes #1879
2022-12-06 20:00:44 +00:00
Matt Corallo
be7b82b5b8 Correctly include the sha256_hash_of_onion field in BADONION errs
The spec mandates that we copy the `sha256_hash_of_onion` field
from the `UpdateFailMalformedHTLC` message into the error message
we send back to the sender, however we simply ignored it. Here we
copy it into the message correctly.
2022-12-06 20:00:44 +00:00
Matt Corallo
5e7e3d57bf Drop the stale final_expiry_too_soon error code
This replaces `final_expiry_too_soon` with
`incorrect_or_unknown_payment` as was done in
https://github.com/lightning/bolts/pull/608. Note that the
rationale for this (that it may expose whether you are the final
recipient for the payment or not) does not currently apply to us -
we don't apply different final CLTV values to different payments.
However, we might in the future, and this will make us slightly
more consistent with other nodes.
2022-12-06 20:00:44 +00:00
Matt Corallo
2485ef38c3 Move HTLCFailReason to onion_utils
Now that it's entirely abstracted, there's no reason for
`HTLCFailReason` to be in `channelmanager`, it's really an
onion-level abstraction.
2022-12-06 20:00:44 +00:00
Matt Corallo
36e6023633
Merge pull request #1902 from tnull/2022-12-payment-received-renaming-follow-up
Also rename variables referring to `PaymentClaimable`
2022-12-06 19:07:09 +00:00
Matt Corallo
8c8bb6d246 Move claimable_htlcs to a struct for more fields in the same mutex 2022-12-06 19:00:17 +00:00
Matt Corallo
aa9630b0cd Lean on the holding cell for commitments when updating fees
Like the previous commit, here we update the update_fee+commit
logic to simply push the fee update into the holding cell and then
use the standard holding-cell-freeing codepaths to actually send
the commitment update. This removes a substantial amount of code,
reducing redundant codepaths and keeping channel state machine
logic in channel.rs.
2022-12-06 18:18:26 +00:00
Matt Corallo
1531378040 Free the holding cells during background timer ticks
We currently free the channel holding cells in
`get_and_clear_pending_msg_events`, blocking outbound messages
while we do so. This is fine, but may block the message pipeline
longer than we need to. In the next commit we'll push
timer-originating channel fee updates out through the holding cell
pipeline, leaning more on that freeing in the future.

Thus, to avoid a regression in message time, here we clear the
holding cell after processing all timer events. This also avoids
needing to change tests in the next commit.
2022-12-06 18:18:26 +00:00
Matt Corallo
cae7c8180b Lean on the holding cell when batch-forwarding/failing HTLCs
When we batch HTLC updates, we currently do the explicit queueing
plus the commitment generation in the `ChannelManager`. This is a
bit strange as its ultimately really a `Channel` responsibility to
generate commitments at the correct time, with the abstraction
leaking into `ChannelManager` with the `send_htlc` and
`get_update_fail_htlc` method docs having clear comments about
how `send_commitment` MUST be called prior to calling other
`Channel` methods.

Luckily `Channel` already has an update queue - the holding cell.
Thus, we can trivially rewrite the batch update logic as inserting
the desired updates into the holding cell and then asking all
channels to clear their holding cells.
2022-12-06 18:18:26 +00:00
Matt Corallo
5588eeb06b
Merge pull request #1867 from wpaulino/remove-signer-persistence
Re-derive signers instead of persisting them
2022-12-06 18:13:49 +00:00
Elias Rohrer
e09ca27c71
Rename variables referring to PaymentClaimable 2022-12-06 10:47:07 +01:00
Matt Corallo
f4ab077a69
Merge pull request #1857 from TheBlueMatt/2022-11-reload-htlc
Fail HTLCs which were removed from a channel but not persisted
2022-12-05 22:54:08 +00:00
Matt Corallo
dbe4aadb89 Fail HTLCs which were removed from a channel but not persisted
When a channel is force-closed, if a `ChannelMonitor` update is
completed but a `ChannelManager` persist has not yet happened,
HTLCs which were removed in the latest (persisted) `ChannelMonitor`
update will not be failed even though they do not appear in the
commitment transaction which went on chain. This is because the
`ChannelManager` thinks the `ChannelMonitor` is responsible for
them (as it is stale), but the `ChannelMonitor` has no knowledge of
the HTLC at all (as it is not stale).

The fix for this is relatively simple - we need to check for this
specific case and fail back such HTLCs when deserializing a
`ChannelManager`
2022-12-05 20:27:35 +00:00
Matt Corallo
0bb87ddad7 Avoid attempting to forward to a closed chan on stale-data reload
If, after forwarding a payment to our counterparty, we restart with
a ChannelMonitor update having been persisted, but the
corresponding ChannelManager update was not persisted, we'll still
have the forwarded HTLC in the `forward_htlcs` map on start. This
will cause us to generate a (spurious) `PendingHTLCsForwardable`
event. However, when we go to forward said HTLC, we'll notice the
channel has been closed and leave it up to the `ChannelMontior` to
finalize the HTLC.

This is all fine today - we won't lose any funds, we'll just
generate an excess forwardable event and then fail to forward.
However, in the future when we allow for forward-time channel
changes this could break. Thus, its worth adding tests for this
behavior today, and, while we're at it, removing the spurious
forwardable HTLCs event.
2022-12-05 20:27:35 +00:00
Wilmer Paulino
444fce71f4
Remove unnecessary byte_utils helpers
Now that to_be_bytes is available under our current MSRV of 1.41, we
can use it instead of our own version.
2022-12-05 12:11:38 -08:00
Wilmer Paulino
607cd6f523
Drop Clone requirement from Sign
Now that we opt to always re-derive channel secrets whenever required,
we can drop the Clone requirement from Sign.
2022-12-05 12:11:35 -08:00
Matt Corallo
de2acc0ee0
Merge pull request #1891 from tnull/2022-12-rename-payment-events
Rename `PaymentReceived` to `PaymentClaimable`
2022-12-04 19:31:52 +00:00
Matt Corallo
52edb35157
Merge pull request #1893 from valentinewallace/2022-12-jit-forwards-followup
HTLC JIT channel interception followup + minor cleanups
2022-12-01 21:51:39 +00:00
Matt Corallo
4ba83381b1 Construct from-message HTLCFailReason via a constructor method 2022-12-01 19:18:16 +00:00
Matt Corallo
6c984bf50d Decode HTLCFailReasons in a util method on the enum 2022-12-01 19:08:53 +00:00
Matt Corallo
9a2e26b9b7 Encode HTLC failure packets in a util method on HTLCFailReason 2022-12-01 18:56:17 +00:00
Elias Rohrer
22d74bf28b
Rename PaymentReceived to PaymentClaimable 2022-12-01 09:39:33 +01:00
Valentine Wallace
e0820aee43
Rename APIError::RouteError to ::InvalidRoute
Soon we're going to need to return an error when ChannelManager is unable to
find a route, so we'll need a way to distinguish between that and the user
supplying an invalid route.
2022-12-01 01:08:57 -05:00
Valentine Wallace
6791d2c307
Clean up HTLC intercept errors
ChannelUnavailable is a better fit for errors regarding unavailable channels
than APIMisuseError.

Also log bytes in errors as hex instead of decimal.
2022-12-01 00:12:32 -05:00