Commit graph

4992 commits

Author SHA1 Message Date
Jeffrey Czyz
c58a1bbaa5
Clean up MessageContext docs 2024-07-22 11:34:02 -05:00
Jeffrey Czyz
c0cae08d66
Assert and document valid Metadata states
Metadata is an internal type used within Offer messages. For any
constructed message, Metadata::Bytes is always used. The other variants
are used during construction or verification time. Document this and
debug_assert!(false) accordingly.
2024-07-22 11:34:02 -05:00
Jeffrey Czyz
e156415048
Add InvoiceRequest::verify_using_recipient_data
Invoice requests are authenticated by checking the metadata in the
corresponding offer. For offers using blinded paths, this will simply be
a 128-bit nonce. Allows checking this nonce explicitly instead of the
metadata. This will be used by an upcoming change that includes the
nonce in the offer's blinded paths instead of the metadata, which
mitigate de-anonymization attacks.
2024-07-22 11:34:02 -05:00
Jeffrey Czyz
219691f9ef
Pass Nonce directly to OfferBuilder
When using OfferBuilder::deriving_signing_pubkey, the nonce generated
needs to be the same one included in any OfferBuilder::paths. This is
because the nonce is used along with the offer TLVs to derive a signing
pubkey and will soon be elided from the metadata entirely.
2024-07-22 11:34:02 -05:00
Jeffrey Czyz
d7aeaa0aad
Move Nonce to a separate offers sub-module
Nonce is used when constructing Offer::metadata and will soon be need
when constructing BlindedPath for use in authentication. Move it to
separate module now that it is public and will be more widely used.
2024-07-22 11:34:01 -05:00
Jeffrey Czyz
0a5918e477
Reorder imports 2024-07-22 11:34:01 -05:00
Jeffrey Czyz
5278d314d5
Change Nonce visibility to pub
A nonce is generated in OfferBuilder::deriving_signing_pubkey from an
EntropySource for use in Offer::metadata. The same nonce will need to be
included as recipient data in any blinded paths in the Offer. Increase
the visibility to allow for this.
2024-07-22 11:34:01 -05:00
Elias Rohrer
37331035f9
Merge pull request #3024 from jbesraa/funding-signed-event
Funding signed event
2024-07-22 15:47:59 +02:00
Matt Corallo
9ce3dd5269
Merge pull request #3152 from alecchendev/2024-06-async-commit-secret-raa
Handle fallible commitment secret
2024-07-18 15:17:57 +00:00
Elias Rohrer
e617a394e8
Document Failure Behavior and Persistence for every event type 2024-07-18 15:54:21 +02:00
Elias Rohrer
f5cea0e57a
Handle fallible events in OnionMessenger
Previously, we would just fire-and-forget in `OnionMessenger`'s event
handling. Since we now introduced the possibility of event handling
failures, we here adapt the event handling logic to retain any
events which we failed to handle to have them replayed upon the next
invocation of `process_pending_events`/`process_pending_events_async`.
2024-07-18 15:54:21 +02:00
Elias Rohrer
018908fe9e
Make event handling fallible
Previously, we would require our users to handle all events
successfully inline or panic will trying to do so. If they would exit
the `EventHandler` any other way we'd forget about the event and
wouldn't replay them after restart.

Here, we implement fallible event handling, allowing the user to return
`Err(())` which signals to our event providers they should abort event
processing and replay any unhandled events later (i.e., in the next
invocation).
2024-07-18 15:54:21 +02:00
Elias Rohrer
b5b57f188f
Hold sep. Mutexes for pending intercepted_msgs/peer_connected events
This is a minor refactor that will allow us to access the individual
event queue Mutexes separately, allowing us to drop the locks earlier
when processing them individually.
2024-07-18 09:05:43 +02:00
Matt Corallo
0cfe55c007
Merge pull request #3160 from TheBlueMatt/2024-07-better-enum-upgradable-ser
Make `impl_writeable_tlv_based_enum*` actually upgradable
2024-07-17 18:05:22 +00:00
Alec Chen
34130327c5 Test fallible commitment secret 2024-07-17 10:57:51 -07:00
Matt Corallo
72f883e0a1 Make impl_writeable_tlv_based_enum* actually upgradable
In cc78b77c71 it was discovered that
`impl_writeable_tlv_based_enum_upgradable` wasn't actually
upgradable - tuple variants weren't written with length-prefixes,
causing downgrades with new tuple variants to be unreadable by
older clients as they wouldn't know where to stop reading.

This was fixed by simply assuming that any new variants will be
non-tuple variants with a length prefix, but no code write-side
changes were made, allowing new code to freely continue to use the
broken tuple-variant serialization.

Here we address this be defining yet more serialization macros
which aren't broken, and convert existing usage of the existing
macros using non-length-prefixed tuple variants to renamed
`*_legacy` macros.

Note that this changes the serialization format of
`impl_writeable_tlv_based_enum[_upgradable]` when tuple fields are
written, and as such deliberately changes the call semantics for
such tuples.

Only the serialization format of `MessageContext` is changed here
which is fine as it has not yet reached a release of LDK.
2024-07-17 15:02:13 +00:00
Matt Corallo
7282aa7a8d Drop excess ()s in router.rs 2024-07-17 14:09:47 +00:00
Matt Corallo
63ecd902db Marginally improve comments around cache lines in gossip.rs 2024-07-17 14:09:47 +00:00
Matt Corallo
ac1463b120
Merge pull request #2803 from TheBlueMatt/2023-12-routing-dist-vec
Misc routing optimization
2024-07-17 14:06:05 +00:00
Alec Chen
5b3d6eabce Handle fallible release_commitment_secret 2024-07-16 15:49:59 -07:00
Matt Corallo
6ed398d42a
Merge pull request #3087 from shaavan/reply_path_diversity
Allow blinded path diversification by expanding `create_blinded_paths`
2024-07-16 19:29:08 +00:00
Matt Corallo
17d5baa67f
Merge pull request #3150 from alecchendev/2024-06-async-commit-point-raa
Handle fallible per commitment point for RAA
2024-07-16 06:30:57 -07:00
Alec Chen
45c0a0f10c Test async get per commitment point for revoke_and_ack
Note: this does not test the CS -> RAA resend ordering, because this
requires handling async get_per_commitment_point for channel
reestablishment, which will be addressed in a follow up PR.
2024-07-15 14:28:23 -07:00
Alec Chen
614da40f19 Allow failing revoke_and_ack if commitment point is not available 2024-07-15 14:28:21 -07:00
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
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