Commit graph

2641 commits

Author SHA1 Message Date
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
Matt Corallo
2f0ddf0763
Merge pull request #1839 from ariard/2022-11-increase-visibility-helpers
Chan_utils helpers visibility relaxation
2022-11-30 18:56:15 +00:00
Valentine Wallace
acff8f6353
Don't forward HTLC intercepts over unestablished channels 2022-11-30 12:52:23 -05:00
Valentine Wallace
7809c5515c
Automatically fail intercepts back on timeout 2022-11-30 12:52:23 -05:00
Valentine Wallace
ddcd9b0463
Add config knob for forwarding intercept payments 2022-11-30 12:52:23 -05:00
Valentine Wallace
f79ad2efb1
Allow failing back intercepted HTLCs
Co-authored-by: John Cantrell <johncantrell97@gmail.com>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2022-11-30 12:52:23 -05:00
Valentine Wallace
c1f1b78ea6
Utils for forwarding intercepted htlcs + getting intercept scids
See ChannelManager::forward_intercepted_htlc and
ChannelManager::get_intercept_scid for details

Co-authored-by: John Cantrell <johncantrell97@gmail.com>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2022-11-30 12:52:23 -05:00
Valentine Wallace
8fe7cbe921
Generate HTLCIntercepted event upon interceptable forward
And store the pending intercepted HTLC in pending_intercepted_htlcs

Co-authored-by: John Cantrell <johncantrell97@gmail.com>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2022-11-30 12:52:23 -05:00
Valentine Wallace
5efc1976cd
Add HTLCIntercepted event
Used in upcoming commit(s) so users can intercept forwarded HTLCs

Co-authored-by: John Cantrell <johncantrell97@gmail.com>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2022-11-30 12:52:17 -05:00
Valentine Wallace
3a1268e177
Add fake scid namespace for intercepted HTLCs
This is useful for LSPs who wish to create a just-in-time channel for end users
receiving a lightning payment. These fake scids will be encoded into route
hints in end user invoices, and signal to LDK to create an event triggering the
JIT channel, after which the payment will be received.

Co-authored-by: John Cantrell <johncantrell97@gmail.com>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2022-11-30 12:43:09 -05:00
Valentine Wallace
129e1f6be2
Persist pending intercepted htlcs in ChannelManager
No htlcs are intercepted yet, that will be added in upcoming commit(s)

Co-authored-by: John Cantrell <johncantrell97@gmail.com>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2022-11-30 12:43:09 -05:00
Matt Corallo
3e88b72c50 Drop unnecessary clone 2022-11-30 05:48:37 +00:00
Matt Corallo
e7ba10383b Drop useless SCID lookup in claim_funds_from_hop
We have the channel_id available in `prev_hop` so there's no reason
to look it up by SCID.
2022-11-30 03:04:19 +00:00