Commit graph

4890 commits

Author SHA1 Message Date
Matt Corallo
b536d01702 Implement PartialEq/Eq for Events 2023-01-26 01:52:10 +00:00
Matt Corallo
abbd295157
Merge pull request #1948 from alecchendev/custom-fail-back-err
Allow specifying an error when failing back HTLC
2023-01-25 23:24:49 +00:00
Matt Corallo
ca5b10884e
Merge pull request #1799 from TheBlueMatt/2022-10-heap-nerdsnipe
Router Optimizations
2023-01-25 23:19:13 +00:00
Alec Chen
48aef2da9e Add test_fail_htlc_backwards_with_reason
Add a test for newly added function failing back a basic payment
and ensuring the intended failure code and data are sent back
to the peer.
2023-01-25 15:36:04 -06:00
Alec Chen
95892e37da Add FailureCode enum and ChannelManager::fail_htlc_backwards_with_reason
FailureCode is used to specify which error code and data to send
to peers when failing back an HTLC.

ChannelManager::fail_htlc_backwards_with_reason
allows a user to specify the error code and
corresponding data to send to peers when failing back an HTLC.
This function is mentioned in Event::PaymentClaimable docs.
ChannelManager::get_htlc_fail_reason_from_failure_code was also
added to assist with this function.
2023-01-25 15:35:59 -06:00
Valentine Wallace
6d819796f2
Disambiguate send_payment_internal from pay_internal 2023-01-25 14:44:10 -05:00
Valentine Wallace
ad486a4596
Payment retries: copy tests from InvoicePayer
As part of migrating payment retries from InvoicePayer to ChannelManager,
several tests don't need a rewrite and can be pretty much copied and pasted.
2023-01-25 14:44:10 -05:00
Valentine Wallace
2f49c8170c
Test ChannelManager automatic retries 2023-01-25 14:44:10 -05:00
Valentine Wallace
acf9292d58
Support sending payments with a retry strategy in ChannelManager 2023-01-25 14:44:10 -05:00
Valentine Wallace
d776dee3a5
Retry HTLCs in process_pending_htlc_forwards 2023-01-25 14:44:07 -05:00
Valentine Wallace
72a7da8d51
Remove AllPathsFailed outbounds at send_payment_internal callsites instead
This makes it easier to retry payments if all paths fail on initial send, in
in which case we'll want to hold off on removing the pending payment
2023-01-25 14:44:07 -05:00
Valentine Wallace
686ef08316
Generate PendingHTLCsForwardable upon retryable payment 2023-01-25 14:44:07 -05:00
Valentine Wallace
6351a99935
Decode onion fail outside of outbound_payments lock
It's not ideal to do all this computation while the lock is held. We also want
to decode the failure *before* taking the lock, so we can store the failed scid
in the relevant outbound for retry in the next commit(s).
2023-01-25 14:44:07 -05:00
Valentine Wallace
c0a22f7174
Store retry data in PendingOutboundPayment::Retryable
Used in upcoming commit(s) to automatically retry HTLCs in ChannelManager
2023-01-25 14:44:03 -05:00
Matt Corallo
bde841e928 Clean up compute_fees and add a saturating variant
Often when we call `compute_fees` we really just want it to
saturate and we deal with `u64::max_value` later. In that case,
we're much better off doing the saturating in the `compute_fees` as
it can use CMOVs rather than branching at each step and then
`unwrap_or`ing at the callsite.
2023-01-25 18:58:51 +00:00
Matt Corallo
e64b5d9d2e Add a fuzzer to check that IndexedMap is equivalent to BTreeMap 2023-01-25 18:58:51 +00:00
Matt Corallo
039fa5255d Swap IndexedMap implementation for a HashMap+B-Tree
Our network graph has to be iterable in a deterministic order and
with the ability to iterate over a specific range. Thus,
historically, we've used a `BTreeMap` to do the iteration. This is
fine, except our map needs to also provide high performance lookups
in order to make route-finding fast. Sadly, `BTreeMap`s are quite
slow due to the branching penalty.

Here we replace the implementation of our `IndexedMap` with a
`HashMap` to store the elements itself and a `BTreeSet` to store
the keys set in sorted order for iteration.

As of this commit on the same hardware as the above few commits,
the benchmark results are:

```
test routing::router::benches::generate_mpp_routes_with_probabilistic_scorer ... bench: 109,544,993 ns/iter (+/- 27,553,574)
test routing::router::benches::generate_mpp_routes_with_zero_penalty_scorer  ... bench:  81,164,590 ns/iter (+/- 55,422,930)
test routing::router::benches::generate_routes_with_probabilistic_scorer     ... bench:  34,726,569 ns/iter (+/- 9,646,345)
test routing::router::benches::generate_routes_with_zero_penalty_scorer      ... bench:  22,772,355 ns/iter (+/- 9,574,418)
```
2023-01-25 18:58:51 +00:00
Matt Corallo
1bd35367d8 Add a new IndexedMap type and use it in network graph storage
Our network graph has to be iterable in a deterministic order and
with the ability to iterate over a specific range. Thus,
historically, we've used a `BTreeMap` to do the iteration. This is
fine, except our map needs to also provide high performance lookups
in order to make route-finding fast. Sadly, `BTreeMap`s are quite
slow due to the branching penalty.

Here we replace the `BTreeMap`s in the scorer with a dummy wrapper.
In the next commit the internals thereof will be replaced with a
`HashMap`-based implementation.
2023-01-25 18:58:51 +00:00
Matt Corallo
a3f7b790b4 Drop A* implementation in the router for simple Dijkstra's
As evidenced by the previous commit, it appears our A* router
does worse than a more naive approach. This isn't super surpsising,
as the A* heuristic calculation requires a map lookup, which is
relatively expensive.

```
test routing::router::benches::generate_mpp_routes_with_probabilistic_scorer ... bench: 169,991,943 ns/iter (+/- 30,838,048)
test routing::router::benches::generate_mpp_routes_with_zero_penalty_scorer  ... bench: 122,144,987 ns/iter (+/- 61,708,911)
test routing::router::benches::generate_routes_with_probabilistic_scorer     ... bench:  48,546,068 ns/iter (+/- 10,379,642)
test routing::router::benches::generate_routes_with_zero_penalty_scorer      ... bench:  32,898,557 ns/iter (+/- 14,157,641)
```
2023-01-25 17:17:55 +00:00
Matt Corallo
828c776746
Merge pull request #1983 from valentinewallace/2022-01-update-contributingmd 2023-01-25 01:59:52 +00:00
Matt Corallo
8bb661449e
Merge pull request #1878 from dunxen/2022-11-config-minfinalcltvexpiry
Allow configurable `min_final_cltv_expiry_delta`
2023-01-24 22:35:55 +00:00
Valentine Wallace
0fee207509
contributing.md: fix link and link to CoC 2023-01-24 17:33:14 -05:00
Arik
45a6f3392d
Merge pull request #1967 from arik-so/2023-01-rename-signer-traits
Split out `EcdsaChannelSigner` method from `BaseSign`, and rename it to `ChannelSigner`
2023-01-24 13:20:26 -08:00
Valentine Wallace
ae34533932
Parameterize add_new_pending_payment with retry strategy and route params 2023-01-24 14:12:04 -05:00
Valentine Wallace
cbb75e27b2
Copy Retry from invoice module to outbound_payment module
Also configure it such that in std tests, it will use SinceEpoch instead of
Instant so time can be manually advanced.
2023-01-24 14:11:59 -05:00
Duncan Dean
5b53670172
Add new payment type and metadata bytes
Adds two new payment `Method`s for identifying payments with custom
`min_final_cltv_expiry_delta` as payments with LDK or user payment
hashes.

The `min_final_cltv_expiry_delta` value is packed into the first 2
bytes of the expiry timestamp in the payment secret metadata.
2023-01-24 21:01:27 +02:00
Duncan Dean
1d72e87e78
Add min_final_cltv_expiry parameter to invoice utils
All utility functions for invoice construction will now also accept an
Option<>al `min_final_cltv_expiry_delta` which is useful for things like
swaps etc. The `min_final_cltv_expiry_delta` will default back to
`MIN_FINAL_CLTV_EXPIRY_DELTA` if `None` is provided.
2023-01-24 13:07:46 +02:00
Duncan Dean
fb10fc9590
Add _delta suffix to min_final_cltv_expiry
This matches the spec and helps avoid any confusion around
naming. We're also then consistent with `cltv_expiry` in an HTLC being
the actual block height value for the CLTV and not a delta.
2023-01-24 13:07:23 +02:00
Arik Sosman
712c60e1c7
Make Channel and ChannelManager less particular about their Signer type. 2023-01-23 15:48:39 -08:00
Arik Sosman
394491115c
Rename Sign to WriteableEcdsaChannelSigner. 2023-01-23 15:48:38 -08:00
Arik Sosman
30c45469e5
Separate channel-type-agnostic methods into ChannelSigner trait. 2023-01-23 15:48:26 -08:00
Arik Sosman
1b245da370
Rename BaseSign to EcdsaChannelSigner. 2023-01-23 15:47:43 -08:00
Matt Corallo
869b71dccf impl Display for NodeId
`NodeId` is a public key, there's not much reason to not implement
`Display` for it and only `Debug`.
2023-01-23 23:39:40 +00:00
Matt Corallo
e0a0add9fe
Merge pull request #1926 from jkczyz/2022-12-invoice
BOLT 12 `invoice` encoding and building
2023-01-23 23:18:09 +00:00
Valentine Wallace
948ebc67f0
Copy PaymentAttempts from invoice module to outbound_payment module 2023-01-23 15:12:09 -05:00
Elias Rohrer
99fe52e93c
Expose impl_writeable_msg 2023-01-20 19:03:59 -06:00
Jeffrey Czyz
15f12953b2
Fix unused_imports warning in no-std tests 2023-01-20 16:04:39 -06:00
Jeffrey Czyz
a452551480
Invoice parsing tests
Tests for checking invoice semantics when parsing invoice bytes as
defined by BOLT 12.
2023-01-20 16:04:39 -06:00
Jeffrey Czyz
bf1147f23f
Invoice building tests
Tests for checking invoice message semantics when building an invoice as
defined by BOLT 12.
2023-01-20 16:04:39 -06:00
Jeffrey Czyz
fe83aede0c
Expand invoice module docs and include an example 2023-01-20 16:04:38 -06:00
Jeffrey Czyz
7f52d26e6e
Use SystemTime::now() for Invoice creation time
For std builds, Invoice::created_at can be automatically set upon
construction using SystemTime::now() offset by SystemTime::UNIX_EPOCH.
Change InvoiceRequest::respond_with and Refund::respond_with to only
take a created_at parameter in no-std builds.
2023-01-20 16:04:38 -06:00
Jeffrey Czyz
f779bc066f
Builder for creating invoices for refunds
Add a builder for creating invoices for a refund and required fields.
Other settings are optional and duplicative settings will override
previous settings. Building produces a semantically valid `invoice`
message for the refund, which then can be signed with the key associated
with the provided signing pubkey.
2023-01-20 16:04:38 -06:00
Jeffrey Czyz
88c5197e44
Builder for creating invoices for offers
Add a builder for creating invoices for an offer from a given request
and required fields. Other settings are optional and duplicative
settings will override previous settings. Building produces a
semantically valid `invoice` message for the offer, which then can be
signed with the key associated with the offer's signing pubkey.
2023-01-20 16:04:37 -06:00
Jeffrey Czyz
e1aa18aed8
Invoice encoding and parsing
Define an interface for BOLT 12 `invoice` messages. The underlying
format consists of the original bytes and the parsed contents.

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

Invoices may be created for an Offer (from an InvoiceRequest) or for a
Refund. The primary difference is how the signing pubkey is given -- by
the writer of the offer or the reader of the refund.
2023-01-20 16:04:37 -06:00
Matt Corallo
efdd2217b7 Update min-inbound-fee values on NetworkGraph load
Historically we've had various bugs in keeping the
`lowest_inbound_channel_fees` field in `NodeInfo` up-to-date as we
go. This leaves the A* routing less efficient as it can't prune
hops as aggressively.

In order to get accurate benchmarks, this commit updates the
minimum-inbound-fees field on load. This is not the most efficient
way of doing so, but suffices for fetching benchmarks and will be
removed in the coming commits.

Note that this is *slower* than the non-updating version in the
previous commit. While I haven't dug into this incredibly deeply,
the graph snapshot in use has min-fee info for only 9,618 of
20,818 nodes. Thus, it is my guess that with the graph snapshot
as-is the branch predictor is able to largely remove the A*
heuristic lookups, but with this change it is forced to wait for
A* heuristic map lookups to complete, causing a performance
regression.

```
test routing::router::benches::generate_mpp_routes_with_probabilistic_scorer ... bench: 182,980,059 ns/iter (+/- 32,662,047)
test routing::router::benches::generate_mpp_routes_with_zero_penalty_scorer  ... bench: 151,170,457 ns/iter (+/- 75,351,011)
test routing::router::benches::generate_routes_with_probabilistic_scorer     ... bench:  58,187,277 ns/iter (+/- 11,606,440)
test routing::router::benches::generate_routes_with_zero_penalty_scorer      ... bench:  41,210,193 ns/iter (+/- 18,103,320)
```
2023-01-19 19:10:05 +00:00
Matt Corallo
608c8adfd5 Update the lightning graph snapshot used in benchmarks
The previous copy was more than one and a half years old, the
lightning network has changed a lot since!

As of this commit, performance on my Xeon W-10885M with a
SK hynix Gold P31 storing a BTRFS volume is as follows:

```
test ln::channelmanager::bench::bench_sends                                  ... bench:   5,896,492 ns/iter (+/- 512,421)
test routing::gossip::benches::read_network_graph                            ... bench: 1,645,740,604 ns/iter (+/- 47,611,514)
test routing::gossip::benches::write_network_graph                           ... bench: 234,870,775 ns/iter (+/- 8,301,775)
test routing::router::benches::generate_mpp_routes_with_probabilistic_scorer ... bench: 166,155,032 ns/iter (+/- 30,206,162)
test routing::router::benches::generate_mpp_routes_with_zero_penalty_scorer  ... bench: 136,843,661 ns/iter (+/- 67,111,218)
test routing::router::benches::generate_routes_with_probabilistic_scorer     ... bench:  52,954,598 ns/iter (+/- 11,360,547)
test routing::router::benches::generate_routes_with_zero_penalty_scorer      ... bench:  37,598,126 ns/iter (+/- 17,262,519)
test bench::bench_sends                                                      ... bench:  37,760,922 ns/iter (+/- 5,179,123)
test bench::bench_reading_full_graph_from_file                               ... bench:      25,615 ns/iter (+/- 1,149)
```
2023-01-19 05:06:29 +00:00
Matt Corallo
153b048332
Merge pull request #1951 from wpaulino/remove-get-node-secret 2023-01-19 03:49:15 +00:00
Arik
d66c70eed4
Merge pull request #1964 from TheBlueMatt/2023-01-no-debug-panics
Use test/_test_utils to enable single-threaded debug assertions
2023-01-18 17:41:54 -08:00
Wilmer Paulino
acd2ae606d
Remove NodeSigner::get_node_secret
Secrets should not be exposed in-memory at the interface level as it
would be impossible the implement it against a hardware security
module/secure element.
2023-01-18 17:23:25 -08:00
Wilmer Paulino
9133beaf75
Use NodeSigner::ecdh to compute SharedSecrets 2023-01-18 17:23:23 -08:00