Commit graph

4146 commits

Author SHA1 Message Date
Valentine Wallace
6b66271acf
Add missing keysend preimage check on inbound onion read. 2023-12-12 18:42:38 -05:00
Valentine Wallace
63ebde1d2e
Add test coverage for serialization of malformed HTLCs.
in Channel and ChannelManager.
2023-12-12 18:38:59 -05:00
Valentine Wallace
11bdcdaa08
Add redundant blinded HTLC failure check for posterity.
Although this new check is unreachable right now, it helps prevent potential
future errors where we incorrectly fail blinded HTLCs with an unblinded error.
2023-12-12 18:38:59 -05:00
Valentine Wallace
ae08d0c86a
Make BlindedPath::new_for_payment pub
Because we now support receiving to multi-hop blinded paths.
2023-12-12 18:38:59 -05:00
Valentine Wallace
41808037ac
Fail blinded received HTLCs if they violate PaymentConstraints
.. contained within their encrypted payload.
2023-12-12 18:38:59 -05:00
Valentine Wallace
93ef850670
Test received blinded HTLC failure in process_pending_htlc_forwards 2023-12-12 18:38:59 -05:00
Valentine Wallace
a351301362
Test successful intercept payment to 2-hop blinded path 2023-12-12 18:38:59 -05:00
Valentine Wallace
85d3cb802c
Fix blinded recipient fail on Channel error
If a blinded HTLC errors when added to a Channel, such as if the recipient has
already sent a shutdown message, they should malformed-fail backwards with
error code INVALID_ONION_BLINDING and a zeroed out onion hash per BOLT 4.
2023-12-12 18:38:59 -05:00
Valentine Wallace
eca4dc0799
Fix blinded recipient fail on receive reqs violation
If a blinded HTLC does not satisfy the receiver's requirements, e.g. bad CLTV
or amount, they should malformed-fail backwards with error code
INVALID_ONION_BLINDING and a zeroed out onion hash per BOLt 4.
2023-12-12 18:38:59 -05:00
Valentine Wallace
52f28e63e8
Fix blinded recipient fail on onion decode failure
If a recipient behind a multihop blinded path fails to decode their onion
payload, they should fail backwards with error code INVALID_ONION_BLINDING and
a zeroed out onion hash per BOLT 4.
2023-12-12 18:38:59 -05:00
Valentine Wallace
fbe4bf1cdd
Add find_route test util
And use it in the multihop blinded path receive failure test. Will be used in
the next commit to test receiving an invalid blinded final onion payload.

We can't use the existing get_route test util here because blinded payments
rely on the sender adding a random shadow CLTV offset to the final hop; without
this the payment will be failed with cltv-expiry-too-soon.
2023-12-12 18:38:59 -05:00
Valentine Wallace
d99089e16a
Fix blinded recipient fail on malformed HTLC
If a blinded recipient to a multihop blinded path needs to fail back a
malformed HTLC, they should use error code INVALID_ONION_BLINDING and a zeroed
out onion hash per BOLT 4.
2023-12-12 18:38:59 -05:00
Valentine Wallace
a2b4813c1f
Test recipient failing an HTLC received to a multi-hop blinded path 2023-12-12 18:38:59 -05:00
Valentine Wallace
b26480189e
Support failing blinded non-intro HTLCs after RAA processing.
If an HTLC fails after its RAA is processed, it is failed back with
ChannelManager::fail_htlc_backwards_internal. This method will now correctly
inform the channel that this HTLC is blinded and to construct an
update_malformed message accordingly.
2023-12-12 18:38:58 -05:00
Valentine Wallace
4198edaead
Tweak initialization of HTLCForwardInfo in fail_htlc_backwards_internal
Makes the next commit adding support for failing blinded HTLCs in said method
easier to read.
2023-12-12 18:38:58 -05:00
Valentine Wallace
7bb4a235bc
ChannelManager: add HTLCForwardInfo variant for blinded non-intro htlcs
Necessary to tell the Channel how to fail these htlcs.
2023-12-12 18:38:58 -05:00
Valentine Wallace
846be8147f
Adapt Channel::fail_htlc for failing with malformed OR update_fail_htlc.
Useful for failing blinded payments back with malformed, and will also be
useful in the future when we move onion decoding into
process_pending_htlc_forwards, after which Channel::fail_htlc will be used for
all malformed htlcs.
2023-12-12 18:38:58 -05:00
Valentine Wallace
4ecf3f46df
Set up Channel::fail_htlc to be able to return update_malformed
Currently it returns only update_fail, but we'll want it to be able to return
update_malformed as well in upcoming commits. We'll use this for correctly
failing blinded received HTLCs backwards with malformed and
invalid_onion_blinding error per BOLT 4.
2023-12-12 18:38:58 -05:00
Valentine Wallace
af4d0df1bf
Channel: add holding cell HTLC variant for blinded HTLCs.
For context, blinded HTLCs where we are not the intro node must always be
failed back with malformed and invalid_onion_blinding error per BOLT 4.

Prior to supporting blinded payments, the only way for an update_malformed to
be returned from Channel was if an onion was actually found to be malformed
during initial update_add processing. This meant that any malformed HTLCs would
never live in the holding cell but instead would be returned directly upon
initial RAA processing.

Now, we need to be able to store these HTLCs in the holding cell because the
HTLC failure necessitating an update_malformed may come long after the RAA is
initially processed, and we may not be a state to send the update_malformed
message at that time.

Therefore, add a new holding cell HTLC variant for blinded non-intro node
HTLCs, which will signal to Channel to fail with malformed and the correct
error code.
2023-12-12 18:38:51 -05:00
Valentine Wallace
e4485cffb3
Set HTLCPreviousHopData::blinded for blinded received HTLCs.
Will be used in the next commit(s) to let us know to fail blinded received
HTLCs backwards with the malformed and invalid_onion_blinding error per BOLT 4.
2023-12-12 17:10:23 -05:00
Valentine Wallace
339f3fccf8
Store whether a received HTLC is blinded in PendingHTLCInfo
Useful for knowing to fail back these HTLCs with malformed and
invalid_onion_blinding error per the BOLT 4 spec.
2023-12-12 17:10:09 -05:00
Valentine Wallace
df2d0a47d5
Add variant for non-intro-nodes to BlindedFailure enum
For use in supporting receiving to multi-hop blinded paths.
2023-12-08 14:33:40 -05:00
Valentine Wallace
2a46505815
Test successfully receiving to a multihop blinded path. 2023-12-08 14:33:40 -05:00
Valentine Wallace
51f41ce7ce
Support receiving to multi-hop blinded payment paths.
The only remaining step is to use the update_add blinding point in decoding
inbound onion payloads.

Error handling will be completed in upcoming commits.
2023-12-08 14:33:40 -05:00
Valentine Wallace
87a25c7a5b
Support parsing blinded non-intro onion receive payloads.
Support for receiving to multi-hop blinded payment paths will be completed in
the next commit, sans error handling.
2023-12-08 14:33:40 -05:00
Valentine Wallace
e1ed52fae7
Pass in update add blinding point on onion decode
Will be used to read encrypted_tlvs on non-intro-node onion receipt.
2023-12-08 14:33:40 -05:00
Wilmer Paulino
ec8e0fe478
Merge pull request #2760 from TheBlueMatt/2023-11-chan-close-loop
Fix infinite loop when closing a pre-funding channel
2023-12-08 10:16:12 -08:00
Jeffrey Czyz
1c4d3289ff
Return correct SendSuccess in OnionMessenger
When enqueuing a message for a node already awaiting a connection,
BufferedAwaitingConnection should be returned when a node is not yet
connected as a peer. However, it was only returned when the first
message was enqueued. Any messages enqueued after but before a
connection was established incorrectly returned Buffered.
2023-12-07 23:44:47 -06:00
Jeffrey Czyz
be618bb7b5
Rename OnionMessagePath::addresses
The name itself doesn't provide much meaning to what the addresses
correspond to.
2023-12-07 23:44:41 -06:00
Jeffrey Czyz
5892fd698e
Fix create_onion_message return type documentation 2023-12-07 22:25:25 -06:00
Matt Corallo
5ba4c079bb Immediately error in close_channel_internal if there is no chan
Previously, unfunded channels would be stored outside of
`PeerState::channel_by_id`, and thus if there is no channel when
we look in `PeerState::channel_by_id`, `close_channel_internal`
called `force_close_channel_with_peer` to hunt for unfunded
channels.

However, that is no longer the case, so the call is redundant, and
we can simply return an error instead.
2023-12-08 02:26:00 +00:00
Matt Corallo
3a2690c8aa Move pre-funded-channel immediate shutdown logic to the right place
Because a `Funded` `Channel` cannot possibly be pre-funding, the
logic in `ChannelManager::close_channel_internal` to handle
pre-funding channels is in the wrong place.

Rather than being handled inside the `Funded` branch, it should be
in an `else` following it, handling either of the two
`ChannelPhases` outside of `Funded`.

Sadly, because of a previous control flow management `loop {}`, the
existing code will infinite loop, which is fixed here.
2023-12-08 02:26:00 +00:00
Matt Corallo
1667b4d200 Drop unreachable shutdown code in Channel::get_shutdown
`Channel` is only a thing for funded channels. Thus, checking if a
channel has not yet been funded is dead code and can simply be
elided.
2023-12-08 02:25:59 +00:00
Matt Corallo
8e519b56d6 Limit the scope of get_funding_created_msg to outbound channels
Since we no longer use `ChannelContext::get_funding_created_msg`,
it can be moved back into `UnfundedOutboundV1` channels only,
where it realistically belongs.
2023-12-08 02:22:03 +00:00
Matt Corallo
6ab56a40be Move to Funded after funding_signed rather than on funding
Previously, channels were stored in different maps in `PeerState`
based on whether the funding had been set, keeping the keys across
the maps consistent (pre-funding temporary_channel_ids vs
funding-outpoint-based channel_ids). However, channels are now
stored in a single `channel_by_id` map, making that point moot.

Instead, here, we convert the `ChannelPhase` state transition
boundary to "once we have a `ChannelMonitor`", which makes more
sense now, and was actually the original proposed boundary.

This also requires calling `signer_maybe_unblocked` on a pre-funded
outbound channel, but that nicely also lets us limit the scope of
`FundingCreated` message generation, which we do in the next
commit.
2023-12-08 02:22:03 +00:00
Matt Corallo
2d266794c2
Merge pull request #2723 from jkczyz/2023-11-direct-connect
Direct connect for `OnionMessage` sending
2023-12-08 01:39:13 +00:00
Matt Corallo
61994066f8
Merge pull request #2691 from wpaulino/refactor-channel-state
Refactor ChannelState to decouple state flags from states
2023-12-07 23:59:13 +00:00
Wilmer Paulino
6492cc90ea
Rename certain flags to align with dual funding
`FundingCreated` and `FundingSent` were mostly named after the
respective `funding_created` and `funding_sent` wire messages. They
include the signature for the initial commitment transaction when
opening a channel. With dual funding, these messages are no longer used,
and instead we rely on the existing `commitment_signed` to exchange
those signatures.
2023-12-07 12:37:18 -08:00
Jeffrey Czyz
0b83116436
Rename OnionMessageBuffer to OnionMessageRecipient 2023-12-06 14:45:24 -06:00
Jeffrey Czyz
d46519bbd0
Remove superfluous space from where clause 2023-12-06 14:45:24 -06:00
Jeffrey Czyz
89e630b918
Test pending connection onion message buffering
Add tests for onion message buffering checking that messages are cleared
upon disconnection and timed out after MAX_TIMER_TICKS. Also, checks
that ConnectionNeeded events are generated.
2023-12-06 14:45:18 -06:00
Jeffrey Czyz
210407e1bb
Reuse MessengerNode in spec_test_vector
Additional tests will be added needing a similar node struct, so
consolidate its usage.
2023-12-06 14:43:39 -06:00
Jeffrey Czyz
6ca81ff2bb
Process OnionMessageHandler events in background
OnionMessageHandler implementations now also implement EventsProvider.
Update lightning-background-processor to also process any events the
PeerManager's OnionMessageHandler provides.
2023-12-06 14:43:39 -06:00
Matt Corallo
fcf47a429c
Merge pull request #2765 from TheBlueMatt/2023-12-2314-cleanups-1
Post-#2314 Cleanups
2023-12-06 20:37:06 +00:00
Jeffrey Czyz
cfaa7f3617
Drop buffered messages for timed out nodes
OnionMessenger buffers onion messages for nodes that are pending a
connection. To prevent DoS concerns, add a timer_tick_occurred method to
OnionMessageHandler so that buffered messages can be dropped. This will
be called in lightning-background-processor every 10 seconds.
2023-12-06 14:25:31 -06:00
Jeffrey Czyz
06b05df755
Make OnionMessageHandler extend EventsProvider
An OnionMessageHandler may buffer messages that can't be sent because
the recipient is not a peer. Have the trait extend EventsProvider so
that implementation so that an Event::ConnectionNeeded can be generated
for any nodes that fall into this category. Also, implement
EventsProvider for OnionMessenger and IgnoringMessageHandler.
2023-12-06 08:51:23 -06:00
Jeffrey Czyz
ba2a8221c4
Add Event::ConnectionNeeded for onion messages
A MessageRouter may be unable to find a complete path to an onion
message's destination. This could because no such path exists or any
needs on a potential path don't support onion messages. Add an event
that indicates a connection with a node is needed in order to send the
message.
2023-12-06 08:47:34 -06:00
Jeffrey Czyz
b86f02afad
Return socket addresses from DefaultMessageRouter
When there isn't a direct connection with the Destination of an
OnionMessage, look up socket addresses from the NetworkGraph. This is
used to signal to OnionMessenger that a direct connection is needed to
send the message.
2023-12-06 08:47:34 -06:00
Jeffrey Czyz
1114c3c5aa
Add Option<Vec<SocketAddress>> to OnionMessagePath
MessageRouter::find_path is given a Destination to reach via a set of
peers. If a path cannot be found, it may return a partial path such that
OnionMessenger can signal a direct connection to the first node in the
path is needed. Include a list of socket addresses in the returned
OnionMessagePath to allow OnionMessenger to know how to connect to the
node.

This allows DefaultMessageRouter to use its NetworkGraph to return
socket addresses for gossiped nodes.
2023-12-06 08:47:33 -06:00
Jeffrey Czyz
17af8d5f09
Add NetworkGraph reference to DefaultMessageRouter
When buffering onion messages for a node that is not connected as a
peer, it's possible that the node does not exist. Include a NetworkGraph
reference in DefaultMessageRouter so that it can be used to check if the
node actually exists. Otherwise, an malicious node may send an onion
message where the reply path's introduction node doesn't exist. This
would result in buffering messages that may never be delivered.
2023-12-06 08:47:33 -06:00