Commit graph

2461 commits

Author SHA1 Message Date
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
Matt Corallo
4ba83381b1 Construct from-message HTLCFailReason via a constructor method 2022-12-01 19:18:16 +00:00
Matt Corallo
fe3cf29595 Fix impl_writeable_tlv_based_enum to not require DecodeError
`impl_writeable_tlv_based_enum` shouldn't be assuming that
`DecodeError` is in scope, which we address here.
2022-12-01 19:14:43 +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
Matt Corallo
4dafa43a75
Merge pull request #1880 from tcharding/11-29-move-lock-outside-loop
Do not lock while looping `htlcs_to_fail`
2022-12-01 18:03:35 +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
7203c8328a
Fix weird import format in persist 2022-12-01 00:22:44 -05:00
Valentine Wallace
d30122d32a
HTLC intercept test: swap hardcoded value for const 2022-12-01 00:16:31 -05:00
Valentine Wallace
7858010dfc
Test for unknown HTLC intercept id error 2022-12-01 00:13:54 -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
Matt Corallo
5e577cb94a
Merge pull request #1862 from valentinewallace/2022-11-chanman-retries-prep
Prepare for Payment Retries in `ChannelManager`
2022-12-01 04:24:10 +00:00
Tobin C. Harding
1dd3184805 Do not lock while looping htlcs_to_fail
Currently we loop over `htlcs_to_fail` locking `channel_state` for each
element only to call `get_htlc_inbound_temp_fail_err_and_data` with the
same inputs on each iteration. This is unnecessary, we can refactor and
call `get_htlc_inbound_temp_fail_err_and_data` outside of the loop.
2022-12-01 13:32:36 +11:00
Tobin C. Harding
c21378fa47 Make fail_htlc_backwards_internal borrow parameters
Currently `fail_htlc_backwards_internal` takes ownership of its source
and reason parameters however they are not consumed so we can borrow them.

Includes refactoring to use local variables before the function call.
2022-12-01 13:32:34 +11:00
Tobin C. Harding
555cb4024f Add constructors to HTLCFailReason
We create `HTLCFailReason` inline in function calls in a bunch of places
in the `channelmanager` module, we can make the code more terse with no
loss of clarity by implementing a couple of constructor methods.
2022-12-01 13:30:10 +11:00
Matt Corallo
fb6e018eb8
Merge pull request #1835 from valentinewallace/2022-11-jit-chan-htlc-intercept
Intercept HTLC forwards for JIT channels
2022-12-01 00:04:14 +00:00
Matt Corallo
2cfc1dbb44 Remove unreachable Err cases when constructing TxCreationKeys 2022-11-30 22:43:29 +00:00
Matt Corallo
5671d2930d Remove unreachable Err cases on derive_*_revocation_key
The `derive_{public,private}_revocation_key` methods hash the two
input keys and then multiply the two input keys by hashed values
before adding them together. Because addition can fail if the tweak
is the inverse of the secret key this method currently returns a
`Result`.

However, it is not cryptographically possible to reach the error
case - in order to create an issue, the point-multiplied-by-hash
values must be the inverse of each other, however each point
commits the SHA-256 hash of both keys together. Thus, because
changing either key changes the hashes (and the ultimate points
added together) in an unpredictable way, there should be no way to
construct such points.
2022-11-30 22:34:11 +00:00
Matt Corallo
27461902ab Remove unreachable Err cases on derive_{public,private}_key
The `derive_{public,private}_key` methods hash the two input keys
and then add them to the input public key. Because addition can
fail if the tweak is the inverse of the secret key this method
currently returns a `Result`.

However, it is not cryptographically possible to reach the error
case - in order to create an issue, the SHA-256 hash of the
`base_point` (and other data) must be the inverse of the
`base_point`('s secret key). Because changing the `base_point`
changes the hash in an unpredictable way, there should be no way to
construct such a `base_point`.
2022-11-30 22:21:24 +00:00
Valentine Wallace
8a51a792aa
Move DefaultRouter to router module 2022-11-30 16:29:57 -05:00
Valentine Wallace
3f9868f235
Move ScorerAccountingForInFlightHtlcs to router + make public
We move it to router instead of scoring because it pairs with the InFlightHtlcs
struct in router and is useful for custom Router trait implementations
2022-11-30 16:20:31 -05:00