Commit graph

4914 commits

Author SHA1 Message Date
Matt Corallo
2d1306a324 Test the new RAAMonitorUpdateBlockingAction::ClaimedMPPPayment 2024-07-08 19:07:11 +00:00
Matt Corallo
9cc6e08965 Block monitor updates to ensure preimages are in each MPP part
If we claim an MPP payment and only persist some of the
`ChannelMonitorUpdate`s which include the preimage prior to
shutting down, we may be in a state where some of our
`ChannelMonitor`s have the preimage for a payment while others do
not.

This, it turns out, is actually mostly safe - on startup
`ChanelManager` will detect there's a payment it has as unclaimed
but there's a corresponding payment preimage in a `ChannelMonitor`
and go claim the other MPP parts. This works so long as the
`ChannelManager` has been persisted after the payment has been
received but prior to the `PaymentClaimable` event being processed
(and the claim itself occurring). This is not always true and
certainly not required on our API, but our
`lightning-background-processor` today does persist prior to event
handling so is generally true subject to some race conditions.

In order to address this we need to use copy payment preimages
across channels irrespective of the `ChannelManager`'s payment
state, but this introduces another wrinkle - if one channel makes
substantial progress while other channel(s) are still waiting to
get the payment preimage in `ChannelMonitor`(s) while the
`ChannelManager` hasn't been persisted after the payment was
received, we may end up without the preimage on disk.

Here, we address this issue by using the new
`RAAMonitorUpdateBlockingAction` variant for this specific case. We
block persistence of an RAA `ChannelMonitorUpdate` which may remove
the preimage from disk until all channels have had the preimage
added to their `ChannelMonitor`.

We do this only in-memory (and not on disk) as we can recreate this
blocker during the startup re-claim logic.

This will enable us to claim MPP parts without using the
`ChannelManager`'s payment state in later work.
2024-07-08 19:07:11 +00:00
Matt Corallo
8933a71aed Add a RAAMonitorUpdateBlockingAction::ClaimedMPPPayment
If we claim an MPP payment and only persist some of the
`ChannelMonitorUpdate`s which include the preimage prior to
shutting down, we may be in a state where some of our
`ChannelMonitor`s have the preimage for a payment while others do
not.

This, it turns out, is actually mostly safe - on startup
`ChanelManager` will detect there's a payment it has as unclaimed
but there's a corresponding payment preimage in a `ChannelMonitor`
and go claim the other MPP parts. This works so long as the
`ChannelManager` has been persisted after the payment has been
received but prior to the `PaymentClaimable` event being processed
(and the claim itself occurring). This is not always true and
certainly not required on our API, but our
`lightning-background-processor` today does persist prior to event
handling so is generally true subject to some race conditions.

In order to address this race we need to use copy payment preimages
across channels irrespective of the `ChannelManager`'s payment
state, but this introduces another wrinkle - if one channel makes
substantial progress while other channel(s) are still waiting to
get the payment preimage in `ChannelMonitor`(s) while the
`ChannelManager` hasn't been persisted after the payment was
received, we may end up without the preimage on disk.

Here, we address this issue with a new
`RAAMonitorUpdateBlockingAction` variant for this specific case. We
block persistence of an RAA `ChannelMonitorUpdate` which may remove
the preimage from disk until all channels have had the preimage
added to their `ChannelMonitor`.

We do this only in-memory (and not on disk) as we can recreate this
blocker during the startup re-claim logic.

This will enable us to claim MPP parts without using the
`ChannelManager`'s payment state in later work.
2024-07-08 19:06:59 +00:00
Matt Corallo
453ed11f80 Track the cp node_id which originated an HTLC in the HTLCSource
Because we track pending `ChannelMonitorUpdate`s per-peer, we
really need to know what peer an HTLC came from when we go to claim
it on-chain, allowing us to look up any blocked actions in the
`PeerState`. While we could do this by moving the blocked actions
to some global "closed-channel update queue", its simpler to do it
this way.

While this trades off `ChannelMonitorUpdate` size somewhat (the
`HTLCSource` is included in many of them), which we should be
sensitive to, it will also allow us to (eventually) remove the
SCID -> peer + channel_id lookups we do when claiming or failing
today, which are somewhat annoying to deal with.
2024-07-08 19:06:59 +00:00
Matt Corallo
1c6ce8e789 Add skipping variants to impl_writeable_tlv_based_enum_upgradable
In some cases, we have variants of an enum serialized using
`impl_writeable_tlv_based_enum_upgradable` which we don't want to
write/read. Here we add support for such variants by writing them
using the (odd) type 255 without any contents and using
`MaybeReadable` to decline to read them.
2024-07-08 19:06:59 +00:00
valentinewallace
88e1b56d66
Merge pull request #3125 from valentinewallace/2024-06-async-payments-prefactor
Async payments message encoding and prefactor
2024-06-24 12:06:17 -04:00
Valentine Wallace
5c7af8c6d3
Cfg-gate async payments module and public enum variants. 2024-06-20 14:24:09 -04:00
Valentine Wallace
f20bd3de9f
Remove unused imports in static_invoice.rs 2024-06-20 14:24:09 -04:00
Valentine Wallace
6553d15aad
Add OffersMessage variant for static invoices. 2024-06-20 14:24:09 -04:00
Valentine Wallace
691e10f01e
DRY responder instantiation for onion messages. 2024-06-20 14:24:08 -04:00
Valentine Wallace
e8f154dd3c
AsyncPaymentsMessageHandler trait for OnionMessenger
Add a trait for handling async payments messages to OnionMessenger. This allows
users to either provide their own custom handling for async payments messages
or rely on a version provided by LDK.
2024-06-20 14:24:03 -04:00
Matt Corallo
07d991c82f
Merge pull request #2966 from G8XSU/2647-distribute
Optimize ChannelMonitor persistence on block connections.
2024-06-20 09:51:54 -07:00
Matt Corallo
38285167b1
Merge pull request #3127 from wvanlint/non_strict_forwarding
Implement non-strict forwarding
2024-06-20 07:26:43 -07:00
Valentine Wallace
445ec8d508
Onion message payload for async payments
Async payments uses onion messages to indicate when HTLCs are held and
released. Add these types along with the necessary parsing and encoding.
2024-06-20 10:09:03 -04:00
Willem Van Lint
05e6252b20 Implement non-strict forwarding
This change implements non-strict forwarding, allowing the node to
forward an HTLC along a channel other than the one specified
by short_channel_id in the onion message, so long as the receiver has
the same node public key intended by short_channel_id
([BOLT](57ce4b1e05/04-onion-routing.md (non-strict-forwarding))).
This can improve payment reliability when there are multiple channels
with the same peer e.g. when outbound liquidity is replenished by
opening a new channel.

The implemented forwarding strategy now chooses the channel with the
lowest outbound liquidity that can forward an HTLC to maximize the
probability of being able to successfully forward a subsequent HTLC.

Fixes #1278.
2024-06-19 21:24:21 -07:00
Elias Rohrer
f45a840c47
Merge pull request #3132 from jkczyz/2024-06-bolt12-unannounced
Blinded paths with unannounced introduction nodes
2024-06-19 14:42:08 +02:00
G8XSU
bf28957f6d
Optimize ChannelMonitor persistence on block connections.
Currently, every block connection triggers the persistence of all
ChannelMonitors with an updated best_block. This approach poses
challenges for large node operators managing thousands of channels.
Furthermore, it leads to a thundering herd problem
(https://en.wikipedia.org/wiki/Thundering_herd_problem), overwhelming
the storage with simultaneous requests.

To address this issue, we now persist ChannelMonitors at a
regular cadence, spreading their persistence across blocks to
mitigate spikes in write operations.

Outcome: After doing this, Ldk's IO footprint should be reduced
by ~50 times. The processing time required to sync each block
will be significantly reduced, particularly for nodes with 1000s
of channels, as write latency plays a significant role in this process.
As a result, the Node/ChainMonitor will be blocked for a shorter
duration, leading to further efficiency gains.
2024-06-19 00:04:23 -07:00
Gursharan Singh
87fc324c71
Merge pull request #3126 from TheBlueMatt/2024-06-preimage-removal-blocked-by-claim-event
(Very) marginally expand `test_monitor_update_fail_claim`
2024-06-18 13:31:38 -07:00
Matt Corallo
68af6d0809 (Very) marginally expand test_monitor_update_fail_claim
For some reason I wasn't sure this happened yet, so making the
test for no-`PaymentClaimed`-event-before-preimage-on-disk explicit
is nice.
2024-06-18 17:59:12 +00:00
Matt Corallo
c39ff87f44
Merge pull request #3115 from alecchendev/2024-06-specific-async-sign
Refactor async signing test utils to toggle specific method availability
2024-06-18 08:33:43 -07:00
Jeffrey Czyz
c1eda4ba3d
Relax channel count check for unannounced nodes
When creating blinded paths, introduction nodes are limited to peers
with at least three channels to prevent easily guessing the recipient.
Relax this check when the recipient is unannounced since they won't be
in the NetworkGraph.
2024-06-18 09:26:29 -05:00
Jeffrey Czyz
642913c586
Advance self blinded payment paths
DefaultRouter will ignore blinded paths where the sender is the
introduction node. Similar to message paths, advance such paths by one
so that payments may be sent to them.
2024-06-18 09:26:29 -05:00
Jeffrey Czyz
55ba2aab41
Re-order imports 2024-06-18 09:26:29 -05:00
Jeffrey Czyz
79122d69d4
Rename variable 2024-06-18 09:26:29 -05:00
Jeffrey Czyz
b6fcd84a13
Add advance_path_by_one for blinded payment paths
Similar to blinded paths for onion messages, if given a blinded payment
path where we are the introduction node, the path must be advanced by
one in order to use it.
2024-06-18 09:26:28 -05:00
Jeffrey Czyz
efc4647c65
Don't modify path when advance_path_by_one errors
When using advance_path_by_one when we are the introduction node, any
error will result having the first hop of the input blinded path
removed. Instead, only remove the first hop on success. Otherwise, the
path will be invalid.
2024-06-18 09:26:28 -05:00
Jeffrey Czyz
3bf84204e3
Blinded payments to unannounced introduction node
When creating blinded paths for receiving onion payments, allow using
the recipient's only peer as the introduction node when the recipient is
unannounced. This allows for sending payments without going through an
intermediary, which is useful for testing or when only connected to an
LSP with an empty NetworkGraph.
2024-06-18 09:26:28 -05:00
Jeffrey Czyz
7d1745e721
BlindedPath with unannounced introduction node
When creating blinded paths for receiving onion messages, allow using
the recipient's only peer as the introduction node when the recipient is
unannounced. This allows for sending messages without going through an
intermediary, which is useful for testing or when only connected to an
LSP with an empty NetworkGraph.
2024-06-18 09:26:26 -05:00
Elias Rohrer
249886497e
Merge pull request #3130 from TheBlueMatt/2024-06-doc-fee-present
Document when `Events::PaymentSent::pending_fee_msat` is `Some`
2024-06-18 14:49:38 +02:00
Alec Chen
21eeca4add Remove global availability logic for testing async signing 2024-06-17 16:34:56 -07:00
Alec Chen
1fb2b8e0ff Refactor tests to use specific async signing ops 2024-06-17 16:32:07 -07:00
Alec Chen
e8939e26bc Allow toggling specific signing methods in test channel signer 2024-06-17 16:32:06 -07:00
G8XSU
1912d8df01
Start tracking chain_sync_monitor_persistences in TestPersister
It is helpful to assert that chain-sync did trigger a monitor
persist.
2024-06-17 12:50:04 -07:00
valentinewallace
737df0f46f
Merge pull request #3128 from TheBlueMatt/2024-06-no-claim-ret
Drop unused return value from `claim_funds_from_hop`
2024-06-17 10:13:04 -04:00
Matt Corallo
f2005ba79d Document when Events::PaymentSent::pending_fee_msat is Some 2024-06-17 13:54:20 +00:00
Matt Corallo
fea6393083 Drop unused return value from claim_funds_from_hop 2024-06-16 14:15:07 +00:00
Duncan Dean
0881b24895
Move accept_channel checks into ChannelContext::do_accept_channel_checks
This is done ahead of getting the dual-funding implementation to reduce
refactoring noise there.
2024-06-14 10:52:32 +02:00
Valentine Wallace
66df329236
ser_macros: support messages with no fields 2024-06-13 12:20:43 -04:00
Elias Rohrer
3ec0dcdac1
Merge pull request #3078 from jkczyz/2024-05-invoice-event
Asynchronous `Bolt12Invoice` payment
2024-06-13 08:25:59 +02:00
Jeffrey Czyz
97c1d656ee
Add pays_bolt12_invoice_asynchronously test 2024-06-12 19:39:49 -05:00
Jeffrey Czyz
65efd92c4a
Expose send_payment_for_bolt12_invoice
UserConfig::manually_handle_bolt12_invoices allows deferring payment of
BOLT12 invoices by generating Event::InvoiceReceived. Expose
ChannelManager::send_payment_for_bolt12_invoice to allow users to pay
the Bolt12Invoice included in the event. While the event contains the
PaymentId for reference, that parameter is now removed from the method
in favor of extracting the PaymentId from the invoice's payer_metadata.
2024-06-12 19:38:22 -05:00
Jeffrey Czyz
232959cd27
Reformat and tick config docs 2024-06-12 19:38:21 -05:00
Jeffrey Czyz
a9dcfaf952
Add UserConfig::manually_handle_bolt12_invoices
BOLT12 invoices are automatically paid once they have been verified.
Users may want to manually pay them by first performing additional
checks. Add a manually_handle_bolt12_invoices configuration option that
when set generates an Event::InvoiceReceived instead of paying the
invoice.
2024-06-12 19:38:16 -05:00
Matt Corallo
f267ffe2fc
Merge pull request #3082 from valentinewallace/2024-04-bolt12-keysend-invoice
BOLT 12 static invoice encoding and building
2024-06-12 15:17:48 -07:00
Matt Corallo
08c566ccfb
Merge pull request #3103 from TheBlueMatt/2024-06-real-bench
Use a real (probing-generated) scorer in benchmarks
2024-06-12 15:17:25 -07:00
Valentine Wallace
bafe4ed218
Static invoice parsing tests 2024-06-12 16:42:29 -04:00
Valentine Wallace
7970de47a6
Static invoice building tests 2024-06-12 16:42:29 -04:00
Valentine Wallace
1e58066868
Builder for creating static invoices from offers
Add a builder for creating static invoices for an offer. Building produces a
semantically valid static invoice for the offer, which can then be signed with
the key associated with the offer's signing pubkey.
2024-06-12 16:42:27 -04:00
Jeffrey Czyz
4666c33c0f
Add an InvoiceReceived event
Some users may want to handle a Bolt12Invoice asynchronously, either in
a different process or by first performing additional verification
before paying the invoice. Add an InvoiceReceived event to facilitate
this.
2024-06-12 15:24:08 -05:00
Matt Corallo
7ae311d64c Drop unnecessary key derivation in route tests
In our route tests we need some "random" bytes for the router to
randomize amounts using. We generate this by building an actual
`KeysManager` and then deriving some random bytes using the
`EntropySource` trait. However, `get_route` (what we're normally
testing) doesn't actually use the random bytes, and even if it did,
using a `KeysManager` is just a fancy way of creating a constant,
so there's really no reason to do all the fancy crypto.

Instead, here, we change our routing tests and benchmarks to simply
use `[42; 32]` as the "random" bytes.
2024-06-12 19:35:06 +00:00