Commit graph

5721 commits

Author SHA1 Message Date
Jeffrey Czyz
faf8367e9a
Require a PaymentContext in payment::ReceiveTlvs
UnknownPaymentContext is used when payment::ReceiveTlvs doesn't contain
a PaymentContext. This is only needed for a legacy BlindedPaymentPath.
Since these paths a short-lived, UnknownPaymentContext is no longer
needed. Remove it and require that payment::ReceiveTlvs always contains
a PaymentContext.

Any such path would fail authentication since the payment::ReceiveTlvs
would be missing an HMAC and Nonce, so this is a good time to remove
UnknownPaymentContext.
2024-12-13 09:26:03 -06:00
Jeffrey Czyz
62cdf5d60b
Verify that an HTLC's ReceiveTlvs is authentic
When receiving a payment over a BlindedPaymentPath, a PaymentContext is
included but was not authenticated. The previous commit adds an HMAC of
the payment::ReceiveTlvs (which contains the PaymentContext) and the
nonce used to create the HMAC. This commit verifies the authenticity
when parsing the InboundOnionPayload. This prevents a malicious actor
from for forging it.
2024-12-13 09:26:03 -06:00
Jeffrey Czyz
55c02fdee1
Include HMAC and Nonce in payment::ReceiveTlvs
In order to authenticate a PaymentContext, an HMAC and Nonce must be
included along with it in payment::ReceiveTlvs. Compute the HMAC when
constructing a BlindedPaymentPath and include it in the recipient's
BlindedPaymentTlvs. Authentication will be added in an upcoming commit.
2024-12-13 09:26:03 -06:00
Jeffrey Czyz
a29153025f
Remove KeyMaterial
Now that NodeSigner::get_inbound_payment_key returns an ExpandedKey
instead of KeyMaterial, the latter is no longer needed. Remove
KeyMaterial and replace its uses with [u8; 32].
2024-12-13 09:26:02 -06:00
Jeffrey Czyz
09bec6eee9
Return ExpandedKey from NodeSigner
NodeSinger::get_inbound_payment_key_material returns KeyMaterial, which
is used for constructing an ExpandedKey. Change the trait to return an
ExpandedKey directly instead. This allows for direct access to the
ExpandedKey when a NodeSigner referenced is available. Otherwise, it
would either need to be reconstructed or passed in separately.
2024-12-13 09:25:56 -06:00
Jeffrey Czyz
bd0dd9b9a8
HMAC construction/verification for ReceiveTlvs
When receiving a PaymentContext from a blinded payment, the context must
be authenticated. Otherwise, the context can be forged and would appear
within a PaymentPurpose. Add functions for constructing and verifying an
HMAC for the ReceiveTlvs, which contains the PaymentContext.
2024-12-12 13:07:31 -06:00
Matt Corallo
830a027cb9
Merge pull request #3365 from TheBlueMatt/2024-10-commitment-point-init
Set `holder_commitment_point` to `Available` on upgrade
2024-12-12 15:09:43 +00:00
Elias Rohrer
91ae8b87ca
Define MigratableKVStore trait with list_all_keys method
.. which will be used for generic `KVStore`-to-`KVStore` migration
logic.
2024-12-12 13:31:47 +01:00
Elias Rohrer
6f5f249f35
Merge pull request #3482 from morehouse/fix_rebroadcast_logging
Fix incorrect logging during RBF or rebroadcast
2024-12-12 12:43:50 +01:00
Matt Morehouse
94ad5beacb
Fix incorrect logging during RBF or rebroadcast
We were incorrectly logging RBF'd transactions as not RBF'd and vice
versa.
2024-12-12 02:49:17 -06:00
Arik
641e40f69d
Merge pull request #3430 from valentinewallace/2024-11-remove-old-send-api
Remove deprecated `send_payment_with_route` API and friends
2024-12-11 20:13:37 -08:00
Matt Corallo
98a46ac303
Merge pull request #3452 from arik-so/stop-disbursement-underflow
Detect underflows in `build_closing_transaction`
2024-12-11 23:04:10 +00:00
Matt Corallo
9728e5140a
Merge pull request #3453 from morehouse/defer_claims_at_next_height
Fix premature claims broadcast
2024-12-11 22:25:24 +00:00
Matt Corallo
e0d81ca1af
Merge pull request #3458 from shaavan/i3455
Include counterparty node ids in PaymentForwarded
2024-12-11 21:18:00 +00:00
shaavan
29eace9149 Update test utils 2024-12-11 23:00:33 +05:30
shaavan
6e7367826e Include Counterparty Node IDs in PaymentForwarded
This commit adds counterparty node IDs to `PaymentForwarded`
to address the potential ambiguity of using `ChannelIds` alone,
especially in cases like v1 0conf opens where `ChannelIds`
may not be unique. Including the counterparty node IDs
provides better clarity and makes the information more useful.
2024-12-11 23:00:32 +05:30
Matt Morehouse
463ba153ec
Fix premature claims broadcast
A claim transaction with locktime T can only be mined at block heights
of T+1 or above, so it should only be broadcast at height T or above.
Due to an off-by-one bug, we were broadcasting some claim transactions
too early at T-1.

AFAICT, nothing bad resulted from this bug -- later rebroadcasts of the
transaction would eventually succeed once the correct height was
reached.
2024-12-11 10:25:08 -06:00
Elias Rohrer
9c9b4a8df7
Use lightning::sync via symlink in lightning-liquidity
.. to which end we also need to make some additions to `debug_sync` and
`nostd_sync`.
2024-12-11 17:01:04 +01:00
Elias Rohrer
3a8e1bffcc
Use LDK-internal Hash{Map,Set} types in lightning-liquidity 2024-12-11 17:01:03 +01:00
valentinewallace
ddeaab68d4
Merge pull request #3340 from wvanlint/claim_batching
Batch on-chain claims more aggressively per channel
2024-12-11 09:52:15 -05:00
Arik Sosman
a652980045
Force-close channels on underflow
Following up on the previous commit, where we added debug_asserts
within `build_closing_transaction` to ensure neither
`value_to_holder` nor `value_to_counterparty` underflow, we now also
force-close the channel in the (presumably impossible) event that it
did happen.
2024-12-10 21:40:58 -08:00
Willem Van Lint
0fe90c6f7c Batch on-chain claims more aggressively per channel
When batch claiming was first added, it was only done so for claims
which were not pinnable, i.e. those which can only be claimed by us.

This was the conservative choice - pinning of outputs claimed by a batch
would leave the entire batch unable to confirm on-chain. However, if
pinning is considered an attack that can be executed with a high
probability of success, then there is no reason not to batch claims of
pinnable outputs together, separate from unpinnable outputs.

Whether specific outputs are pinnable can change over time - those that
are not pinnable will eventually become pinnable at the height at which
our counterparty can spend them. Outputs are treated as pinnable if
they're within `COUNTERPARTY_CLAIMABLE_WITHIN_BLOCKS_PINNABLE` of that
height.

Aside from outputs being pinnable or not, locktimes are also a factor
for batching claims. HTLC-timeout claims have locktimes fixed by the
counterparty's signature and thus can only be aggregated with other
HTLCs of the same CLTV, which we have to check for.

The complexity required here is worth it - aggregation can save users a
significant amount of fees in the case of a force-closure, and directly
impacts the number of UTXOs needed as a reserve for anchors.

Co-authored-by: Matt Corallo <git@bluematt.me>
2024-12-10 19:39:56 -08:00
Matt Corallo
a688f1cca2
Merge pull request #3450 from TheBlueMatt/2024-12-no-prune-with-preimages
Ensure monitors are not archived if they have a preimage we need
2024-12-11 03:19:45 +00:00
Valentine Wallace
bcaba29f92
Remove deprecated send_payment_with_route usage from fuzzing
This allows us to make the PaymentSendFailure error type private, as well as
reduce the visibility of the vestigial send_payment_with_route method that was
already made test and fuzz-only in a previous commit.
2024-12-10 15:24:23 -05:00
Valentine Wallace
66bdc62712
Stop using PaymentSendFailure within ProbeSendFailure
Removes the final usage of PaymentSendFailure from public API.

This (confusing) error matched with prior versions of LDK where users had to
handle payment retries themselves. Since auto-retry was introduced, the only
non-deprecated use remaining was for probe send errors. Probes only have
one path, though, so refactor ProbeSendFailure to omit usage of
PaymentSendFailure.

We don't make this error private yet because it's still used by some fuzzing
code as well as internally to outbound_payments, but it isn't returned by any
public functions anymore.
2024-12-10 15:24:23 -05:00
Valentine Wallace
351efc852e
Remove support for specifying route with send_spontaneous_payment
The old API is confusing and we want to remove it for 0.1.
2024-12-10 15:24:23 -05:00
Valentine Wallace
90b1e7d144
Mark deprecated send_payment_with_route as test and fuzz only
This method has been deprecated for several versions in favor of
ChannelManager::send_payment, and we want to remove it from the public API
entirely prior to the 0.1 release. However, >150 tests use it so put off
removing the method entirely.
2024-12-10 15:24:23 -05:00
Willem Van Lint
bbf1d93efe Make PackageTemplate::merge_package fallible
This moves panics to a higher level, allows failures to be handled
gracefully in some cases, and supports more explicit testing without
using `#[should_panic]`.
2024-12-09 23:10:43 -08:00
Willem Van Lint
69e1c70998 Refactor package_locktime in terms of signed and minimum locktimes
There are multiple factors affecting the locktime of a package:
- HTLC transactions rely on a fixed timelock due to the counterparty's
  signature.
- HTLC timeout claims on the counterparty's commitment transaction
  require satisfying a CLTV timelock.
- The locktime can be set to the latest height to avoid fee sniping.
These factors were combined in a single method, making the separate
factors less clear.
2024-12-09 23:10:43 -08:00
Arik Sosman
13835c04cc
Detect underflows in build_closing_transaction
In `build_closing_transaction`, we check that `value_to_holder` and
`value_to_counterparty`, which are signed, are not lower than the
dust limit. However, in doing this check, we convert them to signed
integers, which could result in an underflow and a failed detection.

This scenario should not be reachable, but here we add debug_asserts
to positive ensure that scenario isn't hit.
2024-12-09 11:11:17 -08:00
Jeffrey Czyz
fe1cf69b58
Merge pull request #3449 from TheBlueMatt/2024-12-event-processing-logging
Log before and after `Event` processing calls
2024-12-08 21:59:41 -06:00
Matt Corallo
8ab722235e Ensure monitors are not archived if they have a preimage we need
When a `ChannelMonitor` sees a payment preimage on chain for an
outbound HTLC, it creates a `MonitorEvent` containing the preimage
to pass to the inbound edge. The inclusion of the transaction
containing the payment preimage (plus six confirmations) also
results in the corresponding `Balance` being removed from the live
balance set, allowing the `ChannelMonitor` to be pruned (after a
further 4032 blocks).

While `MonitorEvent`s should always be processed in a timely
manner, if a node is suffering from a bug  where they are not, its
possible for 4038 blocks to pass with the preimage-containing
`MonitorEvent` still pending. If that happens, its possible the
`ChannelMonitor` is archived even though the preimage in it is
needed in another channel (or `ChannelMonitor`), causing funds
loss.

Luckily the fix is simple - check for pending events before
allowing a `ChannelMonitor` to be archived.

Fixes #2153
2024-12-08 20:36:26 +00:00
Matt Corallo
e2b964e793 Log before and after Event processing calls
At various points we've had issues where `Event` processing for a
user possibly is taking a long time, causing other things to stall.
However, due to lack of logging during `Event` processing itself
this can be rather difficult to debug. In
85eb8145fb we attempted to add
logging for this, but in doing so ended up with more verbose
logging than we were comfortable with.

Instead, here, we log only when we actually process an `Event`,
directly in the callsite passing the `Event` to the user.

Fixes #3331
2024-12-08 00:34:44 +00:00
Matt Corallo
020be440b6
Merge pull request #3442 from arik-so/archive-monitor-persistence-trigger
Persist unresolved ChannelMonitors on empty height change
2024-12-06 19:23:15 +00:00
Matt Corallo
b96b19a65d
Merge pull request #3413 from TheBlueMatt/2024-11-async-persist-claiming-from-closed-chan-1
Misc updates to tee up async `ChannelMonitorUpdate` persist for claims against closed channels
2024-12-06 19:13:53 +00:00
Arik Sosman
6f9300f115
Persist unresolved ChannelMonitors on empty height change 2024-12-06 08:27:37 -08:00
Gursharan Singh
8a54da6527
Merge pull request #3390 from arik-so/gossip-filtration-fix
Gossip filtration fix
2024-12-05 18:48:03 -08:00
Matt Corallo
b50354dc48 Check in-flight updates before completing events on closed chans
When we handle a `ChannelMonitorUpdate` completion we always
complete everything that was waiting on any updates to the same
channel all at once. Thus, we need to skip all updates if there's
pending updates besides the one that was just completed.

We handled this correctly for open channels, but the shortcut for
closed channels ignored any other pending updates entirely.

Here we fix this, which is ultimately required for tests which are
added in a few commits to pass.
2024-12-06 01:03:44 +00:00
Matt Corallo
4766e99e6f Properly enforce that all ChannelMonitorUpdates are ordered
c99d3d785d updated
`ChannelMonitorUpdate::update_id` to continue counting up even
after the channel is closed. It, however, accidentally updated the
`ChannelMonitorUpdate` application logic to skip testing that
`ChannelMonitorUpdate`s are well-ordered after the channel has been
closed (in an attempt to ensure other checks in the same
conditional block were applied).

This fixes that oversight.
2024-12-06 01:03:44 +00:00
Matt Corallo
70a375151e Add monitor update handling to update_maps_on_chan_removal
Closing channels requires a two step process - first
`update_maps_on_chan_removal` is called while holding the same
per-peer lock under which the channel reached the terminal state,
then after dropping the same lock(s), `finish_close_channel` is
called.

Because the channel is closed and thus no further
`ChannelMonitorUpdate`s are generated for the off-chain state, we'd
previously applied the `ChannelMonitorUpdate` in
`finish_close_channel`. This was tweaked somewhat in
c99d3d785d when we stopped using
`u64::MAX` for any updates after closure. However, we worked around
the races that implied by setting the `update_id` only when we go
to apply the `ChannelMonitorUpdate`, rather than when we create it.

In a coming commit, we'll need to have an `update_id` immediately
upon creation (to track in-flight updates that haven't reached
application yet). This implies that we can no longer apply closure
`ChannelMonitorUpdate`s after dropping the per-peer lock(s), as the
updates must be well-ordered with any later updates to the same
channel, even after it has been closed.

Thus, here, we add `ChannelMonitorUpdate` handling to
`update_maps_on_chan_removal`, renaming it `locked_close_channel`
to better capture its new purpose.
2024-12-06 01:03:44 +00:00
Matt Corallo
d1c340a0e1 Add additional variants to handle_new_monitor_update!
In the coming commits we'll start handling `ChannelMonitorUpdate`s
during channel closure in-line rather than after dropping locks via
`finish_close_channel`. In order to make that easy, here we add a
new `REMAIN_LOCKED_UPDATE_ACTIONS_PROCESSED_LATER` variant to
`handle_new_monitor_update!` which can attempt to apply an update
without dropping the locks and processing
`MonitorUpdateCompletionAction`s immediately.
2024-12-06 01:02:58 +00:00
Matt Corallo
385799f604 Handle events immediately if we are running during block connection
During block connection, we cannot apply `ChannelMonitorUpdate`s if
we're running during the startup sequence (i.e. before the user has
called any methods outside of block connection). We previously
handled this by simply always pushing any `ChannelMonitorUpdate`s
generated during block connection into the
`pending_background_events` queue.

However, this results in `ChannelMonitorUpdate`s going through the
queue when we could just push them immediately. Here we explicitly
check `background_events_processed_since_startup` and use that to
decide whether to push updates through the background queue
instead.
2024-12-06 01:02:58 +00:00
Matt Corallo
d33687dbef Don't generate dup force-close ChannelMonitorUpdates on startup
On startup, if we have a channel which was closed immediately
before shutdown such that the `ChannelMonitorUpdate` marking the
channel as closed is still in-flight, it doesn't make sense to
generate a fresh `ChannelMonitorUpdate` marking the channel as
closed immediately after the existing in-flight one.

Here we detect this case and drop the extra update, though its not
all that harmful it does avoid some test changes in the coming
commits.
2024-12-06 01:02:58 +00:00
Matt Corallo
f9374b8377 When removing PeerState check for in-flight mon updates deeply
When deciding if we should remove a `PeerState` entry we want to
ensure we don't remove if there are pending updates in
`in_flight_monitor_updates`. Previously this was done with a simple
`in_flight_monitor_updates.is_empty()`, however this can prevent
removal of `PeerState` entries if a channel had an update at some
point (leaving an entry in the map) but the update was ultimately
completed.

Instead, we need to iterate over the entries in
`in_flight_monitor_updates` and decline to remove `PeerState`s only
if there is an entry for a pending update still in-flight.
2024-12-06 01:02:58 +00:00
Arik Sosman
145e1aba38
Trigger full sync only on old GossipTimestampFilters
Previously, upon receipt of a GossipTimestampFilter message, we would
immediately start unloading the entire network graph on our
unsuspecting peer.

This commit modifies our behavior to only do so if the timestamp of
the filter message is at least six hour old. Otherwise, we only send
updated sync data as it comes in.
2024-12-05 10:06:39 -08:00
Matt Corallo
f33738d6cd
Merge pull request #3439 from valentinewallace/2024-12-remove-mpp-keysend-cfg
Remove `UserConfig::accept_mpp_keysend`
2024-12-05 15:31:14 +00:00
Arik Sosman
b4f21d16f4
Explicitly specify elided lifetime for LockTestExt 2024-12-05 00:48:35 -08:00
Arik Sosman
340ef29032
Remove blank lines following doc comments 2024-12-05 00:48:35 -08:00
Matt Corallo
24b8390b6d Move PackageTemplate merging decisions entirely into package.rs
Currently our package merging logic is strewn about between
`package.rs` (which decides various flags based on the package
type) and `onchaintx.rs` (which does the actual merging based on
the derived flags as well as its own logic), making the logic hard
to follow.

Instead, here we consolidate the package merging logic entirely
into `package.rs` with a new `PackageTemplate::can_merge_with`
method that decides if merging can happen. We also simplify the
merge pass in `update_claims_view_from_requests` to try to
maximally merge by testing each pair of `PackageTemplate`s we're
given to see if they can be merged.

This is overly complicated (and inefficient) for today's merge
logic, but over the coming commits we'll expand when we can merge
and not having to think about the merge pass' behavior makes that
much simpler (and O(N^2) for <1000 elements done only once when a
commitment transaction confirms is fine).
2024-12-04 17:02:27 -08:00
Matt Corallo
fd8b84ef92 Make several tests more robust against harmless transaction changes
In the next commit we'll be changing the order some transactions
get spent in packages, causing some tests to spuriously fail. Here
we update a few tests to avoid that by checking sets of inputs
rather than specific ordering.
2024-12-04 17:02:27 -08:00