Commit graph

594 commits

Author SHA1 Message Date
Jeffrey Czyz
46b68c517d
Include PaymentPathRetry data in PaymentPathFailed
When a payment path fails, it may be retried. Typically, this means
re-computing the route after updating the NetworkGraph and channel
scores in order to avoid the failing hop. The last hop in
PaymentPathFailed's path field contains the pubkey, amount, and CLTV
values needed to pass to get_route. However, it does not contain the
payee's features and route hints from the invoice.

Include the entire set of parameters in PaymentPathRetry and add it to
the PaymentPathFailed event. Add a get_retry_route wrapper around
get_route that takes PaymentPathRetry. This allows an EventHandler to
retry failed payment paths using the payee's route hints and features.
2021-10-25 10:18:11 -05:00
Matt Corallo
f5b6e7f58a Implement HashMap read for MaybeReadable values
This allows us to read a `HashMap` that has values which may be
skipped if they are some backwards-compatibility type.

We also take this opportunity to fail deserialization if keys are
duplicated.
2021-10-22 18:41:42 +00:00
Matt Corallo
107c6c7939
Merge pull request #1108 from TheBlueMatt/2021-10-persist-mon-blocks
Persist ChannelMonitors after new blocks are connected
2021-10-20 00:53:26 +00:00
Matt Corallo
3016ed2d91 Update test_dup_htlc_onchain_fails_on_reload for new persist API
ChannelMonitors now require that they be re-persisted before
MonitorEvents be provided to the ChannelManager, the exact thing
that test_dup_htlc_onchain_fails_on_reload was testing for when it
*didn't* happen. As such, test_dup_htlc_onchain_fails_on_reload is
now testing that we bahve correctly when the API guarantees are not
met, something we don't need to do.

Here, we adapt it to test the new API requirements through
ChainMonitor's calls to the Persist trait instead.
2021-10-20 00:06:18 +00:00
Matt Corallo
5c2ff2cb30 Persist ChannelMonitors after new blocks are connected
This resolves several user complaints (and issues in the sample
node) where startup is substantially delayed as we're always
waiting for the chain data to sync.

Further, in an upcoming PR, we'll be reloading pending payments
from ChannelMonitors on restart, at which point we'll need the
change here which avoids handling events until after the user
has confirmed the `ChannelMonitor` has been persisted to disk.
It will avoid a race where we
 * send a payment/HTLC (persisting the monitor to disk with the
   HTLC pending),
 * force-close the channel, removing the channel entry from the
   ChannelManager entirely,
 * persist the ChannelManager,
 * connect a block which contains a fulfill of the HTLC, generating
   a claim event,
 * handle the claim event while the `ChannelMonitor` is being
   persisted,
 * persist the ChannelManager (before the CHannelMonitor is
   persisted fully),
 * restart, reloading the HTLC as a pending payment in the
   ChannelManager, which now has no references to it except from
   the ChannelMonitor which still has the pending HTLC,
 * replay the block connection, generating a duplicate PaymentSent
   event.
2021-10-20 00:06:17 +00:00
Matt Corallo
89ad059548 Use an opaque type to describe monitor updates in Persist
In the next commit, we'll be originating monitor updates both from
the ChainMonitor and from the ChannelManager, making simple
sequential update IDs impossible.

Further, the existing async monitor update API was somewhat hard to
work with - instead of being able to generate monitor_updated
callbacks whenever a persistence process finishes, you had to
ensure you only did so at least once all previous updates had also
been persisted.

Here we eat the complexity for the user by moving to an opaque
type for monitor updates, tracking which updates are in-flight for
the user and only generating monitor-persisted events once all
pending updates have been committed.
2021-10-19 23:49:04 +00:00
Matt Corallo
df8bde9b2e Move the two-AtomicUsize counter in peer_handler to a util struct
We also take this opportunity to drop byte_utils::le64_to_array, as
our MSRV now supports the native to_le_bytes() call.
2021-10-18 22:04:56 +00:00
Matt Corallo
e9f1934e5b Move MonitorEvent serialization to TLV-enum-upgradable from custom 2021-10-18 22:04:56 +00:00
Matt Corallo
001bc7113a
Merge pull request #1121 from TheBlueMatt/2021-10-return-temp-id
Expose temporary channel ID and user channel ID pre-funding
2021-10-16 02:15:07 +00:00
Matt Corallo
bb7ef6c290 Rename create_channel param to user_channel_id to standardize it 2021-10-16 00:27:04 +00:00
Matt Corallo
c396dc6ee5 Use Persister to return errors in tests not chain::Watch
As ChainMonitor will need to see those errors in a coming PR,
we need to return errors via Persister so that our ChainMonitor
chain::Watch implementation sees them.
2021-10-14 00:19:42 +00:00
Matt Corallo
79541b11e8 Make ChainMonitor::monitors private and expose monitor via getter
Exposing a `RwLock<HashMap<>>` directly was always a bit strange,
and in upcoming changes we'd like to change the internal
datastructure in `ChainMonitor`.

Further, the use of `RwLock` and `HashMap` meant we weren't able
to expose the ChannelMonitors themselves to users in bindings,
leaving a bindings/rust API gap.

Thus, we take this opportunity go expose ChannelMonitors directly
via a wrapper, hiding the internals of `ChainMonitor` behind
getters. We also update tests to use the new API.
2021-10-14 00:17:31 +00:00
Matt Corallo
6a7c48b60d Move ChannelMonitorUpdateErr to chain as it is a chain::Watch val 2021-10-14 00:16:50 +00:00
Matt Corallo
0dfb24e661 Move Persist trait to chainmonitor as that's the only reference 2021-10-14 00:16:50 +00:00
Matt Corallo
9a5c9b3680 Include the user channel id counter in Event::ChannelClosed
This makes it more practical for users to track channels using
their own IDs, especially across funding.
2021-10-13 21:44:58 +00:00
Matt Corallo
fe8c10db95
Merge pull request #1077 from jkczyz/2021-09-failing-route-hop
Include short channel id in PaymentPathFailed
2021-10-13 01:13:41 +00:00
Jeffrey Czyz
202acd9e16
Add failing short channel id to PaymentPathFailed
This will be useful for scoring channels when a payment fails.
2021-10-12 18:40:06 -05:00
Matt Corallo
00ffa4397d Fix loop label shadowing warning 2021-10-12 02:29:56 +00:00
Matt Corallo
d66574803e
Merge pull request #1098 from 1nF0rmed/2021-09-adds-discard-funding-event
Add Event::DiscardFunding generation
2021-10-09 17:17:55 +00:00
1nF0rmed
1955008d6d Adds DiscardFunding event
During the event of a channel close, if the funding transaction
is yet to be broadcasted then a DiscardFunding event is issued
along with the ChannelClose event.
2021-10-09 16:43:50 +05:30
Matt Corallo
e635db0da3
Merge pull request #1062 from galderz/t_payment_hash_999 2021-10-08 20:49:19 +00:00
Galder Zamarreño
204bfd260c Add payment_hash to PaymentSent #999 2021-10-08 06:53:10 +02:00
Matt Corallo
ab0739e3ea
Merge pull request #1079 from TheBlueMatt/2021-09-stale-docs
Update Event::PaymentReceived docs since we require payment secret
2021-10-05 23:16:20 +00:00
Matt Corallo
7478207d5c Update Event::PaymentReceived docs since we require payment secret
Users no longer need to verify the amounts of received payments as
the payment secret will protect us against the probing attacks such
verification was intended to fix.
2021-10-05 22:35:04 +00:00
Matt Corallo
2352587811
Merge pull request #1076 from TheBlueMatt/2021-09-forwardable-regen 2021-09-29 20:24:37 +00:00
Matt Corallo
0fcc34b9b5 Regenerate PendingHTLCsForwardable on reload instead of serializing
When we are prepared to forward HTLCs, we generate a
PendingHTLCsForwardable event with a time in the future when the
user should tell us to forward. This provides some basic batching
of forward events, improving privacy slightly.

After we generate the event, we expect users to spawn a timer in
the background and let us know when it finishes. However, if the
user shuts down before the timer fires, the user will restart and
have no idea that HTLCs are waiting to be forwarded/received.

To fix this, instead of serializing PendingHTLCsForwardable events
to disk while they're pending (before the user starts the timer),
we simply regenerate them when a ChannelManager is deserialized
with HTLCs pending.

Fixes #1042
2021-09-29 19:20:38 +00:00
Matt Corallo
e82318d374 Use Infallible for the unconstructable default custom message type
When we landed custom messages, we used the empty tuple for the
custom message type for `IgnoringMessageHandler`. This was fine,
except that we also implemented `Writeable` to panic when writing
a `()`. Later, we added support for anchor output construction in
CommitmentTransaction, signified by setting a field to `Some(())`,
which is serialized as-is.

This causes us to panic when writing a `CommitmentTransaction`
with `opt_anchors` set. Note that we never set it inside of LDK,
but downstream users may.

Instead, we implement `Writeable` to write nothing for `()` and use
`core::convert::Infallible` for the default custom message type as
it is, appropriately, unconstructable.

This also makes it easier to implement various things in bindings,
as we can always assume `Infallible`-conversion logic is
unreachable.
2021-09-22 22:01:40 +00:00
Valentine Wallace
e5310dd5f0
Add path field to PaymentPathFailed event 2021-09-21 17:40:09 -04:00
Valentine Wallace
bf16dfd153
Rename PaymentFailed -> PaymentPathFailed
Since we don't want to imply to users that a payment has
completely failed when it really has just partially
failed
2021-09-21 17:39:40 -04:00
Matt Corallo
71e0173d8d Fix future unknown Event variant backwards compatibility
In 8ffc2d1742, in 0.0.100, we added
a backwards compatibility feature to the reading of `Event`s - if
the type was unknown and odd, we'd simply ignore the event and
treat it as no event. However, we failed to read the
length-prefixed TLV stream when doing so, resulting in us reading
some of the skipped-event data as the next event or other data in
the ChannelManager.

We fix this by reading the varint length prefix written, then
skipping that many bytes when we come across an unknown odd event
type.
2021-09-21 20:37:35 +00:00
Matt Corallo
2cf42aa388
Merge pull request #997 from ariard/2021-07-add-chan-closed
Add Event::ChannelClosed generation
2021-09-21 20:35:29 +00:00
Antoine Riard
e64467f512 Add Event::ChannelClosed generation at channel shutdown 2021-09-21 15:46:40 -04:00
Matt Corallo
801d6e5256
Merge pull request #1068 from TheBlueMatt/2021-09-ser-cleanup
Simplify Message Serialization and Parse TLV Suffix
2021-09-18 01:42:29 +00:00
Matt Corallo
997dc5f5f3 Convert most P2P msg serialization to a new macro with TLV suffixes
The network serialization format for all messages was changed some
time ago to include a TLV suffix for all messages, however we never
bothered to implement it as there isn't a lot of use validating a
TLV stream with nothing to do with it. However, messages are
increasingly utilizing the TLV suffix feature, and there are some
compatibility concerns with messages written as a part of other
structs having their format changed (see previous commit).

Thus, here we go ahead and convert most message serialization to a
new macro which includes a TLV suffix after a series of fields,
simplifying several serialization implementations in the process.
2021-09-18 01:01:41 +00:00
Matt Corallo
07b674ecb1 Drop writer size hinting/message vec preallocation
In order to avoid significant malloc traffic, messages previously
explicitly stated their serialized length allowing for Vec
preallocation during the message serialization pipeline. This added
some amount of complexity in the serialization code, but did avoid
some realloc() calls.

Instead, here, we drop all the complexity in favor of a fixed 2KiB
buffer for all message serialization. This should not only be
simpler with a similar reduction in realloc() traffic, but also
may reduce heap fragmentation by allocating identically-sized
buffers more often.
2021-09-18 01:01:41 +00:00
Valentine Wallace
c828ff42c0
Add all_paths_failed field to PaymentFailed
see field docs for details
2021-09-17 15:36:27 -04:00
Valentine Wallace
8f1763159e
Prevent duplicate PaymentSent events
by removing all pending outbound payments associated with the same
MPP payment after the preimage is received
2021-09-17 15:36:24 -04:00
Valentine Wallace
641e20763f
Implement Readable/Writeable for HashSet
To be used in upcoming commits for MPP ID storage
2021-09-17 15:23:45 -04:00
Matt Corallo
35573bb3d7
Merge pull request #1034 from TheBlueMatt/2021-07-maturing-claims
Expose in-flight claim balances
2021-09-15 18:44:04 +00:00
Jeffrey Czyz
ba2c00b3f8
EventHandler for applying NetworkUpdate
PaymentFailed events contain an optional NetworkUpdate describing
changes to the NetworkGraph as conveyed by a node along a failed payment
path according to BOLT 4. An EventHandler should apply the update to the
graph so that future routing decisions can account for it.

Implement EventHandler for NetGraphMsgHandler to update NetworkGraph.
Previously, NetGraphMsgHandler::handle_htlc_fail_channel_update
implemented this behavior.
2021-09-15 11:50:31 -05:00
Jeffrey Czyz
eff9a47075
Refactor PaymentFailureNetworkUpdate event
MessageSendEvent::PaymentFailureNetworkUpdate served as a hack to pass
an HTLCFailChannelUpdate from ChannelManager to NetGraphMsgHandler via
PeerManager. Instead, remove the event entirely and move the contained
data (renamed NetworkUpdate) to Event::PaymentFailed to be processed by
an event handler.
2021-09-15 11:50:27 -05:00
Jeffrey Czyz
a6e650630d
Pass Event by reference to EventHandler
Passing an Event by reference rather and by move gives more flexibility
for composing event handlers without needing to clone events.
2021-09-09 22:57:51 -05:00
Devrandom
eebc0a921e Use ClosingTransaction in BaseSign 2021-09-09 20:49:24 +02:00
Matt Corallo
4c4d99b291
Merge pull request #1055 from lightning-signer/2021-08-anchor-tx 2021-09-02 21:54:11 +00:00
Devrandom
8275698f3a Add anchor outputs pair in CommitmentTransaction
The anchor ouputs pair is added if there are pending HTLCs. Or a
a per-party anchor is added if the party has a pending balance.
2021-09-02 09:13:46 +02:00
Devrandom
608ed12a5b Allow BaseSign validation functions to return an Err 2021-08-28 11:04:20 +02:00
Devrandom
181d319630 Always use KeysInterface.read_chan_signer for de-serializing EnforcingSigner in tests 2021-08-28 11:01:15 +02:00
Devrandom
285b3faf77 Enforce signing counterparty commitment only after revocation 2021-08-28 11:01:15 +02:00
Devrandom
241e448d37 Enforce that revocation can only occur after we validated a new commitment 2021-08-28 11:01:15 +02:00
Devrandom
50966e25ba Introduce EnforcementState for EnforcingSigner
as we add more enforcement state variables, we want to keep track of them under a single structure
2021-08-28 11:01:15 +02:00