Commit graph

551 commits

Author SHA1 Message Date
Matt Corallo
3b6f7f1199 Split NetworkGraph message handling fns into unsigned and signed
This takes the now-public `NetworkGraph` message handling functions
and splits them all into two methods - one which takes a required
Secp256k1 context and verifies signatures and one which takes only
the unsigned part of the message and does not take a Secp256k1
context.

This both clarifies the public API as well as simplifies it, all
without duplicating code.

Finally, this adds an assertion in the Router fuzzer to make sure
the constants used for message deserialization are correct.
2020-11-24 16:33:33 -05:00
Matt Corallo
d9c03f26f9 Move UTXO-lookup into pub utility function from RoutingMsgHandler
This makes the public utility methods in `NetworkGraph` able to do
UTXO lookups ala `NetworkMsgHandler`'s `RoutingMessageHandler`
implementation, slightly simplifying the public interface.

We also take this opportunity to verify signatures before calling
out to UTXO lookups, under the "do actions in order of
cheapest-to-most-expensive to reduce DoS surface" principle.
2020-11-24 14:00:02 -05:00
Matt Corallo
fc7df54f8d
Merge pull request #748 from TheBlueMatt/2020-11-router-fuzzer
Make router_target a bit easier for fuzzers to explore and fix two found bugs
2020-11-24 08:36:14 -08:00
Matt Corallo
c53d8a3596 Expose manual-update methods in NetworkGraph.
These functions were created but previously not exported, however
they are useful if we want to skip signature checking when accepting
routing messages (which we really should be doing in the routing
fuzzer).
2020-11-24 11:04:11 -05:00
Matt Corallo
3c4a0c1fb3
Merge pull request #750 from TheBlueMatt/2020-11-dup-chan-id-crash
Do not generate a channel-closed mon update for never-signed chans
2020-11-23 14:28:11 -08:00
Matt Corallo
36063eeadc Don't create chan-closed mon update for outbound never-signed chans
Like the previous commit for channel-closed monitor updates for
inbound channels during processing of a funding_created message,
this resolves a more general issue for closing outbound channels
which have sent a funding_created but not yet received a
funding_signed.

This issue was also detected by full_stack_target.

To make similar issues easier to detect in testing and fuzzing, an
additional assertion is added to panic on updates to a channel
monitor before registering it.
2020-11-23 17:00:07 -05:00
Matt Corallo
22de94afdd Do not generate a channel-closed mon update for never-signed chans
The full_stack_target managed to find a bug where, if we receive
a funding_created message which has a channel_id identical to an
existing channel, we'll end up
 (a) having the monitor update for the new channel fail (due to
     duplicate outpoint),
 (b) creating a monitor update for the new channel as we
     force-close it,
 (c) panicing due to the force-close monitor update is applied to
     the original channel and is considered out-of-order.

Obviously we shouldn't be creating a force-close monitor update for
a channel which can never appear on chain, so we do that here and
add a test which previously failed and checks a few
duplicate-channel-id cases.
2020-11-23 17:00:07 -05:00
Matt Corallo
423073dfe5 [netgraph] Do not allow capacity_sats * 1000 to overflow-panic
In updating the router fuzzer, it discovered that a remote peer can
cause us to overflow while multiplying the channel capacity value.
Since the value should never exceed 21 million BTC, we just add a
check for that.
2020-11-23 13:52:51 -05:00
Matt Corallo
50b348c4fa [router] Fix + test routing via next/last-hop hints only
We had code in the router to support sending a payment via a single
hop across channels exclusively provided by the next-/last-hop hints.
However, in updating the fuzzer, I noted that this case not only
didn't work, but paniced in some cases.

Here, we both fix the panic, as well as write a new test which
ensures we don't break support for such routing in the future.
2020-11-23 13:52:51 -05:00
Matt Corallo
d737111044 derive(Clone) for several pub simple data structs.
There is no reason not to and Clone can be useful especially in the
bindings context.
2020-11-23 11:08:34 -05:00
Valentine Wallace
6f1a0bf0e4
Claim HTLC output on-chain if preimage is recv'd after force-close
If we receive a preimage for an outgoing HTLC that solves an output on a
backwards force-closed channel, we need to claim the output on-chain.

Note that this commit also gets rid of the channel monitor redundantly setting
`self.counterparty_payment_script` in `check_spend_counterparty_transaction`.

Co-authored-by: Antoine Riard <ariard@student.42.fr>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2020-11-16 15:41:31 -05:00
Valentine Wallace
e70f485011
Split channelmonitor's broadcast_by_holder_state
Now callers will separately retrieve the claim requests/
holder revokable script and the new watched holder outputs.
This will be used in the next commit for times when we
need to get holder claim requests, but don't have access to
the holder commitment transaction.
2020-11-16 15:41:31 -05:00
Valentine Wallace
a3e4f9c967
Extend update_monitor logging
Helpful for debugging. I also included the change in the provide_preimage method
signature which will be used in an upcoming commit, because commit-wise it was
easier to combine the changes.
2020-11-16 15:41:30 -05:00
Valentine Wallace
4ece5fd0f6
Update monitor with preimage after channel close
If the channel is hitting the chain right as we receive a preimage,
previous to this commit the relevant ChannelMonitor would never
learn of this preimage.
2020-11-16 15:41:28 -05:00
Valentine Wallace
50ad627426
Add prev_channel_outpoint to previous hop data
This will be used in upcoming commits to allow us to update a channel
monitor with a preimage after its channel has closed.
2020-11-12 18:52:06 -05:00
Matt Corallo
23a1d7aab5
Merge pull request #721 from TheBlueMatt/2020-09-649-bindings
Bindings Updates for #649
2020-11-12 13:22:54 -08:00
Valentine Wallace
8e7b29160b
Remove unnecessary todo
The ChannelMonitor already monitors the chain for counterparties
revealing preimages, and will give the HTLCSources back to the
ChannelManager for claiming. Thus it's unnecessary for the ChannelManager
to monitor these HTLCs itself.

See is_resolving_htlc_output:
- if the counterparty broadcasted and then claimed one of the HTLCs we
  offered them, line 2015 is where the ChannelMonitor gives the ChannelManager
  the HTLC source
- if we broadcasted and they claimed an HTLC we offered them, line 2025 is
  where the ChannelMonitor gives the ChannelManager the HTLC source
2020-11-08 17:22:20 -05:00
Matt Corallo
2342550af5 Move a struct in bindings up to define it before it is used
This is a limitations in the bindings crate, but not one that's
going to be fixed right now.
2020-10-21 14:50:22 -04:00
Valentine Wallace
cda8fb7f6f
Fix clippy 2020-10-16 13:41:40 -04:00
Valentine Wallace
fc68afb21b
Rename ChannelMonitor::write_for_disk --> serialize_for_disk
This function does not necessarily write to disk, it can serialize to anything
that implements Writer.
2020-10-16 13:41:39 -04:00
Valentine Wallace
a8e82cb3fb
Test that Persist temp and perm failures behave as expected.
If a persister returns a temporary failure, the channel monitor should be able
to be put on ice and then revived later. If a persister returns a permanent
failure, the channel should be force closed.
2020-10-16 13:41:39 -04:00
Valentine Wallace
82f5a1cbda
Add a sample module FilesystemPersister.
Intended to be a cross-platform implementation of the
channelmonitor::Persist trait.

This adds a new lightning-persister crate, that uses the
newly exposed lightning crate's test utilities.

Notably, this crate is pretty small right now. However, due to
future plans to add more data persistence (e.g. persisting the
ChannelManager, etc) and a desire to avoid pulling in filesystem
usage into the core lightning package, it is best for it to be
separated out.

Note: Windows necessitates the use of OpenOptions with the `write`
permission enabled to `sync_all` on a newly opened channel's
data file.
2020-10-16 13:41:36 -04:00
Valentine Wallace
9c3f3e76e5
Integrate Persist into ChainMonitor.
- The ChainMonitor should:
  Whenever a new channel is added or updated, these updates
  should be conveyed to the persister and persisted to disk.
  Even if the update errors while it's being applied, the
  updated monitor still needs to be persisted.
2020-10-16 11:30:34 -04:00
Valentine Wallace
ec4715c09b
Add Persist trait.
Implementors of Persist are responsible for persisting new ChannelMonitors
and ChannelMonitor updates to disk/backups.
2020-10-16 11:30:33 -04:00
Valentine Wallace
4350cc615c
Put test utilities behind a feature flag.
The utilities will still be part of cfg(test). The purpose of this
is to enable other crates in the workspace to use the test utilities.
2020-10-16 11:30:33 -04:00
Antoine Riard
27ee1150dd Assert on correct registeration of outputs index
We remove test_no_failure_dust_htlc_local_commitment from our test
framework as this test deliberately throwing junk transaction in
our monitoring parsing code is hitting new assertions.

This test was added in #333, but it sounds as an oversight as the
correctness intention of this test (i.e verifying lack of dust
HTLCs canceling back in case of junk commitment transaction) doesn't
currently break.
2020-10-14 09:23:56 -04:00
Antoine Riard
613ac6e5f0 Add test_htlc_no_detection
This test is a mutation to underscore the detetection logic bug
we had before #653. HTLC value routed is above the remaining
balance, thus inverting HTLC and `to_remote` output. HTLC
will come second and it wouldn't be seen by pre-#653 detection
as we were eneumerate()'ing on a watched outputs vector (Vec<TxOut>)
thus implictly relying on outputs order detection for correct
spending children filtering.
2020-10-10 18:51:05 -04:00
Antoine Riard
30aad79df8 Add transaction index in watched_outputs
Previously, outputs were monitored based on txid and an index yelled
from an enumeration over the returned selected outputs by monitoring
code. This is always have been broken but was only discovered while
introducing anchor outputs as those ones rank always first per BIP69.
We didn't have test cases where a HTLC was bigger than a party balance
on a holder commitment and thus not ranking first.

Next commit introduce test coverage.
2020-10-10 18:51:05 -04:00
Ryan Loomba
c8e49aa398 trivial changes to fix clippy::write_with_newline warnings 2020-10-08 16:06:03 -07:00
Ryan Loomba
426c5b227d add clippy to travis integration 2020-10-08 09:59:52 -07:00
Ryan Loomba
1276cc72de fix all clippy::redundant_field_names warnings 2020-10-07 11:20:21 -07:00
Matt Corallo
8e99800329 Drop now-unused Vec of outpoints in remote-commitment-tx-tracking
This nearly fully reverts 6f08779b04,
removing the extra data storage that it added.
2020-10-05 12:19:41 -04:00
Matt Corallo
a162840ffd Drop the redundant/broken ChannelMonitor::get_monitored_outpoints
In review of the final doc changes in #649, I noticed there
appeared to be redundant monitored-outpoints function in
`ChannelMonitor` - `get_monitored_outpoints()` and
`get_outputs_to_watch()`.

In 6f08779b04,
get_monitored_outpoints() was added, with its behavior largely the
same as today's - only returning the set of remote commitment txn
outputs that we've learned about on-chain. This is clearly not
sufficient, and in 73dce207dd,
`get_outputs_to_watch` was added which was overly cautious to
ensure nothing was missed. Still, the author of 73dce207dd
(me) seemed entirely unaware of the work in 6f08779b04
(also me), despite the function being the literal next function in
the same file. This is presumably because it was assumed that
`get_monitored_outpoints` referred to oupoints for which we should
monitor for spends of (which is true), while `get_outputs_to_watch`
referred to outpouts which we should monitor for the transaction
containing said output (which is not true), or something of that
nature. Specifically, it is the expected behavior that the only
time we care about `Filter::register_tx` is for the funding
transaction (which we aren't aware of the inputs of), but for all
other transactions we register interest on the basis of an outpoint
in the previous transaction (ie via `Filter::register_output`).

Here we drop the broken-on-day-one `get_monitored_outpoints()`
version, but assert in testing that the values which it would return
are all present in `get_outputs_to_watch()`.
2020-10-05 12:19:41 -04:00
Matt Corallo
3219926258 Test that txn pay at least a minimum relay fee in functional tests
This also pays a fee on the transactions we generate in response to
SpendableOutputDescriptors in tests.

This fixes the known issues in #630, though we should test for
standardness in other ways as well.
2020-10-05 11:54:24 -04:00
Matt Corallo
b43a7e3ef3 Fix feerate calculation on closing transactions
This resolves a number of bugs around how we calculate feerates on
closing transactions:

 * We previously calculated the weight wrong both by always
   counting two outputs instead of counting the number of outputs
   that actually exist in the closing transaction and by not
   counting the witness redeemscript.
 * We use assertions to check the calculated weight matches what we
   actually build (with debug_assertions for variable-length sigs).
 * As an additional sanity check, we really should check that the
   transaction had at least min-relay-fee when we were the channel
   initator.
2020-10-05 11:32:30 -04:00
Matt Corallo
6366fc7154 Fix max fee_satoshis constant to be 21 million, not 2.1 million.
Though hopefully we never see a fee of 2.1 million BTC, either...
2020-10-05 11:32:30 -04:00
Matt Corallo
937d1d8bae
Merge pull request #731 from TheBlueMatt/2020-10-fix-big-lock
Actually hold the total_consistency_lock instead of take-and-drop
2020-10-02 11:53:48 -07:00
Matt Corallo
ddebf36eae Actually hold the total_consistency_lock instead of take-and-drop
It was noticed (via clippy) by @casey that we were taking and then
immediately dropping the total_consistency_lock because `let _ =`
doesn't actually bind the response to anything. This appears to be
a consequence of wanting `if let Some(_) =` to not hold a ref to
the contained value at all, but is relatively surprising to me.
2020-10-02 12:51:25 -04:00
Matt Corallo
2bd571d2f9 Drop last bits of rescan as its too complicated to be worth having
Previously, we had a concept of "rescaning" blocks when we detected
a need to monitor for a new set of outputs in future blocks while
connecting a block. In such cases, we'd need to possibly learn about
these new spends later in the *same block*, requiring clients who
filter blocks to get a newly-filtered copy of the same block. While
redoing the chain access API, it became increasingly clear this was
an overly complicated API feature, and it seems likely most clients
will not use it anyway.

Further, any client who *does* filter blocks can simply update their
filtering algorithm to include any descendants of matched
transactions in the filter results, avoiding the need for rescan
support entirely.

Thus, it was decided that we'd move forward without rescan support
in #649, however to avoid significant further changes in the
already-large 649, we decided to fully remove support in a
follow-up.

Here, we remove the API features that existed for rescan and fix
the few tests to not rely on it.

After this commit, we now only ever have one possible version of
block connection transactions, making it possible to be
significantly more confident in our test coverage actually
capturing all realistic scenarios.
2020-10-02 12:36:46 -04:00
Matt Corallo
b627aa6e7c Drop stale comment about a rescan that doesn't happen in tests 2020-10-02 12:36:46 -04:00
Jeffrey Czyz
6cd6816cd7
Merge branch '2020-06-refactor-chain-listener-move-chainmonitor' into 2020-06-refactor-chain-listener 2020-10-01 09:35:05 -07:00
Jeffrey Czyz
51a5a1a50f
Move ln/channelmonitor.rs to chain/chainmonitor.rs 2020-10-01 08:50:15 -07:00
Jeffrey Czyz
819a8653af
Move channelmonitor.rs from ln to chain module
Given the chain::Watch interface is defined in terms of ChannelMonitor
and ChannelMonitorUpdateErr, move channelmonitor.rs from the ln module
to the chain module.
2020-09-30 22:41:52 -07:00
Jeffrey Czyz
e09767ba2d
Merge ChainMonitor impl blocks 2020-09-30 22:41:52 -07:00
Jeffrey Czyz
8b1e5afddd
Define type alias for enumerated transaction data
Transaction data from a block may be filtered before it is passed to
block_connected functions, which may need the index of each transaction
within the block. Rather than define each function in terms of a slice
of tuples, define a type alias for the slice where it can be documented.
2020-09-30 22:41:52 -07:00
Jeffrey Czyz
71230c995c
Replace WatchEvent usage with get_outputs_to_watch
Outputs to watch are tracked by ChannelMonitor as of
73dce207dd. Instead of determining new
outputs to watch independently using ChainWatchedUtil, do so by
comparing against outputs already tracked. Thus, ChainWatchedUtil and
WatchEvent are no longer needed.
2020-09-30 22:41:23 -07:00
Jeffrey Czyz
9e14256b71
Include funding TXO in outputs to watch
The funding TXO was never added to ChannelMonitor's outputs_to_watch in
73dce207dd. Include it when constructing a
ChannelMonitor.
2020-09-30 22:41:23 -07:00
Jeffrey Czyz
98bc46beb9
Replace WatchEventProvider with chain::Filter
WatchEventProvider served as a means for replacing ChainWatchInterface.
However, it requires users to explicitly fetch WatchEvents, even if not
interested in them. Replace WatchEventProvider by chain::Filter, which
is an optional member of ChainMonitor. If set, interesting transactions
and output spends are registered such that blocks containing them can be
retrieved from a chain source in an efficient manner.

This is useful when the chain source is not a full node. For Electrum,
it allows for pre-filtered blocks. For BIP157/158, it serves as a means
to match against compact filters.
2020-09-30 22:40:33 -07:00
Jeffrey Czyz
1599a13643
Remove ChainListener
BlockNotifier was removed in the previous commit, thus ChainListener is
no longer needed. Instead, anything needing chain events should be
notified directly.
2020-09-30 22:40:12 -07:00
Jeffrey Czyz
367834ca90
Remove BlockNotifier
BlockNotifier is a convenience for handing blocks to listeners. However,
it requires that each listener conforms to the ChainListener interface.
Additionally, there are only two listeners, ChannelManager and
ChainMonitor, the latter of which may not be used when monitoring
channels remotely. Remove BlockNotifier since it doesn't provide much
value and constrains each listener to a specific interface.
2020-09-30 22:39:56 -07:00