Commit graph

593 commits

Author SHA1 Message Date
Matt Corallo
ac078c103c [bindings] Don't require trait impl for-structs to have no generics
This (finally) exposes `ChannelManager`/`ChannelMonitor` _write
methods, which were (needlessly) excluded as the structs themselves
have generic parameters. Sadly, we also now need to parse
`(C-not exported)` doc comments on impl blocks as we otherwise try
to expose _write methods for `&Vec<RouteHop>`, which doesn't work
(and isn't particularly interesting for users anyway). We add such
doc comments there.
2021-02-03 10:11:28 -05:00
Matt Corallo
b2bf57eb82 [bindings] Don't export new functions with unexportable types
`CommitmentTransaction::new_with_auxiliary_htlc_data()` includes a
unbounded generic parameter which we can't concretize and it's of
limited immediate use for users in any case. We should eventually
add a non-generic version which uses `()` for the generic but that
can come later.

`CommitmentTransaction::htlcs()` returns a reference to a Vec,
which we cannot currently map. It should, however, be exposed to
users, so in the future we'll need to have a duplication function
which returns Vec of references or a cloned Vec.
2021-02-02 17:04:31 -05:00
Matt Corallo
70440a529e [bindings] Use consistent imports for MessageSendEvents traits
Our bindings generator is braindead with respect to the idents
used in a trait definition - it treats them as if they were used
where the trait is being used, instead of where the trait is
defined. Thus, if the idents used in a trait definition are not
also imported the same in the files where the traits are used, we
will claim the idents are bogus.

I spent some time trying to track the TypeResolvers globally
through the entire conversion run so that we could use the original
file's TypeResolver later when using the trait, but it is somewhat
of a lifetime mess. While likely possible, import consistency is
generally the case anyway, so unless it becomes more of an issue in
the future, it likely makes the most sense to just keep imports
consistent.

This commit keeps imports consistent across trait definition files
around `MessageSendEvent` and `MessageSendEventsProvider`.
2021-02-01 16:52:57 -05:00
Matt Corallo
151d4ac0a3
Merge pull request #783 from ariard/2021-01-disconnect-node-id
Add PeerManager::disconnect_node_id()
2021-02-01 11:46:40 -08:00
Antoine Riard
5b7d7ed940 Add PeerManager::disconnect_by_node_id()
This public method allows a client to easily disconnect peers while only
owning its node id. It will clean up peer state and disconnect properly
its descriptor.
2021-02-01 14:13:37 -05:00
Matt Corallo
f151c02975
Merge pull request #764 from lightning-signer/revoke-enforcement
Revocation enforcement
2021-01-25 09:06:43 -08:00
Matt Corallo
25c9a37e18
Merge pull request #777 from sr-gi/cm-force-close-checks
Makes ChannelManager::force_close_channel fail for unknown chan_ids
2021-01-25 07:25:25 -08:00
Devrandom
142b0d624e Let some tests disable revocation policy check
When simulating a bad actor that broadcasts a revoked tx, the policy check would otherwise panic.
2021-01-21 11:37:28 -08:00
Sergi Delgado Segura
821f6cdd1e
Makes ChannelManager::force_close_channel fail for unknown chan_ids
ChannelManager::force_close_channel does not fail if a non-existing channel id is being passed, making it hard to catch from an API point of view.

Makes force_close_channel return in the same way close_channel does so the user calling the method with an unknown id can be warned.
2021-01-21 16:12:57 +01:00
Devrandom
bd4345d6a2 Fix fuzzing issue with revocation 2021-01-18 17:59:43 -08:00
Devrandom
a5869b9281 Revocation enforcement in signer
We want to make sure that we don't sign revoked transactions.

Given that ChannelKeys are not singletons and revocation enforcement is stateful,
we need to store the revocation state in KeysInterface.
2021-01-18 17:59:43 -08:00
Devrandom
2cbb8358f1 Use TestKeysInterface in functional tests
This allows stateful validation in EnforcingChannelKeys
2021-01-18 11:59:39 -08:00
Devrandom
0b20cf62e7 Simplify handling of OnChainTx.holder_commitment
It is no longer optional since it is available at construction time.
2021-01-18 10:24:31 -08:00
Devrandom
63c56a4a86 Panic if signing fails in OnChainTx
Signatures in OnChainTx must not fail, or we stand to lose funds
2021-01-18 10:24:31 -08:00
Devrandom
cb83cfe366 Fold sign_holder_commitment_htlc_transactions into sign_holder_commitment
Signing the commitment transaction is almost always followed by signing the attached HTLC transactions, so fold the signing operations into a single method.
2021-01-18 10:24:31 -08:00
Matt Corallo
d529a8827b
Merge pull request #771 from jkczyz/2021-01-bolt3-test-vectors
Add BOLT 3 test vector for CLTV tiebreaker
2021-01-13 14:40:41 -08:00
Jeffrey Czyz
caa16e0fb5
Add BOLT 3 test vector for CLTV tiebreaker
The test vector was added in https://github.com/lightningnetwork/lightning-rfc/pull/539.
2021-01-08 12:48:24 -08:00
Matt Corallo
b2f1327536
Merge pull request #761 from TheBlueMatt/2020-10-chansigner-no-ser
Add a new method `read_chan_signer` to `KeysInterface`
2021-01-08 08:59:19 -08:00
Sergi Delgado Segura
ee51de9bce
Adds Copy to ChannelHandshakeConfig and UserConfig
This commits adds Copy to ChannelHandshakeConfig and UserConfig for consistency with the rest of config structs.
2021-01-05 20:20:35 +01:00
Matt Corallo
990d1de99a Use KeysInterface::read_chan_signer for all channel keys deser
This drops any direct calls to a generic `ChannelKeys::read()` and
replaces it with the new `KeysInterface::read_chan_signer()`. Still,
under the hood all of our own `KeysInterface::read_chan_signer()`
implementations simply call out to a `Readable::read()` implemention.
2021-01-04 12:40:40 -05:00
Matt Corallo
c07b4de983 Expose test_utils to fuzztarget (in addition to _test_utils feature) 2021-01-04 12:40:40 -05:00
Matt Corallo
45d4d26987 Add a new method read_chan_signer to KeysInterface
This adds a new method to the general cross-channel `KeysInterface`
which requires it to handle the deserialization of per-channel
signer objects. This allows the deserialization of per-channel
signers to have more context available, which, in the case of the
C bindings, includes the actual KeysInterface information itself.
2021-01-04 12:40:40 -05:00
Matt Corallo
0f5580afd4 Use Writeable for ChannelMonitor instead of a specific function.
There's no reason to have ChannelMonitor::write_for_disk instead of
just using the Writeable trait anymore. Previously, it was used to
differentiate with `write_for_watchtower`, but support for
watchtower-mode ChannelMonitors was never completed and the partial
bits were removed long ago.

This has the nice benefit of hitting the custom Writeable codepaths
in C bindings instead of trying to hit trait-generics paths.
2021-01-04 12:40:40 -05:00
Matt Corallo
4345aa88ae Universally Require Writeable for ChannelKeys
It doesn't make sense to ever build a lightning node which doesn't
ever write ChannelMonitors to disk, so having a ChannelKeys object
which doesn't implement Writeable is nonsense.

Here we require Writeable for all ChannelKeys objects, simplifying
code generation for C bindings somewhat.
2021-01-04 12:40:40 -05:00
Matt Corallo
c5fca8c41c Cache fields instead of storing a ChannelKeys in ChannelMonitor
We only actually use two of the fields in ChannelKeys inside a
ChannelMonitor - the holder revocation_basepoint and the
derivation parameters. Both are relatively small, so there isn't
a lot of reason to hold a full copy of the ChannelKeys (with most
of the interaction with it being inside the OnchainTxHandler).

Further, this will avoid calling read on a `ChannelKeys` twice,
which is a somewhat strange API quirk.
2021-01-04 12:37:47 -05:00
Devrandom
9291a38c70 Use new get_commitment_transaction_number_obscure_factor utility function 2020-12-30 13:40:18 -08:00
Devrandom
2de29ae049 Introduce CommitmentTransaction, ChannelTransactionParameters
CommitmentTransaction maintains the per-commitment transaction fields needed to construct the associated bitcoin transactions (commitment, HTLC).  It replaces passing around of Bitcoin transactions.  The ChannelKeys API is modified accordingly.

By regenerating the transaction when implementing a validating external signer, this allows a higher level of assurance that all relevant aspects of the transactions were checked for policy violations.

ChannelTransactionParameters replaces passing around of individual per-channel fields that are needed to construct Bitcoin transactions.

Eliminate ChannelStaticData in favor of ChannelTransactionParameters.

Use counterparty txid instead of tx in channelmonitor update.
2020-12-30 13:40:18 -08:00
Devrandom
a294a3f906 Unwrap unneeded macros 2020-12-18 15:31:51 -08:00
bmancini55
c026764e8d Fix comment for sync_routing_table
Corrects the comment for sync_routing_table in RoutingMessageHandler to
be less prescriptive about the implementor's actions.
2020-12-15 13:48:14 -05:00
bmancini55
e0bb63bc60 Remove should_request_full_sync from RoutingMessageHandler
This method was used to set the initial_routing_sync flag when sending
an outbound Init message to a peer. Since we are now relying on
gossip_queries instead of initial_routing_sync, synchronization can be
fully encapsulate into RoutingMessageHandler via sync_routing_table.

This commit removes should_request_full_sync from the trait
RoutingMessageHandler. The implementation is still used in
NetGraphMsgHandler and has been converted into a private method instead
of a trait function.
2020-12-14 12:52:59 -05:00
bmancini55
e742894492 Change routing table sync to use gossip_queries
This commit changes outbound routing table sync to use gossip_queries
instead of the effectively deprecated initial_routing_sync feature.

This change removes setting of initial_routing_sync in our outbound Init
message. Instead we now call sync_routing_table after receiving an Init
message from a peer. If the peer supports gossip_queries and
should_request_full_sync returns true, we initiate a full gossip_queries
sync.
2020-12-14 12:52:54 -05:00
bmancini55
7a4a29ffe0 Pass Init message to sync_routing_table method
This commit modifies sync_routing_table in RoutingMessageHandler to
accept a reference to the Init message received by the peer. This allows
the method to use the Peer's features to drive the operations of the
gossip_queries routing table sync.
2020-12-09 15:02:32 -05:00
bmancini55
7e1e0ac97c Pass gossip_queries messages to handler via ownership
This change modifies gossip_queries methods in RoutingMessageHandler to
move the message instead of passing a reference. This allows the message
handler to be more efficient by not requiring a full copy of SCIDs
passed in messages.
2020-12-09 15:02:32 -05:00
bmancini55
14d4492b68 Refactor gossip_queries sync to be stateless
This commit simplifies the sync process for routing gossip messages. When
a sync is initiated, the process is handled statelessly by immediately
issuing SCID queries as channel range replies are received. This greatly
simplifies the state machine at the cost of fully validating and
conforming to the current spec.
2020-12-09 15:02:32 -05:00
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
bmancini55
69da2daeae Implement gossip_queries sync methods in NetGraphMsgHandler
To perform a sync of routing gossip messages with a peer requires a two
step process where we first initiate a channel range query to discover
channels in a block range. Next we request the routing gossip messages
for discovered channels.

This code implements logic in NetGraphMsgHandler for performing these two
tasks while taking into account the specification and variance in
implementation.
2020-12-09 15:02:32 -05:00
bmancini55
55e5aafcfe Add gossip_queries methods to RoutingMessageHandler trait
Defines message handlers for gossip_queries messages in the RoutingMessageHandler
trait. The MessageSendEventsProvider supertrait is added to RoutingMessageHandler
so that the implementor can use SendMessageEvents to send messages to a
peer at the appropriate time.

The trait methods are stubbed in NetGraphMsgHandler which implements
RoutingMessageHandler and return a "not implemented" error.
2020-12-09 15:02:32 -05:00
bmancini55
34271fb750 Add send message events for gossip_queries
This change enables initiating gossip queries with a peer using the
SendMessageEvent enum. Specifically we add an event for sending
query_channel_range to discover the existance of channels and an event
for sending query_short_channel_ids to request routing gossip messages
for a set of channels. These events are handled inside the process_events
method of PeerManager which sends the serialized message to the peer.
2020-12-09 15:02:22 -05:00
bmancini55
3220f3b182 Add gossip_queries messages to wire decoding
To enable gossip_queries message decoding, this commit implements the
wire module's Encoding trait for each message type. It also adds these
messages to the wire module's Message enum and the read function to
enable decoding of a buffer.
2020-12-01 17:18:24 -05:00
bmancini55
5af299e7d8 Add gossip_queries feature flag
Support for the gossip_queries feature flag (bits 6/7) is added to the
Features struct. This feature is available in the Init and Node
contexts. The gossip_queries feature is not fully implemented so this
feature is disabled when sent to peers in the Init message.
2020-12-01 17:18:24 -05:00
Matt Corallo
773c2d1dc3
Merge pull request #759 from TheBlueMatt/2020-11-0.0.12
Bump version to 0.0.12
2020-11-24 14:14:16 -08:00
Matt Corallo
4aa8e9cda7 Bump version to 0.0.12 2020-11-24 16:35:20 -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
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