Matt Corallo [Sun, 1 Aug 2021 02:34:08 +0000 (02:34 +0000)]
Don't initialise Vecs being read with VecReadWrapper explicitly
This simplifies the tlv serialization read macro somewhat by
allowing callsites to simply read into an `Option<Vec>` instead of
needing to read into an `Option<VecReadWrapper>` when using
`vec_type`.
Matt Corallo [Wed, 18 Aug 2021 00:33:56 +0000 (00:33 +0000)]
Fix trailing semicolon warnings on latest rustc nightly
Latest rustc nightly compiles are filled with warnings like the
following, which we fix here:
```
warning: trailing semicolon in macro used in expression position
--> lightning/src/util/macro_logger.rs:163:114
|
163 | $logger.log(&$crate::util::logger::Record::new($lvl, format_args!($($arg)+), module_path!(), file!(), line!()));
| ^
|
::: lightning/src/chain/chainmonitor.rs:165:9
|
165 | log_debug!(self.logger, "New best block {} at height {} provided via block_connected", header.block_hash(), height);
| -------------------------------------------------------------------------------------------------------------------- in this macro invocation
|
= note: `#[warn(semicolon_in_expressions_from_macros)]` on by default
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= note: for more information, see issue #79813 <https://github.com/rust-lang/rust/issues/79813>
= note: this warning originates in the macro `log_internal` (in Nightly builds, run with -Z macro-backtrace for more info)
```
Matt Corallo [Tue, 17 Aug 2021 00:46:21 +0000 (00:46 +0000)]
Log chain calls in ChainMonitor, reducing logs in ChannelMonitor
For users with many ChannelMonitors, we log a large volume per
block simply because each ChannelMonitor lots several times per
block. Instead, we move to log only once at the TRACE level per
block call in ChannelMonitors, relying instead on a DEBUG level
log in ChainMonitor before we call any ChannelMonitor functions.
For most users, this will reduce redundant logging and also log at
the DEBUG level for block events, which is appropriate.
Matt Corallo [Tue, 17 Aug 2021 00:04:34 +0000 (00:04 +0000)]
Stop printing to stderr in lightning-net-tokio for disconnections
It isn't exactly a critical error situation when we disconnect a
socket, so we shouldn't be printing to stderr, entirely bypassing
user logging, when it happens. We do still print to stderr if we
fail to write the first message to the socket, but this should
never happen unless the user has a reasonably-configured system
with at least one packet in bytes available for the socket buffer.
Matt Corallo [Fri, 13 Aug 2021 23:01:31 +0000 (23:01 +0000)]
Add relatively simple tests of the legacy and target closing fee
This doesn't exhaustively test closing fee negotiation at all, but
ensures that it is at least basically able to come to consensus and
sign cooperative closing transactions.
Matt Corallo [Mon, 26 Jul 2021 18:59:03 +0000 (18:59 +0000)]
Move shutdown-related tests into a new module
Because ln::functional_tests if over 9000 LoC long, its useful to
move tests into new modules as we can. Here we move all
cooperative shutdown related tests into a new module entitled
`shutdown_tests`
Matt Corallo [Tue, 20 Jul 2021 03:19:01 +0000 (03:19 +0000)]
Clean up existing and add range-based closing_signed negotiation
This adds the new range-based closing_signed negotiation specified
in https://github.com/lightningnetwork/lightning-rfc/pull/847 as
well as cleans up the existing closing_signed negotiation to unify
the new codepaths and the old ones.
Note that because the new range-based closing_signed negotiation
allows the channel fundee to ultimately select the fee out of a
range specified by the funder, which we, of course, always select
the highest allowed amount from. Thus, we've added an extra round
of closing_signed in the common case as we will not simply accept
the first fee we see, always preferring to make the funder pay as
much as they're willing to.
Matt Corallo [Mon, 19 Jul 2021 19:57:37 +0000 (19:57 +0000)]
Send initial closing_signed message asynchronously and handle errs
When we added the support for external signing, many of the
signing functions were allowed to return an error, closing the
channel in such a case. `sign_closing_transaction` is one such
function which can now return an error, except instead of handling
it properly we'd simply never send a `closing_signed` message,
hanging the channel until users intervene and force-close it.
Piping the channel-closing error back through the various callsites
(several of which already have pending results by the time they
call `maybe_propose_first_closing_signed`) may be rather
complicated, so instead we simply attempt to propose the initial
`closing_signed` in `get_and_clear_pending_msg_events` like we do
for holding-cell freeing.
Further, since we now (possibly) generate a `ChannelMonitorUpdate`
on `shutdown`, we may need to wait for monitor updating to complete
before we can send a `closing_signed`, meaning we need to handle
the send asynchronously anyway.
This simplifies a few function interfaces and has no impact on
behavior, aside from a few message-ordering edge-cases, as seen in
the two small test changes required.
Matt Corallo [Wed, 28 Jul 2021 20:24:20 +0000 (20:24 +0000)]
Migrate OnchainEvent serialization to be MaybeReadable.
This adds a new TLV-based enum serialization macro entitled
`impl_writeable_tlv_based_enum_upgradable`. As the name implies,
the new macro allows us to ignore odd-numbered variant entries.
Because the new macro implements only `MaybeReadable` and not
`Readable`, it is not applicable in many contexts, here only being
added for the two `OnchainEvent` structs.
Matt Corallo [Wed, 28 Jul 2021 20:18:10 +0000 (20:18 +0000)]
Implement `VecReadWrapper` for `MaybeReadable`
This makes it much simpler to deal with `MaybeReadable` types in
`Vec`s in TLVs as we can transparently deal with them as `vec`,
with the wrapper doing the Right Thing.
This requires we implement `MaybeReadable` for all `Readable` which
has some downstream implications, but nothing too bad.
Matt Corallo [Mon, 19 Jul 2021 18:32:11 +0000 (18:32 +0000)]
Do not serialize `Channel::last_sent_closing_fee` to disk
We're supposed to write `Channel` to disk as if
`remove_uncommitted_htlcs_and_mark_paused` had just run, however we
were writing `last_sent_closing_fee` to disk (if it is not-None),
whereas `remove_uncommitted_htlcs_and_mark_paused` clears it.
Indeed, the BOLTs say fee "... negotiation restarts on
reconnection."
Matt Corallo [Mon, 5 Jul 2021 23:21:36 +0000 (23:21 +0000)]
Implement the closing_signed TLV suffix with allowed fee ranges
This adds the serialization and structures for the new fee range
specifiers in closing_signed as added upstream at
https://github.com/lightningnetwork/lightning-rfc/pull/847
Matt Corallo [Mon, 5 Jul 2021 23:18:41 +0000 (23:18 +0000)]
Improve TLV serialization macro callability very slightly
This allows decode_tlv_stream!() to be called with either a mutable
reference to a stream or a stream itself and allows
encode_tlv_stream!() to be called with an excess , at the end of
the parameter list.
Matt Corallo [Mon, 26 Jul 2021 20:01:36 +0000 (20:01 +0000)]
Add new `ChannelError` category to send `warning` messages
We don't actually yet support `warning` messages as there are
issues left to resolve in the spec PR, but there's nothing to stop
us adding an internal enum variant for sending a warning message
before we actually support doing so.
Matt Corallo [Wed, 30 Jun 2021 03:16:01 +0000 (03:16 +0000)]
Limit inbound fee updates by dust exposure instead of our estimator
Inbound fee udpates are rather broken in lightning as they can
impact the non-fundee despite the funder paying the fee, but only
in the dust exposure it places on the fundee.
At least lnd is fairly aggressively high in their (non-anchor) fee
estimation, running the risk of force-closure. Further, because we
relied on a fee estimator we don't have full control over, we
were assuming our users' fees are particularly conservative, and
thus were at a lot of risk to force-closures.
This converts our fee limiting to use an absurd upper bound,
focusing on whether we are over-exposed to in-flight dust when we
receive an update_fee.
Matt Corallo [Mon, 12 Jul 2021 15:39:27 +0000 (15:39 +0000)]
Fix handling of inbound uncommitted feerate updates
If we receive an update_fee but do not receive a commitment_signed,
we should not persist the pending fee update to disk or hold on to
it after our peer disconnects.
In order to make the code the most readable, we add a state enum
which matches the relevant states from InboundHTLCState, allowing
for more simple code comparison between inbound HTLC handling and
update_fee handling.
Matt Corallo [Wed, 30 Jun 2021 18:12:51 +0000 (18:12 +0000)]
Fix re-sending commitment updates with an outbound fee update
When we send an update_fee to our counterparty on an outbound
channel, if we need to re-send a commitment update after
reconnection, the update_fee must be present in the re-sent
commitment update messages. However, wewere always setting the
update_fee field in the commitment update to None, causing us to
generate invalid commitment signatures and get channel
force-closures.
This fixes the issue by correctly detecting when an update_fee
needs to be re-sent, doing so when required.
Matt Corallo [Mon, 28 Jun 2021 03:41:44 +0000 (03:41 +0000)]
Automatically update fees on outbound channels as fees change
Previously we'd been expecting to implement anchor outputs before
shipping 0.1, thus reworking our channel fee update process
entirely and leaving it as a future task. However, due to the
difficulty of working with on-chain anchor pools, we are now likely
to ship 0.1 without requiring anchor outputs.
In either case, there isn't a lot of reason to require that users
call an explicit "prevailing feerates have changed" function now
that we have a timer method which is called regularly. Further, we
really should be the ones deciding on the channel feerate in terms
of the users' FeeEstimator, instead of requiring users implement a
second fee-providing interface by calling an update_fee method.
Finally, there is no reason for an update_fee method to be
channel-specific, as we should be updating all (outbound) channel
fees at once.
Thus, we move the update_fee handling to the background, calling it
on the regular 1-minute timer. We also update the regular 1-minute
timer to fire on startup as well as every minute to ensure we get
fee updates even on mobile clients that are rarely, if ever, open
for more than one minute.
Antoine Riard [Wed, 28 Jul 2021 23:55:11 +0000 (19:55 -0400)]
Enforce `max_balance_dust_htlc_msat` at HTLC reception/forward
At `update_add_htlc()`/`send_htlc()`, we verify that the inbound/
outbound dust or the sum of both, on either sides of the link isn't
above new config setting `max_balance_dust_htlc_msat`.
A dust HTLC is hence defined as a trimmed-to-dust one, i.e including
the fee cost to publish its claiming transaction.
Antoine Riard [Wed, 28 Jul 2021 23:51:02 +0000 (19:51 -0400)]
Add new config setting `max_balance_dust_htlc_msat`
Trimmed-to-dust HTLCs are at risk of being burnt as miner fees
at anytime during their lifetime due to the broadcast of either
holder commitment transaction or counterparty's one.
To hedge against this risk, we introduce a new config setting
`max_balance_dust_htlc_msat`, with the initial value of
5_000_000 msat.
Jeffrey Czyz [Mon, 9 Aug 2021 20:15:12 +0000 (15:15 -0500)]
Remove unreachable BroadcastChannelUpdate
When handling shutdown messages, Channel cannot move to
ChannelState::ShutdownComplete. Remove the code in ChannelManager that
adds a MessageSendEvent::BroadcastChannelUpdate in this case since it is
unreachable.
When a shutdown script is omitted from open_channel or accept_channel,
it must be provided when sending shutdown. Generate the shutdown script
at channel closing time in this case rather at channel opening.
This requires producing a ChannelMonitorUpdate with the shutdown script
since it is no longer known at ChannelMonitor creation.
KeysInterface::get_shutdown_pubkey is used to form P2WPKH shutdown
scripts. However, BOLT 2 allows for a wider variety of scripts. Refactor
KeysInterface to allow any supported script while still maintaining
serialization backwards compatibility with P2WPKH script pubkeys stored
simply as the PublicKey.
Add an optional TLV field to Channel and ChannelMonitor to support the
new format, but continue to serialize the legacy PublicKey format.
BOLT 2 enumerates the script formats that may be used for a shutdown
script. KeysInterface::get_shutdown_pubkey returns a PublicKey used to
form one of the acceptable formats (P2WPKH). Add a ShutdownScript
abstraction to encapsulate all accept formats and be backwards
compatible with P2WPKH scripts serialized as the corresponding
PublicKey.
Matt Corallo [Wed, 4 Aug 2021 16:21:36 +0000 (16:21 +0000)]
Suggest faster ping in `PeerManager::timer_tick_occurred` docs
This clarifies the docs for `PeerManager::timer_tick_occurred` to
note that the call rate is entirely up to the user, and also
suggests a faster ping rate of "once every five to ten seconds"
instead of "every 30 seconds". There isn't a lot of reason to want
to ping less often, and faster ping means we detect disconnects
sooner, which is important.
Matt Corallo [Sat, 31 Jul 2021 03:34:16 +0000 (03:34 +0000)]
Correctly detect missing HTLCs when a local commitment tx was broadcast
If we forward an HTLC to our counterparty, but we force-closed the
channel before our counterparty provides us an updated commitment
transaction, we'll end up with a commitment transaction that does
not contain the HTLC which we attempted to forward. In this case,
we need to wait `ANTI_REORG_DELAY` blocks and then fail back the
HTLC as there is no way for us to learn the preimage and the
confirmed commitment transaction paid us the value of the HTLC.
However, check_spend_holder_transaction did not do this - it
instead only looked for dust HTLCs in the confirmed commitment
transaction, paying no attention to what other HTLCs may exist that
are missed.
This will eventually lead to channel force-closure as the channel
on which we received the inbound HTLC to forward will be closed in
time for the initial sender to claim the HTLC on-chain.
Matt Corallo [Sat, 31 Jul 2021 03:31:10 +0000 (03:31 +0000)]
DRY HTLC failure code in check_spend_counterparty_transaction
This extracts the HTLC-not-in-broadcasted-commitment-transaction
code from check_spend_counterparty_transaction and moves it to a
global macro, DRYing up the two very similar codepaths (fixing
some minor logging inconsistencies) in the process.
This macro will be used for local commitment transaction HTLC
failure as well in the next commit.
This commit has no functional change outside of logging.
Matt Corallo [Sun, 1 Aug 2021 02:42:42 +0000 (02:42 +0000)]
Make BackgroundProcessor `#[must_use]` to avoid dropping immediately
It is easy for users to have a bug where they drop a
`BackgroundProcessor` immediately, causing it to start and then
immediately stop. Instead, add a `#[must_use]` tag to provide a
compiler warning for such instances.
Matt Corallo [Sun, 1 Aug 2021 02:13:36 +0000 (02:13 +0000)]
Log when a channel is closed on startup due to stale ChannelManager
This is one of the riskiest parts of our API from the perspective
of accidental force-closes - if users delay persisting the
ChannelManager much at all after a ChannelMonitor we may hit a
force-close after restart.
The fact that we don't log at all when this happens is criminal.
Matt Corallo [Fri, 16 Jul 2021 18:10:37 +0000 (18:10 +0000)]
Add additional TLV serialization type of (default_value, N)
This allows TLV serialization macros to read non-Option-wrapped
types but allow them to be missing, filling them in with the
provided default value as needed.
Matt Corallo [Tue, 3 Aug 2021 16:49:31 +0000 (16:49 +0000)]
Change return value of `claim_funds` to ignore duplicate claims
While we should never reach `ClaimFundsFromHop::DuplicateClaim` in
most cases, if we do, it likely indicates the HTLC was timed out
some time ago and is no longer available to be claimed. Thus, it
does not make sense to imply that we `claimed_any_htlcs`.
Matt Corallo [Fri, 16 Jul 2021 02:16:50 +0000 (02:16 +0000)]
Generate a PaymentForwarded event when a forwarded HTLC is claimed
It is useful for accounting and informational reasons for users to
be informed when a payment has been successfully forwarded. Thus,
when an HTLC which represents a forwarded leg is claimed, we
generate a new `PaymentForwarded` event.
This requires some additional plumbing to return HTLC values from
`OnchainEvent`s. Further, when we have to go on-chain to claim the
inbound side of the payment, we do not inform the user of the fee
reward, as we cannot calculate it until we see what is confirmed
on-chain.
Matt Corallo [Thu, 29 Jul 2021 19:49:09 +0000 (19:49 +0000)]
Fix to_remote SpendableOutputs generation in rare reorg cases
If we first see a local commitment transaction, and then a reorg
causes the confirmed channel close transaction to instead be a
remote commitment transaction, we would fail a spurious `if else`
check, resulting in us not generating the correct `SpendableOutput`
event for the to_remote output now confirmed on chain.
This resolves the incorrect logic and adds a regression test.
Matt Corallo [Mon, 2 Aug 2021 15:04:40 +0000 (15:04 +0000)]
Check IO errors in test using `raw_os_error()` instead of `kind()`
std::io::ErrorKind is a `#[non_exhaustive]` enum as more specific
error types are to be added in the future. It was unclear in the
docs until very recently, however, that this is to be done by
re-defining `ErrorKind::Other` errors to new enum variants. Thus,
our tests which check explicitly for `ErrorKind::Other` as a
result of trying to access a directory as a file were incorrect.
Sadly, these generated no meaningful feedback from rustc at all,
except that they're suddenly failing in rustc beta!
After some back-and-forth, it seems rustc is moving forward
breaking existing code in future versions, so we move to the
"correct" check here, which is to check the raw IO error.
See rust-lang/rust#86442 and rust-lang/rust#85746 for more info.
The previous commit wraps the background thread's JoinHandle in an
Option. Providing a dedicated method to join hides this implementation
detail from users.
Matt Corallo [Fri, 16 Jul 2021 00:21:52 +0000 (00:21 +0000)]
Test preimages are learned instantly in test_onchain_to_onchain_claim
test_onchain_to_onchain_claim was connecting additional blocks in
order to reach HTLC timeout and broadcast an HTLC-Timeout
transaction, resulting in it not testing whether HTLC preimages are
learned instantly in response to HTLC-Success transactions.
Matt Corallo [Thu, 15 Jul 2021 16:30:52 +0000 (16:30 +0000)]
Ignore unknown Events serialized with an odd type value.
This should provide some additional future extensibility, allowing
for new informational events which can be safely ignored to be
ignored by older versions.