278 Commits
Author SHA1 Message Date
Joost Jager 0042a1ffeb invoices: fix htlc timer deadlock 2020-02-04 15:22:08 +01:00
Joost Jager 51324ac7ae invoices: move log into update context 2020-02-04 15:22:06 +01:00
Joost Jager a339065fdc invoices: add hash to update context 2020-02-04 15:22:04 +01:00
Andras Banki-Horvath e6561ca86d invoices: fix log messages
This commit fixes incorrect log messages.
2020-02-03 15:12:55 +01:00
Andras Banki-Horvath fabcdf754a invoices: fix slow startup with many expired invoices
This commit intends to fix slow first startup time when there are many
invoices that need to be canceled. The slowdown is caused by a combination
of adding invoices to the expiry watcher one-by-one and slow
cancellation. Due to slow cancellation and the unbuffered channel which
we use to pass invoices to the expiry watcher blocks the registry.
With this fix we'll instead batch add invoices to the expiry watcher and
thereby won't block the registry startup.
2020-01-24 21:01:10 +01:00
Joost Jager b573a5e61a Merge pull request #3797 from joostjager/explicit-now-dependency-channeldb
channeldb: inject clock into database
2020-01-23 14:27:02 +01:00
Joost Jager c7208ffb2c invoices: add missing String() cases 2020-01-21 16:52:13 +01:00
Joost Jager b600ecda86 channeldb: inject clock into database
Use our standard clock mock for database time queries.
2020-01-20 11:08:30 +01:00
Conner Fromknecht 51dbdd3b38 multi: rename key_send, key-send and key send to keysend 2020-01-16 18:37:16 -08:00
Joost Jager 59a7a9d308 invoices: pre-check key send expiry 2020-01-10 21:35:14 +01:00
Joost Jager 1d5844c196 invoices: jit insert key send invoices
This commit adds handling code for the key send custom record. If this
record is present and its hash matches the payment hash, invoice
registry will insert a new invoice into the database "just in time". The
subsequent settle flow is unchanged. The newly inserted invoice is
picked up and settled. Notifications will be broadcast as usual.
2019-12-23 21:51:19 +01:00
Joost Jager 202b7c26a7 invoices: move lock acquisition down
Move in separate commit to make next commit more readable.
2019-12-23 21:51:17 +01:00
Joost Jager c89f0dc7c1 invoices/test: lower test delay
Previously 5 seconds was used, which made the otherwise fast registry
tests relatively slow.
2019-12-23 21:51:10 +01:00
carla d2e395d5f2 multi: replace errInvoiceNotFound with resolution result
This commit moves handling of invoice not found
errors into NotifyExitHopHtlc and exposes a
resolution result to the calling functions. The
intention of this change is to make calling
functions as naive of the invoice registry's
mechanics as possible.

When NotifyExitHopHtlc is called and an invoice
is not found, calling functions can take action
based on the HtlcResolution's InvoiceNotFound
outcome rather than having to add a special error
check on every call to handle the error.
2019-12-20 13:01:15 +02:00
carla 7b5dda0417 invoices: add resolution result to htlcResolution
This commit adds the resolution result obtained
while updating an invoice in the registry to
htlcResolution. The field can be used by calling
functions to determine the outcome of the
update and act appropriately.
2019-12-20 13:00:53 +02:00
carla 273cd84355 invoices+contractcourt: add HtlcResolution constructor
This commit adds a constructor for HtlcResolution creation
to enforce provision of all relevant values when an
event is created. A custom construstor which also takes
a preimage is added for settle events.
2019-12-20 13:00:24 +02:00
carla 6464f0dda0 invoices: rename event to resolution in tests 2019-12-20 12:59:33 +02:00
carla 2c1eb17192 mutli: rename HodlEvent to HtlcResolution
This commit renames HodlEvent to HtlcResolution
to better reflect the fact that the struct is
only used for htlc settles and cancels, and that
it is not specifically used for hodl invoices.
2019-12-20 12:58:07 +02:00
carla db85c51b77 invoices: export and rename update result
This commit exports UpdateResult so that
calling functions can interpret the outcome
of an invoice update. This is useful for
determining the wire failure required
(fail invalid details or mpp_timeout once
implemented) and for notifying specific
htlc failure details. The enum is renamed
to ResolutionResult.
2019-12-20 12:25:07 +02:00
Andras Banki-Horvath 44f13d1d60 invoices: adding InvoiceExpryWatcher to cancel expired invoices
This commit adds InvoiceExpryWatcher which is a separate class that
receives new invoices (and existing ones upon restart) from InvoiceRegistry
and actively watches their expiry. When an invoice is expired
InvoiceExpiryWatcher will call into InvoiceRegistry to cancel the
invoice and by that notify all subscribers about the state change.
2019-12-13 17:03:08 +01:00
Andras Banki-Horvath 7024f36a76 general: adding the Clock interface to aid testing
This commit adds Clock and DefaultClock and moves the private
invoices.testClock under the clock package while adding basic
unit tests for it.
Clock is an interface currently encapsulating Now() and TickAfter().
It can be added as an external dependency to any class. This way
tests can stub out time.Now() or time.After().

The DefaultClock class simply returns the real time.Now() and
time.After().
2019-12-13 16:52:22 +01:00
Andras Banki-Horvath 88e01fa1fa invoices: refactor test helpers
This commit moves test helpers to their on file, while also adding helper to
create an invoice with a valid payment request.
2019-12-13 16:52:18 +01:00
Joost Jager 8b5bb0ac63 record: move CustomRecordSet 2019-12-12 00:12:19 +01:00
Joost Jager b2f43858c3 invoices: accept mpp payments 2019-12-11 16:14:49 +01:00
Joost Jager 56958493fe invoices/test: add test clock 2019-12-11 16:08:09 +01:00
Joost Jager 499f2b16cf invoices: add RegistryConfig struct 2019-12-11 16:08:07 +01:00
Joost Jager 5f4bd136cd invoices: store custom records in invoice database 2019-12-10 06:54:24 +01:00
Joost Jager 970187ace4 invoices: remove unnecessary invoice value check 2019-12-04 14:51:48 +01:00
Joost Jager a33474ca0e invoices: update NotifyExitHop method comment 2019-12-04 14:51:46 +01:00
Joost Jager 00d93ed87b channeldb: stricter validation of invoice updates 2019-12-04 14:51:42 +01:00
Joost Jager a4a3c41924 channeldb: split cancel and add htlc updates
Previously the cancel and add actions were combined in a single map.
Nil values implictly signaled cancel actions. This wasn't very obvious.
Furthermore this split prepares for processing the adds and cancels
separately, which is more efficient if there are already two maps.
2019-12-04 14:51:40 +01:00
Joost Jager c45891ecf7 invoices: move update logic into separate file 2019-12-04 14:51:38 +01:00
Joost Jager 915867e90f invoiceregistry: promote update closure to method
This commit moves the update code into its own function as a preparation
for extending the logic further for mpp.

In order to make this change cleanly, structured result codes are
introduced. This also prepares for a future htlc notifier rpc hook that
reports htlc settle decisions to external applications.

Furthermore the awkward use of errNoUpdate as a way to signal no update
is removed.
2019-12-04 14:51:36 +01:00
Joost Jager fa010de548 invoices/test: add test context
This commit adds a test context for invoice registry and additionally
passed in a payload object to NotifyExitHopHtlc. This makes the test
match the reality better where a payload is always provided.
2019-12-04 14:51:32 +01:00
Conner Fromknecht 3b253e05f6 multi: restructure invoice Terms field
This commit restructures an invoice's ContractTerms to better encompass
the restrictions placed on settling. For instance, the final ctlv delta
and invoice expiry are moved from the main invoice body (where
additional metadata is stored). Additionally, it moves the State field
outside of the terms since it is rather metadata about the invoice
instead of any terms offered to the sender in the payment request.
2019-11-22 02:25:02 -08:00
Conner Fromknecht 4c872c438b channeldb: complete migration 12 for TLV invoices 2019-11-22 02:24:28 -08:00
Conner Fromknecht 9e9b912d3a multi: replace NotifyExitHopHtlc eob param with hop.Payload 2019-11-04 15:10:32 -08:00
Joost Jager b58dbb2d70 multi: fix canceled spelling 2019-10-03 17:27:36 +02:00
Joost Jager d3e206ef95 invoices: return accept height in hodl event
This is a preparation for passing back the accept height in the
incorrect payment details failure message to the sender.
2019-09-16 10:10:16 +02:00
Joost Jager 4e140213f9 htlcswitch+invoices: circuit key based hodl notifications
This commit modifies hodl htlc notification from invoice registry from a
single notification per hash to distinct notifications per htlc. This
prepares for htlc-specific information (accept height) to be added to the
notification.
2019-09-16 10:10:14 +02:00
Joost Jager 49a20a87a2 channeldb+invoices: make htlc cancelation stricter
Previously it was possible to cancel a canceled htlc. This would
subtract the htlc amount from the invoice amount again.
2019-09-16 10:10:13 +02:00
Lars Lehtonen 3b29ecb921 routing: Fix dropped errors in tests.
lnd: Fix dropped errors in tests.

contractcourt: Fix dropped errors in tests.

htlcswitch: Fix dropped errors in tests.

invoices: Fix dropped error in tests.

lnwallet: Fix dropped errors in tests.

macaroons: Fix dropped error in tests.
2019-09-13 06:28:05 +00:00
Joost Jager 3d7de2ad39 multi: remove dead code 2019-09-10 17:21:59 +02:00
Joost Jager d6d9ec6aa5 invoices: replay awareness
Previously the invoice registry wasn't aware of replayed htlcs. This was
dealt with by keeping the invoice accept/settle logic idempotent, so
that a replay wouldn't have an effect.

This mechanism has two limitations:

1. No accurate tracking of the total amount paid to an invoice. The total
amount couldn't just be increased with every htlc received, because it
could be a replay which would lead to counting the htlc amount multiple
times. Therefore the total amount was set to the amount of the first
htlc that was received, even though there may have been multiple htlcs
paying to the invoice.

2. Impossible to check htlc expiry consistently for hodl invoices. When
an htlc is new, its expiry needs to be checked against the invoice cltv
delta. But for a replay, that check must be skipped. The htlc was
accepted in time, the invoice was moved to the accepted state and a
replay some blocks later shouldn't lead to that htlc being cancelled.
Because the invoice registry couldn't recognize replays, it stopped
checking htlc expiry heights when the invoice reached the accepted
state. This prevents hold htlcs from being cancelled after a restart.
But unfortunately this also caused additional htlcs to be accepted on an
already accepted invoice without their expiry being checked.

In this commit, the invoice registry starts to persistently track htlcs
so that replays can be recognized. For replays, an htlc resolution
action is returned early. This fixes both limitations mentioned above.
2019-09-04 19:20:31 +02:00
Joost Jager c8fa51f865 invoices: refactor invoice update callback
This commit refactors the invoice registry accept/settle logic so that
it doesn't rely anymore on a set of error values to indirectly
communicate from the update callback to the main function what action is
required on the htlc.
2019-09-04 19:20:27 +02:00
Joost Jager 144856757d channeldb+invoices: move invoice cancel logic into registry
This commit is a continuation of the centralization of invoice state
transition logic in the invoice registry.
2019-09-04 19:20:25 +02:00
Joost Jager 416bc8c68c channeldb+invoices: move hold invoice settle logic into registry
This commit is a continuation of the centralization of invoice state
transition logic in the invoice registry.
2019-09-04 19:20:23 +02:00
Joost Jager ad3522f1a6 channeldb+invoices: move invoice accept or settle logic into registry
As the logic around invoice mutations gets more complex, the friction
caused by having this logic split between invoice registry and channeldb
becomes more apparent. This commit brings a clearer separation of
concerns by centralizing the accept/settle logic in the invoice
registry.

The original AcceptOrSettle method is renamed to UpdateInvoice because
the update to perform is controlled by the callback.
2019-09-04 19:20:21 +02:00
Joost Jager c1345a4117 multi: use separate cltv expiry field from invoice
Now that the Invoice struct contains the decoded final cltv delta value,
the decoding of payment requests can be removed from the invoice
registry.
2019-09-04 19:20:19 +02:00
Joost Jager 05e6b62cb2 cnct+htlcswitch+invoices: report circuit key to invoice registry
Currently the invoice registry cannot tell apart the htlcs that pay to
an invoice. Because htlcs may also be replayed on startup, it isn't
possible to determine the total amount paid to an invoice.

This commit is a first step towards fixing that. It reports the circuit
keys of htlcs to the invoice registry, which forms the basis for
accurate invoice accounting.
2019-09-04 19:20:13 +02:00