Commit graph

2032 commits

Author SHA1 Message Date
jurvis
c70bd1fd55
Keep track of inflight HTLCs across payments
Added two methods, `process_path_inflight_htlcs` and
`remove_path_inflight_htlcs`, that updates that `payment_cache` map with
path information that may have failed, succeeded, or have been given up
on.

Introduced `AccountForInflightHtlcs`, which will wrap our user-provided
scorer. We move the `S:Score` type parameterization from the `Router` to
`find_route`, so we can use our newly introduced
`AccountForInflightHtlcs`.

`AccountForInflightHtlcs` keeps track of a map of inflight HTLCs by
their short channel id, direction, and give us the value that is being
used up.

This map will in turn be populated prior to calling `find_route`, where
we’ll use `create_inflight_map`, to generate a current map of all
inflight HTLCs based on what was stored in `payment_cache`.
2022-08-31 18:50:02 -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
Arik Sosman
f53492eff2
Fix script order in gossip key mismatch error. 2022-08-19 10:07:55 -07: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
Elias Rohrer
2f4457fd7e Drop return value from Filter::register_output
This commit removes the return value from `Filter::register_output` as
creating a suitable value almost always entails blocking operations
(e.g., lookups via network request), which however conflicts with the
requirement that user calls should avoid blocking calls at all cost.

Removing the return value also rendered quite a bit of test code for
dependent transaction handling superfluous, which is therefore also
removed with this commit.
2022-08-15 20:47:33 +02:00
valentinewallace
ca4e31d751
Merge pull request #1659 from valentinewallace/2022-08-fix-payload-decode
Fix payment onion payload decode
2022-08-15 14:32:47 -04:00
Valentine Wallace
81b7b03d4f
Fix fuzzer-found underflow 2022-08-15 12:46:39 -04:00
Valentine Wallace
dfbebbf4c3
Rename HighZeroBytesDroppedVarInt to HighZeroBytesDroppedBigSize
As observed by @wpaulino, this struct encodes its bytes as big-endian,
therefore it's a BigSize, not a VarInt.
2022-08-15 11:41:59 -04:00
Valentine Wallace
a23681fde1
Fix bug in onion payment payload decode
Prior to this change, we could have failed to decode a valid payload of size
>253. This is because we were decoding the length (a BigSize, big-endian) as a
VarInt (little-endian).

Found in #1652.
2022-08-15 11:41:56 -04:00
Matt Corallo
b414c0641b
Merge pull request #1658 from lightning-signer/2022-08-bitcoin-0-29
Update bitcoin crate to 0.29.0
2022-08-12 23:51:06 +00:00
Devrandom
11166aa836 Modify ecdh to take Scalar 2022-08-11 00:21:26 +02:00
Devrandom
7e05623bef Update bitcoin crate to 0.29.0 2022-08-11 00:21:26 +02:00
Matt Corallo
4f6da92c4e Clarify comment on BUFFER_DRAIN_MSGS_PER_TICK. 2022-08-10 19:29:39 +00:00
Valentine Wallace
4a1ee5f9a9 Use util methods in Peer to decide when to forward
This consolidates our various checks on peer buffer space into the
`Peer` impl itself, making the thresholds at which we stop taking
various actions on a peer more readable as a whole.

This commit was primarily authored by `Valentine Wallace
<vwallace@protonmail.com>` with some amendments by `Matt Corallo
<git@bluematt.me>`.
2022-08-10 19:29:39 +00:00
valentinewallace
d2191d9c6d
Merge pull request #1655 from valentinewallace/2022-08-fix-ci-doc-links-check
Fix CI to error on doc links to private items
2022-08-09 12:49:40 -04:00
Valentine Wallace
c242003dd3
Fix CI to error on doc links to private items
Somehow we weren't doing this.
2022-08-07 13:49:12 -04:00
Valentine Wallace
dcef41d17b
Minor msgs::OnionHopData cleanups 2022-08-05 22:34:36 -04:00
Matt Corallo
fc771d3b20 [C Bindings] Expose channel and nodes list in ReadOnlyNetworkGraph 2022-08-05 21:14:36 +00:00
Matt Corallo
b4521f52e2
Merge pull request #1638 from ViktorTigerstrom/2022-07-update-decode-update-add-htlc-onion-return-parameters
Don't return `channel_state` from `decode_update_add_htlc_onion`
2022-08-03 17:44:46 +00:00
Matt Corallo
736c0b9e7f
Merge pull request #1619 from G8XSU/main
Add config support for 'their_channel_reserve_proportional_millionths' [#1498]
2022-08-03 17:37:51 +00:00
Matt Corallo
28c9b56113
Merge pull request #1503 from valentinewallace/2022-05-onion-msgs
Onion messages v1
2022-08-03 04:39:56 +00:00
Valentine Wallace
17ec697f8f
Fix possible incomplete read bug on onion packet decode
Pre-existing to this PR, we were reading next packet bytes with io::Read::read,
which is not guaranteed to read all the bytes we need, only guaranteed to read
*some* bytes.

We fix this to be read_exact, which is guaranteed to read all the next hop
packet bytes.
2022-08-02 19:19:39 -04:00
Valentine Wallace
39397d4e14
Error when attempting to send an OM to a blinded route with 0 hops 2022-08-02 19:19:39 -04:00
Valentine Wallace
6500c99f29
Add SendError enum for onion messages and error on too-big packets 2022-08-02 19:19:39 -04:00
Valentine Wallace
eaff561e24
Add test utilities and integration tests for onion messages 2022-08-02 19:19:39 -04:00
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