Commit graph

3871 commits

Author SHA1 Message Date
Valentine Wallace
f4834def9d
Implement buffering onion messages for peers.
In this commit, we check if a peer's outbound buffer has room for onion
messages, and if so pulls them from an implementer of a new trait,
OnionMessageProvider.

Makes sure channel messages are prioritized over OMs, and OMs are prioritized
over gossip.

The onion_message module remains private until further rate limiting is added.
2022-08-26 19:03:07 -04:00
Valentine Wallace
b28783a0b5
Implement OnionMessageProvider for OnionMessenger 2022-08-26 19:03:05 -04:00
Valentine Wallace
544f72b37c
PeerManager: bump the read pause limit
...to make sure we can still get channel messages out after enqueuing some big gossip messages.
2022-08-26 19:03:05 -04:00
Valentine Wallace
4adff1039f
Add boilerplate for sending and receiving onion messages in PeerManager
Adds the boilerplate needed for PeerManager and OnionMessenger to work
together, with some corresponding docs and misc updates mostly due to the
PeerManager public API changing.
2022-08-26 19:02:59 -04:00
Matt Corallo
2358b0b05f
Merge pull request #1683 from valentinewallace/2022-08-multiqueue-peerman 2022-08-25 21:30:15 +00:00
Matt Corallo
fa62b00f14
Merge pull request #1673 from TheBlueMatt/2022-08-may-learn-preimage-balance
Expose a `Balance` for inbound HTLCs even without a preimage
2022-08-25 20:26:30 +00:00
Valentine Wallace
47e818f198
Separate gossip broadcasts into their own queue in PeerManager
This allows us to better prioritize channel messages over gossip broadcasts and
lays groundwork for rate limiting onion messages more simply, since they won't
be competing with gossip broadcasts for space in the main message queue.
2022-08-25 14:57:43 -04:00
Valentine Wallace
ab149dc9d5
PeerMan: rename drop_gossip util to be more accurate
It's more accurate to name it as dropping gossip broadcasts, as it won't drop
all gossip.
2022-08-25 14:57:43 -04:00
Valentine Wallace
1de698fdd9
PeerMan: fix bug in drop_gossip util
Fixes a flipped bool that was introduced in
4a1ee5f9a9
2022-08-25 14:56:50 -04:00
Matt Corallo
39cede60d0 Rename MaybeClaimableHTLCAwaitingTimeout for consistency
As we now have a MaybePreimageClaimableHTLC, its more consistent
to rename `MaybeClaimableHTLCAwaitingTimeout` to
`MaybeTimeoutClaimableHTLC`.
2022-08-25 18:51:42 +00:00
Matt Corallo
4c9231371b Expose a Balance for inbound HTLCs even without a preimage
If we don't currently have the preimage for an inbound HTLC, that
does not guarantee we can never claim it, but instead only that we
cannot claim it unless we receive the preimage from the channel we
forwarded the channel out on.

Thus, we cannot consider a channel to have no claimable balances if
the only remaining output on the commitment ransaction is an
inbound HTLC for which we do not have the preimage, as we may be
able to claim it in the future.

This commit addresses this issue by adding a new `Balance` variant
- `MaybePreimageClaimableHTLCAwaitingTimeout`, which is generated
until the HTLC output is spent.

Fixes #1620
2022-08-25 18:51:42 +00:00
Matt Corallo
414d8e1c4a
Merge pull request #1682 from tnull/2022-08-export-all-log-levels 2022-08-24 18:41:46 +00:00
Jeffrey Czyz
f2ece975d3
Merge pull request #1652 from valentinewallace/2022-08-reply-paths
Onion messages: support reply paths
2022-08-24 09:55:20 -05:00
Elias Rohrer
c7c88a6572 Export and document all log macros.
Previously, only `log_error` and `log_trace` macros have been exported.
This change exports the macros of all log levels, which enables them to
be used downstream.
2022-08-24 13:59:58 +02:00
Valentine Wallace
950b7d777a
Support sending and receiving reply paths 2022-08-23 20:37:26 -04:00
Valentine Wallace
351349c845
Fix bug in onion message payload decode
Previously, we were decoding payload lengths as a VarInt. Per the spec, this is
wrong -- it should be decoded as a BigSize.  This bug also exists in our
payment payload decoding, to be fixed separately.

Upcoming reply path tests caught this bug because we hadn't encoded a payload
greater than 253 before, so we hadn't hit the problem that VarInts are encoded
as little-endian whereas BigSizes are encoded as big-endian.
2022-08-23 20:33:58 -04:00
Valentine Wallace
dceca5b590
OM functional tests: update util to take nodes by reference
And fix one test to be uniform with the others
2022-08-23 20:33:58 -04:00
Arik
17ae116be0
Merge pull request #1679 from TheBlueMatt/2022-08-no-double-access
Avoid querying the chain for outputs for channels we already have
2022-08-23 13:11:36 -07:00
Matt Corallo
5847abd92d Avoid querying the chain for outputs for channels we already have
If we receive a ChannelAnnouncement message but we already have the
channel, there's no reason to do a chain lookup. Instead of
immediately calling the user-provided `chain::Access` when handling
a ChannelAnnouncement, we first check if we have the corresponding
channel in the graph.

Note that if we do have the corresponding channel but it was not
previously checked against the blockchain, we should still check
with the `chain::Access` and update if necessary.
2022-08-23 17:46:12 +00:00
Jeffrey Czyz
187d18a714
Merge pull request #1681 from dunxen/2022-08-githubactionsfix
Workaround GH Actions issue for 1.45
2022-08-23 09:22:48 -05:00
Duncan Dean
88e562053a
Workaround GH Actions issue for 1.45 2022-08-23 10:54:43 +02:00
Matt Corallo
ea5b62fff6
Merge pull request #1677 from ok300/ok300-patch-1
Bump bitcoin_hashes to v0.11
2022-08-19 22:18:59 +00:00
Matt Corallo
19536c6433
Merge pull request #1676 from arik-so/2022-08-gossip-error-message-fix
Fix script order in gossip key mismatch error.
2022-08-19 19:03:08 +00:00
Matt Corallo
49a0233634
Merge pull request #1575 from NicolaLS/hash-invoice 2022-08-19 17:45:11 +00:00
Arik Sosman
f53492eff2
Fix script order in gossip key mismatch error. 2022-08-19 10:07:55 -07:00
NicolaLS
e263d904a1 derive Hash for Invoice 2022-08-19 16:40:54 +02:00
ok300
1cc2bc5145
Bump bitcoin_hashes to v0.11 2022-08-19 16:40:19 +02:00
Matt Corallo
65d71cdc6b
Merge pull request #1670 from TheBlueMatt/2022-08-mon-size-guidelines
Provide guidance on ChannelMonitorUpdate serialized size
2022-08-18 16:33:20 +00:00
Matt Corallo
dc54c583de
Merge pull request #1495 from TheBlueMatt/2022-04-all-claimables
Expose counterparty-revoked-outputs in `get_claimable_balance`
2022-08-17 22:50:49 +00:00
Matt Corallo
36e9725d9a Provide guidance on ChannelMonitorUpdate serialized size
Users need to make decisions about storage sizing and we need to
have advice on the maximum size of various things users need to
store. ChannelMonitorUpdates are likely the worst case of this,
they're usually at max a few KB, but can get up to a few hundred
KB for commitment transactions that have 400+ HTLCs pending.

We had one user report an update (likely) going over 400 KiB, which
isn't immediately obvious to me is practical, but its within a few
multiples of trivially-reachable sizes, so its likely that did
occur. To be on the safe side, we simply recommend users ensure
they can support "upwards of 1 MiB" here.
2022-08-17 20:49:01 +00:00
valentinewallace
558c460d8a
Merge pull request #1648 from valentinewallace/2022-08-fuzz-onion-msgs
Onion messages: add fuzz testing
2022-08-17 15:48:29 -04:00
Matt Corallo
b50f59d0e5
Merge pull request #1666 from TheBlueMatt/2022-08-fix-script-check
Correct the on-chain script checked in gossip verification
2022-08-17 19:34:26 +00:00
Matt Corallo
6265da0d39 Correct the on-chain script checked in gossip verification
The `bitcoin_key_1` and `bitcoin_key_2` fields in
`channel_announcement` messages are sorted according to node_ids
rather than the keys themselves, however the on-chain funding
script is sorted according to the bitcoin keys themselves. Thus,
with some probability, we end up checking that the on-chain script
matches the wrong script and rejecting the channel announcement.

The correct solution is to use our existing channel funding script
generation function which ensure we always match what we generate.

This was found in testing the Java bindings, where a test checks
that retunring the generated funding script in `chain::Access`
results in the constructed channel ending up in our network graph.
2022-08-17 16:26:23 +00:00
Valentine Wallace
8424f3f054
Fuzz test onion messages
Also update the fuzz ChaCha20Poly1305 to not mark as finished after a single
encrypt_in_place.  This is because more bytes may still need to be encrypted,
causing us to panic at the assertion that finished == false when we go to
encrypt more.

Also fix unused_mut warning in messenger + add log on OM forward for testing
2022-08-16 12:12:10 -04:00
Matt Corallo
12687d75d5
Merge pull request #1660 from TheBlueMatt/2022-08-cleanup-ratelimits
Backfill gossip without buffering directly in LDK
2022-08-16 04:43:02 +00:00
Matt Corallo
d8651e3dd5 Move per-HTLC logic out of get_claimable_balances into a helper
Val suggested this as an obvious cleanup to separate per_HTLC logic
from the total commitment transaction logic, separating the large
function into two.
2022-08-16 01:06:00 +00:00
Matt Corallo
5a8ede09fb Expose counterparty-revoked-outputs in get_claimable_balance
This uses the various new tracking added in the prior commits to
expose a new `Balance` type - `CounterpartyRevokedOutputClaimable`.

Some nontrivial work is required, however, as we now have to track
HTLC outputs as spendable in a transaction that comes *after* an
HTLC-Success/HTLC-Timeout transaction, which we previously didn't
need to do. Thus, we have to check if an
`onchain_events_awaiting_threshold_conf` event spends a commitment
transaction's HTLC output while walking events. Further, because
we now need to track HTLC outputs after the
HTLC-Success/HTLC-Timeout confirms, and because we have to track
the counterparty's `to_self` output as a contentious output which
could be claimed by either party, we have to examine the
`OnchainTxHandler`'s set of outputs to spend when determining if
certain outputs are still spendable.

Two new tests are added which test various different transaction
formats, and hopefully provide good test coverage of the various
revoked output paths.
2022-08-16 01:06:00 +00:00
Matt Corallo
8ffaacb3c3 Drop uneccessary if {...; bool} pattern in PeerManager 2022-08-16 00:52:44 +00:00
Matt Corallo
0ee88d029b Replace manual iteration with for loops in outbound gossip sync 2022-08-16 00:52:44 +00:00
Matt Corallo
f712eb4157 Simplify claimable balance trivially with consistent accessors 2022-08-15 23:18:12 +00:00
Matt Corallo
a17794c380 Scan onchain_events_awaiting_threshold_conf once in balance calc
Instead of a series of different
`onchain_events_awaiting_threshold_conf.iter()...` calls to scan
for HTLC status in balance calculation, pull them all out into one
`for ... { match ... }` to do it once and simplify the code
somewhat.
2022-08-15 23:18:12 +00:00
Matt Corallo
04d8f5e3f1 Track the txid that resolves HTLCs even after resolution completes
We need this information when we look up if we still need to spend
a revoked output from an HTLC-Success/HTLC-Timeout transaction for
balance calculation.
2022-08-15 23:18:12 +00:00
Matt Corallo
9469ce0f38 Track HTLC-Success/HTLC-Timeout claims of revoked outputs
When a counterparty broadcasts a revoked commitment transaction,
followed immediately by HTLC-Success/-Timeout spends thereof, we'd
like to have an `onchain_events_awaiting_threshold_conf` entry
for them.

This does so using the `HTLCSpendConfirmation` entry, giving it
(slightly) new meaning. Because all existing uses of
`HTLCSpendConfirmation` already check if the relevant commitment
transaction is revoked first, this should be trivially backwards
compatible.

We will ultimately figure out if something is being spent via the
`OnchainTxHandler`, but to do so we need to look up the output via
the HTLC transaction txid, which this allows us to do.
2022-08-15 23:18:12 +00:00
Matt Corallo
e76ac33330 Fix off-by-one in test_onchain_htlc_claim_reorg_remote_commitment
The test intended to disconnect a transaction previously connected
but didn't disconnect enough blocks to do so, leading to it
confirming two conflicting transactions.

In the next few commits this will become an assertion failure.
2022-08-15 23:18:12 +00:00
Matt Corallo
66ced68ec6 Clean up nesting in get_counterparty_output_claim_info 2022-08-15 23:18:12 +00:00
Matt Corallo
0ec54ecd97 Track counterparty payout info in counterparty commitment txn
When handling a revoked counterparty commitment transaction which
was broadcast on-chain, we occasionally need to look up which
output (and its value) was to the counterparty (the `to_self`
output). This will allow us to generate `Balance`s for the user for
the revoked output.
2022-08-15 23:18:11 +00:00
Matt Corallo
9115f66dbc Store the full event transaction in OnchainEvent structs
When we see a transaction which generates some `OnchainEvent`, its
useful to have the full transaction around for later analysis.
Specifically, it lets us check the list of outputs which were spent
in the transaction, allowing us to look up, e.g. which HTLC
outpoint was spent in a transaction.

This will be used in a few commits to do exactly that - figure out
which HTLC a given `OnchainEvent` corresponds with.
2022-08-15 23:17:22 +00:00
Matt Corallo
4005724ae6
Merge pull request #1663 from tnull/2022-08-drop-register-output-return
Drop return value from `Filter::register_output`
2022-08-15 23:10:32 +00:00
Matt Corallo
7717fa23a8 Backfill gossip without buffering directly in LDK
Instead of backfilling gossip by buffering (up to) ten messages at
a time, only buffer one message at a time, as the peers' outbound
socket buffer drains. This moves the outbound backfill messages out
of `PeerHandler` and into the operating system buffer, where it
arguably belongs.

Not buffering causes us to walk the gossip B-Trees somewhat more
often, but avoids allocating vecs for the responses. While its
probably (without having benchmarked it) a net performance loss, it
simplifies buffer tracking and leaves us with more room to play
with the buffer sizing constants as we add onion message forwarding
which is an important win.

Note that because we change how often we check if we're out of
messages to send before pinging, we slightly change how many
messages are exchanged at once, impacting the
`test_do_attempt_write_data` constants.
2022-08-15 21:35:05 +00:00
Elias Rohrer
c562f3338b Clarify 'should' vs 'will' in get_relevant_txids 2022-08-15 20:55:37 +02:00