Commit graph

3083 commits

Author SHA1 Message Date
Matt Corallo
05d7a33a58 Make attempting to retry a succeeded payment an APIError, not Route
This is symmetric with the new failure once a payment is abandoned.
2021-12-15 03:57:13 +00:00
Matt Corallo
3086bd8c8e Use Event::PaymentFailed in InvoicePayer to remove retry count
This finally fixes the bug described in the previous commits where
we retry a payment after its retry count has expired due to early
removal of the payment from the retry count tracking map. A test is
also added which demonstrates the bug in previous versions and
which passes now.

Fixes #1164.
2021-12-15 03:57:13 +00:00
Matt Corallo
7782d0a1ef Expose an event when a payment has failed and retries complete
When a payment fails, a payer needs to know when they can consider
a payment as fully-failed, and when only some of the HTLCs in the
payment have failed. This isn't possible with the current event
scheme, as discovered recently and as described in the previous
commit.

This adds a new event which describes when a payment is fully and
irrevocably failed, generating it only after the payment has
expired or been marked as expired with
`ChannelManager::mark_retries_exceeded` *and* all HTLCs for it
have failed. With this, a payer can more simply deduce when a
payment has failed and use that to remove payment state or
finalize a payment failure.
2021-12-15 03:57:13 +00:00
Matt Corallo
0b3240ee6a Add a variant to PendingOutboundPayment for retries-exceeded
When a payer gives up trying to retry a payment, they don't know
for sure what the current state of the event queue is.
Specifically, they cannot be sure that there are not multiple
additional `PaymentPathFailed` or even `PaymentSuccess` events
pending which they will see later. Thus, they have a very hard
time identifying whether a payment has truly failed (and informing
the UI of that fact) or if it is still pending. See [1] for more
information.

In order to avoid this mess, we will resolve it here by having the
payer give `ChannelManager` a bit more information - when they
have given up on a payment - and using that to generate a
`PaymentFailed` event when all paths have failed.

This commit adds the neccessary storage and changes for the new
state inside `ChannelManager` and a public method to mark a payment
as failed, the next few commits will add the new `Event` and use
the new features in our `PaymentRetrier`.

[1] https://github.com/lightningdevkit/rust-lightning/issues/1164
2021-12-15 03:57:13 +00:00
Matt Corallo
8c9615e8d6 DRY up payment failure macros in functional_test_utils
... with a more extensible expectation-checking framework for them.
2021-12-15 03:55:55 +00:00
Matt Corallo
3ec529d7cc
Merge pull request #1201 from jkczyz/2021-12-idempotent-channelmanager
Ensure ChannelManager methods are idempotent
2021-12-07 00:42:02 +00:00
Jeffrey Czyz
c453d04137
Ensure ChannelManager methods are idempotent
During event handling, ChannelManager methods may need to be called as
indicated in the Event documentation. Ensure that these calls are
idempotent for the same event rather than panicking. This allows users
to persist events for later handling without needing to worry about
processing the same event twice (e.g., if ChannelManager is not
persisted but the events were, the restarted ChannelManager would return
some of the same events).
2021-12-06 17:18:33 -06:00
Matt Corallo
9c6961ea84
Merge pull request #1204 from TheBlueMatt/2021-12-no-torv2
Remove OnionV2 parsing support
2021-12-06 22:49:18 +00:00
Matt Corallo
d47aebca38
Merge pull request #1130 from TheBlueMatt/2021-10-mon-fail-after-conf
Always return failure in update_monitor after funding spend
2021-12-06 19:35:58 +00:00
Matt Corallo
1b88f1638e Add trivial test of monitor update failure during block connection 2021-12-06 18:39:37 +00:00
Matt Corallo
9fe0cf19f6 Add a test for MonitorEvent holding when they complete out-of-order 2021-12-06 18:39:37 +00:00
Matt Corallo
fa62775f9d Add a simple test for ChainMonitor MonitorUpdate-holding behavior 2021-12-06 18:39:37 +00:00
Matt Corallo
6bcb270ae1 Make APIError debug output more clear by including the variant 2021-12-06 18:39:37 +00:00
Matt Corallo
87b6879622 Drop MonitorUpdateErr in favor of opaque errors.
We don't expect users to ever change behavior based on the string
contained in a `MonitorUpdateErr`, except log it, so there's little
reason to not just log it ourselves and return a `()` for errors.

We do so here, simplifying the callsite in `ChainMonitor` as well.
2021-12-06 18:39:37 +00:00
Matt Corallo
25542b8157 Always return failure in update_monitor after funding spend
Previously, monitor updates were allowed freely even after a
funding-spend transaction confirmed. This would allow a race
condition where we could receive a payment (including the
counterparty revoking their broadcasted state!) and accept it
without recourse as long as the ChannelMonitor receives the block
first, the full commitment update dance occurs after the block is
connected, and before the ChannelManager receives the block.

Obviously this is an incredibly contrived race given the
counterparty would be risking their full channel balance for it,
but its worth fixing nonetheless as it makes the potential
ChannelMonitor states simpler to reason about.

The test in this commit also tests the behavior changed in the
previous commit.
2021-12-06 18:39:37 +00:00
Matt Corallo
361639decf Remove OnionV2 parsing support
OnionV2s don't (really) work on Tor anymore anyway, and the field
is set for removal in the BOLTs [1]. Sadly because of the way
addresses are parsed we have to continue to understand that type 3
addresses are 12 bytes long. Thus, for simplicity we keep the
`OnionV2` enum variant around and just make it an opaque 12 bytes,
with the documentation updated to note the deprecation.

[1] https://github.com/lightning/bolts/pull/940
2021-12-06 00:12:35 +00:00
Matt Corallo
ea89286569
Merge pull request #1197 from jkczyz/2021-11-score-successful-payment-path
Score successful payment paths
2021-12-03 21:04:41 +00:00
Jeffrey Czyz
857b4c08a5
Fix shift overflow in Scorer::channel_penalty_msat
An unchecked shift of more than 64 bits on u64 values causes a shift
overflow panic. This may happen if a channel is penalized only once and
(1) is not successfully routed through and (2) after 64 or more half
life decays. Use a checked shift to prevent this from happening.
2021-12-03 14:00:52 -06:00
Jeffrey Czyz
d28d6a5403
Decay channel failure penalty upon success
If a payment failed to route through a channel, a penalty is applied to
the channel in the future when finding a route. This penalty decays over
time. Immediately decay the penalty by one half life when a payment is
successfully routed through the channel.
2021-12-03 14:00:52 -06:00
Jeffrey Czyz
c36bf92499
Score successful payment paths
Expand the Score trait with a payment_path_successful function for
scoring successful payment paths. Called by InvoicePayer's EventHandler
implementation when processing PaymentPathSuccessful events. May be used
by Score implementations to revert any channel penalties that were
applied by calls to payment_path_failed.
2021-12-03 14:00:51 -06:00
Matt Corallo
a3e4af0bb8
Merge pull request #1196 from TheBlueMatt/2021-11-bad-rustdoc
Fix compilation in `payment` rustdoc examples
2021-12-01 20:14:14 +00:00
Matt Corallo
ef246ed786 Fix compilation in payment rustdoc examples
The samples were not valid rust, but previous versions of rustc had
a bug where they were accepted anyway. Latest rustc beta no longer
accepts these.
2021-11-30 22:54:28 +00:00
Matt Corallo
0cdea66b0e
Merge pull request #1195 from TheBlueMatt/2021-11-chanman-read-regression
Fix regression when reading `Event::PaymentReceived` in some cases
2021-11-30 15:35:27 +00:00
Matt Corallo
e62bd9d137 Fix regression when reading Event::PaymentReceived in some cases
For some reason rustc was deciding on a type for the `Option` being
deserialized for us as `_user_payment_id`. This really, really,
absolutely should have been a compile failure - the type (with
methods called on it!) was ambiguous! Instead, rustc seems to have
been defaulting to `Option<()>`, causing us to read zero of the
eight bytes in the `user_payment_id` field, which returns an
`Err(InvalidValue)` error as TLVs must always be read fully.

This should likely be reported to rustc as its definitely a bug,
but I cannot seem to cause the same error on any kinda of
vaguely-minimized version of the same code.

Found by `chanmon_consistency` fuzz target.
2021-11-29 21:03:12 +00:00
Matt Corallo
9fcc626ee4
Merge pull request #1163 from TheBlueMatt/2021-11-support-insecure-counterparty
Explicitly support counterparty setting 0 channel reserve
2021-11-29 21:02:36 +00:00
Matt Corallo
25f4a54a2b Explicitly support counterparty setting 0 channel reserve
A peer providing a channel_reserve_satoshis of 0 (or less than our
dust limit) is insecure, but only for them. Because some LSPs do it
with some level of trust of the clients (for a substantial UX
improvement), we explicitly allow it. Because its unlikely to
happen often in normal testing, we test it explicitly here.
2021-11-29 16:57:21 +00:00
Matt Corallo
e9774aeb2e
Merge pull request #1189 from TheBlueMatt/2021-11-trivial-impls
Derive `Clone` and friends on additional public structs
2021-11-29 16:56:23 +00:00
Matt Corallo
f118bb776a Implement Clone for InvalidShutdownScript
Users hopefully shouldn't have much of a reason to use this, but
the bindings may need it to ensure no leaking pointers over an ffi.
2021-11-29 01:30:50 +00:00
Matt Corallo
04d0cca872 Implement Clone, Hash, PartialEq for ClosingTransaction
This is a public struct intended to be used as an object by users,
so it should likely have common implementations, given they're
trivial.
2021-11-29 01:30:44 +00:00
Matt Corallo
37c6c18789 Continue after a single failure in ChannelMonitor::update_monitor
`ChannelMonitorUpdate`s may contain multiple updates, including, eg
a payment preimage after a commitment transaction update. While
such updates are generally not generated today, we shouldn't return
early out of the update loop, causing us to miss any updates after
an earlier update fails.
2021-11-26 20:40:51 +00:00
Matt Corallo
1ce922c631 Prefer fully-specified paths in test macros
This avoids macros being context-specific use-dependent.
2021-11-26 20:40:51 +00:00
Matt Corallo
63698ecbbf Allow missing-docs on test-only macros 2021-11-26 19:31:21 +00:00
Matt Corallo
1a743672b9
Merge pull request #1184 from TheBlueMatt/2021-11-c-bindings-tweaks
C Bindings Compatibility Tweaks
2021-11-24 20:03:14 +00:00
Matt Corallo
3539f270c4 Seal scoring::Time and only use Instant or Eternity publicly
`scoring::Time` exists in part to make testing the passage of time
in `Scorer` practical. To allow no-std users to provide a time
source it was exposed as a trait as well. However, it seems
somewhat unlikely that a no-std user is going to have a use for
providing their own time source (otherwise they wouldn't be a
no-std user), and likely they won't have a graph in memory either.

`scoring::Time` as currently written is also exceptionally hard to
write C bindings for - the C bindings trait mappings relies on the
ability to construct trait implementations at runtime with function
pointers (i.e. `dyn Trait`s). `scoring::Time`, on the other hand,
is a supertrait of `core::ops::Sub` which requires a `sub` method
which takes a type parameter and returns a type parameter. Both of
which aren't practical in bindings, especially given the
`Sub::Output` associated type is not bound by any trait bounds at
all (implying we cannot simply map the `sub` function to return an
opaque trait object).

Thus, for simplicity, we here simply seal `scoring::Time` and make
it effectively-private, ensuring the bindings don't need to bother
with it.
2021-11-24 19:08:12 +00:00
Matt Corallo
a173ded03f Make Score : Writeable in c_bindings and impl on LockedScore
Ultimately we likely need to wrap the locked `Score` in a struct
that exposes writeable somehow, but because all traits have to be
fully concretized for C bindings we'll still need `Writeable` on
all `Score` in order to expose `Writeable` on the locked score.
Otherwise, we'll only have a `LockedScore` with a `Score` visible
that only has the `Score` methods, never the original type.
2021-11-24 19:08:12 +00:00
Matt Corallo
937403ed78
Merge pull request #1186 from TheBlueMatt/2021-11-fix-log-select
Fix the `max_level_trace` feature
2021-11-24 16:31:32 +00:00
Matt Corallo
4831de41ec Test all log-limiting features in CI 2021-11-24 02:52:53 +00:00
Matt Corallo
31e592bedf Fix compilation with the max_level_trace feature 2021-11-23 23:03:13 +00:00
Matt Corallo
f69311ccff Store holder channel reserve and max-htlc-in-flight explicitly
Previously, `holder_selected_channel_reserve_satoshis` and
`holder_max_htlc_value_in_flight_msat` were constant functions
of the channel value satoshis. However, in the future we may allow
allow users to specify it. In order to do so, we'll need to track
them explicitly, including serializing them as appropriate.

We go ahead and do so here, in part as it will make testing
different counterparty-selected channel reserve values easier.
2021-11-23 21:05:07 +00:00
Matt Corallo
ef86a3e209
Merge pull request #1162 from TheBlueMatt/2021-11-fix-accept-chan-checks
Correct initial commitment tx fee affordability checks on open
2021-11-23 20:46:38 +00:00
Matt Corallo
19191b450c
Merge pull request #1178 from jkczyz/2021-11-payment-path-successful
Generate PaymentPathSuccessful event for each path
2021-11-23 20:39:28 +00:00
Matt Corallo
2b78957888
Merge pull request #1176 from lightning-signer/2021-11-htlc-anchors
Add anchors support for HTLCs
2021-11-23 19:37:09 +00:00
Jeffrey Czyz
2c4f16d5e3
Generate PaymentPathSuccessful event for each path
A single PaymentSent event is generated when a payment is fulfilled.
This is occurs when the preimage is revealed on the first claimed HTLC.
For subsequent HTLCs, the event is not generated.

In order to score channels involved with a successful payments, the
scorer must be notified of each successful path involved in the payment.
Add a PaymentPathSuccessful event for this purpose. Generate it whenever
a part is removed from a pending outbound payment. This avoids duplicate
events when reconnecting to a peer.
2021-11-23 13:29:45 -06:00
Ken Sedgwick
530abc5efd
Add test vectors for get_htlc_redeemscript wrt anchors 2021-11-23 08:05:23 -08:00
Ken Sedgwick
50d81220df
Adjust HTLC_{SUCCESS,TIMEOUT}_TX_WEIGHT when anchors used 2021-11-23 08:05:22 -08:00
Ken Sedgwick
6c36e011a8
Add anchor support to build_htlc_transaction 2021-11-23 08:05:16 -08:00
Ken Sedgwick
c077f36b4b
Increase visibility of anchor related methods 2021-11-23 08:01:34 -08:00
Ken Sedgwick
3efcbab5d4
Add anchor support to commitment HTLC outputs 2021-11-23 08:00:42 -08:00
Matt Corallo
016eb96fc7 Support logger::Record in C by String-ing the fmt::Arguments
This adds a new (non-feature) cfg argument `c_bindings` which will
be set when building C bindings. With this, we can (slightly) tweak
behavior and API based on whether we are being built for Rust or C
users.

Ideally we'd never need this, but as long as we can keep the API
consistent-enough to avoid material code drift, this gives us a
cheap way of doing the "right" thing for both C and Rust when the
two are in tension.

We also move lightning-background-processor to support the same
MSRV as the main lightning crate, instead of only
lightning-net-tokio's MSRV.
2021-11-23 03:30:48 +00:00
Matt Corallo
13e4fd586e Test fixed channel reserve checks on channel open 2021-11-23 01:20:43 +00:00