rust-lightning
2 years agoMove `Score` into a `scoring` module instead of a top-level module 2021-11-chan-size-scoring
Matt Corallo [Fri, 12 Nov 2021 15:52:59 +0000 (15:52 +0000)]
Move `Score` into a `scoring` module instead of a top-level module

Traits in top-level modules is somewhat confusing - generally
top-level modules are just organizational modules and don't contain
things themselves, instead placing traits and structs in
sub-modules. Further, its incredibly awkward to have a `scorer`
sub-module, but only have a single struct in it, with the relevant
trait it is the only implementation of somewhere else. Not having
`Score` in the `scorer` sub-module is further confusing because
it's the only module anywhere that references scoring at all.

2 years agoPenalize large HTLCs relative to channels in default `Scorer`
Matt Corallo [Fri, 12 Nov 2021 04:16:23 +0000 (04:16 +0000)]
Penalize large HTLCs relative to channels in default `Scorer`

Sending HTLCs which are any greater than a very small fraction of the
channel size tend to fail at a much higher rate. Thus, by default
we start applying a penalty at only 1/8th the channel size and
increase it linearly as the amount reaches the channel's capacity,
20 msat per 1024th of the channel capacity.

2 years agoProvide `Score` the HTLC amount and channel capacity
Matt Corallo [Fri, 12 Nov 2021 03:52:58 +0000 (03:52 +0000)]
Provide `Score` the HTLC amount and channel capacity

This should allow `Score` implementations to make substantially
better decisions, including of the form "willing to pay X to avoid
routing over this channel which may have a high failure rate".

2 years agoMerge pull request #1160 from jkczyz/2011-11-spontaneous-invoice-payer
Matt Corallo [Tue, 16 Nov 2021 20:56:40 +0000 (20:56 +0000)]
Merge pull request #1160 from jkczyz/2011-11-spontaneous-invoice-payer

Retries for spontaneous payments

2 years agoAdd PaymentHash parameter to Router::find_route
Jeffrey Czyz [Fri, 12 Nov 2021 03:47:59 +0000 (21:47 -0600)]
Add PaymentHash parameter to Router::find_route

Implementations of Router may need the payment hash in order to look up
pre-computed routes from a probe for a given payment. Add a PaymentHash
parameter to Router::find_route to allow for this.

2 years agoTest retrying payment on partial send failure
Jeffrey Czyz [Tue, 16 Nov 2021 04:47:40 +0000 (22:47 -0600)]
Test retrying payment on partial send failure

Add some test coverage for when InvoicePayer retries within pay_invoice
because the Payer returned a partial failure.

2 years agoReplace expect_value_msat with expect_send
Jeffrey Czyz [Wed, 10 Nov 2021 00:07:23 +0000 (18:07 -0600)]
Replace expect_value_msat with expect_send

Modify all InvoicePayer unit tests to use expect_send instead of
expect_value_msat, since the former can discern whether the send was for
an invoice, spontaneous payment, or a retry. Updates tests to set payer
expectations if they weren't already and assert these before returning a
failure.

2 years agoSupport spontaneous payments in InvoicePayer
Jeffrey Czyz [Tue, 9 Nov 2021 23:32:37 +0000 (17:32 -0600)]
Support spontaneous payments in InvoicePayer

InvoicePayer handles retries not only when handling PaymentPathFailed
events but also for some types of PaymentSendFailure on the initial
send. Expand InvoicePayer's interface with a pay_pubkey function for
spontaneous (keysend) payments. Add a send_spontaneous_payment function
to the Payer trait to support this and implement it for ChannelManager.

2 years agoRefactor InvoicePayer for spontaneous payments
Jeffrey Czyz [Tue, 9 Nov 2021 15:37:34 +0000 (09:37 -0600)]
Refactor InvoicePayer for spontaneous payments

To support spontaneous payments, InvoicePayer's sending logic must be
invoice-agnostic. Refactor InvoicePayer::pay_invoice_internal such that
invoice-specific code is in pay_invoice_using_amount and the remaining
logic is in pay_internal.

Further refactor the code's payment_cache locking such that it is
accessed consistently when needed, and tidy up the code a bit.

2 years agoMerge pull request #1161 from TheBlueMatt/2021-11-fix-chan-type-ser
Matt Corallo [Tue, 16 Nov 2021 18:18:01 +0000 (18:18 +0000)]
Merge pull request #1161 from TheBlueMatt/2021-11-fix-chan-type-ser

Correct Channel type serialization logic

2 years agoCorrect Channel type serialization logic 2021-11-fix-chan-type-ser
Matt Corallo [Tue, 9 Nov 2021 21:22:47 +0000 (21:22 +0000)]
Correct Channel type serialization logic

Currently, we write out the Channel's `ChannelTypeFeatures` as an
odd type, implying clients which don't understand the
`ChannelTypeFeatures` field can simply ignore it. This is obviously
nonsense if the channel type is some future version - the client
needs to fail to deserialize as it doesn't understand the channel's
type.

We adapt the serialization logic here to only write out the
`ChannelTypeFeatures` field if it is something other than
only-static-remote-key, and simply consider that "default" (as it
is the only supported type today). Then, we write out the channel
type as an even TLV, implying clients which do not understand it
must fail to read the `Channel`.

Note that we do not need to bother reserving the TLV type no longer
written as it never appeared in a release (merged post-0.0.103).

2 years agoMerge pull request #1119 from TheBlueMatt/2021-10-less-aggressive-htlc-timeouts
Matt Corallo [Tue, 16 Nov 2021 16:18:20 +0000 (16:18 +0000)]
Merge pull request #1119 from TheBlueMatt/2021-10-less-aggressive-htlc-timeouts

Be less aggressive in outbound HTLC CLTV timeout checks

2 years agoBe less aggressive in outbound HTLC CLTV timeout checks 2021-10-less-aggressive-htlc-timeouts
Matt Corallo [Wed, 13 Oct 2021 04:19:13 +0000 (04:19 +0000)]
Be less aggressive in outbound HTLC CLTV timeout checks

We currently assume our counterparty is naive and misconfigured and
may force-close a channel to get an HTLC we just forwarded them.

There shouldn't be any reason to do this - we don't have any such
bug, and we shouldn't start by assuming our counterparties are
buggy. Worse, this results in refusing to forward payments today,
failing HTLCs for largely no reason.

Instead, we keep a fairly conservative check, but not one which
will fail HTLC forwarding spuriously - testing only that the HTLC
doesn't expire for a few blocks from now.

Fixes #1114.

2 years agoMerge pull request #1131 from TheBlueMatt/2021-10-upstream-dust
Matt Corallo [Sat, 13 Nov 2021 00:23:28 +0000 (00:23 +0000)]
Merge pull request #1131 from TheBlueMatt/2021-10-upstream-dust

Use upstream rust-bitcoin's dust calculation instead of our own

2 years agoUse upstream rust-bitcoin's dust calculation instead of our own 2021-10-upstream-dust
Matt Corallo [Mon, 11 Oct 2021 17:22:08 +0000 (17:22 +0000)]
Use upstream rust-bitcoin's dust calculation instead of our own

Not only does this move to common code, but it fixes handling of
all output types except for a few trivial cases.

2 years agoMerge pull request #1143 from TheBlueMatt/2021-10-no-payment-id-leaks
Matt Corallo [Fri, 12 Nov 2021 17:59:18 +0000 (17:59 +0000)]
Merge pull request #1143 from TheBlueMatt/2021-10-no-payment-id-leaks

Fix a minor memory leak on PermanentFailure mon errs when sending

2 years agoMerge pull request #1165 from lightning-signer/2021-11-fix-anchors
Matt Corallo [Fri, 12 Nov 2021 15:22:41 +0000 (15:22 +0000)]
Merge pull request #1165 from lightning-signer/2021-11-fix-anchors

Fix countersignatory (to_remote) output redeemscript when anchors enabled

2 years agoRenamed script_for_p2wpkh to get_p2wpkh_redeemscript to match convention
Ken Sedgwick [Thu, 11 Nov 2021 20:01:44 +0000 (12:01 -0800)]
Renamed script_for_p2wpkh to get_p2wpkh_redeemscript to match convention

2 years agoFix to_remote output redeemscript when anchors enabled
Ken Sedgwick [Wed, 10 Nov 2021 08:09:24 +0000 (00:09 -0800)]
Fix to_remote output redeemscript when anchors enabled

2 years agoFix a minor memory leak on PermanentFailure mon errs when sending 2021-10-no-payment-id-leaks
Matt Corallo [Tue, 26 Oct 2021 21:40:14 +0000 (21:40 +0000)]
Fix a minor memory leak on PermanentFailure mon errs when sending

If we send a payment and fail to update the first-hop channel state
with a `PermanentFailure` ChannelMonitorUpdateErr, we would have an
entry in our pending payments map, but possibly not return the
PaymentId back to the user to retry the payment, leading to a (rare
and relatively minor) memory leak.

2 years agoMerge pull request #1105 from TheBlueMatt/2021-10-log-persist-time
Matt Corallo [Thu, 11 Nov 2021 15:47:37 +0000 (15:47 +0000)]
Merge pull request #1105 from TheBlueMatt/2021-10-log-persist-time

Log before+after ChannelMonitor/Manager updates for visibility

2 years agoLog before+after ChannelMonitor/Manager updates for visibility 2021-10-log-persist-time
Matt Corallo [Mon, 4 Oct 2021 03:11:36 +0000 (03:11 +0000)]
Log before+after ChannelMonitor/Manager updates for visibility

I realized on my own node that I don't have any visibility into how
long a monitor or manager persistence call takes, potentially
blocking other operations. This makes it much more clear by adding
a relevant log_trace!() print immediately before and immediately
after persistence.

2 years agoMerge pull request #1158 from jkczyz/2021-11-scorer-tests
Matt Corallo [Mon, 8 Nov 2021 23:56:47 +0000 (23:56 +0000)]
Merge pull request #1158 from jkczyz/2021-11-scorer-tests

Scorer unit tests

2 years agoAdd unit tests for Scorer
Jeffrey Czyz [Thu, 4 Nov 2021 21:55:01 +0000 (16:55 -0500)]
Add unit tests for Scorer

Test basic and channel failure penalties, including after a
(de-)serialization round trip.

2 years agoAdd SinceEpoch time to test Scorer hermetically
Jeffrey Czyz [Thu, 4 Nov 2021 18:58:11 +0000 (13:58 -0500)]
Add SinceEpoch time to test Scorer hermetically

In order to test Scorer hermetically, sleeps must be avoided. Add a
SinceEpoch abstraction for manually advancing time. Implement the Time
trait for SinceEpoch so that it can be used with ScorerUsingTime in
tests.

2 years agoMerge pull request #1154 from TheBlueMatt/2021-11-103-doc-tweaks
Matt Corallo [Fri, 5 Nov 2021 15:45:21 +0000 (15:45 +0000)]
Merge pull request #1154 from TheBlueMatt/2021-11-103-doc-tweaks

0.0.103 CHANGELOG tweaks from Jeff

2 years agoAdd note about PaymentId fields to 0.0.103 changelog 2021-11-103-doc-tweaks
Matt Corallo [Wed, 3 Nov 2021 16:51:56 +0000 (16:51 +0000)]
Add note about PaymentId fields to 0.0.103 changelog

2 years ago0.0.103 CHANGELOG tweaks from Jeff
Matt Corallo [Wed, 3 Nov 2021 16:05:02 +0000 (16:05 +0000)]
0.0.103 CHANGELOG tweaks from Jeff

2 years agoMerge pull request #1078 from TheBlueMatt/2021-09-chan-types
Matt Corallo [Wed, 3 Nov 2021 16:58:33 +0000 (16:58 +0000)]
Merge pull request #1078 from TheBlueMatt/2021-09-chan-types

Implement channel_type negotiation

2 years agoMerge pull request #1153 from TheBlueMatt/2021-11-0.0.103 v0.0.103
Matt Corallo [Wed, 3 Nov 2021 03:30:39 +0000 (03:30 +0000)]
Merge pull request #1153 from TheBlueMatt/2021-11-0.0.103

One final fix + cut 0.0.103

2 years agoBump crate versions to 0.0.103/invoice 0.11 2021-11-0.0.103
Matt Corallo [Wed, 3 Nov 2021 01:14:53 +0000 (01:14 +0000)]
Bump crate versions to 0.0.103/invoice 0.11

2 years agoUpdate CHANGELOG for 0.0.103
Matt Corallo [Wed, 3 Nov 2021 01:05:05 +0000 (01:05 +0000)]
Update CHANGELOG for 0.0.103

2 years agoTweak serialization of ScorerUsingTime for better forward compat
Matt Corallo [Wed, 3 Nov 2021 01:11:25 +0000 (01:11 +0000)]
Tweak serialization of ScorerUsingTime for better forward compat

2 years agoMerge pull request #1150 from TheBlueMatt/2021-11-103-bindings
Matt Corallo [Tue, 2 Nov 2021 21:56:00 +0000 (21:56 +0000)]
Merge pull request #1150 from TheBlueMatt/2021-11-103-bindings

Pre-0.0.103 Bindings Tweaks

2 years agoAdd `(C-not exported)` tags as required in tuple types 2021-11-103-bindings
Matt Corallo [Mon, 1 Nov 2021 22:01:57 +0000 (22:01 +0000)]
Add `(C-not exported)` tags as required in tuple types

This prepares us for C bindings auto-exporting tuple type fields.

2 years agoAdd `(C-not exported)` tag to a `Payee` modifier with move semantics
Matt Corallo [Mon, 1 Nov 2021 04:23:30 +0000 (04:23 +0000)]
Add `(C-not exported)` tag to a `Payee` modifier with move semantics

This matches the other `Payee` move-modifier functions.

2 years agoRemove now-unused import in routing/mod.rs
Matt Corallo [Sun, 31 Oct 2021 00:26:54 +0000 (00:26 +0000)]
Remove now-unused import in routing/mod.rs

2 years agoMake payment_path_failed path type bindings-mappable
Matt Corallo [Sun, 31 Oct 2021 18:21:46 +0000 (18:21 +0000)]
Make payment_path_failed path type bindings-mappable

The bindings don't currently support passing `Vec`s of objects
which it mappes as "opaque types". This is because it will require
clones to convert its own list of references to Rust's list of
objects.

In the near future we should resolve this limitation, allowing us
to revert this (and make `find_route`'s method signature similarly
cleaner), but for now we must avoid `Vec<OpaqueType>`.

2 years agoRename Payee::new to Payee::from_node_id to clarify it somewhat
Matt Corallo [Sun, 31 Oct 2021 18:20:27 +0000 (18:20 +0000)]
Rename Payee::new to Payee::from_node_id to clarify it somewhat

This also differentiates it from the bindings default-constructed
`new` method which is constructed when all fields are exposed and
of mappable types.

2 years agoRemove trailing ;s from macro calls to silence new rustc warnings
Matt Corallo [Sun, 31 Oct 2021 18:19:39 +0000 (18:19 +0000)]
Remove trailing ;s from macro calls to silence new rustc warnings

2 years agoMerge pull request #1146 from jkczyz/2021-10-score-serialization
Matt Corallo [Tue, 2 Nov 2021 20:36:22 +0000 (20:36 +0000)]
Merge pull request #1146 from jkczyz/2021-10-score-serialization

Scorer serialization

2 years agoImplement (de)serialization for Scorer
Jeffrey Czyz [Fri, 29 Oct 2021 04:44:26 +0000 (23:44 -0500)]
Implement (de)serialization for Scorer

Scorer should be serialized to retain penalty data between restarts.
Implement (de)serialization for Scorer by serializing last failure times
as duration since the UNIX epoch. For no-std, the zero-Duration is used.

2 years agoParameterize Scorer by a Time trait
Jeffrey Czyz [Fri, 29 Oct 2021 13:52:27 +0000 (08:52 -0500)]
Parameterize Scorer by a Time trait

Scorer uses time to determine how much to penalize a channel after a
failure occurs. Parameterizing it by time cleans up the code such that
no-std support is in a single AlwaysPresent struct, which implements the
Time trait. Time is implemented for std::time::Instant when std is
available.

This parameterization also allows for deterministic testing since a
clock could be devised to advance forward as needed.

2 years agoRefactor channel failure penalty logic
Jeffrey Czyz [Fri, 29 Oct 2021 04:23:45 +0000 (23:23 -0500)]
Refactor channel failure penalty logic

Move channel failure penalty logic into a ChannelFailure abstraction.
This encapsulates the logic for accumulating penalties and decaying them
over time. It also is responsible for the no-std behavior. This cleans
up Scorer and will make it easier to serialize it.

2 years agoClarify Scorer docs around penalizing channels
Jeffrey Czyz [Mon, 1 Nov 2021 21:35:39 +0000 (16:35 -0500)]
Clarify Scorer docs around penalizing channels

2 years agoMerge pull request #1149 from jkczyz/2021-11-network-graph
Matt Corallo [Mon, 1 Nov 2021 22:19:08 +0000 (22:19 +0000)]
Merge pull request #1149 from jkczyz/2021-11-network-graph

Shared ownership of NetworkGraph

2 years agoMake NetGraphMsgHandler::network_graph private
Jeffrey Czyz [Mon, 1 Nov 2021 20:01:39 +0000 (15:01 -0500)]
Make NetGraphMsgHandler::network_graph private

Since NetworkGraph has shared ownership, NetGraphMsgHandler does not
need to expose its field.

2 years agoParameterize NetGraphMsgHandler with NetworkGraph
Jeffrey Czyz [Mon, 1 Nov 2021 18:14:14 +0000 (13:14 -0500)]
Parameterize NetGraphMsgHandler with NetworkGraph

NetworkGraph is owned by NetGraphMsgHandler, but DefaultRouter requires
a reference to it. Introduce shared ownership to NetGraphMsgHandler so
that both can use the same NetworkGraph.

2 years agoMerge pull request #1141 from TheBlueMatt/2021-10-payment-id-on-partial-failure
Matt Corallo [Sun, 31 Oct 2021 18:33:16 +0000 (18:33 +0000)]
Merge pull request #1141 from TheBlueMatt/2021-10-payment-id-on-partial-failure

Rewrite InvoicePayer retry to correctly handle MPP partial failures

2 years agoCheck for invoice expiry in InvoicePayer before we send any HTLCs 2021-10-payment-id-on-partial-failure
Matt Corallo [Thu, 28 Oct 2021 18:46:02 +0000 (18:46 +0000)]
Check for invoice expiry in InvoicePayer before we send any HTLCs

2 years agoAdd integration test for InvoicePayerretry on an immediate failure
Matt Corallo [Wed, 27 Oct 2021 22:22:48 +0000 (22:22 +0000)]
Add integration test for InvoicePayerretry on an immediate failure

2 years agoAdd an integration test for InvoicePayer paying when one part fails
Matt Corallo [Wed, 27 Oct 2021 22:15:11 +0000 (22:15 +0000)]
Add an integration test for InvoicePayer paying when one part fails

This tests the multi-part-single-failure-immediately fixes in the
previous commit.

2 years agoRewrite InvoicePayer retry to correctly handle MPP partial failures
Matt Corallo [Tue, 26 Oct 2021 22:52:06 +0000 (22:52 +0000)]
Rewrite InvoicePayer retry to correctly handle MPP partial failures

This rewrites a good chunk of the retry logic in `InvoicePayer` to
address two issues:
 * it was not considering the return value of `send_payment` (and
   `retry_payment`) may indicate a failure on some paths but not
   others,
 * it was not considering that more failures may still come later
   when removing elements from the retry count map. This could
   result in us seeing an MPP-partial-failure, failing to retry,
   removing the retries count entry, and then retrying other parts,
   potentially forever.

2 years agoDont unwrap `RouteParameter::expiry_time` as users can set it
Matt Corallo [Wed, 27 Oct 2021 22:12:07 +0000 (22:12 +0000)]
Dont unwrap `RouteParameter::expiry_time` as users can set it

Users can provide anything they want as `RouteParameters` so we
shouldn't assume any fields are set any particular way, including
`expiry_time` set at all.

2 years agoExpand `InvoicePayer` documentation somewhat to clarify edge-cases
Matt Corallo [Sat, 30 Oct 2021 01:52:43 +0000 (01:52 +0000)]
Expand `InvoicePayer` documentation somewhat to clarify edge-cases

2 years agoProvide payment retry data when an MPP payment failed partially
Matt Corallo [Tue, 26 Oct 2021 21:39:31 +0000 (21:39 +0000)]
Provide payment retry data when an MPP payment failed partially

This will allow `InvoicePayer` to properly retry payments that only
partially failed to send.

2 years agoMove PaymentId to a [u8; 32] in bindings as for other hash objects
Matt Corallo [Tue, 26 Oct 2021 21:38:46 +0000 (21:38 +0000)]
Move PaymentId to a [u8; 32] in bindings as for other hash objects

This should allow us to fix
https://github.com/lightningdevkit/ldk-garbagecollected/issues/52

2 years agoMerge pull request #1144 from jkczyz/2021-10-invoice-payer-scoring
Matt Corallo [Fri, 29 Oct 2021 20:16:36 +0000 (20:16 +0000)]
Merge pull request #1144 from jkczyz/2021-10-invoice-payer-scoring

Penalize failed channels

2 years agoTest InvoicePayer in BackgroundProcessor
Jeffrey Czyz [Tue, 24 Aug 2021 04:56:59 +0000 (23:56 -0500)]
Test InvoicePayer in BackgroundProcessor

Proof of concept showing InvoicePayer can be used with an
Arc<ChannelManager> passed to BackgroundProcessor. Likely do not need to
merge this commit.

2 years agoPenalize failed channels in Scorer
Jeffrey Czyz [Wed, 27 Oct 2021 15:39:22 +0000 (10:39 -0500)]
Penalize failed channels in Scorer

As payments fail, the channel responsible for the failure may be
penalized. Implement Scorer::payment_path_failed to penalize the failed
channel using a configured penalty. As time passes, the penalty is
reduced using exponential decay, though penalties will accumulate if the
channel continues to fail. The decay interval is also configurable.

2 years agoNotify scorer of failing payment path and channel
Jeffrey Czyz [Thu, 14 Oct 2021 18:04:39 +0000 (13:04 -0500)]
Notify scorer of failing payment path and channel

Upon receiving a PaymentPathFailed event, the failing payment may be
retried on a different path. To avoid using the channel responsible for
the failure, a scorer should be notified of the failure before being
used to find a new route.

Add a payment_path_failed method to routing::Score and call it in
InvoicePayer's event handler. Introduce a LockableScore parameterization
to InvoicePayer so the scorer is locked only once before calling
find_route.

2 years agoMerge pull request #1137 from TheBlueMatt/2021-10-ping-fixes
Matt Corallo [Thu, 28 Oct 2021 20:57:21 +0000 (20:57 +0000)]
Merge pull request #1137 from TheBlueMatt/2021-10-ping-fixes

Give peers which are sending us messages/receiving messages from us longer to respond to ping

2 years agoLog peer public key more thoroughly when logging in peer_handler 2021-10-ping-fixes
Matt Corallo [Wed, 27 Oct 2021 20:06:13 +0000 (20:06 +0000)]
Log peer public key more thoroughly when logging in peer_handler

2 years agoGive peers one timer tick to finish handshake before disconnecting
Matt Corallo [Thu, 21 Oct 2021 22:33:42 +0000 (22:33 +0000)]
Give peers one timer tick to finish handshake before disconnecting

This ensures we don't let a hung connection stick around forever if
the peer never completes the initial handshake.

This also resolves a race where, on receiving a second connection
from a peer, we may reset their_node_id to None to prevent sending
messages even though the `channel_encryptor`
`is_ready_for_encryption()`. Sending pings only checks the
`channel_encryptor` status, not `their_node_id` resulting in an
`unwrap` on `None` in `enqueue_message`.

2 years agoGive peers which are sending us messages longer to respond to ping
Matt Corallo [Mon, 11 Oct 2021 04:24:08 +0000 (04:24 +0000)]
Give peers which are sending us messages longer to respond to ping

See comment for rationale.

2 years agoUtil-ify enqueueing an encoded message in peer_handler
Matt Corallo [Thu, 28 Oct 2021 17:43:58 +0000 (17:43 +0000)]
Util-ify enqueueing an encoded message in peer_handler

This marginally simplifies coming commits.

2 years agoConstify the ratio in buf limits between forward and init sync msgs
Matt Corallo [Mon, 25 Oct 2021 17:31:34 +0000 (17:31 +0000)]
Constify the ratio in buf limits between forward and init sync msgs

2 years agoMerge pull request #1142 from TheBlueMatt/2021-10-on-sent-fees
Matt Corallo [Wed, 27 Oct 2021 22:13:36 +0000 (22:13 +0000)]
Merge pull request #1142 from TheBlueMatt/2021-10-on-sent-fees

Track the amount spent on fees as payments are retried

2 years agoTrack the amount spent on fees as payments are retried 2021-10-on-sent-fees
Matt Corallo [Wed, 27 Oct 2021 19:04:22 +0000 (19:04 +0000)]
Track the amount spent on fees as payments are retried

Especially once we merge the `InvoicePayer` logic soon, we'll want
to expose the total fee paid in the `PaymentSent` event.

2 years agoPass the failing/succeeding `Path` to PendingOutboundPayment meths
Matt Corallo [Wed, 27 Oct 2021 18:57:16 +0000 (18:57 +0000)]
Pass the failing/succeeding `Path` to PendingOutboundPayment meths

This will make the next commit much simpler

2 years agoAdd a utility trait in `router` to get the fees along a given path
Matt Corallo [Wed, 27 Oct 2021 18:24:44 +0000 (18:24 +0000)]
Add a utility trait in `router` to get the fees along a given path

2 years agoMerge pull request #1059 from jkczyz/2021-08-payment-retry
Matt Corallo [Wed, 27 Oct 2021 16:58:19 +0000 (16:58 +0000)]
Merge pull request #1059 from jkczyz/2021-08-payment-retry

Payment Retries

2 years agoImplement Payer and Router for lightning crate
Jeffrey Czyz [Tue, 24 Aug 2021 05:14:10 +0000 (00:14 -0500)]
Implement Payer and Router for lightning crate

Implements Payer for ChannelManager and Rotuer for find_route, which can
be used to parameterize InvoicePayer when needing payment retries.

2 years agoFail payment retry if Invoice is expired
Jeffrey Czyz [Thu, 30 Sep 2021 18:30:24 +0000 (11:30 -0700)]
Fail payment retry if Invoice is expired

According to BOLT 11:

- after the `timestamp` plus `expiry` has passed
  - SHOULD NOT attempt a payment

Add a convenience method for checking if an Invoice has expired, and use
it to short-circuit payment retries.

2 years agoSupport paying zero-value invoices
Jeffrey Czyz [Mon, 4 Oct 2021 16:39:59 +0000 (11:39 -0500)]
Support paying zero-value invoices

2 years agoAdd InvoicePayer for retrying failed payments
Jeffrey Czyz [Tue, 24 Aug 2021 05:08:15 +0000 (00:08 -0500)]
Add InvoicePayer for retrying failed payments

When a payment fails, it's useful to retry the payment once the network
graph and channel scores are updated. InvoicePayer is a utility for
making payments which will retry any failed payment paths for a payment
up to a configured number of total attempts. It is parameterized by a
Payer and Router for ease of customization and testing.

Implement EventHandler for InvoicePayer as a decorator that intercepts
PaymentPathFailed events and retries that payment using the parameters
from the event. It delegates to the decorated EventHandler after retries
have been exhausted and for other events.

2 years agoUnify route finding methods
Jeffrey Czyz [Mon, 25 Oct 2021 23:48:52 +0000 (18:48 -0500)]
Unify route finding methods

An upcoming Router interface will be used for finding a Route both when
initially sending a payment and also when retrying failed payment paths.
Unify the three varieties of get_route so the interface can consist of a
single method implemented by the new `find_route` method. Give get_route
pub(crate) visibility so it can still be used in tests.

2 years agoRewrite Invoice's interface in terms of msats
Jeffrey Czyz [Mon, 4 Oct 2021 14:20:49 +0000 (09:20 -0500)]
Rewrite Invoice's interface in terms of msats

InvoiceBuilder's interface was changed recently to work in terms of
msats. Update Invoice's interface to return the amount in msats, too,
and make amount_pico_btc private.

2 years agoAdd PaymentId to PaymentPathFailed event
Jeffrey Czyz [Thu, 30 Sep 2021 22:29:44 +0000 (15:29 -0700)]
Add PaymentId to PaymentPathFailed event

The PaymentId is needed when retrying payments. Include it in the
PaymentPathFailed event so it can be used in that manner.

2 years agoAdd PaymentId to PaymentSent event
Jeffrey Czyz [Sun, 17 Oct 2021 22:21:01 +0000 (17:21 -0500)]
Add PaymentId to PaymentSent event

The payment_hash may not uniquely identify the payment if it has been
reused. Include the payment_id in PaymentSent events so it can
correlated with the send_payment call.

2 years agoExpose log_bytes! macro for use in other crates
Jeffrey Czyz [Tue, 24 Aug 2021 04:55:28 +0000 (23:55 -0500)]
Expose log_bytes! macro for use in other crates

Needed to log PaymentHash in the lightning-invoice crate when retrying
payments.

2 years agoAdd `PeerManager::disconnect_all_peers` to avoid complexity in BP
Matt Corallo [Tue, 26 Oct 2021 02:03:02 +0000 (02:03 +0000)]
Add `PeerManager::disconnect_all_peers` to avoid complexity in BP

In the coming commits simply calling `timer_tick_occurred` will no
longer disconnect all peers, so its helpful to have a utility
method.

2 years agoCorrect send-bounding logic in `TestRoutingMessageHandler`
Matt Corallo [Mon, 25 Oct 2021 21:51:08 +0000 (21:51 +0000)]
Correct send-bounding logic in `TestRoutingMessageHandler`

The `cmp::min` appeared to confused `end` for a count.

2 years agoMerge pull request #1138 from TheBlueMatt/2021-10-payee-in-monitors
Matt Corallo [Mon, 25 Oct 2021 20:32:27 +0000 (20:32 +0000)]
Merge pull request #1138 from TheBlueMatt/2021-10-payee-in-monitors

Store `Payee` info with HTLCs

2 years agoStore `Payee` information in `HTLCSource::OutboundRoute`. 2021-10-payee-in-monitors
Matt Corallo [Mon, 25 Oct 2021 04:46:26 +0000 (04:46 +0000)]
Store `Payee` information in `HTLCSource::OutboundRoute`.

This stores and tracks HTLC payee information with HTLCSource info,
allowing us to provide it back to the user if the HTLC fails and
ensuring persistence by keeping it with the HTLC itself as it
passes between Channel and ChannelMonitor.

2 years agoCopy `Payee` into `Route`s to provide them to `ChannelManager`
Matt Corallo [Mon, 25 Oct 2021 04:42:29 +0000 (04:42 +0000)]
Copy `Payee` into `Route`s to provide them to `ChannelManager`

2 years agoMake `Payee::pubkey` pub.
Matt Corallo [Mon, 25 Oct 2021 17:52:30 +0000 (17:52 +0000)]
Make `Payee::pubkey` pub.

`Payee` is expected to be used by users to get routes for payment
retries, potentially with their own router. Thus, its helpful if it
is pub, even if it is redundant with the last hop in the `path`
field in `Events::PaymentPathFailed`.

2 years agoMerge pull request #1134 from jkczyz/2021-10-payee-arg
Matt Corallo [Mon, 25 Oct 2021 16:09:06 +0000 (16:09 +0000)]
Merge pull request #1134 from jkczyz/2021-10-payee-arg

Payee abstraction for use in get_route and PaymentPathFailed

2 years agoUse option TLV decoding for short_channel_id
Jeffrey Czyz [Fri, 22 Oct 2021 05:27:58 +0000 (00:27 -0500)]
Use option TLV decoding for short_channel_id

Using ignorable TLV decoding is only applicable for an Option containing
an enum, but short_channel_id is an Option<u64>. Use option TLV encoding
instead.

2 years agoInclude PaymentPathRetry data in PaymentPathFailed
Jeffrey Czyz [Thu, 21 Oct 2021 22:52:53 +0000 (17:52 -0500)]
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.

2 years agoRemove outdated line from get_route docs
Jeffrey Czyz [Wed, 20 Oct 2021 14:22:38 +0000 (09:22 -0500)]
Remove outdated line from get_route docs

2 years agoDefine Payee abstraction for use in get_route
Jeffrey Czyz [Wed, 20 Oct 2021 14:15:31 +0000 (09:15 -0500)]
Define Payee abstraction for use in get_route

A payee can be identified by a pubkey and optionally have an associated
set of invoice features and route hints. Use this in get_route instead
of three separate parameters. This may be included in PaymentPathFailed
later to use when finding a new route.

2 years agoSupport send/recv'ing the new channel_type field in open_channel 2021-09-chan-types
Matt Corallo [Fri, 17 Sep 2021 17:32:36 +0000 (17:32 +0000)]
Support send/recv'ing the new channel_type field in open_channel

This implements the channel type negotiation, though as we currently
only support channels with only static_remotekey set, it doesn't
implement the negotiation explicitly.

2 years agoSupport de/ser of the new channel_type field in open_channel
Matt Corallo [Fri, 17 Sep 2021 17:32:11 +0000 (17:32 +0000)]
Support de/ser of the new channel_type field in open_channel

2 years agoAdd a ChannelTypeFeatures features object for the new channel_type
Matt Corallo [Fri, 17 Sep 2021 16:59:09 +0000 (16:59 +0000)]
Add a ChannelTypeFeatures features object for the new channel_type

Its semantics are somewhat different from existing features,
however not enough to merit a different struct entirely.
Specifically, it only supports required features (if you send a
channel_type, the counterparty has to accept it wholesale or try
again, it cannot select only a subset of the flags) and it is
serialized differently (only appearing in TLVs).

2 years agoFix `cargo doc` on older rustc
Matt Corallo [Fri, 17 Sep 2021 16:00:24 +0000 (16:00 +0000)]
Fix `cargo doc` on older rustc

Apparently at least rustc 1.48 doesn't support `Self` in doc links,
so we make it explicit.

2 years agoMerge pull request #1104 from TheBlueMatt/2021-10-payment-id-in-monitors
Matt Corallo [Fri, 22 Oct 2021 19:33:33 +0000 (19:33 +0000)]
Merge pull request #1104 from TheBlueMatt/2021-10-payment-id-in-monitors

Reload pending outbound payments from ChannelMonitors on startup

2 years agoAdd a test of an HTLC being fulfilled and then later failed 2021-10-payment-id-in-monitors
Matt Corallo [Wed, 20 Oct 2021 23:40:09 +0000 (23:40 +0000)]
Add a test of an HTLC being fulfilled and then later failed

Peers probably shouldn't do this, but if they want to give us free
money, we should take it and not generate any spurious events.

2 years agoMove test_dup_htlc_onchain_fails_on_reload to payment_tests
Matt Corallo [Sun, 10 Oct 2021 23:42:03 +0000 (23:42 +0000)]
Move test_dup_htlc_onchain_fails_on_reload to payment_tests

test_dup_htlc_onchain_fails_on_reload is now more of a
payment_test than a functional_test, testing for handling of
pending payments.

2 years agoAdd some basic test coverage of monitor payment data reloading
Matt Corallo [Sun, 10 Oct 2021 23:36:57 +0000 (23:36 +0000)]
Add some basic test coverage of monitor payment data reloading