Commit graph

4400 commits

Author SHA1 Message Date
Matt Corallo
02235782f0 Drop excess mut on OnchainTxHandler::generate_external_htlc_claim 2022-12-07 23:17:58 +00:00
Matt Corallo
60f3c0a206 DRY the comparison blocks in update_claims_view_from_matched_txn
In `update_claims_view_from_matched_txn` we have two different
tx-equivalence checks which do the same thing - both check that the
tx which appeared on chain spent all of the outpoints which we
intended to spend in a given package. While one is more effecient
than the other (but only usable in a subset of cases), the
difference between O(N) and O(N^2) when N is 1-5 is trivial.

Still, it is possible we hit this code with just shy of 900 HTLC
outputs in a channel, and a transaction with a ton of inputs.

While having to spin through a few million entries if our
counterparty wastes a full block isn't really a big deal, we go
ahead and use a sorted vec and binary searches because its trivial.
2022-12-07 23:17:58 +00:00
Matt Corallo
399b3eb3f7 Use PackageId rather than Txid in OnchainEvent::Claim
In 19daccf7fb5ea81c8d235c1628a91efe0aa07b96, a `PackageId` type was
added to differentiate between an opaque Id for packages and the
`Txid` type which was being used for that purpose. It, however,
failed to also replace the single inner field in
`OnchainEvent::Claim` which was also a package ID. We do so here.
2022-12-07 23:17:58 +00:00
Wilmer Paulino
b5784803c6
Use even types for opt_anchors
This prevents downgrading to older versions of LDK that are not capable
of supporting anchor channels when the field is serialized (i.e.,
opt_anchors is `Some`).
2022-12-07 10:35:54 -08: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
eea56e967f
Merge pull request #1825 from wpaulino/anchors-bump-htlc-resolution-event
Introduce new BumpTransactionEvent variant HTLCResolution
2022-12-07 05:51:27 +00:00
Wilmer Paulino
ec1f3346c1
Extend BaseSign with HTLC output signing support for external claims 2022-12-06 16:48:25 -08:00
Wilmer Paulino
8170c84d9d
Yield BumpHTLCResolution events 2022-12-06 16:48:24 -08:00
Wilmer Paulino
c17ce2e593
Expose HTLC transaction construction helpers 2022-12-06 16:48:23 -08:00
Wilmer Paulino
0aaba2ce45
Rename set_equality within update_claims_view_from_matched_txn 2022-12-06 16:48:21 -08:00
Wilmer Paulino
d0847bd0c6
Generate ClaimEvent for HolderHTLCOutput inputs from anchor channels 2022-12-06 16:48:20 -08:00
Wilmer Paulino
68741263ae
Introduce internal package ID to track pending claims
Now that our txids will no longer be stable for package claims that
require external funds to be allocated, we transition to a 32-byte array
identifier to remain compatible with them.
2022-12-06 16:48:14 -08:00
Wilmer Paulino
d7027c2d5b
Support HolderHTLCOutput inputs from anchor channels 2022-12-06 16:48:13 -08:00
Wilmer Paulino
baf2dec06b
Specify amount units in HolderHTLCOutput
This is only a name change, there is no change in behavior.
2022-12-06 16:48:07 -08:00
Wilmer Paulino
d618bf125e
Update HTLC transaction detection from revoked counterparty commitments
Previously, this method assumed that all HTLC transactions have 1 input
and 1 output, with the sole input having a witness of 5 elements. This
will no longer be the case for HTLC transactions on channels with
anchors outputs since additional inputs and outputs can be attached to
them to allow fee bumping.
2022-12-06 16:48:02 -08:00
Wilmer Paulino
af02d10c3b
Track HTLC resolving transaction to determine input index 2022-12-06 16:47:59 -08: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
8ec1480724 Assert that all onion error messages are correct len in tests
When we're constructing an HTLCFailReason, we should check that we
set the data to at least the correct length for the given failure
code, which we do here.
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
4011db57f7 Encapsulate HTLCFailReason to not expose struct variants
Now that `HTLCFailReason` is opaque and in `onion_utils`, we should
encapsulate it so that `ChannelManager` can no longer directly
access its inner fields.
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
18330702f2 Correct confusing docs on Channel methods
The methods return `Ok(())` always, they just happen to never
return in the case of a duplicate claim if debug assertions are
enabled.
2022-12-06 18:18:26 +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
Matt Corallo
e142f67917 Expose the full set of outbound HTLCs from a ChannelMonitor
This expands the outbound-HTLC-listing support in `ChannelMonitor`
to include not only the set of outbound HTLCs which have not yet
been resolved but to also include the full set of HTLCs which the
`ChannelMonitor` is currently able to to or has already finalized.

This will be used in the next commit to fail-back HTLCs which were
removed from a channel in the ChannelMonitor but not in a Channel.
Using the existing `get_pending_outbound_htlcs` for this purpose is
subtly broken - if the channel is already closed, an HTLC fail may
have completed on chain and is no longer "pending" to the monitor,
but the fail event is still in the monitor waiting to be handed
back to the `ChannelMonitor` when polled.
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
Wilmer Paulino
215619bace
Avoid use of OnlyReadsKeysInterface
Since `ChannelMonitor`s will now re-derive signers rather than
persisting them, we can no longer use the OnlyReadsKeysInterface
concrete implementation.
2022-12-05 12:11:33 -08:00
Wilmer Paulino
bfc848e892
Re-derive signers upon deserializing OnchainTxHandler
Similar to the previous commit, we introduce a new serialization version
that doesn't store a monitor's signer. Since the monitor already knows
of a channel's `channel_keys_id`, there's no need to store any new data
to re-derive all private key material for said channel.
2022-12-05 12:11:31 -08:00
Wilmer Paulino
f053860f2e
Re-derive signers upon deserializing Channel
To do so, we introduce a new serialization version that doesn't store a
channel's signer, and instead stores its signer's `channel_keys_id`.
This is a unique identifier that can be provided to our `KeysInterface`
to re-derive all private key material for said channel.

We choose to not upgrade the minimum compatible serialization version
until a later time, which will also remove any signer serialization
logic on implementations of `KeysInterface` and `Sign`.
2022-12-05 12:11:28 -08:00
Wilmer Paulino
648a69a1e3
Rename KeysInterface ready_channel to provide_channel_parameters
Now that ready_channel is also called on startup upon deserializing
channels, we opt to rename it to a more indicative name.

We also derive `PartialEq` on ChannelTransactionParameters to allow
implementations to determine whether `provide_channel_parameters` calls
are idempotent after the channel parameters have already been provided.
2022-12-05 12:11:26 -08:00
Wilmer Paulino
b04d1b868f
Split KeysInterface::get_channel_signer into two
`get_channel_signer` previously had two different responsibilites:
generating unique `channel_keys_id` and using said ID to derive channel
keys. We decide to split it into two methods `generate_channel_keys_id`
and `derive_channel_signer`, such that we can use the latter to fulfill
our goal of re-deriving signers instead of persisting them. There's no
point in storing data that can be easily re-derived.
2022-12-05 12:11:23 -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
Elias Rohrer
c17e9fb1b3
Update docs and add pending changelog 2022-12-04 17:44:39 +01:00
Matt Corallo
14d2e97965
Merge pull request #1887 from TheBlueMatt/2022-11-definitely-valid
Remove cryptographically unreachable error conditions
2022-12-03 19:01:15 +00:00
Jeffrey Czyz
3e17e7f9bb
Remove unused mut from OfferBuilder::amount_msats
Seen when removing `#[allow(unused)]` from `offers` module.
2022-12-02 15:28:02 -08:00
Jeffrey Czyz
4b8b17d72f
Reduce visibility for offer auxiliary types 2022-12-02 15:26:27 -08: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