Commit graph

3575 commits

Author SHA1 Message Date
Jeffrey Czyz
85c471aac2
Move BOLT 12 InvoiceRequest method implementations 2023-08-21 19:14:28 -05:00
Jeffrey Czyz
270bc2e4c0
Move BOLT 12 offer method implementations 2023-08-21 19:14:28 -05:00
Jeffrey Czyz
230f081e5a
Unsigned BOLT 12 message parsing and serialization 2023-08-21 19:14:28 -05:00
Jeffrey Czyz
889848d5ed
Rename field of unsigned BOLT message contents
Using `contents` for the field name is more consistent with the signed
messages.
2023-08-21 19:14:27 -05:00
Jeffrey Czyz
239f22c0c9
Wrap KeyPair by DerivedSigningPubkey
InvoiceBuilder is parameterized by a SigningPubkeyStrategy, either
ExplicitSigningPubkey and DerivedSigningPubkey. It also holds an
Option<KeyPair>, which may be None and Some for those strategies,
respectively. This leads to methods for InvoiceBuilder parameterized by
DerivedSigningPubkey needing to blindly unwrap the Option<KeyPair>.
Instead, have DerivedSigningPubkey wrap KeyPair.
2023-08-21 19:14:27 -05:00
Jeffrey Czyz
1811ebff32
TaggedHash for BOLT 12 signing function
The function used to sign BOLT 12 messages only takes a message digest.
This doesn't allow signers to independently verify the message before
signing nor does it allow them to derive the necessary signing keys, if
needed.

Introduce a TaggedHash wrapper for a message digest, which each unsigned
BOLT 12 message type constructs upon initialization. Change the signing
function to take AsRef<TaggedHash>, which each unsigned type implements.
This allows the signing function to take any unsigned message and obtain
its tagged hash.
2023-08-21 19:14:27 -05:00
Matt Corallo
b14927968f Send warning messages when repeating shutdown messages at volume 2023-08-21 23:04:47 +00:00
Matt Corallo
2b898a3385
Merge pull request #2498 from arik-so/arik/2023-08-gossip-logging
Improve network graph update logging
2023-08-21 22:34:34 +00:00
Matt Corallo
4bd4f02fa7
Merge pull request #2478 from waterson/settle-htlcs
Provide the HTLCs that settled a payment.
2023-08-21 22:32:54 +00:00
Chris Waterson
2e562a2ad8 Provide the HTLCs that settled a payment.
Creates a new `events::ClaimedHTLC` struct that contains the relevant
information about a claimed HTLC; e.g., the channel it arrived on, its ID, the
amount of the HTLC, the overall amount of the payment, etc. Adds appropriate
serialization support.

Adds a `Vec<events::ClaimedHTLC>` to the `ClaimingPayment`
structure. Populates this when creating the struct by converting the
`payment.htlcs` (which are `ClaimingHTLC` structs) into `event::ClaimedHTLC`
structs. This is a straightforward transformation.

Adds a `Vec<events::ClaimedHTLC>` to the `events::Event::PaymentClaimed`
enum. This is populated directly from the `ClaimingPayment`'s `htlcs` vec.

Fixes #2477.
2023-08-21 11:30:12 -04:00
Valentine Wallace
7b31712557
Struct-ify decoded onion failures
To avoid several long hard-to-read tuple return values.
2023-08-19 18:59:06 -04:00
Valentine Wallace
fb9ad5686e
Document and test 0-len channel update onion error case 2023-08-19 18:55:34 -04:00
Valentine Wallace
cd2ebb769c
Add missing test coverage for bogus err packet with valid hmac 2023-08-19 18:55:34 -04:00
Valentine Wallace
ec5e837cc2
Generalize next_hop_packet_pubkey onion util
Useful for generating a next hop blinding point when forwarding a blinded
payment.
2023-08-19 18:55:34 -04:00
Valentine Wallace
cf13f78cd1
Blinded paths: support constructing onion keys + handling onion errors
We don't bother actually parsing errors from within a blinded path, since all
errors should be wiped by the introduction node by the time it gets back to us
(the sender).
2023-08-19 18:54:07 -04:00
Matt Corallo
bb7c4d1853 Work around LND bug 6039
LND hasn't properly handled shutdown messages ever, and
force-closes any time we send one while HTLCs are still present.
The issue is tracked at
https://github.com/lightningnetwork/lnd/issues/6039 and has had
multiple patches to fix it but none so far have managed to land
upstream. The issue appears to be very low priority for the LND
team despite being marked "P1".

We're not going to bother handling this in a sensible way, instead
simply repeated the Shutdown message on repeat until morale
improves.
2023-08-17 22:47:01 +00:00
Matt Corallo
31049ed961 Delay RAA-after-next processing until PaymentSent is are handled
In 0ad1f4c943 we fixed a nasty bug
where a failure to persist a `ChannelManager` faster than a
`ChannelMonitor` could result in the loss of a `PaymentSent` event,
eventually resulting in a `PaymentFailed` instead!

As noted in that commit, there's still some risk, though its been
substantially reduced - if we receive an `update_fulfill_htlc`
message for an outbound payment, and persist the initial removal
`ChannelMonitorUpdate`, then respond with our own
`commitment_signed` + `revoke_and_ack`, followed by receiving our
peer's final `revoke_and_ack`, and then persist the
`ChannelMonitorUpdate` generated from that, all prior to completing
a `ChannelManager` persistence, we'll still forget the HTLC and
eventually trigger a `PaymentFailed` rather than the correct
`PaymentSent`.

Here we fully fix the issue by delaying the final
`ChannelMonitorUpdate` persistence until the `PaymentSent` event
has been processed and document the fact that a spurious
`PaymentFailed` event can still be generated for a sent payment.

The original fix in 0ad1f4c943 is
still incredibly useful here, allowing us to avoid blocking the
first `ChannelMonitorUpdate` until the event processing completes,
as this would cause us to add event-processing delay in our general
commitment update latency. Instead, we ultimately race the user
handling the `PaymentSent` event with how long it takes our
`revoke_and_ack` + `commitment_signed` to make it to our
counterparty and receive the response `revoke_and_ack`. This should
give the user plenty of time to handle the event before we need to
make progress.

Sadly, because we change our `ChannelMonitorUpdate` semantics, this
change requires a number of test changes, avoiding checking for a
post-RAA `ChannelMonitorUpdate` until after we process a
`PaymentSent` event. Note that this does not apply to payments we
learned the preimage for on-chain - ensuring `PaymentSent` events
from such resolutions will be addressed in a future PR. Thus, tests
which resolve payments on-chain switch to a direct call to the
`expect_payment_sent` function with the claim-expected flag unset.
2023-08-17 22:19:48 +00:00
Matt Corallo
495c38c0d9 Pass OutPoint, rather than channel id to claim_funds_internal
This is a trivial refactor which will be used in the next commit.
2023-08-17 22:19:26 +00:00
Arik Sosman
d162c4069f
Improve network graph update logging. 2023-08-17 11:14:15 -07:00
Elias Rohrer
7e270cbae9
Don't require import of internal macros
Commit f560320b introduced changes that require users of
`impl_writeable_tlv_based`/`impl_writeable_tlv_based_enum` to import
`_encode_varint_length_prefixed_tlv` and `alloc` separately.

Here, we take care of the necessary imports in
`_encode_varint_length_prefixed_tlv` itself, allowing users to just
import the `impl_writeable_tlv_based` variant they need.
2023-08-17 10:50:23 +02:00
Elias Rohrer
f609fcf8fb
Merge pull request #2501 from TheBlueMatt/2023-08-err-pre-accept
Ensure we wipe pending un-accepted channel requests on err/discon.
2023-08-17 09:05:08 +02:00
Matt Corallo
ef5a38715d Update documentation on Channel::set_outbound_scid_alias
...and replace an assertion with a debug_assertion
2023-08-17 03:35:56 +00:00
Alec Chen
83f0dbc002 Address custom HTLC TLV fixups
Don't collect iterators to compare, minorly simplify encoding the
keysend TLV, combine the _encode_tlv_stream variants to check that the
ordering of TLVs is correct including custom TLVs.
2023-08-16 19:06:19 -05:00
Alec Chen
bd90bd2055
Simplify custom HTLC TLV tests
Remove print statement, remove some unnecessary checks copied over from
test utils, make minor simplifications, wrap especially long lines.
2023-08-16 13:06:39 -05:00
Matt Corallo
9b13862506
Merge pull request #2500 from TheBlueMatt/2023-08-fix-test-lifetimes 2023-08-16 05:39:50 +00:00
Matt Corallo
8e92223a44 Use more human-readable lifetime names in test structs 2023-08-15 23:19:03 +00:00
Matt Corallo
e1bfea3029 Correct test struct initialization ordering
When reloading a node in the test framework, we end up with a new
`ChannelManager` that has references to various test util structs.
In order for the tests to compile reliably in the face of unrelated
changes, those test structs need to always be initialized before
both the new but also the original `ChannelManager`.

Here we make that change.
2023-08-15 23:19:03 +00:00
Matt Corallo
1bf27bfd33 Drop now-unused outbound_scid_alias param to channel constructor
01847277b9 switched around the logic
for inbound channel construction to assign the outbound SCID alias
after constructing the `InboundV1Channel` object. Thus, the SCID
alias argument is now unused, and we remove it here.
2023-08-15 22:32:55 +00:00
Matt Corallo
e0255c0fff Ensure we wipe pending un-accepted channel requests on err/discon.
If we have a pending inbound un-accepted channel but receive an
error message for it from our peer, or our peer disconnects, we
should remove the pending entry, ensuring any attempts to accept
it fail.
2023-08-15 22:22:45 +00:00
Matt Corallo
fe0f845582
Merge pull request #2428 from waterson/create-channel-after-accept
Wait to create a channel until after accepting.
2023-08-15 22:15:09 +00:00
Matt Corallo
417e866792 Correct lifetimes on _reload_node
For some reason an unrelated PR caused all our tests with
`reload_node` calls to fail to compile. This is due, in part, to
the lifetimes on `reload_node` implying that the new and original
`ChannelManager` (or some of the structs they reference) must live
for the same lifetime.

This fixes that issue by correcting the lifetimes to be consistent
across `Node` and `_reload_node`.
2023-08-15 19:17:31 +00:00
Willem Van Lint
ef5be580f5 Remove AvailableBalances::balance_msat
The ChannelMonitor::get_claimable_balances method provides a more
straightforward approach to the balance of a channel, which satisfies
most use cases. The computation of AvailableBalances::balance_msat is
complex and originally had a different purpose that is not applicable
anymore.
2023-08-15 11:42:00 -07:00
Matt Corallo
a24626b919
Merge pull request #2496 from dunxen/2023-08-fix2488
Send error message to peer if we drop an unfunded channel on timeout
2023-08-14 19:39:23 +00:00
Valentine Wallace
6144e30c0e
Wrap process_onion_failure comments at 100chars
And fix an its vs it's grammar
2023-08-14 14:12:10 -04:00
Valentine Wallace
9f5e574b0b
Clean up process_onion_failure
Get rid of a bunch of indentation and be more idiomatic.
2023-08-14 14:12:09 -04:00
valentinewallace
4a4163fcf4
Merge pull request #2458 from valentinewallace/2023-07-om-test-vectors
Onion message test vectors
2023-08-14 11:44:24 -04:00
Duncan Dean
eda6e9d0ec
Send error message to peer if we drop an unfunded channel on timeout 2023-08-14 16:22:33 +02:00
Chris Waterson
01847277b9 Wait to create a channel until after accepting.
Create a new table in 'peer_state' to maintain unaccepted inbound
channels; i.e., a channel for which we've received an 'open_channel'
message but that user code has not yet confirmed for acceptance. When
user code accepts the channel (e.g. via 'accept_inbound_channel'),
create the channel object and as before.

Currently, the 'open_channel' message eagerly creates an
InboundV1Channel object before determining if the channel should be
accepted. Because this happens /before/ the channel has been assigned
a user identity (which happens in the handler for OpenChannelRequest),
the channel is assigned a random user identity. As part of the
creation process, the channel's cryptographic material is initialized,
which then uses this randomly generated value for the user's channel
identity e.g. in SignerProvider::generate_channel_keys_id.

By delaying the creation of the InboundV1Channel until /after/ the
channel has been accepted, we ensure that we defer cryptographic
initialization until we have given the user the opportunity to assign
an identity to the channel.
2023-08-13 19:40:17 -07:00
Matt Corallo
131560e08f
Merge pull request #2387 from vladimirfomene/add_extra_fields_to_ChannelClosed_event
Add counterparty_node_id & channel_capacity to ChannelClosed event
2023-08-10 22:43:21 +00:00
Matt Corallo
9e4a35a60d
Merge pull request #2308 from alecchendev/2023-05-custom-htlc-tlvs
Add support for custom HTLC TLVs
2023-08-10 17:26:30 +00:00
Matt Corallo
7a63ab77da
Merge pull request #1789 from jkczyz/2022-10-scorer-decay
Smooth out channel liquidity bounds decay
2023-08-09 16:34:33 +00:00
Matt Corallo
7e3de70448
Merge pull request #2481 from TheBlueMatt/2023-08-keysend-robust-test
Scope payment preimage in do_test_keysend_payments
2023-08-09 00:40:53 +00:00
Alec Chen
dec3fb316a
Enforce explicit claims on payments with even custom TLVs
Because we don't know which custom TLV type numbers the user is
expecting (and it would be cumbersome for them to tell us), instead of
failing unknown even custom TLVs on deserialization, we accept all
custom TLVs, and pass them to the user to check whether they recognize
them and choose to fail back if they don't. However, a user may not
check for custom TLVs, in which case we should reject any even custom
TLVs as unknown.

This commit makes sure a user must explicitly accept a payment with
even custom TLVs, by (1) making the default
`ChannelManager::claim_funds` fail if the payment had even custom TLVs
and (2) adding a new function
`ChannelManager::claim_funds_with_known_custom_tlvs` that accepts them.

This commit also refactors our custom TLVs test and updates various
documentation to account for this.
2023-08-08 16:16:45 -05:00
Alec Chen
8ff1604647
Add FailureCode::InvalidOnionPayload variant
When a user decodes custom TLVs, if they fail to recognize even type
numbers they should fail back with the correct failure code and fail
data. This new variant adds the proper failure variant for the user to
pass into `ChannelManager::fail_htlc_backwards_with_reason`.

Note that the enum discriminants were removed because when adding a
struct variant we can no longer make use of the discriminant through
casting like we previously did, and instead have to manually define the
associated failure code anyway.
2023-08-08 16:16:44 -05:00
Alec Chen
e84fb067aa
Drop non-matching custom TLVs when receiving MPP
Upon receiving multiple payment parts with custom TLVs, we fail payments
if they have any non-matching or missing even TLVs, and otherwise just
drop non-matching TLVs if they're odd.
2023-08-08 16:16:43 -05:00
Alec Chen
0a2dbdf247
Handle receiving custom HTLC TLVs
This completes basic receiver-side support for custom TLVs and adds
functional testing for sending and receiving.
2023-08-08 16:16:38 -05:00
Alec Chen
f560320b5f
De/serialize custom TLVs on {Inbound,Outbound}OnionPayload
When serialized, the TLVs in `OutboundOnionPayload`, unlike a normal
TLV stream, are prefixed with the length of the stream. To allow a user
to add arbitrary custom TLVs, we aren't able to communicate to our
serialization macros exactly which fields to expect, so this commit
adds new macro variants to allow appending an extra set of bytes (and
modifying the prefixed length accordingly).

Because the keysend preimage TLV has a type number in the custom type
range, and a user's TLVs may have type numbers above and/or below
keysend's type number, and because TLV streams must be serialized in
increasing order by type number, this commit also ensures the keysend
TLV is properly sorted/serialized amongst the custom TLVs.
2023-08-08 16:13:19 -05:00
Alec Chen
d2e9cb4bcd
Add custom tlvs to PendingOutboundPayment::Retryable 2023-08-08 15:55:06 -05:00
Alec Chen
039b1c8d10
Allow users to provide custom TLVs through RecipientOnionFields
Custom TLVs allow users to send extra application-specific data with
a payment. These have the additional flexibility compared to
`payment_metadata` that they don't have to reflect recipient generated
data provided in an invoice, in which `payment_metadata` could be
reused.

We ensure provided type numbers are unique, increasing, and within the
experimental range with the `RecipientOnionFields::with_custom_tlvs`
method.

This begins sender-side support for custom TLVs.
2023-08-08 15:55:00 -05:00
Matt Corallo
4b24135738
Merge pull request #2128 from valentinewallace/2023-03-route-blinding-groundwork
Route blinding groundwork
2023-08-08 19:59:05 +00:00