Commit Graph

235 Commits

Author SHA1 Message Date
Carla Kirk-Cohen
40f695743a
invoice/test: move payment address optional into test using it
No need for a global when this is only used once.
2023-07-24 10:54:00 -04:00
Carla Kirk-Cohen
ceff879f37
invoices/test: replace global testInvoice with constructor
For our relatively "static" test invoice, add a simple constructor
using the globals defined, rather than having a global invoice which
risks being mutated.
2023-07-24 10:53:58 -04:00
Carla Kirk-Cohen
17f529bfad
invoices/test: rename newInvoice to reflect return value better
Test utils currently have two different test invoices - a minimalist
one that is used in a variety of test cases, and a customizable one
that is used specifically for tests concerning invoice expiry. The
latter is renamed and moved closer to the calling code to more clearly
indicate its use.
2023-07-17 09:39:40 -04:00
Carla Kirk-Cohen
590d898ba0
invoices/test: fix data race in TestMppPaymentWithOverpayment
Details of race:
- A global testInvoice is used in various invoice related tests.
- All tests are running with t.Parallel.
- TestMppPaymentWithOverpayment passes a reference to the testInvoice
  to AddInvoice which writes the AddIndex.
- TestMultipleSetHeightExpiry reads the testInvoice to make a copy.
2023-07-13 10:39:35 -04:00
Keagan McClelland
f2bb4ff559 invoices+htlcswitch: fix style nits 2023-07-07 13:17:01 -06:00
Keagan McClelland
36bf471a1f invoices+htlcswitch: add tests for relaxed link and invoice checks 2023-07-07 10:48:57 -06:00
Keagan McClelland
e0c0815c96 invoices: allow overpayment in mpps 2023-06-29 17:13:36 -06:00
Oliver Gugger
56dba2df03
multi: update linter, fix new issues 2023-06-13 11:58:33 +02:00
positiveblue
6ff6c45a6b
channeldb: split addHTLCs logic in the UpdateInvoice method 2023-03-14 19:26:18 -07:00
positiveblue
27fbc2f60b
channeldb: split cancelInvoice logic in the UpdateInvoice method 2023-03-14 19:26:15 -07:00
positiveblue
cbaec4382a
channeldb: split settleHodlInvoice logic in the UpdateInvoice method 2023-03-14 19:01:52 -07:00
positiveblue
a3f6d5c97e
channeldb: split cancelHTLCs logic in the UpdateInvoice method
Previous to this commit we were able to call `UpdateInvoice` with an
updated that added and cancelled htlcs at the same time. The function
returned an error if there was overlapping between the two htlc set.
However, that behavior was not used in the LND code itself.

Eventually we want to split this method in multiple ones, among them one
for canceling htlcs and another one for adding them. For that reason,
this behavior is not supported anymore.
2023-03-14 18:56:01 -07:00
positiveblue
84387992ec
invoices: add UpdateType to InvoiceUpdateDesc
Make the kind of update explicit in the `InvoiceUpdateDesc` struct.
2023-03-13 13:11:06 -07:00
yyforyongyu
4a7d3bc9c2
invoices: fix flake in TestInvoiceExpiryWithRegistry 2023-02-10 20:52:36 +08:00
positiveblue
8563e1f409
channeldb/invoices: add IsAMP and IsKeysend helpers
The only way to know if an invoice is AMP, Keysend, etc is to look at
its shape/characteristics. This commit adds a couple of helper functions
to encapsulate the logic of these checks.

If all these types cannot intersect: an invoice cannot be AMP and
Keysend or Keysend and Bolt12, etc it could be useful to add an extra
field to store this information instead of relying on checking how the
invoice looks like.
2023-01-20 03:43:39 -08:00
Oliver Gugger
d960fcd68a
multi: remove gomnd disable directives 2023-01-17 19:43:26 +01:00
positiveblue
5ff5225245
multi: break invoice depenency on channeldb
Now that we have the new package `lnd/channeldb/models` we can invert the
depenency between `channeldb` and `invoices`.

- Move all the invoice related types and errors to the
`invoices` package.

- Ensure that all the packages dealing with invoices use the types and
  interfaces defined in the `invoices` package.

- Implement the InvoiceDB interface (defined in `lnd/invoices`) in
  channeldb.

- Add new mock for InterfaceDB.

- `InvoiceRegistery` tests are now in its own subpacakge (they need to
  import both invoices & channeldb). This is temporary until we can
  decouple them.
2023-01-16 07:31:09 -08:00
positiveblue
383cb40f8d
multi: create channeldb/models package
Add a new subpackage to `lnd/channeldb` to hold some of the types that
are used in the package itself and in other packages that should not
depend on `channeldb`.
2023-01-16 07:14:55 -08:00
Eng Zer Jun
945180f0ea
invoices: replace defer cleanup with t.Cleanup
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-10-13 17:47:03 +08:00
Eng Zer Jun
6c4ce69b26
invoices: use T.TempDir to create temporary test directory
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
2022-08-24 09:03:04 +08:00
ErikEk
6146b8d644 trivial typo[skip ci] 2022-07-01 11:21:46 +02:00
Tommy Volk
9a10c80bcb multi: move many t.Fatalf calls to require.NoError 2022-06-17 04:26:55 +00:00
Andras Banki-Horvath
98f39deb38
invoices: fix deadlock in hodl subscription handling 2022-05-31 17:36:43 +02:00
Andras Banki-Horvath
cc8a1f0d2b
invoices: fix deadlock in notification handling 2022-05-31 17:36:40 +02:00
Olaoluwa Osuntokun
2c4136da1b
Merge pull request #6525 from Crypt-iQ/amp_preimage_fix
invoices: properly set Preimage field for success resolution
2022-05-13 15:15:03 -07:00
Olaoluwa Osuntokun
4949243d67
Merge pull request #6477 from Crypt-iQ/testmultihopflake
invoices: add client to map before delivering backlog notif
2022-05-11 16:45:36 -07:00
eugene
4eea395a7f
channeldb+invoices: refactor invoice logic when updating 2022-05-11 13:57:46 -04:00
eugene
d099991339
invoices: remove unused invoiceSubscriptionKit wait group 2022-05-11 13:54:45 -04:00
eugene
5ddad90a17
invoices: fix OOO notifications, use cancelChan instead of canceled
This commit fixes OOO notifications between backlog and non-backlog
events by having the non-backlog goroutines wait on a chan that
signals that backlog processing is complete.

This commit also replaces usage of the canceled atomic variable with
the cancelChan to signal that delivery of an event should no longer
occur. Atomics do not make perfect "sequence" points as the atomic
may be checked too early and the end-result of delivering to a stopped
ntfnQueue is the same. Using the cancelChan ensures that we do not
hang on sending to ntfnQueue.
2022-05-11 13:54:45 -04:00
eugene
93f87acb59
invoices: add client to map before delivering backlog notif
Prior to this change, if SubscribeSingleInvoice or
SubscribeNotifications was called, it was possible that a state
change would never be delivered to the client. The sequence of
events would be:
- delivery of backlog events with invoice in the Open state
- invoice goes to the Accepted state, no client to notify
- client added to map

This is fixed by adding the client to the map first. However, with
this change alone it then becomes possible for notifications to be
delivered out of order. This is addressed in a following commit.
2022-05-11 13:54:43 -04:00
eugene
d76278bc1e
invoices: properly set Preimage for settle resolutions
This fixes a nil-pointer-dereference that would occur if this was
called for a settled AMP invoice. Terms.PaymentPreimage is always
false for AMP invoices.
2022-05-11 13:51:12 -04:00
eugene
6dcec862aa
invoices: remove redundant code
HTLCSet already removes any HTLCs that are not in the desired state,
so doing it again on the same set is redundant.
2022-05-11 13:51:10 -04:00
Jesse van Papenrecht
eff52ef3b7
fix typo for scanInvoicesOnStart [skip ci] 2022-05-02 10:49:59 +02:00
Joost Jager
62ae0387ff
htlcswitch+invoices: log payment metadata 2022-04-13 22:55:42 +02:00
Andras Banki-Horvath
c548a70e0d
invoices: fix deadlock in invoice registry 2022-03-14 15:15:58 +01:00
Oliver Gugger
7dfe4018ce
multi: use btcd's btcec/v2 and btcutil modules
This commit was previously split into the following parts to ease
review:
 - 2d746f68: replace imports
 - 4008f0fd: use ecdsa.Signature
 - 849e33d1: remove btcec.S256()
 - b8f6ebbd: use v2 library correctly
 - fa80bca9: bump go modules
2022-03-09 19:02:37 +01:00
yyforyongyu
1ad6bbfbc2
multi: add logs when subservers are starting
Also unified the log messages.
2022-02-11 21:17:03 +08:00
Oliver Gugger
895a2e497b
multi: formatting and comment fixes 2022-02-10 11:02:02 +01:00
Dimitris Apostolou
530a2059e5
multi: Fix typos [skip ci] 2022-01-24 12:19:02 +02:00
yyforyongyu
df2ecd6bc5
invoices: refactor invoice subscriptions to avoid blocking
This commit fixes a potential blocking when notifying invoice updates.
When a new subscription client is created followed by an immediate
cancel, it's likely the client will be removed from the registry's
map(noop) and then added to its map again. This subscription will then
be kept in registry until lnd is restarted. Another more serious issue
is when multiple subscriptions are made for the same invoice, when the
above case happens, other subscriptions may never send invoice updates
because a previous client has a stopped notification queue that blocks
following notifications.
2021-12-15 02:38:51 +08:00
yyforyongyu
3230cc4822
invoices: remove redundant mutex lock 2021-12-15 02:26:32 +08:00
yyforyongyu
f4f446519b
invoices: remove potential cyclic pointer references
This commit removes the pointer reference to InvoiceRegistry when
creating an InvoiceSubscriptionKit and mounts the channels needed for
communication.
2021-12-15 02:26:32 +08:00
Andras Banki-Horvath
76065e84d3
invoices: do not log when invoice is already deleted 2021-11-01 16:10:45 +01:00
Olaoluwa Osuntokun
31336f91b0
invoices: update testSpontaneousAmpPayment for new AMP logic
Rather than checking the AMP invoice state itself, we'll instead check
the HTLC state.
2021-10-28 15:50:32 -07:00
Olaoluwa Osuntokun
3935725329
invoices: update cancel logic for AMP invoices to be more efficient
By passing in the set ID here, we ensure that we don't need to read ALL
the invoices to update the state of a set of them.
2021-10-28 15:50:30 -07:00
Olaoluwa Osuntokun
01f408ca38
invoices: recognize AMP invoice settles during ntnf dispatch+catchup
In this commit, we add the setID to the invoiceEvent struct as it will
be useful when we need to be able to distinguish a new open invoice,
from an AMP invoice that's being settled for the first time.

we then update the logic during notification dispatch to utilize the new
field to allow it to detect the repeated settles of AMP invoices.
2021-10-28 15:50:27 -07:00
Olaoluwa Osuntokun
a4f8842831
channeldb: optimize updateInvoice for AMP by only reading relevant HTLCs
In this commit, we update the logic in `updateInvoice` to allow callers
to pass in either a hint, or the setID in the update callback. This
makes things more efficient for AMP invoices with thousands of recurring
payments, as we no longer need to read out _all_ the invoices each time
we go to update the state of a few HTLCs.
2021-10-28 15:50:15 -07:00
Olaoluwa Osuntokun
8299d632e8
lnrpc/invoicesrpc: add new invoice ref LookupInvoiceV2 method
In this commit, we add a new `LookupInvoiceV2` method attached to the
invoice RPC sub-server. Compared to the existing version, this version
allows an invoice to be looked up by an invoice ref. This enables users
to query an AMP invoice based on a specific _set ID_, so they can get
the information related to the set of cumulative settles to that
invoices.
2021-10-28 15:49:25 -07:00
Oliver Gugger
8b7c88537c
multi: refactor SignDigestCompact into SignMessageCompact
To make it possible to use a remote lnrpc server as a signer for our
wallet, we need to change our main interface to sign the message instead
of the message's digest. Otherwise we'd need to alter the
lnrpc.SignMessage RPC to accept a digest instead of only the message
which has security implications.
2021-10-08 12:06:52 +02:00
yyforyongyu
3204e2d74b
multi: add shutdown logs in subservers
This commit adds a simple shutdown to every subserver to assist
debugging.
2021-09-15 19:52:03 +08:00
Olaoluwa Osuntokun
47403c0be4
Merge pull request #5226 from yyforyongyu/small-fix-invoice
invoices: remove unused param in notifyClients
2021-05-19 16:36:06 -07:00
Olaoluwa Osuntokun
86c5e483b2
Merge pull request #5206 from carlaKC/4987-heightexpirywatcher
invoices: add height based expiry watcher
2021-05-12 13:41:52 -07:00
carla
74373f26b9
invoices/test: add height expiry watcher tests with registry 2021-05-11 08:47:17 +02:00
carla
85e56dbfb7
invoices/test: add test for hodl invoices 2021-05-11 08:45:30 +02:00
carla
34de5922ed
multi: add height-based invoice expiry
This commit adds height-based invoice expiry for hodl invoices
that have active htlcs. This allows us to cancel our intentionally
held htlcs before channels are force closed. We only add this for
hodl invoices because we expect regular invoices to automatically
be resolved.

We still keep hodl invoices in the time-based expiry queue,
because we want to expire open invoices that reach their timeout
before any htlcs are added. Since htlcs are added after the
invoice is created, we add new htlcs as they arrive in the
invoice registry. In this commit, we allow adding of duplicate
entries for an invoice to be added to the expiry queue as each
htlc arrives to keep implementation simple. Our cancellation
logic can already handle the case where an entry is already
canceled, so this is ok.
2021-05-11 08:45:29 +02:00
Conner Fromknecht
2ecd1de713
config: expose distinct accept-amp flag
This mirrors the accept-keysend flag, but also permits users to
eventually toggle off keysend separately from AMP.
2021-05-10 22:02:15 -07:00
Conner Fromknecht
b0949054e5
invoices: fail HTLCs that target the wrong type of invoice 2021-05-10 22:02:14 -07:00
Conner Fromknecht
8402e346f5
channeldb/invoice: fail extra HTLC sets immeidately 2021-05-10 16:55:18 -07:00
Conner Fromknecht
70efaa5fe2
invoices: use HTLCSet method with state filter 2021-05-10 16:55:17 -07:00
Conner Fromknecht
d93c3298b7
channeldb+invoice: add state filter to HTLCSet 2021-05-10 16:55:17 -07:00
Olaoluwa Osuntokun
41c089fbf4
Merge pull request #5224 from bhandras/invoice-gc-errors
invoices: do not fail DeleteInvoice if payment addr isn't indexed
2021-04-29 15:39:35 -07:00
carla
7536dd8179
invoices: refactor - add method to handle expected cancelation errors 2021-04-23 08:19:57 +02:00
carla
039e9f439c
invoices: refactor - update timestamp expiry to use specific names 2021-04-23 08:19:56 +02:00
carla
d29f2fe4f9
invoices: refactor - move cancel check into separate function
Useful for tests which need to mock cancelInvoiceImpl
2021-04-23 08:19:55 +02:00
carla
4cd48c52ea
invoices: refactor - add interface for expiry items
In preparation for having more than one expiry type, we
alias the queue.PrioirtyQueueItem interface for readability.
2021-04-23 08:19:54 +02:00
carla
9c6e83b15f
invoices: refactor - rename expiry queue to be timestamp specific 2021-04-23 08:19:53 +02:00
carla
f5f1e9e6c7
invoices: refactor - rename invoiceExpiry to invoiceExpiryTs
We're going to add block based expiry, so we clarify now.
2021-04-23 08:19:52 +02:00
Andras Banki-Horvath
ec50f2ccce
invoices: more verbose logging for startup invoice GC 2021-04-22 15:37:53 +02:00
yyforyongyu
72097059cc
invoices: remove unused param in notifyClients 2021-04-20 23:47:19 +08:00
Roei Erez
3223df74e5 channelnotifier+discover+invoices: return error in Stop functions
In order to be consistent with other sub systems an error is now
returned from the Stop functions.
This also allows writing a generic cleanup mechanism to stop all
sub systems in case of a failure.
2021-04-13 13:26:01 +03:00
Conner Fromknecht
ea934e1be9
invoices: add TestSpontaneousAmpPayment
Adds a set of test cases that exercise the spontaneous AMP payment flow
with valid and invalid reconstructions, as well as with single and
multiple HTLCs. This also asserts that spontaneous AMP is gated behind
the existing AcceptKeysend flag.
2021-04-07 12:08:35 -07:00
Conner Fromknecht
90a255078d
invoices: reconstruct AMP child preimages 2021-04-07 12:08:35 -07:00
Conner Fromknecht
88b72ab398
invoices: add processAMP 2021-04-07 12:08:34 -07:00
Conner Fromknecht
3fb70dd936
invoices: add checkSettleResolution and checkFailResolution
Also refactor existing unit tests to use them.
2021-04-07 12:08:34 -07:00
Conner Fromknecht
7e2f5a184b
channeldb: validate feature dependencies when adding invoice 2021-04-07 12:08:34 -07:00
Conner Fromknecht
7bed359296
channeldb: refactor InvoiceRef.String() with all optional fields 2021-04-07 12:08:34 -07:00
Conner Fromknecht
174d577524
channeldb: make payhash on InvoiceRef optional
Currently we support queries by payHash or payHash+payAddr. For handling
of AMP HTLCs, we only need to support querying by payAddr.
2021-04-07 12:08:34 -07:00
Conner Fromknecht
68688d6346
invoices: replace all asserts with requires 2021-03-04 10:11:22 -08:00
Conner Fromknecht
137258fb58
invoices/invoiceregistry: unbundle invalid preimage error 2021-03-03 16:28:03 -08:00
Conner Fromknecht
9da3e7fc90
invoices: filter HTLC set by set id from AMP payload 2021-03-03 16:28:02 -08:00
Conner Fromknecht
b2234703f6
invoices+channeldb: add HTLCSet method to invoice 2021-03-03 16:26:43 -08:00
Conner Fromknecht
464dff09ac
invoices: expose AMPRecord() method on Payload 2021-03-03 16:26:39 -08:00
Olaoluwa Osuntokun
530059f18b
invoices: force MPP payload inclusion for non-keysend payments
In this commit, we move to start rejecting any normal payments that
aren't keysend, if they don't also include the MPP invoice payload. With
this change, we require that some sort of e2e secret (either the payment
addr or the keysend pre-image) is present in a payload before we'll
accept the payment.

The second portion of the commit also updates all current tests in the
package. We kept the base `TestSettleInvoice` test in-tact as it still
exercises some useful behavior. However, we've removed all cases that
allow an overpayment, as the new MPP logic doesn't allow overpayment for
various reasons. In addition to this, some of the returned errors are
slightly different, tho the actual behavior is equivalent.
2020-11-25 16:31:59 -08:00
Andras Banki-Horvath
478cf704be
invoices: reduce heap usage when starting the expiry watcher 2020-10-28 16:26:47 +01:00
Andras Banki-Horvath
2aa680ede2
invoices: optionally garbage collect invoices on the fly
This commit extends invoice garbage collection to also remove invoices
which are canceled when LND is already up and running. When the option
GcCanceledInvoicesOnTheFly is false (default) then invoices are kept and
the behavior is unchanged.
2020-08-05 12:24:17 +02:00
Andras Banki-Horvath
a0d7877d9a
multi: make canceled invoice garbage collection configurable
This commit extends the application config with a flag to control canceled
invoice garbage collection upon startup.
2020-08-05 12:24:16 +02:00
Andras Banki-Horvath
0ea763d83c
invoices: attempt to delete old invoices upon start 2020-08-05 12:24:15 +02:00
Andras Banki-Horvath
92f3b0a30c
channeldb+invoices: add ScanInvoices and integrate with InvoiceRegistry
This commit adds channeldb.ScanInvoices to scan through all invoices in
the database. The new call will also replace the already existing
channeldb.FetchAllInvoicesWithPaymentHash call in preparation to collect
invoices we'd like to delete and watch for expiry in one scan in later
commits.
2020-08-05 12:24:14 +02:00
Andras Banki-Horvath
ba3c65bfd6
invoices: re-format overreaching code lines 2020-08-05 12:24:13 +02:00
Olaoluwa Osuntokun
81f6b4dcfe
invoices: add String() method to failure resolution outcome
In this commit, we add a String() method to the failure resolution
outcome. Without this, logs aren't very useful as the integer version of
the outcome is printed rather than the description.
2020-07-12 17:25:53 -07:00
Joost Jager
24e3234dfa
invoices: optionally hold and auto-cancel keysend payments
Adds a new configuration flag to lnd that will keep keysend payments in
the accepted state. An application can then inspect the payment
parameters and decide whether to settle or cancel.

The on-the-fly inserted keysend invoices get a configurable expiry time.
This is a safeguard in case the application that should decide on the
keysend payments isn't active.
2020-06-27 09:54:11 +02:00
Conner Fromknecht
20dea0d6bc
invoices/invoiceregistry: explicitly set blank pay addr for keysend 2020-06-26 02:17:18 -07:00
Conner Fromknecht
47cf144170
invoices: ensure ntfns are sent in the same order
SettleHodlInvoice and CancelInvoice both notifyClients after
notifyHodlSubscribers. This commit changes UpdateInvoice to follow the
same pattern so that we are consistent.
2020-06-18 16:28:13 -07:00
Joost Jager
d416ed59ea
multi: add explicit hodl invoice flag to invoice
Previously it wasn't possible to store a preimage in the invoice
database and signal that a payment should not be settled right away. The
only way to hold a payment was to insert the magic UnknownPreimage value
in the invoice database. This commit introduces a distinct flag to
signal that an invoice is a hold invoice and thereby allows the preimage
to be present in the database already.

Preparation for (key send) hodl invoices for which we already know the
preimage.
2020-06-02 07:34:23 +02:00
Conner Fromknecht
0f3ab775c7
Merge pull request #4285 from cfromknecht/pay-addr-index
channeldb: index payments by payment addr, use payment hash as fallback
2020-05-27 17:36:30 -07:00
Conner Fromknecht
cbf71b5452
channeldb+invoices: use payment addr as primary index 2020-05-26 18:55:27 -07:00
Conner Fromknecht
3522f09a08
channeldb+invoices: track invoices by InvoiceRef 2020-05-26 18:55:27 -07:00
Conner Fromknecht
2799202fd9
invoices/invoiceregistry: rename updateCtx to ctx 2020-05-26 18:55:27 -07:00
Conner Fromknecht
6a02fa1107
invoices/invoiceregistry: properly synchronize backlog
This commit moves the db calls for retrieving add and settle backlogs
outide of the main event loop. All other db operations are performed
outside of the event loop and synchronized via the invoice registry's
mutex, which also synchronizes the order in which events submitted to be
processed.

This resolves various concurrency issues where notifications can be
missed of inconsistent reads against the databse. This is especially
important in this case because we are actually making two separate
database calls.
2020-05-26 17:50:46 -07:00
Conner Fromknecht
5b747715fc
invoices: return error from SubscribeNotifications on shutdown 2020-05-26 17:50:45 -07:00