Commit graph

1800 commits

Author SHA1 Message Date
Matt Corallo
28c70ac506 Ensure all HTLCs for a claimed payment are claimed on startup
While the HTLC-claim process happens across all MPP parts under one
lock, this doesn't imply that they are claimed fully atomically on
disk. Ultimately, an application can crash after persisting one
`ChannelMonitorUpdate` out of multiple monitor updates needed for
the full claim.

Previously, this would leave us in a very bad state - because of
the all-channels-available check in `claim_funds` we'd refuse to
claim the payment again on restart (even though the
`PaymentReceived` event will be passed to the user again), and we'd
end up having partially claimed the payment!

The fix for the consistency part of this issue is pretty
straightforward - just check for this condition on startup and
complete the claim across all channels/`ChannelMonitor`s if we
detect it.

This still leaves us in a confused state from the perspective of
the user, however - we've actually claimed a payment but when they
call `claim_funds` we return `false` indicating it could not be
claimed.
2022-05-26 00:53:11 +00:00
Matt Corallo
bd1e20d49e Store an events::PaymentPurpose with each claimable payment
In fc77c57c3c we stopped using the
`FInalOnionHopData` in `OnionPayload::Invoice` directly and intend
to remove it eventually. However, in the next few commits we need
access to the payment secret when claimaing a payment, as we create
a new `PaymentPurpose` during the claim process for a new event.

In order to get access to a `PaymentPurpose` without having access
to the `FinalOnionHopData` we here change the storage of
`claimable_htlcs` to store a single `PaymentPurpose` explicitly
with each set of claimable HTLCs.
2022-05-19 18:16:53 +00:00
Matt Corallo
2a4259566e Enable removal of OnionPayload::Invoice::_legacy_hop_data later
In fc77c57c3c we stopped using the
`FinalOnionHopData` in `OnionPayload::Invoice` directly and renamed
it `_legacy_hop_data` with the intent of removing it in a few
versions. However, we continue to check that it was included in the
serialized data, meaning we would not be able to remove it without
breaking ability to serialize full `ChannelManager`s.

This fixes that by making the `_legacy_hop_data` an `Option` which
we will happily handle just fine if its `None`.
2022-05-19 18:16:53 +00:00
Matt Corallo
36817e0539
Merge pull request #1476 from tnull/2022-05-maximum-path-length
Consider maximum path length during path finding.
2022-05-18 19:22:42 +00:00
Elias Rohrer
87c9684c53 Consider maximum path length during path finding. 2022-05-18 19:20:00 +02:00
valentinewallace
b20aea1cb0
Merge pull request #1472 from TheBlueMatt/2022-06-less-secp-ctx
Pull secp256k1 contexts from per-peer to per-PeerManager
2022-05-17 16:10:09 -04:00
Matt Corallo
998cdc0865
Merge pull request #1418 from bruteforcecat/timeout-outbound-paymnet-retires-instead-of-count-out
add timeout retry strategy to outbound payment
2022-05-17 17:32:46 +00:00
Viktor Tigerström
d543ac04c4 Add missing counterparty_node_id in force_close_channel calls 2022-05-16 22:25:46 +02:00
Arik Sosman
a5629e5ca2
Merge pull request #1479 from ViktorTigerstrom/2022-05-pass-counterparty-id-to-functions
Pass `counterparty_node_id` to `ChannelManager` functions
2022-05-16 12:44:16 -07:00
valentinewallace
257a6f3e48
Merge pull request #1475 from atalw/2022-04-paymentforwarded-event
Expose `next_channel_id` in `PaymentForwarded` event
2022-05-16 14:21:39 -04:00
atalw
1ae1de97fd
Add next_channel_id in PaymentForwarded event
This update also includes a minor refactor. The return type of
`pending_monitor_events` has been changed to a `Vec` tuple with the
`OutPoint` type. This associates a `Vec` of `MonitorEvent`s with a
funding outpoint.

We've also renamed `source/sink_channel_id` to `prev/next_channel_id` in
the favour of clarity.
2022-05-15 09:41:18 +05:30
Matt Corallo
e5c988e00c
Merge pull request #1429 from TheBlueMatt/2022-04-drop-no-conn-possible 2022-05-14 19:35:47 +00:00
Viktor Tigerström
c1798443b0 Update OpenChannelRequest documentation
As the `counterparty_node_id` is now required to be passed back to the
`ChannelManager` to accept or reject an inbound channel request, the
documentation is updated to reflect that.
2022-05-14 20:33:05 +02:00
Viktor Tigerström
70fa465924 Pass counterparty_node_id to accept_inbound_channel 2022-05-14 20:32:44 +02:00
Viktor Tigerström
c581bab8be Pass counterparty_node_id to funding_transaction_generated 2022-05-14 20:32:44 +02:00
Viktor Tigerström
14e52cd7a6 Pass counterparty_node_id to force_close_channel 2022-05-14 20:32:44 +02:00
Viktor Tigerström
84a6e7bc51 Pass counterparty_node_id to close_channel functions 2022-05-14 20:32:44 +02:00
KaFai Choi
10f9795149
move Time trait from scoring mod to util::time and set it visibile within crate 2022-05-14 09:52:59 +07:00
Viktor Tigerström
7893ddc721 Add counterparty_node_id to FundingGenerationReady 2022-05-14 02:15:32 +02:00
Elias Rohrer
79f42d72e2 Fix unused code warnings. 2022-05-13 18:03:51 +02:00
Matt Corallo
e6aaf7c72d Pull secp256k1 contexts from per-peer to per-PeerManager
Instead of including a `Secp256k1` context per
`PeerChannelEncryptor`, which is relatively expensive memory-wise
and nontrivial CPU-wise to construct, we should keep one for all
peers and simply reuse it.

This is relatively trivial so we do so in this commit.

Since its trivial to do so, we also take this opportunity to
randomize the new PeerManager context.
2022-05-11 20:02:29 +00:00
Matt Corallo
b5a63070f5
Merge pull request #1023 from TheBlueMatt/2021-07-par-gossip-processing 2022-05-11 17:24:16 +00:00
Matt Corallo
46009a5f83 Add a few more simple tests of the PeerHandler
These increase coverage and caught previous lockorder inversions.
2022-05-10 23:40:20 +00:00
Matt Corallo
cc7f859c01 Add support for testing recvd messages in TestChannelMessageHandler 2022-05-10 23:40:20 +00:00
Matt Corallo
45c1411b16 Require PartialEq for wire::Message in cfg(test)
...and implement wire::Type for `()` for `feature = "_test_utils"`.
2022-05-10 23:40:20 +00:00
Matt Corallo
101bcd8da5 Drop a needless match in favor of an if let 2022-05-10 23:40:20 +00:00
Matt Corallo
96fc0f3453 Drop PeerHolder as it now only has one field 2022-05-10 23:40:20 +00:00
Matt Corallo
eb17464e78 Keep the same read buffer unless the last message was overly large
This avoids repeatedly deallocating-allocating a Vec for the peer
read buffer after every message/header.
2022-05-10 23:40:20 +00:00
Matt Corallo
ae4ceb71a5 Create a simple FairRwLock to avoid readers starving writers
Because we handle messages (which can take some time, persisting
things to disk or validating cryptographic signatures) with the
top-level read lock, but require the top-level write lock to
connect new peers or handle disconnection, we are particularly
sensitive to writer starvation issues.

Rust's libstd RwLock does not provide any fairness guarantees,
using whatever the OS provides as-is. On Linux, pthreads defaults
to starving writers, which Rust's RwLock exposes to us (without
any configurability).

Here we work around that issue by blocking readers if there are
pending writers, optimizing for readable code over
perfectly-optimized blocking.
2022-05-10 23:40:20 +00:00
Matt Corallo
97711aef96 Limit blocked PeerManager::process_events waiters to two
Only one instance of PeerManager::process_events can run at a time,
and each run always finishes all available work before returning.
Thus, having several threads blocked on the process_events lock
doesn't accomplish anything but blocking more threads.

Here we limit the number of blocked calls on process_events to two
- one processing events and one blocked at the top which will
process all available events after the first completes.
2022-05-10 23:40:20 +00:00
Matt Corallo
4f50a94a3f Avoid the peers write lock unless we need it in timer_tick_occurred
Similar to the previous commit, this avoids "blocking the world" on
every timer tick unless we need to disconnect peers.
2022-05-10 23:40:20 +00:00
Matt Corallo
a5adda18dc Avoid taking the peers write lock during event processing
Because the peers write lock "blocks the world", and happens after
each read event, always taking the write lock has pretty severe
impacts on parallelism. Instead, here, we only take the global
write lock if we have to disconnect a peer.
2022-05-10 23:40:20 +00:00
Matt Corallo
a731efcb68 Process messages with only the top-level read lock held
Users are required to only ever call `read_event` serially
per-peer, thus we actually don't need any locks while we're
processing messages - we can only be processing messages in one
thread per-peer.

That said, we do need to ensure that another thread doesn't
disconnect the peer we're processing messages for, as that could
result in a peer_disconencted call while we're processing a
message for the same peer - somewhat nonsensical.

This significantly improves parallelism especially during gossip
processing as it avoids waiting on the entire set of individual
peer locks to forward a gossip message while several other threads
are validating gossip messages with their individual peer locks
held.
2022-05-10 23:40:20 +00:00
Matt Corallo
7c8b098698 Process messages from peers in parallel in PeerManager.
This adds the required locking to process messages from different
peers simultaneously in `PeerManager`. Note that channel messages
are still processed under a global lock in `ChannelManager`, and
most work is still processed under a global lock in gossip message
handling, but parallelizing message deserialization and message
decryption is somewhat helpful.
2022-05-10 23:40:20 +00:00
Matt Corallo
29727a37fc
Merge pull request #1465 from tnull/2022-05-encode-update-type-bytes
Encode & test `channel_update` message type in failure messages.
2022-05-09 19:11:56 +00:00
Viktor Tigerström
7f0aa9324b Add test for ClosureReason::DisconnectedPeer
Add test that ensures that channels are closed with
`ClosureReason::DisconnectedPeer` if the peer disconnects before the
funding transaction has been broadcasted.
2022-05-09 15:05:24 +02:00
Elias Rohrer
6d8be70c6f Encode channel update type in failure messages. 2022-05-07 08:24:20 +02:00
Jeffrey Czyz
65920818db
Merge pull request #1389 from lightning-signer/2022-03-bitcoin
Update bitcoin crate to 0.28.1
2022-05-05 14:08:16 -05:00
Devrandom
a6501594a5 Improve ShutdownScript::new_witness_program 2022-05-05 18:04:55 +02:00
Devrandom
28d33ff9e0 bitcoin crate 0.28.1 2022-05-05 18:04:42 +02:00
valentinewallace
d96a492b96
Merge pull request #1463 from TheBlueMatt/2022-05-lol-more-underflow
Reject outbound channels if the total reserve is larger than funding
2022-05-04 20:36:03 -04:00
Matt Corallo
9fbafd4b6c
Merge pull request #1430 from vincenzopalazzo/macros/channel_reestablish_v2
send warning when we receive a old commitment transaction
2022-05-04 18:48:19 +00:00
Jeffrey Czyz
23240125b9
Merge pull request #1416 from jurvis/jurvis/persist-scorer
Add utils to persist scorer in BackgroundProcessor
2022-05-04 08:28:06 -05:00
Vincenzo Palazzo
e6300dab2d
send warning when we receive a old commitment transaction
During a `channel_reestablish` now we send a warning message when we receive a old commitment transaction from the peer.

In addition, this commit include the update of functional test to make sure that the receiver will generate warn messages.

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
2022-05-04 09:23:12 +02:00
valentinewallace
72e9f53be7
Merge pull request #1461 from TheBlueMatt/2022-05-unconf-0-not-half
Force-close channels on reorg only if the funding is unconfirmed
2022-05-03 20:44:42 -04:00
Matt Corallo
6418c9ef0d
Merge pull request #1444 from ViktorTigerstrom/2022-04-use-counterparty-htlc-max-for-chan-updates
Set `ChannelUpdate` `htlc_maximum_msat` using the peer's value
2022-05-03 22:44:26 +00:00
Jurvis Tan
5eeb254b82
Add utils to persist scorer in BackgroundProcessor
move last_prune_call back
2022-05-03 15:28:10 -07:00
Viktor Tigerström
224d470d38 Add correct ChannelUpdate htlc_maximum_msat test 2022-05-03 22:42:37 +02:00
Viktor Tigerström
5c7bfa7392 Set ChannelUpdate htlc_maximum_msat using the peer's value
Use the `counterparty_max_htlc_value_in_flight_msat` value, and not the
`holder_max_htlc_value_in_flight_msat` value when creating the
`htlc_maximum_msat` value for `ChannelUpdate` messages.

BOLT 7 specifies that the field MUST be less than or equal to
`max_htlc_value_in_flight_msat` received from the peer, which we
currently are not guaranteed to adhere to by using the holder value.
2022-05-03 22:42:37 +02:00
Viktor Tigerström
d13061c512 Add test for configurable in-flight limit 2022-05-03 22:42:37 +02:00