Commit graph

734 commits

Author SHA1 Message Date
bmancini55
d183b975da Add genesis block hash to NetworkGraph
This changes adds the genesis block hash as a BlockHash to the
NetworkGraph struct. Making the NetworkGraph aware allows the message
handler to validate the chain_hash for received messages. This change
also adds the hash value to the Writeable and Readable methods.
2020-12-09 15:02:32 -05:00
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
b56b4add6a [fuzz] Make router_target a bit easier for fuzzers to explore
It turns out (somewhat obviously) that expecting a fuzzer to
correctly build multiple signatures which verify against multiple
public keys in the same message was a bit too daunting, so we now
skip message signatures in routing messages.

We also take this opportunity to simplify the target itself somewhat,
avoiding reading public keys over and over and instead generating
routes to all the public keys that appeared in messages while running.
2020-11-24 11:04:11 -05:00
Matt Corallo
6563f7aa5c [fuzz] Check that channels don't get stuck in chanmon_consistency
This adds a new command string in the chanmon_consistency fuzzer
which tests that, once all pending HTLCs are settled, at least one
side of a channel can still send funds.

While this should have caught the recent(ish) spec bug where
channels could get stuck, I did not attempt to reproduce said bug
with this patch.
2020-11-21 12:11:56 -05:00
Matt Corallo
63d4365702 [fuzz] Don't allow HandleError in chanmon_consistency
We should never generate Ignore-action HandleError events anymore
2020-11-21 12:11:56 -05:00
Matt Corallo
943153530d [fuzz] Reduce overuse of macros/Arcs in chanmon_consistency
In previous versions of related commits, the macros in
chanmon_consistency ended up blowing up rustc a bit resulting in
20+GB memory usage and long compile times. Shorter function bodies
by avoiding macros where possible fix this.
2020-11-21 12:11:23 -05:00
Matt Corallo
71d22f7e06 [fuzz] Expand the amounts we can send in chanmon_consistency
This should make it a bit easier for the fuzzer to hit any given
balance breakdown during run as well as tweaks the command strings
to be more bit-pattern friendly.
2020-11-21 12:11:23 -05:00
Matt Corallo
acf68eddef [fuzz] Test chanmon_consistency payment-send errors are sane
Instead of simply always considering a payment-send failure as
acceptable (and aborting fuzzing), we check that a payment send
failure is from a list of errors that we know we can hit, mostly
around maxing out our channel balance.

Critically, we keep going after hitting an error, as there's no
reason channels should get out of sync even if a send fails.
2020-11-21 12:10:24 -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
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
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
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
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
851283d9e5
Remove Key parameter from ChainMonitor
ChainMonitor's template Key parameter was meant to allow supporting
both local monitoring, where Key=OutPoint, and watchtowers, where Key=
(PublicKey, u32). Use OutPoint directly since the watchtower case will
not be supported this way.
2020-09-30 22:39:55 -07:00
Jeffrey Czyz
6662e959c8
Rename SimpleManyChannelMonitor to ChainMonitor
ManyChannelMonitor was renamed chain::Watch in the previous commit. Use
a more concise name for an implementation that monitors the chain for
channel activity. Future work will parameterize the struct to allow for
different varieties of persistence. Thus, users usually will be able to
use ChainMonitor directly rather than implementing a chain::Watch that
wraps it.
2020-09-30 22:39:55 -07:00
Jeffrey Czyz
801b775a7d
Replace ManyChannelMonitor with chain::Watch
Rename ManyChannelMonitor to chain::Watch and move to chain/mod.rs,
where chain-related interfaces live. Update the documentation for
clarity and to conform to rustdoc formatting.
2020-09-30 22:39:39 -07:00
Jeffrey Czyz
3ee6a27bc6
Replace ChainWatchInterface in NetGraphMsgHandler
ChainWatchInterface was intended as an interface for watching rather
than accessing the chain. Remove get_chain_utxo and add chain::Access
trait for this behavior. Wrap it with an Option in NetGraphMsgHandler in
order to simplify the error interface.
2020-09-30 22:39:38 -07:00
Jeffrey Czyz
87398be293
Remove ChainWatchInterface from channelmonitor.rs
Use of ChainWatchInterface was replaced with WatchEvent in the previous
commit. Remove it from the parameterization of SimpleManyChannelMonitor
since it is no longer needed.
2020-09-30 22:39:12 -07:00
Jeffrey Czyz
a7b2eb6d98
Remove ChainWatchInterface from BlockNotifier
ChainListeners should be independent of each other, but in practice this
is not the case because ChainWatchInterface introduces a dependency
between them. Push ChainWatchInterface down into the ChainListener
implementations where needed. Update ChainListener's block_connected
method to take a slice of the form &[(usize, &Transaction)] where each
transaction is paired with its position within the block.
2020-09-24 10:21:54 -07:00
Matt Corallo
343aacc50c
Merge pull request #684 from bmancini55/gossip_queries
Adding gossip_queries messages and serializations
2020-09-14 13:45:12 -07:00
bmancini55
10e818ac53 Adding fuzzers for gossip_queries messages
This commit adds ser/deser fuzzers for five new structs in ln::msgs used
for gossip_queries.
2020-09-14 16:19:32 -04:00
Antoine Riard
00d063df5c Overhaul ChannelMonitor/OnchainTxHandler to new nomenclature 2020-09-14 14:39:47 -04:00
Matt Corallo
b9707da138 Update to latest upstream rust-bitcoin 2020-09-10 16:20:01 -04:00
Matt Corallo
3defcc8962
Merge pull request #676 from TheBlueMatt/2020-08-c-bindings-cleanups-3
Pre-C-Bindings Cleanups #3
2020-08-26 08:14:34 -07:00
Matt Corallo
af69fae97b
Merge pull request #674 from TheBlueMatt/2020-08-keyif-rand-names
Simplify + clarify random-bytes-fetching from KeysInterface
2020-08-26 08:07:58 -07:00
Matt Corallo
2ff4ae782e Give ChannelManagerReadArgs HashMap-of-monitors ownership
Its somewhat awkward that ChannelManagerReadArgs requires a mutable
reference to a HashMap of ChannelMonitors, forcing the callsite to
define a scope for the HashMap which they almost certainly won't use
after deserializing the ChannelManager. Worse, to map the current
version to C bindings, we'd need to also create a HashMap binding,
which is overkill for just this one use.

Instead, we just give the ReadArgs struct ownership of the HashMap
and add a constructor which fills the HashMap for you.
2020-08-25 21:26:48 -04:00
Matt Corallo
de8c5dc76d Use slices to references not slices of concrete objects in pub API
Because the C bindings maps objects into new structs which contain
only a pointer to the underlying (immovable) Rust type, it cannot
create a list of Rust types which are contiguous in memory. Thus,
in order to allow C clients to call certain Rust functions, we have
to use &[&Type] not &[Type]. This commit fixes this issue for the
get_route function.
2020-08-25 17:09:51 -04:00
Valentine Wallace
ad18c4d853
Add commitment transaction broadcast as a ChannelMonitor event
To do this, we replace get_and_clear_pending_htlcs_updated with
get_and_clear_pending_monitor_events, and which still transmits HTLCUpdates
as before, but now also transmits a new MonitorEvent::CommitmentTxBroadcasted
event when a channel's commitment transaction is broadcasted.
2020-08-25 16:37:49 -04:00
Matt Corallo
6497465762 Simplify + clarify random-bytes-fetching from KeysInterface
Due to a desire to be able to override temporary channel IDs and
onion keys, KeysInterface had two separate fetch-random-32-bytes
interfaces - an onion-key specific version which fetched 2 random
32 byte strings and a temporary-channel-id specific version.

It turns out, we never actually need to override both at once (as
creating a new channel and sending an outbound payment are always
separate top-level calls), so there's no reason to add two
functions to the interface when both really do the same thing.
2020-08-23 19:39:59 -04:00
Matt Corallo
4395b92cc8 Relicense as dual Apache-2.0 + MIT
This changes the LICENSE file and adds license headers to most files
to relicense under dual Apache-2.0 and MIT. This is helpful in that
we retain the patent grant issued under Apache-2.0-licensed work,
avoiding some sticky patent issues, while still allowing users who
are more comfortable with the simpler MIT license to use that.

See https://github.com/rust-bitcoin/rust-lightning/issues/659 for
relicensing statements from code authors.
2020-08-10 21:12:44 -04:00
Gleb Naumenko
8b4f6e8861 Add htlc_maximum_msat field 2020-07-27 14:06:16 +03:00
Gleb Naumenko
b8fc761dc1 Update comment for test_msg_hole 2020-07-24 10:19:45 +03:00
Matt Corallo
5c370232eb Use usize for transaction-position-in-block values
We use them largely as indexes into a Vec<Transaction> so there's
little reason for them to be u32s. Instead, use them as usize
everywhere.

We also take this opportunity to add range checks before
short_channel_id calculation, as we could otherwise end up with a
bogus short_channel_id due to an output index out of range.
2020-06-23 16:12:55 -04:00
Matt Corallo
7fc07b39a6 Drop unused import in fuzz router.rs 2020-06-23 16:12:55 -04:00
Matt Corallo
bcd65bed7b Take &NetworkGraph as input in get_route, not a NetGraphMsgHandler
This was just an oversight when route calculation was split up into
parts - it makes no sense for get_route to require that we have a
full route message handler, only a network graph (which can always
be accessed from a NetGraphMsgHandler anyway).
2020-06-23 16:12:55 -04:00
Matt Corallo
882db714de Make ChainWatchInterface::filter_block return only idxes, not refs
Instead of making the filter_block fn in the ChainWatchInterface
trait return both a list of indexes of transaction positions within
the block and references to the transactions themselves, return
only the list of indexes and then build the reference list at the
callsite.

While this may be slightly less effecient from a memory locality
perspective, it shouldn't be materially different.

This should make it more practical to generate bindings for
filter_block as it no longer needs to reference Rust Transaction
objects that are contained in a Rust Block object (which we'd
otherwise just pass over the FFI in fully-serialized form).
2020-06-23 16:12:55 -04:00
Valentine Wallace
f91718722a
Switch all feerate u64's to u32's.
The protocol only allows a u32, so if we received or sent something
larger it would be an issue (though it's unlikely).
2020-06-15 21:52:52 -04:00
Valentine Wallace
4c655b2281
Use our actual feerate in open_channel messages, not a new one
When we were sending an open_channel messages we were asking the
feerate estimator for a new value instead of using the one we had.
If the feerate estimator gave a different value than the one it did
when we created the Channel struct, we'd start out-of-sync with our
counterparty and blow up on funding_signed. Even worse, the
ConfirmationTarget used was different, so its highly likely they
would disagree.

Also remove newly unused fee estimator parameter from get_open-channel
API.

Co-authored-by: Matt Corallo <git@bluematt.me>
Co-authored-by: Valentine Wallace <vwallace@protonmail.com>
2020-06-15 18:22:33 -04:00
Valentine Wallace
17ccab4f4a
Update chanmon fuzzer to include small payment actions.
This change should allow the fuzzer to catch more edge cases, such as
channel reserve checks that cut it close when sending payments.
2020-06-15 15:51:15 -04:00
Matt Corallo
2087032e7a
Merge pull request #620 from TheBlueMatt/2020-05-pre-bindings-cleanups
Pre-C Bindings Cleanup
2020-05-28 19:34:42 +00:00
Antoine Riard
2f4f0aa766 Extend KeysInterface with derive_channel_keys
A dynamic-p2wsh-output like `to_local` on local commitment/HTLC txn
require a signature from delayed_payment_key to be spend. Instead of
sending private key in descriptor, we ask for spender to derive again
the corresponding ChannelKeys based on key state, uniquely identifying
a channel and encompassing its unique start data.

Descriptor modification is done in next commit.
2020-05-28 04:21:46 -04:00
Matt Corallo
9d4b6e762c Return a ChannelManager instead of result from ChannelManager::new()
We never actually fail, so we can just drop the Result type.
2020-05-22 14:28:56 -04:00
Matt Corallo
fab5d7e0ae Make RoutingMessageHandler a generic Deref instead of an Arc
We also update to use single idents when referencing the Deref=*
types since the automated code generator is pretty braindead.

This also moves some test utils out of peer_handler.rs and into
util::test_utils to standardize things a little bit, which we need
to concretize the PeerHandler types used in testing.
2020-05-22 14:28:13 -04:00
Matt Corallo
6afda60d88 Make ManyChannelMonitor Keys an associated type
Instead of using a raw generic type, an associted type allows us
to have explicit docs on the type, which is nice. More importantly,
however, our automated bindings generator knows how to read
associated types but not raw generics.

Also, our bindings generator expects things which are referenced to
have already been defined, so we move ManyChannelMonitor below the
ChannelMonitor definition.
2020-05-22 13:59:14 -04:00
Matt Corallo
8ade071d56 Drop OutPoint::new since the struct is all pub
This makes it easier for our automated bindings generator to
function as it tries to automatically create a ::new if the struct
contains only pub elements who's type is convertible.
2020-05-17 23:24:41 -04:00