Commit graph

2372 commits

Author SHA1 Message Date
Matt Corallo
97a6a6b2f8
Merge pull request #1842 from jkczyz/2022-11-channel-monitor-docs
Fix outdated `ChannelMonitor` docs
2022-11-09 19:16:00 +00:00
Matt Corallo
fcf73f0f45 Add a separate PaymentSendFailure for idempotency violation
When a user attempts to send a payment but it fails due to
idempotency key violation, they need to know that this was the
reason as they need to handle the error programmatically
differently from other errors.

Here we simply add a new `PaymentSendFailure` enum variant for
`DuplicatePayment` to allow for that.
2022-11-09 18:44:27 +00:00
Matt Corallo
c90aac26ad Rename PaymentSendFailure::AllFailedRetrySafe ...ResendSafe
It was pointed out that its quite confusing that
`AllFailedRetrySafe` does not allow you to call `retry_payment`,
though the documentation on it does specify this. Instead, we
simply rename it to `AllFailedResendSafe` to indicate that the
action that is safe to take is *resending*, not *retrying*.
2022-11-09 18:44:27 +00:00
Matt Corallo
d03640082b
Merge pull request #1840 from valentinewallace/2022-11-htlc-intercept-prefactor
Pre-refactor for HTLC Interception
2022-11-09 17:48:55 +00:00
Jeffrey Czyz
d4c3e16556
Fix outdated ChannelMonitor docs
ChannelMonitor::get_and_clear_pending_events docs references a method
that had been refactored and is no longer accurate.
2022-11-09 11:19:00 -06:00
Elias Rohrer
9685d6c272
Track block hash, return via get_relevant_txids
Previously, `Confirm::get_relevant_txids()` only returned a list of
transactions that have to be monitored for reorganization out of the
chain. This interface however required double bookkeeping: while we
internally keep track of the best block, height, etc, it would also
require the user to keep track which transaction was previously
confirmed in which block and to take actions based on any change, e.g,
to reconfirm them when the block would be reorged-out and the
transactions had been reconfirmed in another block.

Here, we track the confirmation block hash internally and return it via
`Confirm::get_relevant_txids()` to the user, which alleviates the
requirement for double bookkeeping: the user can now simply check
whether the given transaction is still confirmed and in the given block,
and take action if not.

We also split `update_claims_view`: Previously it was one, now it's two
methods: `update_claims_view_from_matched_txn` and
`update_claims_view_from_requests`.
2022-11-09 11:12:35 +01:00
Duncan Dean
7f4ac0ea81
Add public method to list pending monitor updates from ChainMonitor
Users have requested the feature to list pending monitor updates from
`ChainMonitor` so this adds that.
2022-11-09 06:58:12 +02:00
Matt Corallo
f1428fdf12
Merge pull request #1719 from jkczyz/2022-09-offer-encoding
BOLT 12 `offer` encoding and building
2022-11-08 23:54:55 +00:00
Valentine Wallace
c7935497fc
Fix scid_utils::is_valid* false positive
cargo bench was able to find an scid of 0 as a valid fake scid
2022-11-08 16:02:07 -05:00
Valentine Wallace
203394ff94
Track incoming amount in PendingHTLCInfo
Used in upcoming commit(s) when we generate the PaymentIntercepted event for
intercepted payments.

Co-authored-by: John Cantrell <johncantrell97@gmail.com>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2022-11-08 15:51:10 -05:00
Valentine Wallace
b3d257d70f
Delete unnecessary whitespace in process_pending_forwards
Only whitespace diff
2022-11-08 15:48:53 -05:00
Valentine Wallace
582b827a4d
Refactor HTLCForwardInfo::AddHTLC for intercept forwards
In upcoming commit(s), we'll want to store intercepted HTLC forwards in
ChannelManager before the user signals that they should be forwarded.  It
wouldn't make sense to store a HTLCForwardInfo as-is because the FailHTLC
variant doesn't make sense, so we refactor out the ::AddHTLC contents into its
own struct for storage.

Co-authored-by: John Cantrell <johncantrell97@gmail.com>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2022-11-08 15:48:32 -05:00
Jeffrey Czyz
8ba09e068b
Builder for creating offers
Add a builder for creating offers given a required description and
node_id. Other settings are optional and duplicative settings will
override previous settings for non-Vec fields.
2022-11-08 13:18:15 -06:00
Jeffrey Czyz
904d322923
Serialization macro for TLV streams
BOLT 12's offer message is encoded as a TLV stream (i.e., a sequence of
TLV records). impl_writeable_tlv_based can't be used because it writes
the overall length of the struct, whereas TLV streams only include the
length of each TLV record. Add a `tlv_stream` macro for defining structs
used in encoding.

TLV records containing a single variable-length type should not encode
the types length in the value since it is redundant. Add a wrapper type
that can be used within a TLV stream to support the correct behavior
during serialization and de-serialization.
2022-11-04 15:09:19 -05:00
Jeffrey Czyz
227fd51cb4
Add WithoutLength wrapper
When serializing variable-length types as part of a TLV stream, the
length does not need to be serialized as it is already encoded in TLV
records. Add a WithoutLength wrapper for this encoding. Replace
VecReadWrapper and VecWriteWrapper with this single type to avoid
redundant encoders.
2022-11-04 15:09:14 -05:00
Jeffrey Czyz
24b63de10c
Offer message interface and data format
Define an interface for BOLT 12 `offer` messages. The underlying format
consists of the original bytes and the parsed contents.

The bytes are later needed when constructing an `invoice_request`
message. This is because it must mirror all the `offer` TLV records,
including unknown ones, which aren't represented in the contents.

The contents will be used in `invoice_request` messages to avoid
duplication. Some fields while required in a typical user-pays-merchant
flow may not be necessary in the merchant-pays-user flow (i.e., refund).
2022-11-04 15:07:01 -05:00
Jeffrey Czyz
48bb9edba1
Add PrintableString utility
Strings defined by third parties may contain control characters. Provide
a wrapper such that these are replaced when displayed. Useful in node
aliases and offer fields.
2022-11-04 15:07:01 -05:00
Jeffrey Czyz
50aeee5afb
Offer features for BOLT 12
The offer message in BOLT 12 contains a features TLV record. Add a
corresponding OfferFeatures type where the length is not included in the
serialization as it would be redundant with the record length.
Otherwise, define the features to be the same as InvoiceFeatures.
2022-11-04 15:07:01 -05:00
Viktor Tigerström
b3689412df Make process_pending_htlc_forwards more readable
Refactor `process_pending_htlc_forwards` to ensure that both branches
that fails `pending_forwards` are placed next to eachother for improved
readability.
2022-11-04 20:26:47 +01:00
Viktor Tigerström
ec9db029ea Consider channel_ids in short_to_chan_info as unguaranteed
As the `short_to_chan_info` map has been removed from the
`channel_state`, there is no longer any consistency guarantees between
the `by_id` and `short_to_chan_info` maps. This commit ensures that we
don't force unwrap channels where the channel_id has been queried from
the `short_to_chan_info` map.
2022-11-04 20:26:47 +01:00
Viktor Tigerström
3fa10c801b Remove excess channel_state passing to macros
As the `short_to_chan_info` has been moved out of the `channel_state` to
a standalone lock, several macros no longer need the `channel_state`
passed into the macro.
2022-11-04 20:26:47 +01:00
Viktor Tigerström
c82a65a1f6 Move short_to_chan_info into standalone lock
As the `channel_state` (`ChannelHolder`) struct will be removed, this
commit moves the `short_to_chan_info` map from that lock into a seperate
lock.
2022-11-04 20:26:47 +01:00
benthecarman
83dcd39c6d
Implement Hash for ConfirmationTarget 2022-11-04 02:32:45 -05:00
Matt Corallo
e55e0d53c7
Merge pull request #1811 from valentinewallace/2022-10-chanman-router
Move `InflightHtlcs` and `Router` trait into `ChannelManager`
2022-11-03 23:43:03 +00:00
Matt Corallo
790d26f63f
Merge pull request #1761 from TheBlueMatt/2022-10-user-idempotency-token
Provide `send_payment` idempotency guarantees
2022-11-03 22:38:49 +00:00
Valentine Wallace
9d3324968c
Move InvoicePayer's Router into ChannelManager
This helps prepare to parameterize ChannelManager with a Router, to eventually
use in trampoline payments.
2022-11-03 16:22:40 -04:00
Valentine Wallace
1840cae321
Move InFlightHtlcs into ChannelManager
This is part of moving the Router trait into ChannelManager, which will help
allow ChannelManager to fetch routes on-the-fly as part of supporting
trampoline payments.
2022-11-03 16:19:07 -04:00
Matt Corallo
d15b7cb86e
Merge pull request #1817 from TheBlueMatt/2022-10-removed-no-score-after 2022-11-03 17:22:34 +00:00
Matt Corallo
3ba91cea59
Merge pull request #1743 from tnull/2022-09-channel-events
Add `ChannelReady` event
2022-11-03 16:25:55 +00:00
Elias Rohrer
49dfcb6302
Fix warnings for ununsed anchor imports
Previously introduced during release commit.
2022-11-03 11:45:31 +01:00
Elias Rohrer
0911723804
Rename chan state ChannelFunded to ChannelReady
We rename `ChannelState::ChannelFunded` to `ChannelState::ChannelReady`
as we'll be in this state when both sides sent the `ChannelReady`
messages, which may also be before funding in the 0conf case.
2022-11-03 11:45:31 +01:00
Elias Rohrer
f4c2d40700
Add ChannelReady event
This adds a `ChannelReady` event that is emitted as soon as a new
channel becomes usable, i.e., after both sides have sent
`channel_ready`.
2022-11-03 11:45:28 +01:00
Matt Corallo
00607a5286 Add missing break when scoring a path with a missing channel
If we send payments over a path where a channel ended up being
closed, we'll remove it before we call
`ProbabilisticPaymentScorer::payment_path_failed`. This should be
fine, except that `payment_path_failed` does not break out of its
scoring loop if a channel is missing, causing it to assign a
minimum available-liquidity of the payment amount even to channels
which our attempt never arrived at.

The fix is simple - add the missing check and break.
2022-11-02 20:09:32 +00:00
Matt Corallo
f0775f8379
Merge pull request #1735 from naumenkogs/2022-09-prune-channels-if-either-not-upd
Prune channels if either not updated + track pruning time
2022-11-02 19:23:27 +00:00
Matt Corallo
a6609d6d6a
Merge pull request #1753 from wpaulino/avoid-redundant-claims-after-initial-conf
Avoid generating redundant claims after initial confirmation
2022-11-02 19:23:15 +00:00
Wilmer Paulino
a0891368ee
Avoid generating redundant claims after initial confirmation
These claims will never be valid as a previous claim has already
confirmed. If a previous claim is reorged out of the chain, a new claim
will be generated bypassing the new behavior.

While this doesn't change much for our existing transaction-based
claims, as broadcasting an already confirmed transaction acts as a NOP,
it prevents us from yielding redundant event-based claims, which will be
introduced as part of the anchors patchset.
2022-11-02 10:07:45 -07:00
Gleb Naumenko
080c70f98f Track the time a stale channel was pruned 2022-11-02 09:45:21 +02:00
Matt Corallo
0ae45a2578 Test that PaymentIds are idempotency keys until abandon_payment 2022-11-02 01:09:07 +00:00
Matt Corallo
548f3f8416 Stop timing out payments automatically, requiring abandon_payment
When the `abandon_payment` flow was added there was some concern
that upgrading users may not migrate to the new flow, causing
memory leaks in the pending-payment tracking.

While this is true, now that we're relying on the
pending_outbound_payments map for `send_payment` idempotency, the
risk of removing a payment prematurely goes up from "spurious
retry failure" to "sending a duplicative payment", which is much
worse.

Thus, we simply remove the automated payment timeout here,
explicitly requiring that users call `abandon_payment` when they
give up retrying a payment.
2022-11-02 01:09:07 +00:00
Matt Corallo
166e0c88e4 Delay removal of fulfilled outbound payments for a few timer ticks
Previously, once a fulfilled outbound payment completed and all
associated HTLCs were resolved, we'd immediately remove the payment
entry from the `pending_outbound_payments` map.

Now that we're using the `pending_outbound_payments` map for send
idempotency, this presents a race condition - if the user makes a
redundant `send_payment` call at the same time that the original
payment's last HTLC is resolved, the user would reasonably expect
the `send_payment` call to fail due to our idempotency guarantees.

However, because the `pending_outbound_payments` entry is being
removed, if it completes first the `send_payment` call will
succeed even though the user has not had a chance to see the
corresponding `Event::PaymentSent`.

Instead, here, we delay removal of `Fulfilled`
`pending_outbound_payments` entries until several timer ticks have
passed without any corresponding event or HTLC pending.
2022-11-02 01:09:07 +00:00
Matt Corallo
a10223d1ff Allow users to specify the PaymentId for new outbound payments
In c986e52ce8, an `MppId` was added
to `HTLCSource` objects as a way of correlating HTLCs which belong
to the same payment when the `ChannelManager` sees an HTLC
succeed/fail. This allows it to have awareness of the state of all
HTLCs in a payment when it generates the ultimate user-facing
payment success/failure events. This was used in the same PR to
avoid generating duplicative success/failure events for a single
payment.

Because the field was only used as an internal token to correlate
HTLCs, and retries were not supported, it was generated randomly by
calling the `KeysInterface`'s 32-byte random-fetching function.
This also provided a backwards-compatibility story as the existing
HTLC randomization key was re-used for older clients.

In 28eea12bbe `MppId` was renamed to
the current `PaymentId` which was then used expose the
`retry_payment` interface, allowing users to send new HTLCs which
are considered a part of an existing payment.

At no point has the payment-sending API seriously considered
idempotency, a major drawback which leaves the API unsafe in most
deployments. Luckily, there is a simple solution - because the
`PaymentId` must be unique, and because payment information for a
given payment is held for several blocks after a payment
completes/fails, it represents an obvious idempotency token.

Here we simply require the user provide the `PaymentId` directly in
`send_payment`, allowing them to use whatever token they may
already have for a payment's idempotency token.
2022-11-02 01:09:07 +00:00
Matt Corallo
6ca49948c1
Merge pull request #1791 from valentinewallace/2022-10-we-are-intro-node
Onion messages: fix edge case where we are the intro node
2022-11-01 21:12:30 +00:00
Gleb Naumenko
a39d9bfa03 Prune channels if *either* not updated 2022-11-01 16:51:08 +02:00
Gleb Naumenko
494e3aad9f Non-mandatory "fix" enabling future tests 2022-11-01 16:51:08 +02:00
Valentine Wallace
7af02d0174
OMs: fix panic sending to a two-hop blinded route where we are the intro node 2022-10-31 12:40:09 -04:00
Valentine Wallace
a4e242ba5f
Onion messages: fix edge case where we are the introduction node
If we're sending straight to a blinded route with no unblinded intermediate
hops, and we are the introduction node, we need to advance the blinded route by
one hop so that the second hop is the new introduction node.
2022-10-31 12:38:31 -04:00
Matt Corallo
4df0ff74f5 Make htlc_maximum_msat in EffectiveCapacity non-Optional
Because we now never generate an `EffectiveCapacity` with an
`htlc_maximum_msat` set to `None`, making it non-`Option`al
effectively removes dead code, which we do here.
2022-10-29 20:38:59 +00:00
Matt Corallo
81afc2f172 Require directional updates for a DirectionalChannelInfo
We currently construct `DirectedChannelInfo`s for routing before
checking if the given direction has its directional info filled in.
We then always check for directional info before actually deciding
to route over a channel, as otherwise we assume the channel is not
online.

This makes for somewhat redundant checks, and `DirectedCHannelInfo`
isn't, by itself, a very useful API. Because fetching the HTLC-max
or effective channel capacity gives spurious data if no directional
info is available, there's little reason to have that data
available, and so we here check for directional info first. This
effectively merges `DirectionalChannelInfo` and
`DirectionalChannelInfoWithUpdate`.
2022-10-29 20:38:54 +00:00
Matt Corallo
6957fb63f9
Merge pull request #1809 from valentinewallace/2022-10-custom-om-self
Give us a self when reading a custom onion message
2022-10-27 21:34:45 +00:00
Matt Corallo
150c87a089
Give us a self when reading a custom onion message
+ remove MaybeReadableArgs trait as it is now unused
+ remove onion_utils::DecodeInput as it would've now needed to be parameterized
by the CustomOnionMessageHandler trait, and we'd like to avoid either
implementing DecodeInput in messenger or having onion_utils depend on
onion_message::*

Co-authored-by: Matt Corallo <git@bluematt.me>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2022-10-27 15:58:33 -04:00