Commit Graph

135 Commits

Author SHA1 Message Date
Andras Banki-Horvath
1a13f822c6 invoices: remove dead code from logger 2020-04-24 19:15:08 +02:00
Andras Banki-Horvath
70fc9c1901 invoices: elminitate sleep from expiry watcher tests
This commit removes sleeps from invoice expiry watcher tests in favor of
a timeout guard.
2020-04-09 09:58:22 +02:00
Conner Fromknecht
310e20f8b7
invoices/invoiceregistry: remove debug spew 2020-04-07 11:54:29 -07:00
carla
1ad395ec3f
htlcswitch: add failure details to incoming failures
This commit adds LinkErrors with failure details to htlcs which fail on
our incoming link. This change is made with the intention of notifying
detailed htlc failure reasons in sendHTLCError. The FailureDetail
interface is implemented on FailureResolutionResults so that they can
directly be used to enrich LinkErrors. sendHtlcError is updated to
take a LinkError in preparation for the addition of a htlcnotifier
which will notify the detail of the error.
2020-02-06 19:43:39 +02:00
carla
8cbed23f26
invoices: split resolution result into settle, fail and accept enums
This commit splits the resolution result enum into results divided
by outcome (settled, failed or accepted). This allows us to more
strictly control which resolution results can be used with which
HtlcResolution structs, to prevent the combination of a settle
resolution result with a failure resolution result, for example.
2020-02-06 19:41:39 +02:00
carla
2569b4d08a
multi: replace htlcResolution with an interface
This commit repalces the htlcResolution struct with an interface.
This interface is implemeted by failure, settle and accept resolution
structs. Only settles and fails are exported because the existing
code that handles htlc resolutions uses a nil resolution to indicate
that a htlc was accepted. The accept resolution is used internally
to report on the resolution result of the accepted htlc, but a nil
resolution is surfaced. Further refactoring of all the functions
that call NotifyExitHopHtlc to handle a htlc accept case (rather than
having a nil check) is required.
2020-02-06 19:41:36 +02:00
Olaoluwa Osuntokun
2cd26d7556
Merge pull request #3980 from joostjager/registry-deadlock
invoices: fix htlc timer deadlock
2020-02-04 17:14:17 -08:00
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
Joost Jager
4fe846af6e
invoices/test: extend hodl invoice test
Add logic to specifically exercise the replay behavior of invoice
registry for hodl invoices.
2019-09-04 19:20:09 +02:00
Joost Jager
43bad4af9f
invoices: always check htlc amt with invoice amount
Previously a check was made for accepted and settled invoices against
the paid amount. This opens up a probe vector where an attacker can pay
to an invoice with an amt that is higher than the invoice amount and
find out if the invoice is already paid or not.
2019-09-04 19:20:07 +02:00
Joost Jager
d98a22879f
invoices/test: clarify test parameters 2019-09-04 19:20:04 +02:00
Olaoluwa Osuntokun
23cc8389f2
multi: add TLV awareness to htlcswitch, pass extra EOB to the invoice registry
In this commit, we update the `HopIterator` to gain awareness of the new
TLV hop payload. The default `HopIterator` will now hide the details of
the TLV from the caller, and return the same `ForwardingInfo` struct in
a uniform manner. We also add a new method: `ExtraOnionBlob` to allow
the caller to obtain the raw EOB (the serialized TLV stream) to pass
around.

Within the link, we'll now pass the EOB information into the invoice
registry. This allows the registry to parse out any additional
information from the EOB that it needs to settle the payment, such as a
preimage shard in the AMP case.
2019-08-22 18:53:05 -07:00
Joost Jager
4dd054ae90
multi: remove debug invoices
Debug invoices are rarely used nowadays, but keep asking for maintenance
every time refactoring in primarily the invoice registry occurs. We have
passed the cost/benefit tipping point, so therefore the debug invoice
concept is removed in this commit.

Previously the debughtlc flag also controlled whether hodl masks were
active. It is safe to remove that additional condition because the hodl
masks are still guarded by the dev build tag.
2019-08-14 21:33:03 +02:00
Joost Jager
395e0596c2
invoices: fix synchronization issue with single invoice subscribers
This commit fixes a synchronization issue where a single invoice
subscriber could receive duplicate and/or out of order invoice updates.
2019-08-13 19:02:55 +02:00
Joost Jager
9ab23de197
invoices: create error for invoice registry shutting down 2019-08-13 19:02:53 +02:00
Joost Jager
46e2a9e9b8
invoices: reuse client.notify function 2019-08-13 19:02:51 +02:00
Joost Jager
bed2acea33
invoices: remove redundant state field from invoice event 2019-08-13 19:02:50 +02:00
Joost Jager
98fac9fb99
htlcswitch+invoices: always return incorrect_or_unknown_payment_details
In order to prevent information leaks by nodes probing with a payment
hash, this commit changes exit hop processing so that it always returns
incorrect_or_unknown_payment_details and leaves the prober in the dark
about whether an invoice actually exists.
2019-08-08 11:04:23 +02:00
Joost Jager
9e26e4e8da
invoices: check invoice amount even when already accepted or settled 2019-06-10 16:12:10 +02:00
Joost Jager
18c025151a
invoices+channeldb: move invoice state check to invoiceregistry 2019-06-10 16:12:08 +02:00
Joost Jager
064e8492de
cnct+htlcswitch+invoices: move invoice parameter check out of link
This commit is the final step in making the link unaware of invoices. It
now purely offers the htlc to the invoice registry and follows
instructions from the invoice registry about how and when to respond to
the htlc.

The change also fixes a bug where upon restart, hodl htlcs were
subjected to the invoice minimum cltv delta requirement again. If the
block height has increased in the mean while, the htlc would be canceled
back.

Furthermore the invoice registry interaction is aligned between link and
contract resolvers.
2019-05-15 14:42:12 +02:00
Joost Jager
33a1904dc9
invoices: unify invoice log statements 2019-04-08 13:10:51 +02:00
Joost Jager
32f2b047e8
htlcswitch: hodl invoice
This commit modifies the invoice registry to handle invoices for which
the preimage is not known yet (hodl invoices). In that case, the
resolution channel passed in from links and resolvers is stored until we
either learn the preimage or want to cancel the htlc.
2019-03-15 10:09:17 +01:00
Joost Jager
1f41a2abce
htlcswitch: abtract invoice from link
This commit detaches signaling the invoice registry that an htlc was
locked in from the actually settling of the htlc.

It is a preparation for hodl invoices.
2019-03-15 10:09:15 +01:00
Joost Jager
3b5c2f44c6
invoices: extract invoice decoding from registry
Previously it was difficult to use the invoice registry in unit tests,
because it used zpay32 to decode the invoice. For that to succeed, a
valid signature is required on the payment request.

This commit injects the decode dependency on a different level so that
it is easier to mock.
2019-03-15 10:09:06 +01:00
Joost Jager
19f79613df
channeldb: store hold invoice 2019-03-15 10:08:55 +01:00
Joost Jager
8392f6d28f
lnrpc/invoicesrpc: remove lnrpc type from add invoice 2019-03-15 10:08:52 +01:00
Conner Fromknecht
2d8bc99d9e
lntypes/preimage: add MakePreimage initializer 2019-02-19 17:05:45 -08:00
Joost Jager
1b87fbfab2
invoices+htlcswitch+lnrpc: cancel invoice 2019-02-06 07:29:18 +01:00
Joost Jager
b16357116c
invoices: add SubscribeSingleInvoice test 2019-02-01 09:47:37 +01:00
Joost Jager
acb0162443
invoices: subscribe single invoice 2019-02-01 09:43:03 +01:00
Joost Jager
436dd41c77
channeldb: move idempotency up the call tree
As a preparation for subscribing to single invoices, InvoiceRegistry
needs to become aware of settling a settled invoice.
2019-02-01 09:42:56 +01:00
Joost Jager
78cd07570b
invoiceregistry: extract dispatch to method 2019-02-01 09:42:42 +01:00
Joost Jager
bacd92418a
invoices: use lntypes.Hash and lntypes.Preimage
Previously chainhash.Hash was used, which converts to/from string in
reversed format. Payment hashes and preimages are supposed to be
non-reversed.
2019-02-01 09:42:29 +01:00
Joost Jager
9e012ecc93
multi: move Input interface and related code
This commit is a step to split the lnwallet package. It puts the Input
interface and implementations in a separate package along with all their
dependencies from lnwallet.
2019-01-31 13:25:33 +01:00
Joost Jager
c1eaf60000
invoices: create package
This commit isolates the invoice registry in a separate package. It is
a preparation for the creation of an invoices sub server.
2019-01-07 23:04:05 +01:00