Commit graph

1926 commits

Author SHA1 Message Date
Valentine Wallace
b26fb851cd
Significantly expand onion message documentation 2022-08-02 19:19:39 -04:00
Valentine Wallace
bf007ea763
Implement receiving and forwarding onion messages
This required adapting `onion_utils::decode_next_hop` to work for both payments
and onion messages.

Currently we just print out the path_id of any onion messages we receive. In
the future, these received onion messages will be redirected to their
respective handlers: i.e. an invoice_request will go to an InvoiceHandler,
custom onion messages will go to a custom handler, etc.
2022-08-02 19:19:37 -04:00
Valentine Wallace
9051c38ebe
Support sending onion messages
This adds several utilities in service of then adding
OnionMessenger::send_onion_message, which can send to either an unblinded
pubkey or a blinded route. Sending custom TLVs and sending an onion message
containing a reply path are not yet supported.

We also need to split the construct_keys_callback macro into two macros to
avoid an unused assignment warning.
2022-08-02 19:17:27 -04:00
Valentine Wallace
4c8dc2c2a0
Add baseline OnionMessenger and msgs::OnionMessage and its serialization
OnionMessenger will be hooked up to the PeerManager to send and receive OMs in
a follow-up PR.
2022-08-02 19:17:26 -04:00
Valentine Wallace
6017379b8e
KeysInterface: add new ecdh method
This method will help us avoid retrieving our node secret, something we want to
get rid of entirely.  It will be used in upcoming commits when decoding the
onion message packet, and in future PRs to help us get rid of
KeysInterface::get_node_secret usages across the codebase
2022-08-02 19:17:23 -04:00
Valentine Wallace
33ff2746ef
Add onion_message::Packet and adapt construct_onion_packet_with_init_noise for it
We need to add a new Packet struct because onion message packet hop_data fields
can be of variable length, whereas regular payment packets are always 1366
bytes.

Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
Co-authored-by: Jeffrey Czyz <jkczyz@gmail.com>
2022-08-02 19:17:19 -04:00
Valentine Wallace
4e5381a50f
Add onion messages module + enable the construction of blinded routes
Blinded routes can be provided as destinations for onion messages, when the
recipient prefers to remain anonymous.

We also add supporting utilities for constructing blinded path keys, and
control TLVs structs representing blinded payloads prior to being
encoded/encrypted. These utilities and struct will be re-used in upcoming
commits for sending and receiving/forwarding onion messages.

Finally, add utilities for reading the padding from an onion message's
encrypted TLVs without an intermediate Vec.
2022-08-02 12:11:11 -04:00
Matt Corallo
e403999ffd
Merge pull request #1588 from TheBlueMatt/2022-06-ffs-dumb-ser
Do not execute the default_value expr until we need it in TLV deser
2022-07-05 13:46:43 -07:00
Matt Corallo
f1b9bd34b8 Do not execute the default_value expr until we need it in TLV deser
This fixes an insta-panic in `ChannelMonitor` deserialization where
we always `unwrap` a previous value to determine the default value
of a later field. However, because we always ran the `unwrap`
before the previous field is read, we'd always panic.

The fix is rather simple - use a `OptionDeserWrapper` for
`default_value` fields and only fill in the default value if no
value was read while walking the TLV stream.

The only complexity comes from our desire to support
`read_tlv_field` calls that use an explicit field rather than an
`Option` of some sort, which requires some statement which can
assign both an `OptionDeserWrapper<T>` variable and a `T` variable.
We settle on `x = t.into()` and implement `From<T> for
OptionDeserWrapper<T>` which works, though it requires users to
specify types explicitly due to Rust determining expression types
prior to macro execution, completely guessing with no knowlege for
integer expressions (see
https://github.com/rust-lang/rust/issues/91369).
2022-07-05 17:32:21 +00:00
Matt Corallo
daeb5a6291
Merge pull request #1553 from wvanlint/dns_hostname
Adds DNS hostname to NetAddress
2022-07-05 07:24:17 -07:00
Willem Van Lint
c30dcf183c Adds DNS hostname to NetAddress 2022-07-04 10:19:16 -07:00
Matt Corallo
156cc77753 Bump crate versions to 0.0.109/invoice 0.17 2022-07-01 16:05:33 +00:00
Matt Corallo
6c480ae887 Fix spurious panic on bogus funding txn that confirm and are spent
In c02b6a3807 we moved the
`payment_preimage` copy from inside the macro which only runs if we
are spending an output we know is an HTLC output to doing it for
any script that matches our expected length. This can panic if an
inbound channel is created with a bogus funding transaction that
has a witness program of the HTLC-Success/-Offered length but which
does not have a second-to-last witness element which is 32 bytes.

Luckily this panic is relatively simple for downstream users to
work around - if an invalid-length-copy panic occurs, simply remove
the ChannelMonitor from the bogus channel on startup and run
without it. Because the channel must be funded by a bogus script in
order to reach this panic, the channel will already have closed by
the time the funding transaction is spent, and there can be no
local funds in such a channel, so removing the `ChannelMonitor`
wholesale is completely safe.

In order to test this we have to disable an in-line assertion that
checks that our transactions match expected scripts which we do by
checking for the specific bogus script that we now use in
`test_invalid_funding_tx`.

Thanks to Eugene Siegel for reporting this issue.
2022-07-01 14:47:17 +00:00
Matt Corallo
87a6e013f7 Have find_route take a NetworkGraph instead of a ReadOnly one
Because downstream languages are often garbage-collected, having
the user directly allocate a `ReadOnlyNetworkGraph` and pass a
reference to it to `find_route` often results in holding a read
lock long in excess of the `find_route` call. Worse, some languages
(like JavaScript) tend to only garbage collect when other code is
not running, possibly leading to deadlocks.
2022-06-29 17:45:49 +00:00
Matt Corallo
a600eee87c
Merge pull request #1564 from TheBlueMatt/2022-06-panic-on-behind
Panic if we're running with outdated state instead of force-closing
2022-06-27 09:34:26 -07:00
Elias Rohrer
800ccec0ed Add anti-probing penalty to ProbabilisticScorer
Currently, channel balances may be rather easily discovered through
probing. This however poses a privacy risk, since the analysis of
balance changes over adjacent channels could in the worst case empower an adversary to
mount an end-to-end deanonymization attack, i.e., track who payed whom.

The penalty added here is applied so we prefer nodes with a smaller `htlc_maximum_msat`, which makes
balance discovery attacks harder to execute. As this improves privacy network-wide, we
treat such nodes preferentially and hence create an incentive to restrict
`htlc_maximum_msat`.
2022-06-25 20:06:04 +02:00
Matt Corallo
caa2a9a55b Panic if we're running with outdated state instead of force-closing
When we receive a `channel_reestablish` with a `data_loss_protect`
that proves we're running with a stale state, instead of
force-closing the channel, we immediately panic. This lines up with
our refusal to run if we find a `ChannelMonitor` which is stale
compared to our `ChannelManager` during `ChannelManager`
deserialization. Ultimately both are an indication of the same
thing - that the API requirements on `chain::Watch` were violated.

In the "running with outdated state but ChannelMonitor(s) and
ChannelManager lined up" case specifically its likely we're running
off of an old backup, in which case connecting to peers with
channels still live is explicitly dangerous. That said, because
this could be an operator error that is correctable, panicing
instead of force-closing may allow for normal operation again in
the future (cc #1207).

In any case, we provide instructions in the panic message for how
to force-close channels prior to peer connection, as well as a note
on how to broadcast the latest state if users are willing to take
the risk.

Note that this is still somewhat unsafe until we resolve #1563.
2022-06-25 02:25:32 +00:00
Matt Corallo
5ed3f25b21 Add ChannelManager methods to force close without broadcasting
If a user restores from a backup that they know is stale, they'd
like to force-close all of their channels (or at least the ones
they know are stale) *without* broadcasting the latest state,
asking their peers to do so instead. This simply adds methods to do
so, renaming the existing `force_close_channel` and
`force_close_all_channels` methods to disambiguate further.
2022-06-25 02:25:32 +00:00
Elias Rohrer
57d8257a0b Allow nodes to be avoided during pathfinding
Users may want to - for whatever reasons - prevent payments to be routed
over certain nodes. This change therefore allows to add `NodeId`s to a
list of banned nodes, which then will be avoided during path finding.
2022-06-24 08:31:41 +02:00
Matt Corallo
3676a056c8
Merge pull request #1518 from valentinewallace/2022-06-OMs-prefactor
Onion messages v1 pre-refactor
2022-06-21 16:13:37 -07:00
Valentine Wallace
ee805e97ff
Enable simultaneous deserialization+decryption of a ChaChaPoly stream
In the upcoming onion messages PR, this will allow us to avoid decrypting onion
message encrypted data in an intermediate Vec before decoding it. Instead we
decrypt and decode it at the same time using this new ChaChaPolyReadAdapter object.

In doing so, we need to adapt the decode_tlv_stream macro such that it will
decode a LengthReadableArgs, which is a new trait as well. This trait is
necessary because ChaChaPoly needs to know the total length ahead of time to
separate out the tag at the end.
2022-06-21 17:08:28 -04:00
valentinewallace
10071b51e2
Merge pull request #1556 from danielgranhao/2022-06-improve-docs
Clarify description of get_node_secret() method
2022-06-21 15:59:31 -04:00
Valentine Wallace
945cec3027
chacha20poly1305: enable simultaneous writing+encryption
In the upcoming onion messages PR, this will allow us to avoid encoding onion
message encrypted data into an intermediate Vec before encrypting it.  Instead
we encode and encrypt at the same time using this new ChaChaPolyWriteAdapter object.
2022-06-21 15:09:25 -04:00
Matt Corallo
c502e8d101
Merge pull request #1486 from TheBlueMatt/2022-05-revoked-txn-edge-cases
Fix two edge cases in handling of counterparty revoked commitment txn
2022-06-21 11:47:15 -07:00
Daniel Granhão
526d38169e Change description of get_node_secret() 2022-06-21 17:38:57 +01:00
Matt Corallo
70ae45fea0 Don't fail HTLCs in revoked commitment txn until we spend them
When we see a counterparty revoked commitment transaction on-chain
we shouldn't immediately queue up HTLCs present in it for
resolution until we have spent the HTLC outputs in some kind of
claim transaction.

In order to do so, we first have to change the
`fail_unbroadcast_htlcs!()` call to provide it with the HTLCs which
are present in the (revoked) commitment transaction which was
broadcast. However, this is not sufficient - because all of those
HTLCs had their `HTLCSource` removed when the commitment
transaction was revoked, we also have to update
`fail_unbroadcast_htlcs` to check the payment hash and amount when
the `HTLCSource` is `None`.

Somewhat surprisingly, several tests actually explicitly tested for
the old behavior, which required amending to pass with the new
changes.

Finally, this adds a debug assertion when writing `ChannelMonitor`s
to ensure `HTLCSource`s do not leak.
2022-06-21 16:14:55 +00:00
Matt Corallo
90541c2690
Merge pull request #1527 from wpaulino/update-htlc-relay-policy
Expose API to update a channel's ChannelConfig
2022-06-21 09:02:29 -07:00
Wilmer Paulino
0f30d7643c
Use new Channel::update_config method to update base fee in test 2022-06-20 13:12:59 -07:00
Wilmer Paulino
e14f25ce0c
Allow forwarding HTLCs that were constructed for previous config
This is mostly motivated by the fact that payments may happen while the
latest `ChannelUpdate` indicating our new `ChannelConfig` is still
propagating throughout the network. By temporarily allowing the previous
config, we can help reduce payment failures across the network.
2022-06-20 13:12:57 -07:00
Wilmer Paulino
e2f216b694
Track previous ChannelConfig and expire after enough ticks
We do this to prevent payment failures while the `ChannelUpdate` for the
new `ChannelConfig` still propagates throughout the network. In a follow
up commit, we'll honor forwarding HTLCs that were constructed based on
either the previous or current `ChannelConfig`.

To handle expiration (when we should stop allowing the previous config),
we rely on the ChannelManager's `timer_tick_occurred` method. After
enough ticks, the previous config is cleared from memory, and only the
current config applies moving forward.
2022-06-20 13:12:49 -07:00
Wilmer Paulino
3dff4abfb1
Expose API to update a channel's ChannelConfig
A new `update_channel_config` method is exposed on the `ChannelManger`
to update the `ChannelConfig` for a set of channels atomically. New
`ChannelUpdate` events are generated for each eligible channel.

Note that as currently implemented, a buggy and/or
auto-policy-management client could spam the network with updates as
there is no rate-limiting in place. This could already be done with
`broadcast_node_announcement`, though users are less inclined to update
that as frequently as its data is mostly static.
2022-06-20 13:12:48 -07:00
Wilmer Paulino
dfd56793a7
Expose ChannelConfig within ChannelDetails
As we prepare to expose an API to update a channel's ChannelConfig,
we'll also want to expose this struct to consumers such that they have
insights into the current ChannelConfig applied for each channel.
2022-06-20 13:12:28 -07:00
Elias Rohrer
717365fbf9 Provide simple interface to query est. liquidity 2022-06-18 14:56:34 +02:00
Valentine Wallace
7bd8f8cadb
onion_utils: add next_hop_packet_pubkey method
To get the next hop's packet's pubkey. This will be used to DRY onion message
forwarding in the upcoming Onion Messages PR #1503
2022-06-17 18:36:10 -04:00
Matt Corallo
abf6564a44
Merge pull request #1532 from ariard/2022-06-scaleup-far-away
Scale up CLTV_FAR_FAR_AWAY to 2 weeks of blocks
2022-06-16 17:27:27 -07:00
Antoine Riard
c989ce189c Scale up CLTV_FAR_FAR_AWAY to 2 weeks of blocks 2022-06-16 16:33:57 -04:00
Matt Corallo
d2a7ee2c71
Merge pull request #1544 from jkczyz/2022-06-node-alias
Define `NodeAlias` struct and `Display` impl
2022-06-16 06:34:08 -07:00
Matt Corallo
e53344663c
Merge pull request #1531 from ariard/2022-06-fee-sniping
Funding_tx: add anti-fee sniping recommendation and check if final
2022-06-16 06:12:29 -07:00
Jeffrey Czyz
21aff6f701
Define NodeAlias struct and Display impl
Provide a wrapper struct for 32-byte node aliases, which implements
Display for printing. Support the UTF-8 character encoding, but replace
control characters and terminate at the first null character. Fall back
to ASCII if the byte sequence is an invalid encoding.
2022-06-15 16:20:07 -05:00
Matt Corallo
44d1dfa23d Correct handling of reorg'd-out revoked counterparty transactions
Previously, while processing a confirmed revoked counterparty
commitment transaction, we'd populate `OnchainEvent`s for live
HTLCs with a `txid` source of the txid of the latest counterparty
commitment transactions, not the confirmed revoked one. This meant
that, if the user is using `transaction_unconfirmed` to notify us
of reorg information, we'd end up not removing the entry if the
revoked commitment transaction was reorg'd out. This would
ultimately cause us to spuriously resolve the HTLC(s) as the chain
advanced, even though we were doing so based on a now-reorged-out
transaction.

Luckily the fix is simple - set the correct txid in the
`OnchainEventEntry`. We also take this opportunity to update
logging in a few places with the txid of the transaction causing an
event.
2022-06-15 14:21:35 +00:00
Matt Corallo
c180ddd57a
Merge pull request #1541 from jkczyz/2022-06-nit-follow-ups 2022-06-15 02:52:35 -07:00
Antoine Riard
69344fab61 Recommend funding_tx to apply anti-fee sniping 2022-06-14 15:57:21 -04:00
Antoine Riard
2b7ef4762f Check if funding transaction is final for propagation
If the funding transaction is timelocked beyond the next block of
our best known chain tip, return an APIError instead of silently
failing at broadcast attempt.
2022-06-14 15:57:11 -04:00
Jeffrey Czyz
1aa1d69461
Remove unnecessary identifiers from match pattern 2022-06-13 18:28:01 -05:00
Jeffrey Czyz
cb66dcd3b4
Replace Arc with reference in some tests 2022-06-13 18:28:01 -05:00
Wilmer Paulino
44fa3acae8
Rename UserConfig and LegacyChannelConfig fields
The current names aren't very clear to what each field represents, this
commit aims to improve that.
2022-06-13 13:57:00 -07:00
Matt Corallo
435680904a
Merge pull request #1526 from tnull/2022-06-fix-minimal-value-contrib
Fix per-path minimal value contribution during route finding
2022-06-13 10:50:19 -07:00
Elias Rohrer
1dfabcb91f Add failure test cases for max_mpp_path_count. 2022-06-13 18:25:19 +02:00
Elias Rohrer
13b7cd503b Fix min. contrib. depending on max_mpp_path_count 2022-06-13 18:24:17 +02:00
Matt Corallo
d6feb1c63b
Merge pull request #1514 from mattfaltyn/issue438 2022-06-13 04:19:02 -07:00