Commit graph

1800 commits

Author SHA1 Message Date
Valentine Wallace
3faea33438
Fix phantom malformed onion error packet
Ensure we fail back phantom malformed payments with an update_fail_htlc s.t.
the error contains the sha256 of the onion, per LN protocol.
2022-02-24 22:33:02 -05:00
Valentine Wallace
f1aba79521
Add phantom shared secret to HTLCPreviousHopData
This also fixes a bug where we were failing back phantom payments with the
wrong scid, causing them to never actually be failed backwards (L3022 in
channelmanager.rs)

This new field will be used in upcoming commit(s) to encrypt phantom payment failure
packets.
2022-02-24 22:32:14 -05:00
Valentine Wallace
bafd141d2c
Add phantom shared secret to PendingHTLCRouting::Receive
This will be used in upcoming commit(s) to encrypt phantom payment failure packets.
2022-02-24 21:22:59 -05:00
Matt Corallo
4b77ce1959
Merge pull request #1318 from jurvis/jurvis/2022-02-log-router-penalty-data-4
Implement custom debug for PathBuildingHop
2022-02-24 19:50:51 +00:00
Jurvis Tan
a3c2dfdcbc
Implement custom debug for PathBuildingHop
Add other fields to log for PathBuildingHop

Use DebugStruct to print PathBuildingHop

Fix PathBuildingHop visibility

Add more useful fee print-outs

Remove Features<NodeContext> from hop print-out

Remove logging fields we don’t need

Add fields to log back to PathBuildingHop
2022-02-23 17:50:59 -08:00
Matt Corallo
b7d57ea83e Use &mut self in invoice updaters, not take-self-return-Self
The take-self-return-Self idiom in Rust is substantially less
usable than it is in Java, where its more common. Because we have
to take self by move, it prevents using the update methods to
actually update features, something we occasionally want to do.

See, eg, the change in lightning-invoice where we previously had
to copy and re-create an entire vec of fields just to update the
features field, which is nuts.

There are a few places where this makes things a little less clean,
but the tradeoff to enable more effecient and broader uses of the
update methods seems worth it.
2022-02-23 20:17:36 +00:00
valentinewallace
23f1ec80d3
Merge pull request #1309 from TheBlueMatt/2022-02-first-hop-log
Specify whether we have first-hop hints when routing
2022-02-22 12:23:32 -05:00
Valentine Wallace
1f6700c72d
Follow-up nits from #1199 (phantom node support) 2022-02-22 11:54:35 -05:00
Matt Corallo
040bf2284e Log more during ChannelMonitor updating
Fixes #1323.
2022-02-22 00:18:09 +00:00
Arik Sosman
e43cfe135a
Merge pull request #1314 from TheBlueMatt/2022-02-accept_chan_type
Update channel-type implementation to upstream spec as merged
2022-02-18 13:53:09 -08:00
Jeffrey Czyz
62b1e01ad9
Generate docs with features for docs.rs
Enable generating docs using --all-features or --features="std" where
applicable. Additionally, use doc_auto_cfg to tag items requiring a
feature.

https://doc.rust-lang.org/nightly/rustdoc/unstable-features.html#doc_auto_cfg-automatically-generate-doccfg

This requires building with nightly, which is what is used by docs.rs.

https://docs.rs/about/builds

To test locally, use:

RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc ...
2022-02-18 15:38:31 -06:00
Jeffrey Czyz
27101609c4
Fix rustdoc error on nightly 2022-02-18 15:35:58 -06:00
Jeffrey Czyz
63e3486e8d
Unhide struct docs in scoring module
The docs were hidden since a type alias should be used. However, the
alias docs don't contain much useful information and don't link to the
corresponding struct.
2022-02-18 15:34:07 -06:00
Matt Corallo
637e0d34ca Specify whether we have first-hop hints when routing
This is incredibly useful when debugging issues with the router,
and is somewhat of an oversight currently.
2022-02-18 19:20:18 +00:00
Matt Corallo
7ac4c3bba0
Merge pull request #1316 from TheBlueMatt/2022-02-no-fuzztarget
Drop `fuzztarget` feature entirely
2022-02-18 19:18:30 +00:00
Matt Corallo
b0cb5583b1 Add explicit compile errors when cfg=fuzzing is mis-set 2022-02-18 17:03:04 +00:00
Matt Corallo
acb4c539f7 Drop fuzztarget feature entirely
Some time ago we started transitioning to `cfg(fuzzing)` instead of
exposing a full feature. Here we complete the transition.
2022-02-18 17:03:04 +00:00
Matt Corallo
de1aca5ca2
Merge pull request #1266 from TheBlueMatt/2022-01-fix-double-fail-panic
Fix a debug panic caused by receiving MPP parts after a failure
2022-02-17 03:41:50 +00:00
Matt Corallo
be57e828b8 Fix a debug panic caused by receiving MPP parts after a failure
Prior to cryptographic payment secrets, when we process a received
payment in `process_pending_htlc_fowards` we'd remove its entry
from the `pending_inbound_payments` map and give the user a
`PaymentReceived` event.

Thereafter, if a second HTLC came in with the same payment hash, it
would find no entry in the `pending_inbound_payments` map and be
immediately failed in `process_pending_htlc_forwards`.

Thus, each HTLC will either result in a `PaymentReceived` event or
be failed, with no possibility for both.

As of 8464875555, we no longer
materially have a pending-inbound-payments map, and thus
more-than-happily accept a second payment with the same payment
hash even if we just failed a previous one for having mis-matched
payment data.

This can cause an issue if the two HTLCs are received back-to-back,
with the first being accepted as valid, generating a
`PaymentReceived` event. Then, when the second comes in we'll hit
the "total value {} ran over expected value" condition and fail
*all* pending HTLCs with the same payment hash. At this point,
we'll have a pending failure for both HTLCs, as well as a
`PaymentReceived` event for the user.

Thereafter, if the user attempts to fail the HTLC in response to
the `PaymentReceived`, they'll get a debug panic at channel.rs:1657
'Tried to fail an HTLC that was already failed'.

The solution is to avoid bulk-failing all pending HTLCs for a
payment. This feels like the right thing to do anyway - if a sender
accidentally sends an extra HTLC after a payment has ben fully
paid, we shouldn't fail the entire payment.

Found by the `chanmon_consistency` fuzz test.
2022-02-16 21:40:11 +00:00
Matt Corallo
6d7ae6e174 Update channel-type implementation to upstream spec as merged
Somehow, our channel type implementation doesn't echo back the
channel type as we believe it was negotiated, as we should. Though
the spec doesn't explicitly require this, some implementations may
require it and it appears to have been in the BOLTs from the start
of the channel type logic.
2022-02-16 21:34:16 +00:00
Matt Corallo
92556c868d Drop spurious whitespace in channel.rs 2022-02-16 21:12:22 +00:00
Valentine Wallace
710954f88b
Don't send channel updates for private chans on error
This commit also adds additional checks for the second-to-last (phantom) hop for phantom payments.
2022-02-14 14:25:55 -05:00
Valentine Wallace
c417a51b65
Support phantom payment receive in ChannelManager, with invoice util
See PhantomKeysManager and invoice util's create_phantom_invoice for more info
2022-02-14 14:25:53 -05:00
Valentine Wallace
410eb05365
Add get_phantom_scid and get_phantom_route_hints + scid_utils::fake_scid module
See method and module docs for more details
2022-02-14 14:22:38 -05:00
Valentine Wallace
70f7db9810
channelmanager: DRY PendingHTLCInfo creation for receives
Will be used to facilitate decoding multiple onion layers for phantom payment receive
2022-02-14 14:22:38 -05:00
Valentine Wallace
e1c33d49f0
scid_utils: add utils for retrieving txindex and vout 2022-02-14 14:22:38 -05:00
Valentine Wallace
adeec71ed8
keysinterface: adapt get_node_secret for phantom payments
We want LDK to be able to retrieve the phantom secret key when we see that a payment
is destined for a phantom node.
2022-02-14 14:22:38 -05:00
Valentine Wallace
f6c75d8ec3
KeysInterface::sign_invoice: indicate whether invoice is a phantom 2022-02-14 14:22:38 -05:00
Valentine Wallace
4706c75028
keysmanager: support phantom payments with PhantomKeysManager
To support the feature of generating invoices that can be paid to any of
multiple nodes, a key manager need to be able to share an inbound_payment_key
and phantom secret key. This is because a phantom payment may be received by
any node participating in the invoice, so all nodes must be able to decrypt the
phantom payment (and therefore must share decryption key(s)) in the act of
pretending to be the phantom node. Thus we add a new `PhantomKeysManager` that
supports these features.

To be more specific, the inbound payment key must be shared because it is used
to decrypt the payment details for verification (LDK avoids storing inbound
payment data by encrypting payment metadata in the payment hash and/or payment
secret).

The phantom secret must be shared because enables any real node included in the
phantom invoice to decrypt the final layer of the onion packet, since the onion
is encrypted by the sender using the phantom public key provided in the
invoice.
2022-02-14 14:22:37 -05:00
Valentine Wallace
329ecdf88f
DRY shared hkdf_extract_expand code to new module 2022-02-14 14:22:37 -05:00
Valentine Wallace
f254bb49ac
Implement serialization for ChannelDetails
Will be used in upcoming commit(s) where it may be desirable to cache ChannelDetails routehints
2022-02-14 14:22:37 -05:00
Valentine Wallace
de1b62eacf
Refactor out decode_next_hop util from ChannelManager::decode_update_add_htlc
This will be used in upcoming commit(s) to facilitate decoding multiple onion layers for
multi-node payment receive
2022-02-14 14:22:37 -05:00
Matt Corallo
963f8d93b5
Merge pull request #1301 from TheBlueMatt/2022-02-router-no-test
Work around rustc bug on nightly and make benchmarks not run test code
2022-02-14 18:29:03 +00:00
Viktor Tigerström
1891b37b81 Add tests for responding to inbound channel reqs
Add functional tests for manually responding to inbound channel requests.
Responding to inbound channel requests are required when the
`manually_accept_inbound_channels` config flag is set to true.

The tests cover the following cases:
* Accepting an inbound channel request
* Rejecting an inbound channel request
* FundingCreated message sent by the counterparty before accepting the
inbound channel request
* Attempting to accept an inbound channel request twice
* Attempting to accept an unkown inbound channel
2022-02-13 21:15:35 +01:00
Viktor Tigerström
8dca0b4779 Add option to accept or reject inbound channels
Add a new config flag `UserConfig::manually_accept_inbound_channels`,
which when set to true allows the node operator to accept or reject new
channel requests.

When set to true, `Event::OpenChannelRequest` will be triggered once a
request to open a new inbound channel is received. When accepting the
request, `ChannelManager::accept_inbound_channel` should be called.
Rejecting the request is done through
`ChannelManager::force_close_channel`.
2022-02-13 21:04:19 +01:00
valentinewallace
b8e9e8b834
Merge pull request #1292 from TheBlueMatt/2022-02-override-handshake-limits
Store override counterparty handshake limits until we enforce them
2022-02-11 19:45:44 -05:00
Arik Sosman
c931380cbc
Merge pull request #1268 from TheBlueMatt/2022-01-balance-underflow
Include inbound-claimed-HTLCs in reported channel balances
2022-02-10 16:30:48 -08:00
Matt Corallo
c8e3078ff7 Make router benchmarks more realistic by not running test-only code
`cargo bench` sets `cfg(test)`, causing us to hit some test-only
code in the router when benchmarking, throwing off our benchmarks
substantially. Here we swap from the `unstable` feature to a more
clearly internal feature (`_bench_unstable`) and also checking for
it when enabling test-only code.
2022-02-10 22:28:38 +00:00
Matt Corallo
1818c4a115 Include inbound-claimed-HTLCs in reported channel balances
Given the balance is reported as "total balance if we went to chain
ignoring fees", it seems reasonable to include claimed HTLCs - if
we went to chain we'd get those funds, less on-chain fees. Further,
if we do not include them, its possible to have pending outbound
holding-cell HTLCs underflow the balance calculation, causing a
panic in debug mode, and bogus values in release.

This resolves a subtraction underflow bug found by the
`chanmon_consistency` fuzz target.
2022-02-10 22:25:41 +00:00
Matt Corallo
78c6154d9a Work around rustc compilation regression on nightly
Apparently rustc doesn't (actually) provide any kind of
compilation-stability guarantees, despite their claims. Here we
work around rustc being unstable by making the trait call explicit.

See also https://github.com/rust-lang/rust/issues/93599
2022-02-10 21:11:59 +00:00
Tibo-lg
ba289b8872 Make CounterpartyCommitmentSecrets public 2022-02-10 15:25:08 +09:00
Matt Corallo
d29ae1826e
Merge pull request #1285 from TheBlueMatt/2022-01-remove-closed-issue-ref
Remove stale reference to incomplete BOLT compliance
2022-02-04 19:42:26 +00:00
valentinewallace
a1fedeaec2
Merge pull request #1227 from jkczyz/2021-12-probabilistic-scorer
Probabilistic channel scoring
2022-02-03 10:50:49 -05:00
Jeffrey Czyz
d5db85ecc5
Add ProbabilisticScorer benchmarks 2022-02-02 20:22:27 -06:00
Jeffrey Czyz
28faf89df3
Deprecate Scorer in favor of ProbabilisticScorer 2022-02-02 20:22:27 -06:00
Jeffrey Czyz
2e730cdc9c
Decay channel liquidity balance offsets
ProbabilisticScorer uses successful and unsuccessful payments to gain
more certainty of a channel's liquidity balance. Decay this knowledge
over time to indicate decreasing certainty about the liquidity balance.
2022-02-02 20:22:27 -06:00
Jeffrey Czyz
68d791ad84
Probabilistic channel scoring
Add a Score implementation based on "Optimally Reliable & Cheap Payment
Flows on the Lightning Network" by Rene Pickhardt and Stefan Richter[1].
Given the uncertainty of channel liquidity balances, probability
distributions are defined based on knowledge learned from successful and
unsuccessful attempts. Then the negative log of the success probability
is used to determine the cost of routing a specific HTLC amount through
a channel.

[1]: https://arxiv.org/abs/2107.05322
2022-02-02 19:47:12 -06:00
Jeffrey Czyz
1aaf5fc5d0
Effective channel capacity for router and scoring
A channel's capacity may be inferred or learned and is used to make
routing decisions, including as a parameter to channel scoring. Define
an EffectiveCapacity for this purpose. Score::channel_penalty_msat takes
the effective capacity (less in-flight HTLCs for the same payment), and
never None. Thus, for hops given in an invoice, the effective capacity
is now considered (near) infinite if over a private channel or based on
learned information if over a public channel.

If a Score implementations needs the effective capacity when updating a
channel's score, i.e. in payment_path_failed or payment_path_successful,
it can access the channel's EffectiveCapacity via the NetworkGraph by
first looking up the channel and then specifying which direction is
desired using ChannelInfo::as_directed.
2022-02-02 19:46:58 -06:00
Matt Corallo
649af07205 Store override counterparty handshake limits until we enforce them
We currently allow users to provide an `override_config` in
`ChannelManager::create_channel` which it seems should apply to the
channel. However, because we don't store any of it, the only parts
which we apply to the channel are those which are set in the
`Channel` object immediately in `Channel::new_outbound` and used
from there.

This is great in most cases, however the
`UserConfig::peer_channel_config_limits` `ChannelHandshakeLimits`
object is used in `accept_channel` to bound what is acceptable in
our peer's `AcceptChannel` message. Thus, for outbound channels, we
are given a full `UserConfig` object to "override" the default
config, but we don't use any of the handshake limits specified in
it.

Here, we move to storing the `ChannelHandshakeLimits` explicitly
and applying it when we receive our peer's `AcceptChannel`. Note
that we don't need to store it anywhere because if we haven't
received an `AcceptChannel` from our peer when we reload from disk
we will forget the channel entirely anyway.
2022-02-01 21:40:56 +00:00
valentinewallace
482a2b9250
Merge pull request #1282 from TheBlueMatt/2022-01-fuzz-overflow
Avoid overflow in addition when checking counterparty feerates
2022-01-27 11:42:05 -05:00