Commit graph

1263 commits

Author SHA1 Message Date
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
Matt Corallo
2745bd5ac7 Connect peers on startup in tests
This avoids `ChannelManager` ever being confused by the fact that
it received a message from a peer which it didn't think it was
connected to.
2021-07-30 18:48:29 +00:00
Matt Corallo
1f013c9cc2 Macroize feature printing to ensure we don't miss new flags 2021-07-28 21:06:49 +00:00
Matt Corallo
f438778715 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.
2021-07-28 17:35:09 +00:00
Matt Corallo
8ffc2d1742 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.
2021-07-28 17:35:09 +00:00
Matt Corallo
49ab8c2f9a Drop single-use macro from check_spend_holder_transaction
The wait_threshold_conf!() macro in check_spend_holder_transaction
was only used once, making it a good candidate for inlining at the
callsite. Further, it incorrectly always logged that we were
failing HTLCs from the "latest" commitment transaction, when it is
sometimes actually failing HTLCs from the previous commitment
transaction.
2021-07-28 17:35:09 +00:00
Matt Corallo
1bb9e64ebc
Merge pull request #977 from TheBlueMatt/2021-06-fix-double-claim-close
Handle double-HTLC-claims without failing the backwards channel
2021-07-28 01:24:27 +00:00
Matt Corallo
f06f9d1136 Fail channel if we can't sign a new commitment tx during HTLC claim
Previously, we could fail to generate a new commitment transaction
but it simply indicated we had gone to doule-claim an HTLC. Now
that double-claims are returned instead as Ok(None), we should
handle the error case and fail the channel, as the only way to hit
the error case is if key derivation failed or the user refused to
sign the new commitment transaction.

This also resolves an issue where we wouldn't inform our
ChannelMonitor of the new payment preimage in case we failed to
fetch a signature for the new commitment transaction.
2021-07-28 00:34:53 +00:00
Matt Corallo
c09104f46e Simplify call graph of get_update_fulfill_htlc since it can't Err. 2021-07-28 00:34:53 +00:00
Matt Corallo
7e78fa660c Handle double-HTLC-claims without failing the backwards channel
When receiving an update_fulfill_htlc message, we immediately
forward the claim backwards along the payment path before waiting
for a full commitment_signed dance. This is great, but can cause
duplicative claims if a node sends an update_fulfill_htlc message,
disconnects, reconnects, and then has to re-send its
update_fulfill_htlc message again.

While there was code to handle this, it treated it as a channel
error on the inbound channel, which is incorrect - this is an
expected, albeit incredibly rare, condition. Instead, we handle
these double-claims correctly, simply ignoring them.

With debug_assertions enabled, we also check that the previous
close of the same HTLC was a fulfill, and that we are not moving
from a HTLC failure to an HTLC claim after its too late.

A test is also added, which hits all three failure cases in
`Channel::get_update_fulfill_htlc`.

Found by the chanmon_consistency fuzzer.
2021-07-28 00:34:53 +00:00
Valentine Wallace
6dd6289d38
Clarify decode_update_add_htlc_onion comment
Clearer phrasing
2021-07-27 15:18:25 -04:00
Valentine Wallace
47bcc1823b
tests: make PaymentSecret optional in pass_along path
and use it to make more keysend tests
2021-07-27 15:18:25 -04:00
Valentine Wallace
0328be32f7
Implement utilities for keysending to private nodes 2021-07-27 15:18:23 -04:00
Valentine Wallace
2d94401cca
Implement sending keysend payments (to public nodes) 2021-07-27 15:15:24 -04:00
Valentine Wallace
d32052fbf6
test utils: add optional PaymentPreimage param to pass_along_path
This will allow keysend tests to assert that the PaymentReceived payment preimage is
as expected in upcoming commits.
2021-07-27 15:15:24 -04:00
Valentine Wallace
5a42be07a0
Implement receiving keysend payments 2021-07-27 15:15:24 -04:00
Valentine Wallace
d1e8d9ced5
Refactor PaymentReceived event for keysend receives 2021-07-27 15:15:23 -04:00
Valentine Wallace
f60a65fec3
Add PendingHTLCRouting variant for receiving keysend payments 2021-07-27 15:15:23 -04:00