Commit graph

1130 commits

Author SHA1 Message Date
Matt Corallo
bd14069f04 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.
2021-08-13 23:02:23 +00:00
Matt Corallo
be9ffb4819 Add #[allow(unused_mut)] on reorg_test as older rustc requires mut 2021-08-13 23:02:23 +00:00
Matt Corallo
d3af49e9f0 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.
2021-08-13 21:54:50 +00:00
Matt Corallo
01e8ff5ed8 Log when we change HTLC state while sending a commitment transaction 2021-08-13 21:54:50 +00:00
Matt Corallo
b3d0a8dd4e 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.
2021-08-13 21:54:50 +00:00
Matt Corallo
9d49c5c1a1 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.
2021-08-13 21:54:50 +00:00
Matt Corallo
f8caa325e5 Add fuzz coverage of (potential) fee update messages 2021-08-13 21:54:50 +00:00
Matt Corallo
03439ec99f 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.
2021-08-13 21:54:50 +00:00
Matt Corallo
29b43ba165 Add standard derives for ConfirmationTarget 2021-08-10 22:26:13 +00:00
Matt Corallo
9d8d24f690
Merge pull request #1009 from ariard/2021-07-add-forward-dust-limit
Add new config setting `max_balance_dust_htlc_msat`
2021-08-10 22:11:18 +00:00
Antoine Riard
730f6f334e Add test_max_balance_dust_htlc 2021-08-10 17:30:18 -04:00
Antoine Riard
1cf2b53508 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.
2021-08-10 17:30:15 -04:00
Antoine Riard
29e755bf42 Modify pending inbound/outbound getters to access dust balances 2021-08-10 13:50:24 -04:00
Antoine Riard
53b68236a4 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.
2021-08-10 13:43:32 -04:00
Matt Corallo
d4b6f58ba6
Merge pull request #1025 from TheBlueMatt/2021-07-detect-htlcs-on-local-commitment 2021-08-10 15:10:45 +00:00
Matt Corallo
6a94ff9dae
Merge pull request #1038 from valentinewallace/2021-08-keysend-docs 2021-08-10 00:56:25 +00:00
Valentine Wallace
552a244191
Add requirement of payment secret for multi path payments
in send_payment_internal
2021-08-09 20:09:32 -04:00
Matt Corallo
767f12030b
Merge pull request #1019 from jkczyz/2021-07-shutdown-pubkey
Fetch shutdown script based on `commit_upfront_shutdown_pubkey`
2021-08-09 21:41:02 +00:00
Jeffrey Czyz
1d3861e5f6
Add APIError::IncompatibleShutdownScript 2021-08-09 15:56:29 -05:00
Jeffrey Czyz
4a44bfa3a4
Implement Display for ShutdownScript 2021-08-09 15:56:29 -05:00
Jeffrey Czyz
e1989ada3c
Pass InitFeatures by reference to Channel 2021-08-09 15:56:28 -05:00
Jeffrey Czyz
f1c07b5573
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.
2021-08-09 15:56:10 -05:00
Jeffrey Czyz
ecb0b84241
Generate shutdown script at channel close
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.
2021-08-09 15:55:28 -05:00
Jeffrey Czyz
1c449a39f4
Add features to NodeCfg and use in create_network 2021-08-09 15:55:27 -05:00
Jeffrey Czyz
89fa27432a
Connect nodes in remaining tests
Similar to 2745bd5ac7, this ensures that
ChannelManager knows about the features its peers.
2021-08-09 15:55:26 -05:00
Jeffrey Czyz
ccd11fc35a
Support all shutdown scripts defined in BOLT 2
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.
2021-08-09 15:55:26 -05:00
Jeffrey Czyz
1ab2c7c668
Use ShutdownScript to check scripts from peers 2021-08-09 15:55:26 -05:00
Jeffrey Czyz
ecc70757f9
Add ShutdownScript for BOLT 2 acceptable scripts
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.
2021-08-09 15:55:25 -05:00
Jeffrey Czyz
2833786084
Clean up and add shutdown script functional tests 2021-08-09 15:55:25 -05:00
Matt Corallo
03537cc346
Merge pull request #1035 from TheBlueMatt/2021-08-faster-pings
Suggest faster ping in `PeerManager::timer_tick_occurred` docs
2021-08-09 18:52:25 +00:00
Matt Corallo
70653d0ccb 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.
2021-08-09 18:11:19 +00:00
Matt Corallo
6bfab9d30a 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.
2021-08-09 16:12:53 +00:00
Matt Corallo
925e64228f 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.
2021-08-09 16:12:24 +00:00
Valentine Wallace
929259e546
Update keysend docs 2021-08-08 14:10:21 -04:00
Matt Corallo
853007800e
Merge pull request #1029 from TheBlueMatt/2021-07-log-channel-close
Log when a channel is closed on startup due to stale ChannelManager
2021-08-05 21:05:43 +00:00
Matt Corallo
cab2ca8eeb 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.
2021-08-05 20:24:21 +00:00
Matt Corallo
01bdc15fe6 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.
2021-08-05 12:34:06 -04:00
Matt Corallo
69ee486084
Merge pull request #1004 from TheBlueMatt/2021-07-forward-event
Add a `PaymentForwarded` Event
2021-08-04 22:58:14 +00:00
Matt Corallo
50f47ecc05 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`.
2021-08-04 21:48:21 +00:00
Matt Corallo
2024c5e104 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.

Substantial code structure rewrites by:
Valentine Wallace <vwallace@protonmail.com>
2021-08-04 21:48:21 +00:00
Matt Corallo
09e1670195
Merge pull request #1022 from TheBlueMatt/2021-07-to-remote-reorg
Fix to_remote SpendableOutputs generation in rare reorg cases
2021-08-04 03:08:53 +00:00
Matt Corallo
ad4459080e 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.
2021-08-04 02:34:57 +00:00
Matt Corallo
57feb26307
Merge pull request #1028 from lightning-signer/2021-08-no-std
Actual no_std support
2021-08-03 17:06:59 +00:00
Devrandom
32d13a2ff8 Rename no_std feature to no-std
matches rust-bitcoin
2021-08-03 18:53:33 +02:00
Devrandom
0dfcacd22c Actual no_std support 2021-08-03 09:34:56 +02:00
Jeffrey Czyz
58a4dc0ef4
Fix #[warn(clippy::try_err)] in ser_macros.rs 2021-08-02 10:31:16 -05:00
Matt Corallo
bee9a1e403
Merge pull request #1012 from TheBlueMatt/2021-07-bump-deps
Bump dependencies to bitcoin 0.27 and bech32 0.8
2021-07-31 20:42:59 +00:00
Matt Corallo
0671ca6a17 Add a #[macro_use] on the alloc import for format!() 2021-07-31 18:36:16 +00:00
Matt Corallo
8c16225557 Fix no_std warnings due to unused includes 2021-07-31 18:36:08 +00:00
Matt Corallo
3f229052ea Bump dependencies to bitcoin 0.27 and bech32 0.8 2021-07-31 18:29:07 +00:00