Commit graph

4747 commits

Author SHA1 Message Date
Valentine Wallace
2037a241f4
Remove all_paths_failed from PaymentPathFailed
This field was previous useful in manual retries for users to know when all
paths of a payment have failed and it is safe to retry. Now that we support
automatic retries in ChannelManager and no longer support manual retries, the
field is no longer useful.

For backwards compat, we now always write false for this field. If we didn't do
this, previous versions would default this field's value to true, which can be
problematic because some clients have relied on the field to indicate when a
full payment retry is safe.
2023-02-24 14:21:08 -05:00
Valentine Wallace
f361aa62a4
Add missing import path in ser macro 2023-02-23 17:04:04 -05:00
Matt Corallo
46fd7035b3
Merge pull request #2014 from valentinewallace/2023-02-rework-partial-pmt-fail
Rework auto-retry send errors
2023-02-23 21:54:16 +00:00
Valentine Wallace
a6e9123d3f
Clarify Retry::Timeout vs PaymentParams::expiry_time in docs 2023-02-23 15:50:25 -05:00
Valentine Wallace
12bcc9ae43
Fix outdated PendingOutboundPayment::Abandoned docs 2023-02-23 15:50:25 -05:00
Valentine Wallace
1224dac862
On initial send retries, avoid previously failed scids
Previously, we could have tried the same failed channels over and over until
retries are exhausted.
2023-02-23 15:50:25 -05:00
Valentine Wallace
b826d1735d
In-line retry_with_route method
Since it's only used one place now
2023-02-23 15:50:25 -05:00
Valentine Wallace
d471d9746c
Rework auto retry send errors
Prior to this, we returned PaymentSendFailure from auto retry send payment
methods. This implied that we might return a PartialFailure from them, which
has never been the case. So it makes sense to rework the errors to be a better
fit for the methods.

We're taking error handling in a totally different direction now to make it
more asynchronous, see send_payment_internal for more information.
2023-02-23 15:50:23 -05:00
Valentine Wallace
5e4f0bcff0
Fix InvalidRoute error to be ChannelUnavailable
InvalidRoute is reserved for malformed routes, not routes where a channel or
its peer is unavailable
2023-02-22 23:05:43 -05:00
Matt Corallo
96c8507fbf
Merge pull request #1897 from TheBlueMatt/2022-11-monitor-updates-always-async
Always process `ChannelMonitorUpdate`s asynchronously
2023-02-22 19:12:31 +00:00
Matt Corallo
2adb8eeb49 Don't generate a ChannelMonitorUpdate for closed chans on shutdown
The `Channel::get_shutdown` docs are very clear - if the channel
jumps to `Shutdown` as a result of not being funded when we go to
initiate shutdown we should not generate a `ChannelMonitorUpdate`
as there's no need to bother with the shutdown script - we're
force-closing anyway.

However, this wasn't actually implemented, potentially causing a
spurious monitor update for no reason.
2023-02-22 17:34:46 +00:00
Matt Corallo
685b08d8c1 Use the new monitor persistence flow for funding_created handling
Building on the previous commits, this finishes our transition to
doing all message-sending in the monitor update completion
pipeline, unifying our immediate- and async- `ChannelMonitor`
update and persistence flows.
2023-02-22 17:34:46 +00:00
Matt Corallo
c35253d6a8 Use new monitor persistence flow in funding_signed handling
In the previous commit, we moved all our `ChannelMonitorUpdate`
pipelines to use a new async path via the
`handle_new_monitor_update` macro. This avoids having two message
sending pathways and simply sends messages in the "monitor update
completed" flow, which is shared between sync and async monitor
updates.

Here we reuse the new macro for handling `funding_signed` messages
when doing an initial `ChannelMonitor` persistence. This provides
a similar benefit, simplifying the code a trivial amount, but
importantly allows us to fully remove the original
`handle_monitor_update_res` macro.
2023-02-22 17:34:46 +00:00
Matt Corallo
4e002dcf5c Always process ChannelMonitorUpdates asynchronously
We currently have two codepaths on most channel update functions -
most methods return a set of messages to send a peer iff the
`ChannelMonitorUpdate` succeeds, but if it does not we push the
messages back into the `Channel` and then pull them back out when
the `ChannelMonitorUpdate` completes and send them then. This adds
a substantial amount of complexity in very critical codepaths.

Instead, here we swap all our channel update codepaths to
immediately set the channel-update-required flag and only return a
`ChannelMonitorUpdate` to the `ChannelManager`. Internally in the
`Channel` we store a queue of `ChannelMonitorUpdate`s, which will
become critical in future work to surface pending
`ChannelMonitorUpdate`s to users at startup so they can complete.

This leaves some redundant work in `Channel` to be cleaned up
later. Specifically, we still generate the messages which we will
now ignore and regenerate later.

This commit updates the `ChannelMonitorUpdate` pipeline across all
the places we generate them.
2023-02-22 17:34:46 +00:00
Matt Corallo
46c6fb7f91 Move TODO from handle_monitor_update_res into Channel
The TODO mentioned in `handle_monitor_update_res` about how we
might forget about HTLCs in case of permanent monitor update
failure still applies in spite of all our changes. If a channel is
drop'd in general, monitor-pending updates may be lost if the
monitor update failed to persist.

This was always the case, and is ultimately the general form of the
the specific TODO, so we simply leave comments there
2023-02-22 00:51:13 +00:00
Matt Corallo
9802afa53b Handle MonitorUpdateCompletionActions after monitor update sync
In a previous PR, we added a `MonitorUpdateCompletionAction` enum
which described actions to take after a `ChannelMonitorUpdate`
persistence completes. At the time, it was only used to execute
actions in-line, however in the next commit we'll start (correctly)
leaving the existing actions until after monitor updates complete.
2023-02-22 00:51:13 +00:00
Matt Corallo
435b3b4802
Merge pull request #1988 from TheBlueMatt/2023-01-limited-chans
Limit the number of pending un-funded inbound channel
2023-02-22 00:39:13 +00:00
Matt Corallo
d5fb804a32 Limit the number of pending un-funded inbound channel
Because we store some (not large, but not zero) state per-peer,
it's useful to limit the number of peers we have connected, at
least with some buffer.

Much more importantly, each channel has a relatively large cost,
especially around the `ChannelMonitor`s we have to build for each.

Thus, here, we limit the number of channels per-peer which aren't
(yet) on-chain, as well as limit the number of (inbound) peers
which don't have a (funded-on-chain) channel.

Fixes #1889
2023-02-21 22:01:47 +00:00
Matt Corallo
4155f54716 Add an inbound flag to the peer_connected message handlers
Its useful for the message handlers to know if a peer is inbound
for DoS decision-making reasons.
2023-02-21 22:00:42 +00:00
Matt Corallo
e954ee8256
Merge pull request #2035 from TheBlueMatt/2023-02-fix-no-con-discon
Fix (and DRY) the conditionals before calling peer_disconnected
2023-02-21 21:28:05 +00:00
Matt Corallo
068c856638
Merge pull request #2040 from alecchendev/2023-02-indexed-map-btreeset-to-vec
Replace `BTreeSet` in `IndexedMap` with sorted `Vec`
2023-02-21 19:57:51 +00:00
Matt Corallo
be6f263825 Remove the peer_disconnected no_connection_possible flag
Long ago, we used the `no_connection_possible` to signal that a
peer has some unknown feature set or some other condition prevents
us from ever connecting to the given peer. In that case we'd
automatically force-close all channels with the given peer. This
was somewhat surprising to users so we removed the automatic
force-close, leaving the flag serving no LDK-internal purpose.

Distilling the concept of "can we connect to this peer again in the
future" to a simple flag turns out to be ripe with edge cases, so
users actually using the flag to force-close channels would likely
cause surprising behavior.

Thus, there's really not a lot of reason to keep the flag,
especially given its untested and likely to be broken in subtle
ways anyway.
2023-02-21 19:17:06 +00:00
Matt Corallo
0f07d5c0b0 Add a further debug_assert that disconnecting peers are connected 2023-02-21 18:54:52 +00:00
Matt Corallo
10e06331f3 Correct funding_transaction_generated err msg and fix fuzz check
This fixes new errors in `full_stack_target` pointed out by
Chaincode's generous fuzzing infrastructure. Specifically, there's
no reason to check the error message in the
`funding_transaction_generated` return value - it can only return
a failure if the channel has closed since the funding transaction
was generated (which is fine) or if the signer refuses to sign
(which can't happen in fuzzing).
2023-02-21 18:54:52 +00:00
Matt Corallo
ca1b8bdf60 Correct the "is peer live" checks in PeerManager
In general, we should be checking if a `Peer` has `their_features`
set as the "is this peer connected and have they finished the
handshake" flag as it indicates an `Init` message was received.

While none of these appear to be reachable bugs, there were a
number of places where we checked other flags for this purpose,
which may lead to sending messages before `Init` in the future.

Here we clean these cases up to always use the correct check (via
the new util method).
2023-02-21 18:54:52 +00:00
Matt Corallo
73e2fdf332 Add test of an initial message other than Init
This test fails without the previous commit.
2023-02-21 18:54:52 +00:00
Matt Corallo
3554678e9c Fix (and DRY) the conditionals before calling peer_disconnected
If we have a peer that sends a non-`Init` first message, we'll call
`peer_disconnected` without ever having called `peer_connected`
(which has to wait until we have an `Init` message). This is a
violation of our API guarantees, though should generally not be an
issue.

Because this bug was repeated in a few places, we also take this
opportunity to DRY up the logic which checks the peer state before
calling `peer_disconnected`.

Found by the new `ChannelManager` assertions and the
`full_stack_target` fuzzer.
2023-02-21 17:12:27 +00:00
Alec Chen
62a88f97de Replace BTreeSet in IndexedMap with sorted Vec
The `Vec` is sorted not on `IndexedMap::insert`, but on
`IndexedMap::range` to avoid unnecessary work while reading a network
graph.
2023-02-19 22:22:11 -06:00
Valentine Wallace
9f41bd7f64
Pass pending_events into pay_internal
Useful for generating Payment(Path)Failed events in this method
2023-02-19 18:01:01 -05:00
Valentine Wallace
cb81d27f42
Pass payment hash into pay_internal
Useful for generating Payment(Path)Failed events in this method
2023-02-19 18:01:01 -05:00
Matt Corallo
558b2f2904
Merge pull request #2026 from valentinewallace/2023-02-dedup-pending-forwardable-evs
Deduplicate `PendingHTLCsForwardable` events on generation
2023-02-19 18:25:03 +00:00
Valentine Wallace
a2489b126f
Deduplicate PendingHTLCsForwardable events when queueing 2023-02-17 17:41:21 -05:00
Valentine Wallace
bf03d4ccbe
On retryable update_fail, don't queue redundant PendingHTLCsForwardable 2023-02-17 17:35:09 -05:00
Valentine Wallace
5ea433f71f
Check for abandon-able payments on startup 2023-02-17 17:14:43 -05:00
Matt Corallo
ba07622d05 Add a new monitor update result handling macro
Over the next few commits, this macro will replace the
`handle_monitor_update_res` macro. It takes a different approach -
instead of receiving the message(s) that need to be re-sent after
the monitor update completes and pushing them back into the
channel, we'll not get the messages from the channel at all until
we're ready for them.

This will unify our message sending into only actually fetching +
sending messages in the common monitor-update-completed code,
rather than both there *and* in the functions that call `Channel`
when new messages are originated.
2023-02-17 19:09:28 +00:00
Matt Corallo
34218cc4ee Add storage for ChannelMonitorUpdates in Channels
In order to support fully async `ChannelMonitor` updating, we need
to ensure that we can replay `ChannelMonitorUpdate`s if we shut
down after persisting a `ChannelManager` but without completing a
`ChannelMonitorUpdate` persistence. In order to support that we
(obviously) have to store the `ChannelMonitorUpdate`s in the
`ChannelManager`, which we do here inside the `Channel`.

We do so now because in the coming commits we will start using the
async persistence flow for all updates, and while we won't yet
support fully async monitor updating it's nice to get some of the
foundational structures in place now.
2023-02-17 19:09:28 +00:00
Matt Corallo
56f979be3e Track actions to execute after a ChannelMonitor is updated
When a `ChannelMonitor` update completes, we may need to take some
further action, such as exposing an `Event` to the user initiating
another `ChannelMonitor` update, etc. This commit adds the basic
structure to track such actions and serialize them as required.

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. As
these are still considered beta, breaking downgrades for such users
is considered acceptable here.
2023-02-17 19:09:28 +00:00
Matt Corallo
8aa518f23d Add an infallible no-sign version of send_commitment_no_status_check
In the coming commits we'll move to async `ChannelMonitorUpdate`
application, which means we'll want to generate a
`ChannelMonitorUpdate` (including a new counterparty commitment
transaction) before we actually send it to our counterparty. To do
that today we'd have to actually sign the commitment transaction
by calling the signer, then drop it, apply the
`ChannelMonitorUpdate`, then re-sign the commitment transaction to
send it to our peer.

In this commit we instead split `send_commitment_no_status_check`
and `send_commitment_no_state_update` into `build_` and `send_`
variants, allowing us to generate new counterparty commitment
transactions without actually signing, then build them for sending,
with signatures, later.
2023-02-17 19:09:28 +00:00
Matt Corallo
5be29c6add Fix the err msg provided when a send fails due to peer disconnected
We haven't had a `MonitorUpdateInProgress` check in
`Channel::is_live` for some time.
2023-02-17 19:09:28 +00:00
Matt Corallo
d0b8f455fe
Merge pull request #2009 from TheBlueMatt/2023-02-no-racey-retries
Fix (and test) threaded payment retries
2023-02-16 23:41:09 +00:00
Matt Corallo
d986329734 Fix (and test) threaded payment retries
The new in-`ChannelManager` retries logic does retries as two
separate steps, under two separate locks - first it calculates
the amount that needs to be retried, then it actually sends it.
Because the first step doesn't udpate the amount, a second thread
may come along and calculate the same amount and end up retrying
duplicatively.

Because we generally shouldn't ever be processing retries at the
same time, the fix is trivial - simply take a lock at the top of
the retry loop and hold it until we're done.
2023-02-16 21:35:25 +00:00
Matt Corallo
1dc96cbde2 Always check next_route in TestRouter is fully consumed
...rather than only in std.
2023-02-16 21:35:25 +00:00
Matt Corallo
6090d9e6a8 Test if a given mutex is locked by the current thread in tests
In anticipation of the next commit(s) adding threaded tests, we
need to ensure our lockorder checks work fine with multiple
threads. Sadly, currently we have tests in the form
`assert!(mutex.try_lock().is_ok())` to assert that a given mutex is
not locked by the caller to a function.

The fix is rather simple given we already track mutexes locked by a
thread in our `debug_sync` logic - simply replace the check with a
new extension trait which (for test builds) checks the locked state
by only looking at what was locked by the current thread.
2023-02-16 21:35:23 +00:00
Matt Corallo
a1704787b6
Merge pull request #2037 from valentinewallace/2023-02-fix-probe-leak
Fix outbound payments probe leak
2023-02-16 21:29:26 +00:00
Matt Corallo
4b043c97bc
Merge pull request #1921 from arik-so/2022-12-prohibit-old-rgs-updates
Throw error for RGS data that's more than two weeks old
2023-02-16 21:20:28 +00:00
Arik Sosman
2f36c921b6
Throw error for RGS data that's more than two weeks old, fixing #1785 2023-02-16 12:17:42 -08:00
Valentine Wallace
a2b956d46e
Remove pending probes on update_fail
Previously we had a memory leak where probes would not be removed from
outbound_payments on htlc fail
2023-02-15 23:30:46 -05:00
Matt Corallo
30b9d9fbea
Merge pull request #2008 from valentinewallace/2023-02-remove-manual-retries
Abandon payments on behalf of the user and remove manual retries
2023-02-16 00:49:04 +00:00
Valentine Wallace
da34ada8d4
Reword and fix grammar in PartialFailure docs 2023-02-15 17:59:44 -05:00
Valentine Wallace
5c6d8a7cb8
Remove retry_payments method
We're no longer supporting manual retries since
ChannelManager::send_payment_with_retry can be parameterized by a retry
strategy

This commit also updates all docs related to retry_payment and abandon_payment.
Since these docs frequently overlap with changes in preceding commits where we
start abandoning payments on behalf of the user, all the docs are updated in
one go.
2023-02-15 17:59:39 -05:00