Commit graph

5120 commits

Author SHA1 Message Date
Alec Chen
1f7f3a366c Change get_per_commitment_point to return result type
Includes simple changes to test util signers and tests, as well as
handling the error case for get_per_commitment_point in
HolderCommitmentPoint. This leaves a couple `.expect`s in places
that will be addressed in a separate PR for handling funding.
2024-07-15 14:28:17 -07:00
Alec Chen
1fa67d9434 Add logger as parameter in creating ChannelContext 2024-07-15 11:41:22 -07:00
Alec Chen
9c2a050a35 Return an error if we fail to advance our commitment number 2024-07-15 11:41:19 -07:00
Matt Corallo
ddb40bd04c
Merge pull request #3173 from dunxen/2024-07-cargocheckcfg
Use native check-cfg lint in cargo beta
2024-07-15 11:33:08 -07:00
Alec Chen
516e5e6f58 Split HolderCommitmentPoint::advance off into separate function 2024-07-15 11:30:37 -07:00
shaavan
957b33712a Introduce tests to check verify reply path diversification 2024-07-12 19:45:16 +05:30
Duncan Dean
99aa6e27f6
Use native check-cfg lint in cargo beta
This uses the newly introduced conditional configuration checks that are
now configurable withint Cargo (beta).

This allows us to get rid of our custom python script that checks for
expected features and cfgs.

This does introduce a warning regarding the unknown lint in Cargo
versions prior to the current beta, but since these are not rustc errors,
they won't break any builds with the "-D warnings" RUSTFLAG.

Moving to this lint actually exposed the "strict" feature not being
present in the lightning-invoice crate, as our python script didnt
correctly parse the cfg_attr where it appeared.
2024-07-12 11:48:15 +02:00
Elias Rohrer
5a39cc2b19
Revert "Signal GossipQuery support when using IgnoringMessagHandler"
This reverts commit 843079df72.
2024-07-12 10:49:16 +02:00
Jeffrey Czyz
c8fd681d4f
Refactor initial route finding to separate method
This will be used later in send_payment_for_bolt12_invoice instead of
find_route_and_send_payment as it will allow for returning
RetryableSendFailure when path finding fails.
2024-07-11 18:05:10 -05:00
Jeffrey Czyz
aa55366b37
Add Bolt12PaymentError::SendingFailed variant
Instead of returning Ok when path finding fails, allow returning a
RetryableSendFailure from send_payment_for_bolt12_invoice. Follow up
commits will return such failures.
2024-07-11 18:01:20 -05:00
shaavan
887e99900c Update extract_invoice to also return the reply_path.
- This will be utilised in the following commit for a test.
2024-07-11 19:14:53 +05:30
shaavan
afabf7bc44 Expand create_blinded_path Functionality for Enhanced Path Diversification
- Previously, the `create_blinded_path` function was limited to
  returning a single `BlindedPath`, which restricted the usage of
  `blinded_paths`.
- This commit extends the `create_blinded_path` function to return
  the entire blinded path vector generated by the `MessageRouter`'s
  `create_blinded_paths`.
- The updated functionality is integrated across the codebase, enabling
  the sending of Offers Response messages, such as `InvoiceRequest`
  (in `pay_for_offer`) and `Invoice` (in `request_refund_payment`),
  utilizing multiple reply paths.
2024-07-11 19:12:34 +05:30
Matt Corallo
f689e01b35 Layout channel info to ensure routing uses cache lines well
Because we scan per-channel information in the hot inner loop of
our routefinding immediately after looking a channel up in a
`HashMap`, we end up spending a nontrivial portion of our
routefinding time waiting on memory to be read in.

While there is only so much we can do about that, ensuring the
channel information that we care about is sitting on one or
adjacent cache lines avoids paying that penalty twice. Thus, here
we manually lay out `ChannelInfo` and `ChannelUpdateInfo` and set
them to 128b and 32b alignment, respectively. This wastes some
space in memory in our network graph, but improves routing
performance in return.
2024-07-10 19:38:02 +00:00
Matt Corallo
bed1fb073b Consolidate candidate access in add_entry during routing
Because fetching fields from the `$candidate` often implies an
indirect read, grouping them together may result in one or two
fewer memory loads, so we do so here.
2024-07-10 19:38:02 +00:00
Matt Corallo
9566c272c8 Somewhat optimize the generic Features::requires_unknown_bits
It turns out we spend several percent of our routefinding time just
checking if nodes and channels require unknown features
byte-by-byte. While the cost is almost certainly dominated by the
memory read latency, avoiding doing the checks byte-by-byte should
reduce the branch count slightly, which may reduce the overhead.
2024-07-10 19:38:02 +00:00
Matt Corallo
98f9e8bbf8 Store source/target node_counters in DirectionalChannelInfo
Because we now have some slack space in `PathBuildingHop`, we can
use it to cache some additional hot values. Here we use it to
cache the source and target `node_counter`s for public channels,
effectively prefetching the values from the channel state.
2024-07-10 19:38:02 +00:00
Matt Corallo
3e902401e0 DRY redundant calls to $candidate.htlc_minimum_msat() in routing
While LLVM should inline and elide the redundant calls, because the
router is rather large LLVM can decide against inlining in some
cases where it would be an nice win.

Thus, its worth DRY'ing the redundant calls explicitly.
2024-07-10 19:38:02 +00:00
Matt Corallo
df9c15de75 Cache whether a node is a first-hop target in the per-node state
When processing the main loop during routefinding, for each node,
we check whether it happens to be our peer in one of our channels.
This ensures we never fail to find a route that takes a hop through
a private channel of ours, to a private node, then through
invoice-provided route hints to reach the ultimate payee.

Because this is incredibly hot code, doing a full `HashMap` lookup
to check if each node is a first-hop target ends up eating a good
chunk of time during routing. Luckily, we can trivially avoid this
cost.

Because we're already looking up the per-node state in the `dist`
map, we can store a bool in each first-hop target's state, avoiding
the lookup unless we know its going to succeed.

This requires storing a dummy entry in `dist`, which feels somewhat
strange, but is ultimately fine as we should never be looking at
per-node state unless we've already found a path to that node,
updating the fields in doign so.
2024-07-10 19:38:02 +00:00
valentinewallace
78c0eaae55
Merge pull request #3104 from TheBlueMatt/2024-06-routing-counters 2024-07-10 10:03:53 -04:00
Matt Corallo
4f5e17b54d Move blinded path introduction point resolution to a helper method
This marginally reduces the size of `get_route` by moving a the
blinded path introduction point resolution and blinded path checks
into a helper method.
2024-07-10 01:31:48 +00:00
Matt Corallo
5fb66377ef Align PathBuildingHop to 128b, now that we store them in a Vec
Now that `PathBuildingHop` is stored in a `Vec` (as `Option`s),
rather than `HashMap` entries, they can grow to fill a full two
cache lines without a memory access performance cost. In the next
commit we'll take advantage of this somewhat, but here we update
the assertions and drop the `repr(C)`, allowing rust to lay the
memory out as it wishes.
2024-07-10 01:31:48 +00:00
Matt Corallo
43d250dadc Drop the dist HashMap in routing, replacing it with a Vec.
Now that we have unique, dense, 32-bit identifiers for all the
nodes in our network graph, we can store the per-node information
when routing in a simple `Vec` rather than a `HashMap`. This avoids
the overhead of hashing and table scanning entirely, for a nice
"simple" optimization win.
2024-07-10 01:31:48 +00:00
Matt Corallo
c34980c47f Use NodeCounters NodeIds as the blinded path intro references
The router's `introduction_node_id_cache` is used to cache the
`NodeId`s of blinded path introduction points so that we don't
have to look them up every time we go around the main router loop.
When using it, if the introduction point isn't a public node we
then look up the introduction in our first-hops map.

In either case, we have to end up with a reference to a `NodeId`
that outlives our `dist` map.

Here we consolidate both the initial cache building and the
first-hops map lookup to one place, storing only a reference to a
`NodeId` either in the `NetworkGraph` or in the new `NodeCounters`
to get the required lifetime without needing to reference into the
first-hops map.

We then take this opportunity to avoid `clone`ing the first-hops
map entries as we now no longer reference into it.
2024-07-10 01:31:48 +00:00
Matt Corallo
04dad438d7 Drop private_hop_key_cache in favor of NodeCounters
With the new `NodeCounters` have have a all the `NodeId`s we'll
need during routing, so there's no need to keep the
`private_hop_key_cache` which existed to provide references to
`NodeId`s which are needed during routing.
2024-07-10 01:31:48 +00:00
Matt Corallo
c6cd3c8176 Add a new NodeCounters utility to track counters when routing
In the next commit we'll stop using `NodeId`s to look up nodes when
routing, instead using the new per-node counters. Here we take the
first step, adding a local struct which tracks temporary counters
for route hints/source/destination nodes.

Because we must ensure we have a 1-to-1 mapping from node ids to
`node_counter`s, even across first-hop and last-hop hints, we have
to be careful to check the network graph first, then a new
`private_node_id_to_node_counter` map to ensure we only ever end up
with one counter per node id.
2024-07-10 01:31:48 +00:00
Matt Corallo
6035c83a1d
Merge pull request #3085 from shaavan/issue2837
Introduce RecipientData and use it to allow abandon failed payments
2024-07-09 09:16:15 -07:00
Matt Corallo
1a71725450
Merge pull request #3161 from vincenzopalazzo/macros/fix-docs
meta: update docs to reflect the code
2024-07-09 08:28:07 -07:00
Vincenzo Palazzo
eb7f2e3459
meta: update docs to reflect the code
The code documentation is outdated after the commit 935a716cc6

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2024-07-09 16:55:38 +02:00
Matt Corallo
f48a273211
Merge pull request #3149 from alecchendev/2024-06-async-sign-cs-raa-order
Fix bug failing CS-RAA resend order on pending commitment signatures
2024-07-09 06:48:08 -07:00
shaavan
42c096c491 Allow create_blinded_paths functions to accept MessageContext as input field
- Enabled `create_blinded_paths` to accept `MessageContext` TLVs as
  an input field.
- `MessageContext` is intended to be sent along with the `reply_path`
   to the counterparty.
- Added `MessageContext` in the `create_blinded_paths` flow, optionally
  appending it within the `reply_path`.
- Updated tests to verify the new feature.
2024-07-09 17:27:11 +05:30
shaavan
fbe9dfa938 Update handle_message to accept OffersContext data as an input field
1. Handling Offers Data:
   - Updated `handle_message` to accept `OffersContext` data as an input field.
   - If it is present, it will be utilized by the handler to
     abandon outbound payments that have failed for any reason.

2. Consistency in Custom Message Handling:
   - Updated `handle_custom_message` to accept optional custom data.
     for consistency.
   - Note: `custom_data` will remain unused in this PR.
2024-07-09 17:27:08 +05:30
Alec Chen
315193daec Fix bug failing CS-RAA resend order on pending commitment signatures 2024-07-08 17:35:25 -07:00
Matt Corallo
bcacf85a79
Merge pull request #3120 from TheBlueMatt/2023-11-mon-claim-bug
Block monitor updates to ensure preimages are in each MPP part
2024-07-08 13:50:06 -07:00
shaavan
7f82cde861 Introduce MessageContext in ReceiveTlvs
1. New Enum for Enhanced Data Handling:
   - Introduced the `MessageContext` enum, which allows the node to include
     additional context data along with the `reply_path` sent to the counterparty.
   - The node anticipates receiving this data back for further processing.

2. Variants in MessageContext:
   - The `MessageContext` enum includes two variants: "Offers" and
     "Context"
   - One of the variants, `Offers`, holds the `payment_id`
     of the associated Outbound BOLT12 Payment.

3. Future Usage:
   - This enum will be utilized in a subsequent commit to abandon outbound
     payments that have failed to complete for various reasons.
2024-07-09 01:45:13 +05:30
shaavan
7e2fde7054 Remove path_id from Responder, and OnionMessageResponse struct
1. The path_id will be removed from the codebase in the following
   commits.
2024-07-09 01:44:14 +05:30
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
Matt Corallo
40283e7153
Merge pull request #3151 from alecchendev/2024-06-async-test-util-followup
Async signing test util follow ups
2024-07-08 11:28:16 -07:00
jbesraa
8403755a2a
Add FundingTxBroadcastSafe event
The `FundingTxBroadcastSafe` event indicates that we have received
`funding_signed` message from our counterparty and that you should
broadcast the funding transaction.

This event is only emitted if upon generating the funding transaction
you call `ChannelManager::unsafe_manual_funding_transaction_generated`
that will emit this event instead of `ChannelPending` event.

`ChannelManager::unsafe_manual_funding_transaction_generated` wont check
if the funding transaction is signed, those its unsafe. It is manual
because you are responsibile on broadcasting the transaction once the
event is received.
2024-07-08 20:10:19 +03:00
valentinewallace
bfc20f8f34
Merge pull request #3156 from valentinewallace/2024-07-b12-max-path-len
Set max path length when paying BOLT 12 invoices.
2024-07-08 12:45:20 -04:00
Valentine Wallace
678aac0f0f
Set max path length when paying BOLT 12 invoices. 2024-07-08 10:35:48 -04:00
Duncan Dean
8400cfc0d8
Fix CI for unused PendingOnionMessage 2024-07-04 17:08:16 +02:00
Valentine Wallace
c8888e6d66
Test router: support expecting blinded payment paths. 2024-07-03 14:31:13 -04:00
Valentine Wallace
c239b1c899
Remove spurious commented out code line. 2024-07-03 14:28:20 -04:00
Alec Chen
3775edd7ee Allow sending RAA upon unblocking the signer 2024-07-02 19:11:44 -07:00
Matt Corallo
46ba6a8fbb Fix spelling in comment
From @valentinewallace at
https://github.com/lightningdevkit/rust-lightning/pull/3144#discussion_r1661404243
2024-07-02 22:18:17 +00:00
Matt Corallo
8d240cfe3e
Merge pull request #3144 from TheBlueMatt/2024-06-message-flags
(Re-)add handling for `ChannelUpdate::message_flags`
2024-07-02 15:16:59 -07:00