2019-01-14 12:09:46 +03:00
|
|
|
package invoices
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-12-09 19:50:11 +03:00
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2019-01-14 12:09:46 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-11-12 22:28:43 +03:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2019-01-14 12:09:46 +03:00
|
|
|
)
|
|
|
|
|
2019-02-20 14:11:15 +03:00
|
|
|
// TestSettleInvoice tests settling of an invoice and related notifications.
|
|
|
|
func TestSettleInvoice(t *testing.T) {
|
2019-11-12 22:28:43 +03:00
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
2019-01-14 12:09:46 +03:00
|
|
|
|
2019-11-12 22:28:43 +03:00
|
|
|
allSubscriptions := ctx.registry.SubscribeNotifications(0, 0)
|
2019-01-14 12:09:46 +03:00
|
|
|
defer allSubscriptions.Cancel()
|
|
|
|
|
|
|
|
// Subscribe to the not yet existing invoice.
|
2019-11-20 20:06:51 +03:00
|
|
|
subscription, err := ctx.registry.SubscribeSingleInvoice(testInvoicePaymentHash)
|
2019-08-09 13:22:53 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-01-14 12:09:46 +03:00
|
|
|
defer subscription.Cancel()
|
|
|
|
|
2019-11-20 20:06:51 +03:00
|
|
|
if subscription.hash != testInvoicePaymentHash {
|
2019-01-14 12:09:46 +03:00
|
|
|
t.Fatalf("expected subscription for provided hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the invoice.
|
2019-11-20 20:06:51 +03:00
|
|
|
addIdx, err := ctx.registry.AddInvoice(testInvoice, testInvoicePaymentHash)
|
2019-01-14 12:09:46 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if addIdx != 1 {
|
|
|
|
t.Fatalf("expected addIndex to start with 1, but got %v",
|
|
|
|
addIdx)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect the open state to be sent to the single invoice subscriber.
|
|
|
|
select {
|
|
|
|
case update := <-subscription.Updates:
|
2019-11-22 13:25:02 +03:00
|
|
|
if update.State != channeldb.ContractOpen {
|
2019-01-14 12:09:46 +03:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
update.State)
|
2019-01-14 12:09:46 +03:00
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect a new invoice notification to be sent out.
|
|
|
|
select {
|
|
|
|
case newInvoice := <-allSubscriptions.NewInvoices:
|
2019-11-22 13:25:02 +03:00
|
|
|
if newInvoice.State != channeldb.ContractOpen {
|
2019-01-14 12:09:46 +03:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
newInvoice.State)
|
2019-01-14 12:09:46 +03:00
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
2019-02-11 14:01:05 +03:00
|
|
|
hodlChan := make(chan interface{}, 1)
|
|
|
|
|
2019-08-14 22:21:39 +03:00
|
|
|
// Try to settle invoice with an htlc that expires too soon.
|
2019-11-12 22:28:43 +03:00
|
|
|
event, err := ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, testInvoice.Terms.Value,
|
2019-08-14 22:21:39 +03:00
|
|
|
uint32(testCurrentHeight)+testInvoiceCltvDelta-1,
|
2019-11-12 22:28:43 +03:00
|
|
|
testCurrentHeight, getCircuitKey(10), hodlChan, testPayload,
|
2019-08-14 22:21:39 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if event.Preimage != nil {
|
|
|
|
t.Fatal("expected cancel event")
|
|
|
|
}
|
|
|
|
if event.AcceptHeight != testCurrentHeight {
|
|
|
|
t.Fatalf("expected acceptHeight %v, but got %v",
|
|
|
|
testCurrentHeight, event.AcceptHeight)
|
|
|
|
}
|
|
|
|
|
2019-01-14 12:09:46 +03:00
|
|
|
// Settle invoice with a slightly higher amount.
|
|
|
|
amtPaid := lnwire.MilliSatoshi(100500)
|
2019-11-12 22:28:43 +03:00
|
|
|
_, err = ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-04-16 13:11:20 +03:00
|
|
|
)
|
2019-01-14 12:09:46 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect the settled state to be sent to the single invoice
|
|
|
|
// subscriber.
|
|
|
|
select {
|
|
|
|
case update := <-subscription.Updates:
|
2019-11-22 13:25:02 +03:00
|
|
|
if update.State != channeldb.ContractSettled {
|
2019-01-14 12:09:46 +03:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
update.State)
|
2019-01-14 12:09:46 +03:00
|
|
|
}
|
|
|
|
if update.AmtPaid != amtPaid {
|
|
|
|
t.Fatal("invoice AmtPaid incorrect")
|
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect a settled notification to be sent out.
|
|
|
|
select {
|
|
|
|
case settledInvoice := <-allSubscriptions.SettledInvoices:
|
2019-11-22 13:25:02 +03:00
|
|
|
if settledInvoice.State != channeldb.ContractSettled {
|
2019-01-14 12:09:46 +03:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
settledInvoice.State)
|
2019-01-14 12:09:46 +03:00
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
2019-08-08 16:48:31 +03:00
|
|
|
// Try to settle again with the same htlc id. We need this idempotent
|
|
|
|
// behaviour after a restart.
|
2019-11-12 22:28:43 +03:00
|
|
|
event, err = ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-04-16 13:11:20 +03:00
|
|
|
)
|
2019-01-14 12:09:46 +03:00
|
|
|
if err != nil {
|
2019-06-10 13:20:49 +03:00
|
|
|
t.Fatalf("unexpected NotifyExitHopHtlc error: %v", err)
|
|
|
|
}
|
|
|
|
if event.Preimage == nil {
|
|
|
|
t.Fatal("expected settle event")
|
2019-01-14 12:09:46 +03:00
|
|
|
}
|
|
|
|
|
2019-08-08 16:48:31 +03:00
|
|
|
// Try to settle again with a new higher-valued htlc. This payment
|
|
|
|
// should also be accepted, to prevent any change in behaviour for a
|
|
|
|
// paid invoice that may open up a probe vector.
|
2019-11-12 22:28:43 +03:00
|
|
|
event, err = ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amtPaid+600, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(1), hodlChan, testPayload,
|
2019-04-16 13:11:20 +03:00
|
|
|
)
|
2019-01-14 12:09:46 +03:00
|
|
|
if err != nil {
|
2019-06-10 13:20:49 +03:00
|
|
|
t.Fatalf("unexpected NotifyExitHopHtlc error: %v", err)
|
|
|
|
}
|
2019-08-08 17:25:25 +03:00
|
|
|
if event.Preimage == nil {
|
|
|
|
t.Fatal("expected settle event")
|
2019-06-10 13:20:49 +03:00
|
|
|
}
|
|
|
|
|
2019-08-08 17:25:25 +03:00
|
|
|
// Try to settle again with a lower amount. This should fail just as it
|
|
|
|
// would have failed if it were the first payment.
|
2019-11-12 22:28:43 +03:00
|
|
|
event, err = ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amtPaid-600, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(2), hodlChan, testPayload,
|
2019-06-10 13:20:49 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected NotifyExitHopHtlc error: %v", err)
|
|
|
|
}
|
|
|
|
if event.Preimage != nil {
|
|
|
|
t.Fatal("expected cancel event")
|
2019-01-14 12:09:46 +03:00
|
|
|
}
|
|
|
|
|
2019-08-09 16:09:57 +03:00
|
|
|
// Check that settled amount is equal to the sum of values of the htlcs
|
|
|
|
// 0 and 1.
|
2019-11-20 20:06:51 +03:00
|
|
|
inv, err := ctx.registry.LookupInvoice(testInvoicePaymentHash)
|
2019-01-14 12:09:46 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-09 16:09:57 +03:00
|
|
|
if inv.AmtPaid != amtPaid+amtPaid+600 {
|
|
|
|
t.Fatal("amount incorrect")
|
2019-01-14 12:09:46 +03:00
|
|
|
}
|
2019-01-11 13:19:16 +03:00
|
|
|
|
|
|
|
// Try to cancel.
|
2019-11-20 20:06:51 +03:00
|
|
|
err = ctx.registry.CancelInvoice(testInvoicePaymentHash)
|
2019-01-11 13:19:16 +03:00
|
|
|
if err != channeldb.ErrInvoiceAlreadySettled {
|
|
|
|
t.Fatal("expected cancelation of a settled invoice to fail")
|
|
|
|
}
|
2019-02-11 14:01:05 +03:00
|
|
|
|
|
|
|
// As this is a direct sette, we expect nothing on the hodl chan.
|
|
|
|
select {
|
|
|
|
case <-hodlChan:
|
|
|
|
t.Fatal("unexpected event")
|
|
|
|
default:
|
|
|
|
}
|
2019-01-11 13:19:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestCancelInvoice tests cancelation of an invoice and related notifications.
|
|
|
|
func TestCancelInvoice(t *testing.T) {
|
2019-11-12 22:28:43 +03:00
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
2019-01-11 13:19:16 +03:00
|
|
|
|
2019-11-12 22:28:43 +03:00
|
|
|
allSubscriptions := ctx.registry.SubscribeNotifications(0, 0)
|
2019-01-11 13:19:16 +03:00
|
|
|
defer allSubscriptions.Cancel()
|
|
|
|
|
|
|
|
// Try to cancel the not yet existing invoice. This should fail.
|
2019-11-20 20:06:51 +03:00
|
|
|
err := ctx.registry.CancelInvoice(testInvoicePaymentHash)
|
2019-01-11 13:19:16 +03:00
|
|
|
if err != channeldb.ErrInvoiceNotFound {
|
|
|
|
t.Fatalf("expected ErrInvoiceNotFound, but got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe to the not yet existing invoice.
|
2019-11-20 20:06:51 +03:00
|
|
|
subscription, err := ctx.registry.SubscribeSingleInvoice(testInvoicePaymentHash)
|
2019-08-09 13:22:53 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-01-11 13:19:16 +03:00
|
|
|
defer subscription.Cancel()
|
|
|
|
|
2019-11-20 20:06:51 +03:00
|
|
|
if subscription.hash != testInvoicePaymentHash {
|
2019-01-11 13:19:16 +03:00
|
|
|
t.Fatalf("expected subscription for provided hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the invoice.
|
|
|
|
amt := lnwire.MilliSatoshi(100000)
|
2019-11-20 20:06:51 +03:00
|
|
|
_, err = ctx.registry.AddInvoice(testInvoice, testInvoicePaymentHash)
|
2019-01-11 13:19:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect the open state to be sent to the single invoice subscriber.
|
|
|
|
select {
|
|
|
|
case update := <-subscription.Updates:
|
2019-11-22 13:25:02 +03:00
|
|
|
if update.State != channeldb.ContractOpen {
|
2019-01-11 13:19:16 +03:00
|
|
|
t.Fatalf(
|
|
|
|
"expected state ContractOpen, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
update.State,
|
2019-01-11 13:19:16 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect a new invoice notification to be sent out.
|
|
|
|
select {
|
|
|
|
case newInvoice := <-allSubscriptions.NewInvoices:
|
2019-11-22 13:25:02 +03:00
|
|
|
if newInvoice.State != channeldb.ContractOpen {
|
2019-01-11 13:19:16 +03:00
|
|
|
t.Fatalf(
|
|
|
|
"expected state ContractOpen, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
newInvoice.State,
|
2019-01-11 13:19:16 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel invoice.
|
2019-11-20 20:06:51 +03:00
|
|
|
err = ctx.registry.CancelInvoice(testInvoicePaymentHash)
|
2019-01-11 13:19:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect the canceled state to be sent to the single invoice
|
|
|
|
// subscriber.
|
|
|
|
select {
|
|
|
|
case update := <-subscription.Updates:
|
2019-11-22 13:25:02 +03:00
|
|
|
if update.State != channeldb.ContractCanceled {
|
2019-01-11 13:19:16 +03:00
|
|
|
t.Fatalf(
|
|
|
|
"expected state ContractCanceled, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
update.State,
|
2019-01-11 13:19:16 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect no cancel notification to be sent to all invoice
|
|
|
|
// subscribers (backwards compatibility).
|
|
|
|
|
|
|
|
// Try to cancel again.
|
2019-11-20 20:06:51 +03:00
|
|
|
err = ctx.registry.CancelInvoice(testInvoicePaymentHash)
|
2019-01-11 13:19:16 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected cancelation of a canceled invoice to succeed")
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:11:15 +03:00
|
|
|
// Notify arrival of a new htlc paying to this invoice. This should
|
2019-08-14 22:21:39 +03:00
|
|
|
// result in a cancel event.
|
2019-02-11 14:01:05 +03:00
|
|
|
hodlChan := make(chan interface{})
|
2019-11-12 22:28:43 +03:00
|
|
|
event, err := ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amt, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-04-16 13:11:20 +03:00
|
|
|
)
|
2019-02-20 14:11:15 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected settlement of a canceled invoice to succeed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.Preimage != nil {
|
|
|
|
t.Fatal("expected cancel hodl event")
|
2019-01-11 13:19:16 +03:00
|
|
|
}
|
2019-08-14 22:21:39 +03:00
|
|
|
if event.AcceptHeight != testCurrentHeight {
|
|
|
|
t.Fatalf("expected acceptHeight %v, but got %v",
|
|
|
|
testCurrentHeight, event.AcceptHeight)
|
|
|
|
}
|
2019-01-14 12:09:46 +03:00
|
|
|
}
|
|
|
|
|
2019-08-14 22:21:39 +03:00
|
|
|
// TestSettleHoldInvoice tests settling of a hold invoice and related
|
|
|
|
// notifications.
|
|
|
|
func TestSettleHoldInvoice(t *testing.T) {
|
2019-11-20 20:06:51 +03:00
|
|
|
defer timeout()()
|
2019-02-11 14:01:05 +03:00
|
|
|
|
2019-11-20 20:06:51 +03:00
|
|
|
cdb, cleanup, err := newTestChannelDB()
|
2019-09-13 05:59:07 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-11 14:01:05 +03:00
|
|
|
defer cleanup()
|
|
|
|
|
2019-11-12 22:28:43 +03:00
|
|
|
// Instantiate and start the invoice ctx.registry.
|
2019-12-04 20:47:53 +03:00
|
|
|
cfg := RegistryConfig{
|
|
|
|
FinalCltvRejectDelta: testFinalCltvRejectDelta,
|
2019-12-09 19:50:11 +03:00
|
|
|
Clock: clock.NewTestClock(testTime),
|
2019-12-04 20:47:53 +03:00
|
|
|
}
|
2019-12-09 19:50:11 +03:00
|
|
|
registry := NewRegistry(cdb, NewInvoiceExpiryWatcher(cfg.Clock), &cfg)
|
2019-02-11 14:01:05 +03:00
|
|
|
|
|
|
|
err = registry.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer registry.Stop()
|
|
|
|
|
|
|
|
allSubscriptions := registry.SubscribeNotifications(0, 0)
|
|
|
|
defer allSubscriptions.Cancel()
|
|
|
|
|
|
|
|
// Subscribe to the not yet existing invoice.
|
2019-11-20 20:06:51 +03:00
|
|
|
subscription, err := registry.SubscribeSingleInvoice(testInvoicePaymentHash)
|
2019-08-09 13:22:53 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-11 14:01:05 +03:00
|
|
|
defer subscription.Cancel()
|
|
|
|
|
2019-11-20 20:06:51 +03:00
|
|
|
if subscription.hash != testInvoicePaymentHash {
|
2019-02-11 14:01:05 +03:00
|
|
|
t.Fatalf("expected subscription for provided hash")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the invoice.
|
2019-11-20 20:06:51 +03:00
|
|
|
_, err = registry.AddInvoice(testHodlInvoice, testInvoicePaymentHash)
|
2019-02-11 14:01:05 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect the open state to be sent to the single invoice subscriber.
|
|
|
|
update := <-subscription.Updates
|
2019-11-22 13:25:02 +03:00
|
|
|
if update.State != channeldb.ContractOpen {
|
2019-02-11 14:01:05 +03:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
update.State)
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We expect a new invoice notification to be sent out.
|
|
|
|
newInvoice := <-allSubscriptions.NewInvoices
|
2019-11-22 13:25:02 +03:00
|
|
|
if newInvoice.State != channeldb.ContractOpen {
|
2019-02-11 14:01:05 +03:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
newInvoice.State)
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use slightly higher amount for accept/settle.
|
|
|
|
amtPaid := lnwire.MilliSatoshi(100500)
|
|
|
|
|
|
|
|
hodlChan := make(chan interface{}, 1)
|
|
|
|
|
|
|
|
// NotifyExitHopHtlc without a preimage present in the invoice registry
|
|
|
|
// should be possible.
|
2019-04-16 13:11:20 +03:00
|
|
|
event, err := registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-04-16 13:11:20 +03:00
|
|
|
)
|
2019-02-11 14:01:05 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
|
|
|
if event != nil {
|
2019-08-20 16:51:34 +03:00
|
|
|
t.Fatalf("expected htlc to be held")
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test idempotency.
|
2019-04-16 13:11:20 +03:00
|
|
|
event, err = registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-04-16 13:11:20 +03:00
|
|
|
)
|
2019-02-11 14:01:05 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
|
|
|
if event != nil {
|
2019-08-20 16:51:34 +03:00
|
|
|
t.Fatalf("expected htlc to be held")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test replay at a higher height. We expect the same result because it
|
|
|
|
// is a replay.
|
|
|
|
event, err = registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight+10,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-08-20 16:51:34 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
|
|
|
if event != nil {
|
|
|
|
t.Fatalf("expected htlc to be held")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a new htlc coming in that doesn't meet the final cltv delta
|
2019-08-09 16:09:57 +03:00
|
|
|
// requirement. It should be rejected.
|
2019-08-20 16:51:34 +03:00
|
|
|
event, err = registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amtPaid, 1, testCurrentHeight,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(1), hodlChan, testPayload,
|
2019-08-20 16:51:34 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
2019-08-09 16:09:57 +03:00
|
|
|
if event == nil || event.Preimage != nil {
|
2019-10-03 18:22:43 +03:00
|
|
|
t.Fatalf("expected htlc to be canceled")
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// We expect the accepted state to be sent to the single invoice
|
|
|
|
// subscriber. For all invoice subscribers, we don't expect an update.
|
|
|
|
// Those only get notified on settle.
|
|
|
|
update = <-subscription.Updates
|
2019-11-22 13:25:02 +03:00
|
|
|
if update.State != channeldb.ContractAccepted {
|
2019-02-11 14:01:05 +03:00
|
|
|
t.Fatalf("expected state ContractAccepted, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
update.State)
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
if update.AmtPaid != amtPaid {
|
|
|
|
t.Fatal("invoice AmtPaid incorrect")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settling with preimage should succeed.
|
2019-11-20 20:06:51 +03:00
|
|
|
err = registry.SettleHodlInvoice(testInvoicePreimage)
|
2019-02-11 14:01:05 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected set preimage to succeed")
|
|
|
|
}
|
|
|
|
|
|
|
|
hodlEvent := (<-hodlChan).(HodlEvent)
|
2019-11-20 20:06:51 +03:00
|
|
|
if *hodlEvent.Preimage != testInvoicePreimage {
|
2019-02-11 14:01:05 +03:00
|
|
|
t.Fatal("unexpected preimage in hodl event")
|
|
|
|
}
|
2019-08-14 22:21:39 +03:00
|
|
|
if hodlEvent.AcceptHeight != testCurrentHeight {
|
|
|
|
t.Fatalf("expected acceptHeight %v, but got %v",
|
|
|
|
testCurrentHeight, event.AcceptHeight)
|
|
|
|
}
|
2019-02-11 14:01:05 +03:00
|
|
|
|
|
|
|
// We expect a settled notification to be sent out for both all and
|
|
|
|
// single invoice subscribers.
|
|
|
|
settledInvoice := <-allSubscriptions.SettledInvoices
|
2019-11-22 13:25:02 +03:00
|
|
|
if settledInvoice.State != channeldb.ContractSettled {
|
2019-02-11 14:01:05 +03:00
|
|
|
t.Fatalf("expected state ContractSettled, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
settledInvoice.State)
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
2019-08-20 16:51:34 +03:00
|
|
|
if settledInvoice.AmtPaid != amtPaid {
|
|
|
|
t.Fatalf("expected amount to be %v, but got %v",
|
|
|
|
amtPaid, settledInvoice.AmtPaid)
|
|
|
|
}
|
2019-02-11 14:01:05 +03:00
|
|
|
|
|
|
|
update = <-subscription.Updates
|
2019-11-22 13:25:02 +03:00
|
|
|
if update.State != channeldb.ContractSettled {
|
2019-02-11 14:01:05 +03:00
|
|
|
t.Fatalf("expected state ContractSettled, but got %v",
|
2019-11-22 13:25:02 +03:00
|
|
|
update.State)
|
2019-02-11 14:01:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Idempotency.
|
2019-11-20 20:06:51 +03:00
|
|
|
err = registry.SettleHodlInvoice(testInvoicePreimage)
|
2019-02-11 14:01:05 +03:00
|
|
|
if err != channeldb.ErrInvoiceAlreadySettled {
|
|
|
|
t.Fatalf("expected ErrInvoiceAlreadySettled but got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to cancel.
|
2019-11-20 20:06:51 +03:00
|
|
|
err = registry.CancelInvoice(testInvoicePaymentHash)
|
2019-02-11 14:01:05 +03:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected cancelation of a settled invoice to fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 22:21:39 +03:00
|
|
|
// TestCancelHoldInvoice tests canceling of a hold invoice and related
|
|
|
|
// notifications.
|
|
|
|
func TestCancelHoldInvoice(t *testing.T) {
|
2019-12-09 19:50:11 +03:00
|
|
|
defer timeout()()
|
2019-08-14 22:21:39 +03:00
|
|
|
|
2019-11-20 20:06:51 +03:00
|
|
|
cdb, cleanup, err := newTestChannelDB()
|
2019-08-14 22:21:39 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cleanup()
|
|
|
|
|
2019-11-12 22:28:43 +03:00
|
|
|
// Instantiate and start the invoice ctx.registry.
|
2019-12-04 20:47:53 +03:00
|
|
|
cfg := RegistryConfig{
|
|
|
|
FinalCltvRejectDelta: testFinalCltvRejectDelta,
|
2019-12-09 19:50:11 +03:00
|
|
|
Clock: clock.NewTestClock(testTime),
|
2019-12-04 20:47:53 +03:00
|
|
|
}
|
2019-12-09 19:50:11 +03:00
|
|
|
registry := NewRegistry(cdb, NewInvoiceExpiryWatcher(cfg.Clock), &cfg)
|
2019-08-14 22:21:39 +03:00
|
|
|
|
|
|
|
err = registry.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer registry.Stop()
|
|
|
|
|
|
|
|
// Add the invoice.
|
2019-11-20 20:06:51 +03:00
|
|
|
_, err = registry.AddInvoice(testHodlInvoice, testInvoicePaymentHash)
|
2019-08-14 22:21:39 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
amtPaid := lnwire.MilliSatoshi(100000)
|
|
|
|
hodlChan := make(chan interface{}, 1)
|
|
|
|
|
|
|
|
// NotifyExitHopHtlc without a preimage present in the invoice registry
|
|
|
|
// should be possible.
|
|
|
|
event, err := registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-08-14 22:21:39 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
|
|
|
if event != nil {
|
|
|
|
t.Fatalf("expected htlc to be held")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel invoice.
|
2019-11-20 20:06:51 +03:00
|
|
|
err = registry.CancelInvoice(testInvoicePaymentHash)
|
2019-08-14 22:21:39 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("cancel invoice failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
hodlEvent := (<-hodlChan).(HodlEvent)
|
|
|
|
if hodlEvent.Preimage != nil {
|
|
|
|
t.Fatal("expected cancel hodl event")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Offering the same htlc again at a higher height should still result
|
|
|
|
// in a rejection. The accept height is expected to be the original
|
|
|
|
// accept height.
|
|
|
|
event, err = registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight+1,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-08-14 22:21:39 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
|
|
|
if event.Preimage != nil {
|
|
|
|
t.Fatalf("expected htlc to be canceled")
|
|
|
|
}
|
|
|
|
if event.AcceptHeight != testCurrentHeight {
|
|
|
|
t.Fatalf("expected acceptHeight %v, but got %v",
|
|
|
|
testCurrentHeight, event.AcceptHeight)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:11:20 +03:00
|
|
|
// TestUnknownInvoice tests that invoice registry returns an error when the
|
|
|
|
// invoice is unknown. This is to guard against returning a cancel hodl event
|
|
|
|
// for forwarded htlcs. In the link, NotifyExitHopHtlc is only called if we are
|
|
|
|
// the exit hop, but in htlcIncomingContestResolver it is called with forwarded
|
|
|
|
// htlc hashes as well.
|
|
|
|
func TestUnknownInvoice(t *testing.T) {
|
2019-11-12 22:28:43 +03:00
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
2019-04-16 13:11:20 +03:00
|
|
|
|
|
|
|
// Notify arrival of a new htlc paying to this invoice. This should
|
|
|
|
// succeed.
|
|
|
|
hodlChan := make(chan interface{})
|
|
|
|
amt := lnwire.MilliSatoshi(100000)
|
2019-11-12 22:28:43 +03:00
|
|
|
_, err := ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, amt, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 22:28:43 +03:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-04-16 13:11:20 +03:00
|
|
|
)
|
|
|
|
if err != channeldb.ErrInvoiceNotFound {
|
|
|
|
t.Fatal("expected invoice not found error")
|
|
|
|
}
|
|
|
|
}
|
2019-11-12 22:28:43 +03:00
|
|
|
|
2019-09-03 13:23:39 +03:00
|
|
|
// TestSettleMpp tests settling of an invoice with multiple partial payments.
|
|
|
|
func TestSettleMpp(t *testing.T) {
|
2019-12-09 19:50:11 +03:00
|
|
|
defer timeout()()
|
2019-09-03 13:23:39 +03:00
|
|
|
|
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
|
|
|
|
|
|
|
// Add the invoice.
|
2019-11-20 20:06:51 +03:00
|
|
|
_, err := ctx.registry.AddInvoice(testInvoice, testInvoicePaymentHash)
|
2019-09-03 13:23:39 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mppPayload := &mockPayload{
|
|
|
|
mpp: record.NewMPP(testInvoiceAmt, [32]byte{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send htlc 1.
|
|
|
|
hodlChan1 := make(chan interface{}, 1)
|
|
|
|
event, err := ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, testInvoice.Terms.Value/2,
|
2019-09-03 13:23:39 +03:00
|
|
|
testHtlcExpiry,
|
|
|
|
testCurrentHeight, getCircuitKey(10), hodlChan1, mppPayload,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if event != nil {
|
|
|
|
t.Fatal("expected no direct resolution")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate mpp timeout releasing htlc 1.
|
2019-11-25 13:46:29 +03:00
|
|
|
ctx.clock.SetTime(testTime.Add(30 * time.Second))
|
2019-09-03 13:23:39 +03:00
|
|
|
|
|
|
|
hodlEvent := (<-hodlChan1).(HodlEvent)
|
|
|
|
if hodlEvent.Preimage != nil {
|
|
|
|
t.Fatal("expected cancel event")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send htlc 2.
|
|
|
|
hodlChan2 := make(chan interface{}, 1)
|
|
|
|
event, err = ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, testInvoice.Terms.Value/2,
|
2019-09-03 13:23:39 +03:00
|
|
|
testHtlcExpiry,
|
|
|
|
testCurrentHeight, getCircuitKey(11), hodlChan2, mppPayload,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if event != nil {
|
|
|
|
t.Fatal("expected no direct resolution")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send htlc 3.
|
|
|
|
hodlChan3 := make(chan interface{}, 1)
|
|
|
|
event, err = ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 20:06:51 +03:00
|
|
|
testInvoicePaymentHash, testInvoice.Terms.Value/2,
|
2019-09-03 13:23:39 +03:00
|
|
|
testHtlcExpiry,
|
|
|
|
testCurrentHeight, getCircuitKey(12), hodlChan3, mppPayload,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if event == nil {
|
|
|
|
t.Fatal("expected a settle event")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that settled amount is equal to the sum of values of the htlcs
|
|
|
|
// 0 and 1.
|
2019-11-20 20:06:51 +03:00
|
|
|
inv, err := ctx.registry.LookupInvoice(testInvoicePaymentHash)
|
2019-09-03 13:23:39 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if inv.State != channeldb.ContractSettled {
|
|
|
|
t.Fatal("expected invoice to be settled")
|
|
|
|
}
|
|
|
|
if inv.AmtPaid != testInvoice.Terms.Value {
|
|
|
|
t.Fatalf("amount incorrect, expected %v but got %v",
|
|
|
|
testInvoice.Terms.Value, inv.AmtPaid)
|
|
|
|
}
|
|
|
|
}
|
2019-12-09 19:50:11 +03:00
|
|
|
|
|
|
|
// Tests that invoices are canceled after expiration.
|
|
|
|
func TestInvoiceExpiryWithRegistry(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
cdb, cleanup, err := newTestChannelDB()
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testClock := clock.NewTestClock(testTime)
|
|
|
|
|
|
|
|
cfg := RegistryConfig{
|
|
|
|
FinalCltvRejectDelta: testFinalCltvRejectDelta,
|
|
|
|
Clock: testClock,
|
|
|
|
}
|
|
|
|
|
|
|
|
expiryWatcher := NewInvoiceExpiryWatcher(cfg.Clock)
|
|
|
|
registry := NewRegistry(cdb, expiryWatcher, &cfg)
|
|
|
|
|
|
|
|
// First prefill the Channel DB with some pre-existing invoices,
|
|
|
|
// half of them still pending, half of them expired.
|
|
|
|
const numExpired = 5
|
|
|
|
const numPending = 5
|
|
|
|
existingInvoices := generateInvoiceExpiryTestData(
|
|
|
|
t, testTime, 0, numExpired, numPending,
|
|
|
|
)
|
|
|
|
|
|
|
|
var expectedCancellations []lntypes.Hash
|
|
|
|
|
|
|
|
for paymentHash, expiredInvoice := range existingInvoices.expiredInvoices {
|
|
|
|
if _, err := cdb.AddInvoice(expiredInvoice, paymentHash); err != nil {
|
|
|
|
t.Fatalf("cannot add invoice to channel db: %v", err)
|
|
|
|
}
|
|
|
|
expectedCancellations = append(expectedCancellations, paymentHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
for paymentHash, pendingInvoice := range existingInvoices.pendingInvoices {
|
|
|
|
if _, err := cdb.AddInvoice(pendingInvoice, paymentHash); err != nil {
|
|
|
|
t.Fatalf("cannot add invoice to channel db: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = registry.Start(); err != nil {
|
|
|
|
t.Fatalf("cannot start registry: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now generate pending and invoices and add them to the registry while
|
|
|
|
// it is up and running. We'll manipulate the clock to let them expire.
|
|
|
|
newInvoices := generateInvoiceExpiryTestData(
|
|
|
|
t, testTime, numExpired+numPending, 0, numPending,
|
|
|
|
)
|
|
|
|
|
|
|
|
var invoicesThatWillCancel []lntypes.Hash
|
|
|
|
for paymentHash, pendingInvoice := range newInvoices.pendingInvoices {
|
|
|
|
_, err := registry.AddInvoice(pendingInvoice, paymentHash)
|
|
|
|
invoicesThatWillCancel = append(invoicesThatWillCancel, paymentHash)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that they are really not canceled until before the clock is
|
|
|
|
// advanced.
|
|
|
|
for i := range invoicesThatWillCancel {
|
|
|
|
invoice, err := registry.LookupInvoice(invoicesThatWillCancel[i])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot find invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if invoice.State == channeldb.ContractCanceled {
|
|
|
|
t.Fatalf("expected pending invoice, got canceled")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fwd time 1 day.
|
|
|
|
testClock.SetTime(testTime.Add(24 * time.Hour))
|
|
|
|
|
|
|
|
// Give some time to the watcher to cancel everything.
|
|
|
|
time.Sleep(testTimeout)
|
|
|
|
registry.Stop()
|
|
|
|
|
|
|
|
// Create the expected cancellation set before the final check.
|
|
|
|
expectedCancellations = append(
|
|
|
|
expectedCancellations, invoicesThatWillCancel...,
|
|
|
|
)
|
|
|
|
|
|
|
|
// Retrospectively check that all invoices that were expected to be canceled
|
|
|
|
// are indeed canceled.
|
|
|
|
for i := range expectedCancellations {
|
|
|
|
invoice, err := registry.LookupInvoice(expectedCancellations[i])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("cannot find invoice: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if invoice.State != channeldb.ContractCanceled {
|
|
|
|
t.Fatalf("expected canceled invoice, got: %v", invoice.State)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|