2019-01-14 10:09:46 +01:00
|
|
|
package invoices
|
|
|
|
|
|
|
|
import (
|
2020-07-28 23:39:11 +02:00
|
|
|
"math"
|
2019-01-14 10:09:46 +01:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2019-12-09 17:50:11 +01:00
|
|
|
"github.com/lightningnetwork/lnd/clock"
|
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2019-01-14 10:09:46 +01:00
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
2019-11-12 20:28:43 +01:00
|
|
|
"github.com/lightningnetwork/lnd/record"
|
2020-04-09 10:42:10 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-01-14 10:09:46 +01:00
|
|
|
)
|
|
|
|
|
2019-02-20 12:11:15 +01:00
|
|
|
// TestSettleInvoice tests settling of an invoice and related notifications.
|
|
|
|
func TestSettleInvoice(t *testing.T) {
|
2019-11-12 20:28:43 +01:00
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
2019-01-14 10:09:46 +01:00
|
|
|
|
2020-05-20 05:31:38 +02:00
|
|
|
allSubscriptions, err := ctx.registry.SubscribeNotifications(0, 0)
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Nil(t, err)
|
2019-01-14 10:09:46 +01:00
|
|
|
defer allSubscriptions.Cancel()
|
|
|
|
|
|
|
|
// Subscribe to the not yet existing invoice.
|
2019-11-20 18:06:51 +01:00
|
|
|
subscription, err := ctx.registry.SubscribeSingleInvoice(testInvoicePaymentHash)
|
2019-08-09 12:22:53 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-01-14 10:09:46 +01:00
|
|
|
defer subscription.Cancel()
|
|
|
|
|
2021-03-25 03:48:32 +01:00
|
|
|
require.Equal(t, subscription.invoiceRef.PayHash(), &testInvoicePaymentHash)
|
2019-01-14 10:09:46 +01:00
|
|
|
|
|
|
|
// Add the invoice.
|
2019-11-20 18:06:51 +01:00
|
|
|
addIdx, err := ctx.registry.AddInvoice(testInvoice, testInvoicePaymentHash)
|
2019-01-14 10:09:46 +01: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 11:25:02 +01:00
|
|
|
if update.State != channeldb.ContractOpen {
|
2019-01-14 10:09:46 +01:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
update.State)
|
2019-01-14 10:09:46 +01: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 11:25:02 +01:00
|
|
|
if newInvoice.State != channeldb.ContractOpen {
|
2019-01-14 10:09:46 +01:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
newInvoice.State)
|
2019-01-14 10:09:46 +01:00
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
2019-02-11 12:01:05 +01:00
|
|
|
hodlChan := make(chan interface{}, 1)
|
|
|
|
|
2019-08-14 21:21:39 +02:00
|
|
|
// Try to settle invoice with an htlc that expires too soon.
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err := ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, testInvoice.Terms.Value,
|
2019-08-14 21:21:39 +02:00
|
|
|
uint32(testCurrentHeight)+testInvoiceCltvDelta-1,
|
2019-11-12 20:28:43 +01:00
|
|
|
testCurrentHeight, getCircuitKey(10), hodlChan, testPayload,
|
2019-08-14 21:21:39 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, resolution)
|
|
|
|
failResolution := checkFailResolution(
|
|
|
|
t, resolution, ResultExpiryTooSoon,
|
|
|
|
)
|
|
|
|
require.Equal(t, testCurrentHeight, failResolution.AcceptHeight)
|
2019-08-14 21:21:39 +02:00
|
|
|
|
2019-01-14 10:09:46 +01:00
|
|
|
// Settle invoice with a slightly higher amount.
|
|
|
|
amtPaid := lnwire.MilliSatoshi(100500)
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err = ctx.registry.NotifyExitHopHtlc(
|
2020-02-06 18:35:10 +01:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry,
|
|
|
|
testCurrentHeight, getCircuitKey(0), hodlChan,
|
|
|
|
testPayload,
|
2019-04-16 12:11:20 +02:00
|
|
|
)
|
2019-01-14 10:09:46 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, resolution)
|
|
|
|
settleResolution := checkSettleResolution(
|
|
|
|
t, resolution, testInvoicePreimage,
|
|
|
|
)
|
|
|
|
require.Equal(t, ResultSettled, settleResolution.Outcome)
|
2019-01-14 10:09:46 +01:00
|
|
|
|
|
|
|
// We expect the settled state to be sent to the single invoice
|
|
|
|
// subscriber.
|
|
|
|
select {
|
|
|
|
case update := <-subscription.Updates:
|
2019-11-22 11:25:02 +01:00
|
|
|
if update.State != channeldb.ContractSettled {
|
2019-01-14 10:09:46 +01:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
update.State)
|
2019-01-14 10:09:46 +01: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 11:25:02 +01:00
|
|
|
if settledInvoice.State != channeldb.ContractSettled {
|
2019-01-14 10:09:46 +01:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
settledInvoice.State)
|
2019-01-14 10:09:46 +01:00
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
2019-08-08 15:48:31 +02:00
|
|
|
// Try to settle again with the same htlc id. We need this idempotent
|
|
|
|
// behaviour after a restart.
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err = ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 20:28:43 +01:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-04-16 12:11:20 +02:00
|
|
|
)
|
2019-01-14 10:09:46 +01:00
|
|
|
if err != nil {
|
2019-06-10 12:20:49 +02:00
|
|
|
t.Fatalf("unexpected NotifyExitHopHtlc error: %v", err)
|
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, resolution)
|
|
|
|
settleResolution = checkSettleResolution(
|
|
|
|
t, resolution, testInvoicePreimage,
|
|
|
|
)
|
|
|
|
require.Equal(t, ResultReplayToSettled, settleResolution.Outcome)
|
2019-01-14 10:09:46 +01:00
|
|
|
|
2019-08-08 15:48:31 +02: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-12-20 11:25:07 +01:00
|
|
|
resolution, err = ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, amtPaid+600, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 20:28:43 +01:00
|
|
|
getCircuitKey(1), hodlChan, testPayload,
|
2019-04-16 12:11:20 +02:00
|
|
|
)
|
2019-01-14 10:09:46 +01:00
|
|
|
if err != nil {
|
2019-06-10 12:20:49 +02:00
|
|
|
t.Fatalf("unexpected NotifyExitHopHtlc error: %v", err)
|
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, resolution)
|
|
|
|
settleResolution = checkSettleResolution(
|
|
|
|
t, resolution, testInvoicePreimage,
|
|
|
|
)
|
|
|
|
require.Equal(t, ResultDuplicateToSettled, settleResolution.Outcome)
|
2019-06-10 12:20:49 +02:00
|
|
|
|
2019-08-08 16:25:25 +02: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-12-20 11:25:07 +01:00
|
|
|
resolution, err = ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, amtPaid-600, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 20:28:43 +01:00
|
|
|
getCircuitKey(2), hodlChan, testPayload,
|
2019-06-10 12:20:49 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unexpected NotifyExitHopHtlc error: %v", err)
|
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, resolution)
|
|
|
|
checkFailResolution(t, resolution, ResultAmountTooLow)
|
2019-01-14 10:09:46 +01:00
|
|
|
|
2019-08-09 15:09:57 +02:00
|
|
|
// Check that settled amount is equal to the sum of values of the htlcs
|
|
|
|
// 0 and 1.
|
2019-11-20 18:06:51 +01:00
|
|
|
inv, err := ctx.registry.LookupInvoice(testInvoicePaymentHash)
|
2019-01-14 10:09:46 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-09 15:09:57 +02:00
|
|
|
if inv.AmtPaid != amtPaid+amtPaid+600 {
|
|
|
|
t.Fatal("amount incorrect")
|
2019-01-14 10:09:46 +01:00
|
|
|
}
|
2019-01-11 11:19:16 +01:00
|
|
|
|
|
|
|
// Try to cancel.
|
2019-11-20 18:06:51 +01:00
|
|
|
err = ctx.registry.CancelInvoice(testInvoicePaymentHash)
|
2019-01-11 11:19:16 +01:00
|
|
|
if err != channeldb.ErrInvoiceAlreadySettled {
|
|
|
|
t.Fatal("expected cancelation of a settled invoice to fail")
|
|
|
|
}
|
2019-02-11 12:01:05 +01:00
|
|
|
|
|
|
|
// As this is a direct sette, we expect nothing on the hodl chan.
|
|
|
|
select {
|
|
|
|
case <-hodlChan:
|
2019-12-20 11:25:07 +01:00
|
|
|
t.Fatal("unexpected resolution")
|
2019-02-11 12:01:05 +01:00
|
|
|
default:
|
|
|
|
}
|
2019-01-11 11:19:16 +01:00
|
|
|
}
|
|
|
|
|
2020-08-04 18:28:15 +02:00
|
|
|
func testCancelInvoice(t *testing.T, gc bool) {
|
2019-11-12 20:28:43 +01:00
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
2019-01-11 11:19:16 +01:00
|
|
|
|
2020-08-04 18:28:15 +02:00
|
|
|
// If set to true, then also delete the invoice from the DB after
|
|
|
|
// cancellation.
|
|
|
|
ctx.registry.cfg.GcCanceledInvoicesOnTheFly = gc
|
|
|
|
|
2020-05-20 05:31:38 +02:00
|
|
|
allSubscriptions, err := ctx.registry.SubscribeNotifications(0, 0)
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Nil(t, err)
|
2019-01-11 11:19:16 +01:00
|
|
|
defer allSubscriptions.Cancel()
|
|
|
|
|
|
|
|
// Try to cancel the not yet existing invoice. This should fail.
|
2020-05-20 05:31:38 +02:00
|
|
|
err = ctx.registry.CancelInvoice(testInvoicePaymentHash)
|
2019-01-11 11:19:16 +01:00
|
|
|
if err != channeldb.ErrInvoiceNotFound {
|
|
|
|
t.Fatalf("expected ErrInvoiceNotFound, but got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe to the not yet existing invoice.
|
2019-11-20 18:06:51 +01:00
|
|
|
subscription, err := ctx.registry.SubscribeSingleInvoice(testInvoicePaymentHash)
|
2019-08-09 12:22:53 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-01-11 11:19:16 +01:00
|
|
|
defer subscription.Cancel()
|
|
|
|
|
2021-03-25 03:48:32 +01:00
|
|
|
require.Equal(t, subscription.invoiceRef.PayHash(), &testInvoicePaymentHash)
|
2019-01-11 11:19:16 +01:00
|
|
|
|
|
|
|
// Add the invoice.
|
|
|
|
amt := lnwire.MilliSatoshi(100000)
|
2019-11-20 18:06:51 +01:00
|
|
|
_, err = ctx.registry.AddInvoice(testInvoice, testInvoicePaymentHash)
|
2019-01-11 11:19:16 +01: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 11:25:02 +01:00
|
|
|
if update.State != channeldb.ContractOpen {
|
2019-01-11 11:19:16 +01:00
|
|
|
t.Fatalf(
|
|
|
|
"expected state ContractOpen, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
update.State,
|
2019-01-11 11:19:16 +01: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 11:25:02 +01:00
|
|
|
if newInvoice.State != channeldb.ContractOpen {
|
2019-01-11 11:19:16 +01:00
|
|
|
t.Fatalf(
|
|
|
|
"expected state ContractOpen, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
newInvoice.State,
|
2019-01-11 11:19:16 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel invoice.
|
2019-11-20 18:06:51 +01:00
|
|
|
err = ctx.registry.CancelInvoice(testInvoicePaymentHash)
|
2019-01-11 11:19:16 +01: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 11:25:02 +01:00
|
|
|
if update.State != channeldb.ContractCanceled {
|
2019-01-11 11:19:16 +01:00
|
|
|
t.Fatalf(
|
|
|
|
"expected state ContractCanceled, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
update.State,
|
2019-01-11 11:19:16 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
2020-08-04 18:28:15 +02:00
|
|
|
if gc {
|
|
|
|
// Check that the invoice has been deleted from the db.
|
|
|
|
_, err = ctx.cdb.LookupInvoice(
|
|
|
|
channeldb.InvoiceRefByHash(testInvoicePaymentHash),
|
|
|
|
)
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-01-11 11:19:16 +01:00
|
|
|
// We expect no cancel notification to be sent to all invoice
|
|
|
|
// subscribers (backwards compatibility).
|
|
|
|
|
2020-08-04 18:28:15 +02:00
|
|
|
// Try to cancel again. Expect that we report ErrInvoiceNotFound if the
|
|
|
|
// invoice has been garbage collected (since the invoice has been
|
|
|
|
// deleted when it was canceled), and no error otherwise.
|
2019-11-20 18:06:51 +01:00
|
|
|
err = ctx.registry.CancelInvoice(testInvoicePaymentHash)
|
2020-08-04 18:28:15 +02:00
|
|
|
|
|
|
|
if gc {
|
|
|
|
require.Error(t, err, channeldb.ErrInvoiceNotFound)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
2019-01-11 11:19:16 +01:00
|
|
|
}
|
|
|
|
|
2019-02-20 12:11:15 +01:00
|
|
|
// Notify arrival of a new htlc paying to this invoice. This should
|
2019-12-20 11:25:07 +01:00
|
|
|
// result in a cancel resolution.
|
2019-02-11 12:01:05 +01:00
|
|
|
hodlChan := make(chan interface{})
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err := ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, amt, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 20:28:43 +01:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-04-16 12:11:20 +02:00
|
|
|
)
|
2019-02-20 12:11:15 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected settlement of a canceled invoice to succeed")
|
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, resolution)
|
2020-08-04 18:28:15 +02:00
|
|
|
|
|
|
|
// If the invoice has been deleted (or not present) then we expect the
|
|
|
|
// outcome to be ResultInvoiceNotFound instead of when the invoice is
|
|
|
|
// in our database in which case we expect ResultInvoiceAlreadyCanceled.
|
2021-03-25 03:48:59 +01:00
|
|
|
var failResolution *HtlcFailResolution
|
2020-08-04 18:28:15 +02:00
|
|
|
if gc {
|
2021-03-25 03:48:59 +01:00
|
|
|
failResolution = checkFailResolution(
|
|
|
|
t, resolution, ResultInvoiceNotFound,
|
|
|
|
)
|
2020-08-04 18:28:15 +02:00
|
|
|
} else {
|
2021-03-25 03:48:59 +01:00
|
|
|
failResolution = checkFailResolution(
|
|
|
|
t, resolution, ResultInvoiceAlreadyCanceled,
|
2020-08-04 18:28:15 +02:00
|
|
|
)
|
2019-12-20 11:25:07 +01:00
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
|
|
|
|
require.Equal(t, testCurrentHeight, failResolution.AcceptHeight)
|
2019-01-14 10:09:46 +01:00
|
|
|
}
|
|
|
|
|
2020-08-04 18:28:15 +02:00
|
|
|
// TestCancelInvoice tests cancelation of an invoice and related notifications.
|
|
|
|
func TestCancelInvoice(t *testing.T) {
|
|
|
|
// Test cancellation both with garbage collection (meaning that canceled
|
|
|
|
// invoice will be deleted) and without (meain it'll be kept).
|
|
|
|
t.Run("garbage collect", func(t *testing.T) {
|
|
|
|
testCancelInvoice(t, true)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("no garbage collect", func(t *testing.T) {
|
|
|
|
testCancelInvoice(t, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:21:39 +02:00
|
|
|
// TestSettleHoldInvoice tests settling of a hold invoice and related
|
|
|
|
// notifications.
|
|
|
|
func TestSettleHoldInvoice(t *testing.T) {
|
2019-11-20 18:06:51 +01:00
|
|
|
defer timeout()()
|
2019-02-11 12:01:05 +01:00
|
|
|
|
2020-01-20 10:57:34 +01:00
|
|
|
cdb, cleanup, err := newTestChannelDB(clock.NewTestClock(time.Time{}))
|
2019-09-13 04:59:07 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-11 12:01:05 +01:00
|
|
|
defer cleanup()
|
|
|
|
|
2019-11-12 20:28:43 +01:00
|
|
|
// Instantiate and start the invoice ctx.registry.
|
2019-12-04 18:47:53 +01:00
|
|
|
cfg := RegistryConfig{
|
|
|
|
FinalCltvRejectDelta: testFinalCltvRejectDelta,
|
2019-12-09 17:50:11 +01:00
|
|
|
Clock: clock.NewTestClock(testTime),
|
2019-12-04 18:47:53 +01:00
|
|
|
}
|
2019-12-09 17:50:11 +01:00
|
|
|
registry := NewRegistry(cdb, NewInvoiceExpiryWatcher(cfg.Clock), &cfg)
|
2019-02-11 12:01:05 +01:00
|
|
|
|
|
|
|
err = registry.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer registry.Stop()
|
|
|
|
|
2020-05-20 05:31:38 +02:00
|
|
|
allSubscriptions, err := registry.SubscribeNotifications(0, 0)
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Nil(t, err)
|
2019-02-11 12:01:05 +01:00
|
|
|
defer allSubscriptions.Cancel()
|
|
|
|
|
|
|
|
// Subscribe to the not yet existing invoice.
|
2019-11-20 18:06:51 +01:00
|
|
|
subscription, err := registry.SubscribeSingleInvoice(testInvoicePaymentHash)
|
2019-08-09 12:22:53 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-02-11 12:01:05 +01:00
|
|
|
defer subscription.Cancel()
|
|
|
|
|
2021-03-25 03:48:32 +01:00
|
|
|
require.Equal(t, subscription.invoiceRef.PayHash(), &testInvoicePaymentHash)
|
2019-02-11 12:01:05 +01:00
|
|
|
|
|
|
|
// Add the invoice.
|
2019-11-20 18:06:51 +01:00
|
|
|
_, err = registry.AddInvoice(testHodlInvoice, testInvoicePaymentHash)
|
2019-02-11 12:01:05 +01: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 11:25:02 +01:00
|
|
|
if update.State != channeldb.ContractOpen {
|
2019-02-11 12:01:05 +01:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
update.State)
|
2019-02-11 12:01:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// We expect a new invoice notification to be sent out.
|
|
|
|
newInvoice := <-allSubscriptions.NewInvoices
|
2019-11-22 11:25:02 +01:00
|
|
|
if newInvoice.State != channeldb.ContractOpen {
|
2019-02-11 12:01:05 +01:00
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
newInvoice.State)
|
2019-02-11 12:01:05 +01: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-12-20 11:25:07 +01:00
|
|
|
resolution, err := registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 20:28:43 +01:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-04-16 12:11:20 +02:00
|
|
|
)
|
2019-02-11 12:01:05 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
2019-12-20 11:25:07 +01:00
|
|
|
if resolution != nil {
|
2019-08-20 15:51:34 +02:00
|
|
|
t.Fatalf("expected htlc to be held")
|
2019-02-11 12:01:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test idempotency.
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err = registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 20:28:43 +01:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-04-16 12:11:20 +02:00
|
|
|
)
|
2019-02-11 12:01:05 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
2019-12-20 11:25:07 +01:00
|
|
|
if resolution != nil {
|
2019-08-20 15:51:34 +02:00
|
|
|
t.Fatalf("expected htlc to be held")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test replay at a higher height. We expect the same result because it
|
|
|
|
// is a replay.
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err = registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight+10,
|
2019-11-12 20:28:43 +01:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-08-20 15:51:34 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
2019-12-20 11:25:07 +01:00
|
|
|
if resolution != nil {
|
2019-08-20 15:51:34 +02:00
|
|
|
t.Fatalf("expected htlc to be held")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test a new htlc coming in that doesn't meet the final cltv delta
|
2019-08-09 15:09:57 +02:00
|
|
|
// requirement. It should be rejected.
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err = registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, amtPaid, 1, testCurrentHeight,
|
2019-11-12 20:28:43 +01:00
|
|
|
getCircuitKey(1), hodlChan, testPayload,
|
2019-08-20 15:51:34 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, resolution)
|
|
|
|
checkFailResolution(t, resolution, ResultExpiryTooSoon)
|
2019-02-11 12:01:05 +01: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 11:25:02 +01:00
|
|
|
if update.State != channeldb.ContractAccepted {
|
2019-02-11 12:01:05 +01:00
|
|
|
t.Fatalf("expected state ContractAccepted, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
update.State)
|
2019-02-11 12:01:05 +01:00
|
|
|
}
|
|
|
|
if update.AmtPaid != amtPaid {
|
|
|
|
t.Fatal("invoice AmtPaid incorrect")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settling with preimage should succeed.
|
2019-11-20 18:06:51 +01:00
|
|
|
err = registry.SettleHodlInvoice(testInvoicePreimage)
|
2019-02-11 12:01:05 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("expected set preimage to succeed")
|
|
|
|
}
|
|
|
|
|
2019-12-20 11:25:07 +01:00
|
|
|
htlcResolution := (<-hodlChan).(HtlcResolution)
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, htlcResolution)
|
|
|
|
settleResolution := checkSettleResolution(
|
|
|
|
t, htlcResolution, testInvoicePreimage,
|
|
|
|
)
|
|
|
|
require.Equal(t, testCurrentHeight, settleResolution.AcceptHeight)
|
|
|
|
require.Equal(t, ResultSettled, settleResolution.Outcome)
|
2019-02-11 12:01:05 +01:00
|
|
|
|
|
|
|
// We expect a settled notification to be sent out for both all and
|
|
|
|
// single invoice subscribers.
|
|
|
|
settledInvoice := <-allSubscriptions.SettledInvoices
|
2019-11-22 11:25:02 +01:00
|
|
|
if settledInvoice.State != channeldb.ContractSettled {
|
2019-02-11 12:01:05 +01:00
|
|
|
t.Fatalf("expected state ContractSettled, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
settledInvoice.State)
|
2019-02-11 12:01:05 +01:00
|
|
|
}
|
2019-08-20 15:51:34 +02:00
|
|
|
if settledInvoice.AmtPaid != amtPaid {
|
|
|
|
t.Fatalf("expected amount to be %v, but got %v",
|
|
|
|
amtPaid, settledInvoice.AmtPaid)
|
|
|
|
}
|
2019-02-11 12:01:05 +01:00
|
|
|
|
|
|
|
update = <-subscription.Updates
|
2019-11-22 11:25:02 +01:00
|
|
|
if update.State != channeldb.ContractSettled {
|
2019-02-11 12:01:05 +01:00
|
|
|
t.Fatalf("expected state ContractSettled, but got %v",
|
2019-11-22 11:25:02 +01:00
|
|
|
update.State)
|
2019-02-11 12:01:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Idempotency.
|
2019-11-20 18:06:51 +01:00
|
|
|
err = registry.SettleHodlInvoice(testInvoicePreimage)
|
2019-02-11 12:01:05 +01:00
|
|
|
if err != channeldb.ErrInvoiceAlreadySettled {
|
|
|
|
t.Fatalf("expected ErrInvoiceAlreadySettled but got %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to cancel.
|
2019-11-20 18:06:51 +01:00
|
|
|
err = registry.CancelInvoice(testInvoicePaymentHash)
|
2019-02-11 12:01:05 +01:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("expected cancelation of a settled invoice to fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-14 21:21:39 +02:00
|
|
|
// TestCancelHoldInvoice tests canceling of a hold invoice and related
|
|
|
|
// notifications.
|
|
|
|
func TestCancelHoldInvoice(t *testing.T) {
|
2019-12-09 17:50:11 +01:00
|
|
|
defer timeout()()
|
2019-08-14 21:21:39 +02:00
|
|
|
|
2020-01-20 10:57:34 +01:00
|
|
|
cdb, cleanup, err := newTestChannelDB(clock.NewTestClock(time.Time{}))
|
2019-08-14 21:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer cleanup()
|
|
|
|
|
2019-11-12 20:28:43 +01:00
|
|
|
// Instantiate and start the invoice ctx.registry.
|
2019-12-04 18:47:53 +01:00
|
|
|
cfg := RegistryConfig{
|
|
|
|
FinalCltvRejectDelta: testFinalCltvRejectDelta,
|
2019-12-09 17:50:11 +01:00
|
|
|
Clock: clock.NewTestClock(testTime),
|
2019-12-04 18:47:53 +01:00
|
|
|
}
|
2019-12-09 17:50:11 +01:00
|
|
|
registry := NewRegistry(cdb, NewInvoiceExpiryWatcher(cfg.Clock), &cfg)
|
2019-08-14 21:21:39 +02:00
|
|
|
|
|
|
|
err = registry.Start()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer registry.Stop()
|
|
|
|
|
|
|
|
// Add the invoice.
|
2019-11-20 18:06:51 +01:00
|
|
|
_, err = registry.AddInvoice(testHodlInvoice, testInvoicePaymentHash)
|
2019-08-14 21:21:39 +02: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.
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err := registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 20:28:43 +01:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-08-14 21:21:39 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
2019-12-20 11:25:07 +01:00
|
|
|
if resolution != nil {
|
2019-08-14 21:21:39 +02:00
|
|
|
t.Fatalf("expected htlc to be held")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cancel invoice.
|
2019-11-20 18:06:51 +01:00
|
|
|
err = registry.CancelInvoice(testInvoicePaymentHash)
|
2019-08-14 21:21:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("cancel invoice failed")
|
|
|
|
}
|
|
|
|
|
2019-12-20 11:25:07 +01:00
|
|
|
htlcResolution := (<-hodlChan).(HtlcResolution)
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, htlcResolution)
|
|
|
|
checkFailResolution(t, htlcResolution, ResultCanceled)
|
2019-08-14 21:21:39 +02:00
|
|
|
|
|
|
|
// 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.
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err = registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, amtPaid, testHtlcExpiry, testCurrentHeight+1,
|
2019-11-12 20:28:43 +01:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-08-14 21:21:39 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("expected settle to succeed but got %v", err)
|
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, resolution)
|
|
|
|
failResolution := checkFailResolution(
|
|
|
|
t, resolution, ResultReplayToCanceled,
|
|
|
|
)
|
|
|
|
require.Equal(t, testCurrentHeight, failResolution.AcceptHeight)
|
2019-08-14 21:21:39 +02:00
|
|
|
}
|
|
|
|
|
2019-04-16 12:11:20 +02:00
|
|
|
// TestUnknownInvoice tests that invoice registry returns an error when the
|
2019-12-20 11:25:07 +01:00
|
|
|
// invoice is unknown. This is to guard against returning a cancel htlc
|
|
|
|
// resolution 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.
|
2019-04-16 12:11:20 +02:00
|
|
|
func TestUnknownInvoice(t *testing.T) {
|
2019-11-12 20:28:43 +01:00
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
2019-04-16 12:11:20 +02:00
|
|
|
|
|
|
|
// Notify arrival of a new htlc paying to this invoice. This should
|
|
|
|
// succeed.
|
|
|
|
hodlChan := make(chan interface{})
|
|
|
|
amt := lnwire.MilliSatoshi(100000)
|
2020-02-06 18:35:10 +01:00
|
|
|
resolution, err := ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, amt, testHtlcExpiry, testCurrentHeight,
|
2019-11-12 20:28:43 +01:00
|
|
|
getCircuitKey(0), hodlChan, testPayload,
|
2019-04-16 12:11:20 +02:00
|
|
|
)
|
2019-12-20 11:25:08 +01:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("unexpected error")
|
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, resolution)
|
|
|
|
checkFailResolution(t, resolution, ResultInvoiceNotFound)
|
2019-04-16 12:11:20 +02:00
|
|
|
}
|
2019-11-12 20:28:43 +01:00
|
|
|
|
2020-01-16 13:13:59 +01:00
|
|
|
// TestKeySend tests receiving a spontaneous payment with and without keysend
|
2019-12-05 12:55:17 +01:00
|
|
|
// enabled.
|
|
|
|
func TestKeySend(t *testing.T) {
|
|
|
|
t.Run("enabled", func(t *testing.T) {
|
|
|
|
testKeySend(t, true)
|
|
|
|
})
|
|
|
|
t.Run("disabled", func(t *testing.T) {
|
|
|
|
testKeySend(t, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-01-16 13:13:59 +01:00
|
|
|
// testKeySend is the inner test function that tests keysend for a particular
|
2019-12-05 12:55:17 +01:00
|
|
|
// enabled state on the receiver end.
|
|
|
|
func testKeySend(t *testing.T, keySendEnabled bool) {
|
|
|
|
defer timeout()()
|
|
|
|
|
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
|
|
|
|
|
|
|
ctx.registry.cfg.AcceptKeySend = keySendEnabled
|
|
|
|
|
2020-05-20 05:31:38 +02:00
|
|
|
allSubscriptions, err := ctx.registry.SubscribeNotifications(0, 0)
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Nil(t, err)
|
2019-12-05 12:55:17 +01:00
|
|
|
defer allSubscriptions.Cancel()
|
|
|
|
|
|
|
|
hodlChan := make(chan interface{}, 1)
|
|
|
|
|
|
|
|
amt := lnwire.MilliSatoshi(1000)
|
|
|
|
expiry := uint32(testCurrentHeight + 20)
|
|
|
|
|
2020-01-16 13:13:59 +01:00
|
|
|
// Create key for keysend.
|
2019-12-05 12:55:17 +01:00
|
|
|
preimage := lntypes.Preimage{1, 2, 3}
|
|
|
|
hash := preimage.Hash()
|
|
|
|
|
2020-01-16 13:13:59 +01:00
|
|
|
// Try to settle invoice with an invalid keysend htlc.
|
2019-12-05 12:55:17 +01:00
|
|
|
invalidKeySendPayload := &mockPayload{
|
|
|
|
customRecords: map[uint64][]byte{
|
|
|
|
record.KeySendType: {1, 2, 3},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resolution, err := ctx.registry.NotifyExitHopHtlc(
|
|
|
|
hash, amt, expiry,
|
|
|
|
testCurrentHeight, getCircuitKey(10), hodlChan,
|
|
|
|
invalidKeySendPayload,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-03-25 03:48:59 +01:00
|
|
|
require.NotNil(t, resolution)
|
2019-12-05 12:55:17 +01:00
|
|
|
|
2021-03-25 03:48:59 +01:00
|
|
|
if !keySendEnabled {
|
|
|
|
checkFailResolution(t, resolution, ResultInvoiceNotFound)
|
|
|
|
} else {
|
|
|
|
checkFailResolution(t, resolution, ResultKeySendError)
|
2019-12-05 12:55:17 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 13:13:59 +01:00
|
|
|
// Try to settle invoice with a valid keysend htlc.
|
2019-12-05 12:55:17 +01:00
|
|
|
keySendPayload := &mockPayload{
|
|
|
|
customRecords: map[uint64][]byte{
|
|
|
|
record.KeySendType: preimage[:],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resolution, err = ctx.registry.NotifyExitHopHtlc(
|
|
|
|
hash, amt, expiry,
|
|
|
|
testCurrentHeight, getCircuitKey(10), hodlChan, keySendPayload,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-01-16 13:13:59 +01:00
|
|
|
// Expect a cancel resolution if keysend is disabled.
|
2019-12-05 12:55:17 +01:00
|
|
|
if !keySendEnabled {
|
2021-03-25 03:48:59 +01:00
|
|
|
checkFailResolution(t, resolution, ResultInvoiceNotFound)
|
2019-12-05 12:55:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-26 11:17:18 +02:00
|
|
|
checkSubscription := func() {
|
|
|
|
// We expect a new invoice notification to be sent out.
|
|
|
|
newInvoice := <-allSubscriptions.NewInvoices
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Equal(t, newInvoice.State, channeldb.ContractOpen)
|
2020-06-26 11:17:18 +02:00
|
|
|
|
|
|
|
// We expect a settled notification to be sent out.
|
|
|
|
settledInvoice := <-allSubscriptions.SettledInvoices
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Equal(t, settledInvoice.State, channeldb.ContractSettled)
|
2019-12-05 12:55:17 +01:00
|
|
|
}
|
|
|
|
|
2021-03-25 03:48:59 +01:00
|
|
|
checkSettleResolution(t, resolution, preimage)
|
2020-06-26 11:17:18 +02:00
|
|
|
checkSubscription()
|
|
|
|
|
|
|
|
// Replay the same keysend payment. We expect an identical resolution,
|
|
|
|
// but no event should be generated.
|
|
|
|
resolution, err = ctx.registry.NotifyExitHopHtlc(
|
|
|
|
hash, amt, expiry,
|
|
|
|
testCurrentHeight, getCircuitKey(10), hodlChan, keySendPayload,
|
|
|
|
)
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Nil(t, err)
|
2021-03-25 03:48:59 +01:00
|
|
|
checkSettleResolution(t, resolution, preimage)
|
2020-06-26 11:17:18 +02:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-allSubscriptions.NewInvoices:
|
|
|
|
t.Fatalf("replayed keysend should not generate event")
|
|
|
|
case <-time.After(time.Second):
|
2019-12-05 12:55:17 +01:00
|
|
|
}
|
|
|
|
|
2020-06-26 11:17:18 +02:00
|
|
|
// Finally, test that we can properly fulfill a second keysend payment
|
|
|
|
// with a unique preiamge.
|
|
|
|
preimage2 := lntypes.Preimage{1, 2, 3, 4}
|
|
|
|
hash2 := preimage2.Hash()
|
|
|
|
|
|
|
|
keySendPayload2 := &mockPayload{
|
|
|
|
customRecords: map[uint64][]byte{
|
|
|
|
record.KeySendType: preimage2[:],
|
|
|
|
},
|
2019-12-05 12:55:17 +01:00
|
|
|
}
|
2020-06-26 11:17:18 +02:00
|
|
|
|
|
|
|
resolution, err = ctx.registry.NotifyExitHopHtlc(
|
|
|
|
hash2, amt, expiry,
|
|
|
|
testCurrentHeight, getCircuitKey(20), hodlChan, keySendPayload2,
|
|
|
|
)
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Nil(t, err)
|
2020-06-26 11:17:18 +02:00
|
|
|
|
2021-03-25 03:48:59 +01:00
|
|
|
checkSettleResolution(t, resolution, preimage2)
|
2020-06-26 11:17:18 +02:00
|
|
|
checkSubscription()
|
2019-12-05 12:55:17 +01:00
|
|
|
}
|
|
|
|
|
2020-04-09 10:42:10 +02:00
|
|
|
// TestHoldKeysend tests receiving a spontaneous payment that is held.
|
|
|
|
func TestHoldKeysend(t *testing.T) {
|
|
|
|
t.Run("settle", func(t *testing.T) {
|
|
|
|
testHoldKeysend(t, false)
|
|
|
|
})
|
|
|
|
t.Run("timeout", func(t *testing.T) {
|
|
|
|
testHoldKeysend(t, true)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// testHoldKeysend is the inner test function that tests hold-keysend.
|
|
|
|
func testHoldKeysend(t *testing.T, timeoutKeysend bool) {
|
|
|
|
defer timeout()()
|
|
|
|
|
|
|
|
const holdDuration = time.Minute
|
|
|
|
|
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
|
|
|
|
|
|
|
ctx.registry.cfg.AcceptKeySend = true
|
|
|
|
ctx.registry.cfg.KeysendHoldTime = holdDuration
|
|
|
|
|
|
|
|
allSubscriptions, err := ctx.registry.SubscribeNotifications(0, 0)
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Nil(t, err)
|
2020-04-09 10:42:10 +02:00
|
|
|
defer allSubscriptions.Cancel()
|
|
|
|
|
|
|
|
hodlChan := make(chan interface{}, 1)
|
|
|
|
|
|
|
|
amt := lnwire.MilliSatoshi(1000)
|
|
|
|
expiry := uint32(testCurrentHeight + 20)
|
|
|
|
|
|
|
|
// Create key for keysend.
|
|
|
|
preimage := lntypes.Preimage{1, 2, 3}
|
|
|
|
hash := preimage.Hash()
|
|
|
|
|
|
|
|
// Try to settle invoice with a valid keysend htlc.
|
|
|
|
keysendPayload := &mockPayload{
|
|
|
|
customRecords: map[uint64][]byte{
|
|
|
|
record.KeySendType: preimage[:],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
resolution, err := ctx.registry.NotifyExitHopHtlc(
|
|
|
|
hash, amt, expiry,
|
|
|
|
testCurrentHeight, getCircuitKey(10), hodlChan, keysendPayload,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// No immediate resolution is expected.
|
|
|
|
require.Nil(t, resolution, "expected hold resolution")
|
|
|
|
|
|
|
|
// We expect a new invoice notification to be sent out.
|
|
|
|
newInvoice := <-allSubscriptions.NewInvoices
|
|
|
|
if newInvoice.State != channeldb.ContractOpen {
|
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
|
|
|
newInvoice.State)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect no further invoice notifications yet (on the all invoices
|
|
|
|
// subscription).
|
|
|
|
select {
|
|
|
|
case <-allSubscriptions.NewInvoices:
|
|
|
|
t.Fatalf("no invoice update expected")
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
if timeoutKeysend {
|
|
|
|
// Advance the clock to just past the hold duration.
|
|
|
|
ctx.clock.SetTime(ctx.clock.Now().Add(
|
|
|
|
holdDuration + time.Millisecond),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Expect the keysend payment to be failed.
|
|
|
|
res := <-hodlChan
|
|
|
|
failResolution, ok := res.(*HtlcFailResolution)
|
|
|
|
require.Truef(
|
|
|
|
t, ok, "expected fail resolution, got: %T",
|
|
|
|
resolution,
|
|
|
|
)
|
|
|
|
require.Equal(
|
|
|
|
t, ResultCanceled, failResolution.Outcome,
|
|
|
|
"expected keysend payment to be failed",
|
|
|
|
)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Settle keysend payment manually.
|
|
|
|
require.Nil(t, ctx.registry.SettleHodlInvoice(
|
|
|
|
*newInvoice.Terms.PaymentPreimage,
|
|
|
|
))
|
|
|
|
|
|
|
|
// We expect a settled notification to be sent out.
|
|
|
|
settledInvoice := <-allSubscriptions.SettledInvoices
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Equal(t, settledInvoice.State, channeldb.ContractSettled)
|
2020-04-09 10:42:10 +02:00
|
|
|
}
|
|
|
|
|
2019-12-20 11:25:07 +01:00
|
|
|
// TestMppPayment tests settling of an invoice with multiple partial payments.
|
|
|
|
// It covers the case where there is a mpp timeout before the whole invoice is
|
|
|
|
// paid and the case where the invoice is settled in time.
|
|
|
|
func TestMppPayment(t *testing.T) {
|
2019-12-09 17:50:11 +01:00
|
|
|
defer timeout()()
|
2019-09-03 12:23:39 +02:00
|
|
|
|
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
|
|
|
|
|
|
|
// Add the invoice.
|
2019-11-20 18:06:51 +01:00
|
|
|
_, err := ctx.registry.AddInvoice(testInvoice, testInvoicePaymentHash)
|
2019-09-03 12:23:39 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mppPayload := &mockPayload{
|
|
|
|
mpp: record.NewMPP(testInvoiceAmt, [32]byte{}),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send htlc 1.
|
|
|
|
hodlChan1 := make(chan interface{}, 1)
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err := ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, testInvoice.Terms.Value/2,
|
2019-09-03 12:23:39 +02:00
|
|
|
testHtlcExpiry,
|
|
|
|
testCurrentHeight, getCircuitKey(10), hodlChan1, mppPayload,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-20 11:25:07 +01:00
|
|
|
if resolution != nil {
|
2019-09-03 12:23:39 +02:00
|
|
|
t.Fatal("expected no direct resolution")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simulate mpp timeout releasing htlc 1.
|
2019-11-25 11:46:29 +01:00
|
|
|
ctx.clock.SetTime(testTime.Add(30 * time.Second))
|
2019-09-03 12:23:39 +02:00
|
|
|
|
2019-12-20 11:25:07 +01:00
|
|
|
htlcResolution := (<-hodlChan1).(HtlcResolution)
|
2020-02-06 18:35:10 +01:00
|
|
|
failResolution, ok := htlcResolution.(*HtlcFailResolution)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected fail resolution, got: %T",
|
|
|
|
resolution)
|
2019-09-03 12:23:39 +02:00
|
|
|
}
|
2020-02-06 18:35:10 +01:00
|
|
|
if failResolution.Outcome != ResultMppTimeout {
|
2019-12-20 11:25:07 +01:00
|
|
|
t.Fatalf("expected mpp timeout, got: %v",
|
2020-02-06 18:35:10 +01:00
|
|
|
failResolution.Outcome)
|
2019-12-20 11:25:07 +01:00
|
|
|
}
|
2019-09-03 12:23:39 +02:00
|
|
|
|
|
|
|
// Send htlc 2.
|
|
|
|
hodlChan2 := make(chan interface{}, 1)
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err = ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, testInvoice.Terms.Value/2,
|
2019-09-03 12:23:39 +02:00
|
|
|
testHtlcExpiry,
|
|
|
|
testCurrentHeight, getCircuitKey(11), hodlChan2, mppPayload,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-12-20 11:25:07 +01:00
|
|
|
if resolution != nil {
|
2019-09-03 12:23:39 +02:00
|
|
|
t.Fatal("expected no direct resolution")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send htlc 3.
|
|
|
|
hodlChan3 := make(chan interface{}, 1)
|
2019-12-20 11:25:07 +01:00
|
|
|
resolution, err = ctx.registry.NotifyExitHopHtlc(
|
2019-11-20 18:06:51 +01:00
|
|
|
testInvoicePaymentHash, testInvoice.Terms.Value/2,
|
2019-09-03 12:23:39 +02:00
|
|
|
testHtlcExpiry,
|
|
|
|
testCurrentHeight, getCircuitKey(12), hodlChan3, mppPayload,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-02-06 18:35:10 +01:00
|
|
|
settleResolution, ok := resolution.(*HtlcSettleResolution)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected settle resolution, got: %T",
|
|
|
|
htlcResolution)
|
2019-09-03 12:23:39 +02:00
|
|
|
}
|
2020-02-06 18:35:10 +01:00
|
|
|
if settleResolution.Outcome != ResultSettled {
|
2019-12-20 11:25:07 +01:00
|
|
|
t.Fatalf("expected result settled, got: %v",
|
2020-02-06 18:35:10 +01:00
|
|
|
settleResolution.Outcome)
|
2019-12-20 11:25:07 +01:00
|
|
|
}
|
2019-09-03 12:23:39 +02:00
|
|
|
|
|
|
|
// Check that settled amount is equal to the sum of values of the htlcs
|
2019-12-20 11:25:07 +01:00
|
|
|
// 2 and 3.
|
2019-11-20 18:06:51 +01:00
|
|
|
inv, err := ctx.registry.LookupInvoice(testInvoicePaymentHash)
|
2019-09-03 12:23:39 +02: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 17:50:11 +01:00
|
|
|
|
|
|
|
// Tests that invoices are canceled after expiration.
|
|
|
|
func TestInvoiceExpiryWithRegistry(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
2020-01-20 10:57:34 +01:00
|
|
|
cdb, cleanup, err := newTestChannelDB(clock.NewTestClock(time.Time{}))
|
2019-12-09 17:50:11 +01:00
|
|
|
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.
|
2019-12-14 10:37:23 +01:00
|
|
|
time.Sleep(500 * time.Millisecond)
|
2019-12-09 17:50:11 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 23:39:11 +02:00
|
|
|
|
|
|
|
// TestOldInvoiceRemovalOnStart tests that we'll attempt to remove old canceled
|
|
|
|
// invoices upon start while keeping all settled ones.
|
|
|
|
func TestOldInvoiceRemovalOnStart(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
testClock := clock.NewTestClock(testTime)
|
|
|
|
cdb, cleanup, err := newTestChannelDB(testClock)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
cfg := RegistryConfig{
|
2020-07-28 23:57:51 +02:00
|
|
|
FinalCltvRejectDelta: testFinalCltvRejectDelta,
|
|
|
|
Clock: testClock,
|
|
|
|
GcCanceledInvoicesOnStartup: true,
|
2020-07-28 23:39:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
expiryWatcher := NewInvoiceExpiryWatcher(cfg.Clock)
|
|
|
|
registry := NewRegistry(cdb, expiryWatcher, &cfg)
|
|
|
|
|
|
|
|
// First prefill the Channel DB with some pre-existing expired invoices.
|
|
|
|
const numExpired = 5
|
|
|
|
const numPending = 0
|
|
|
|
existingInvoices := generateInvoiceExpiryTestData(
|
|
|
|
t, testTime, 0, numExpired, numPending,
|
|
|
|
)
|
|
|
|
|
|
|
|
i := 0
|
|
|
|
for paymentHash, invoice := range existingInvoices.expiredInvoices {
|
|
|
|
// Mark half of the invoices as settled, the other hald as
|
|
|
|
// canceled.
|
|
|
|
if i%2 == 0 {
|
|
|
|
invoice.State = channeldb.ContractSettled
|
|
|
|
} else {
|
|
|
|
invoice.State = channeldb.ContractCanceled
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := cdb.AddInvoice(invoice, paymentHash)
|
|
|
|
require.NoError(t, err)
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect all settled invoices for our expectation set.
|
|
|
|
var expected []channeldb.Invoice
|
|
|
|
|
|
|
|
// Perform a scan query to collect all invoices.
|
|
|
|
query := channeldb.InvoiceQuery{
|
|
|
|
IndexOffset: 0,
|
|
|
|
NumMaxInvoices: math.MaxUint64,
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := cdb.QueryInvoices(query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Save all settled invoices for our expectation set.
|
|
|
|
for _, invoice := range response.Invoices {
|
|
|
|
if invoice.State == channeldb.ContractSettled {
|
|
|
|
expected = append(expected, invoice)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start the registry which should collect and delete all canceled
|
|
|
|
// invoices upon start.
|
|
|
|
err = registry.Start()
|
|
|
|
require.NoError(t, err, "cannot start the registry")
|
|
|
|
|
|
|
|
// Perform a scan query to collect all invoices.
|
|
|
|
response, err = cdb.QueryInvoices(query)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check that we really only kept the settled invoices after the
|
|
|
|
// registry start.
|
|
|
|
require.Equal(t, expected, response.Invoices)
|
|
|
|
}
|
2020-11-07 04:20:46 +01:00
|
|
|
|
|
|
|
// TestSettleInvoicePaymentAddrRequired tests that if an incoming payment has
|
|
|
|
// an invoice that requires the payment addr bit to be set, and the incoming
|
|
|
|
// payment doesn't include an mpp payload, then the payment is rejected.
|
|
|
|
func TestSettleInvoicePaymentAddrRequired(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
|
|
|
|
|
|
|
allSubscriptions, err := ctx.registry.SubscribeNotifications(0, 0)
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Nil(t, err)
|
2020-11-07 04:20:46 +01:00
|
|
|
defer allSubscriptions.Cancel()
|
|
|
|
|
|
|
|
// Subscribe to the not yet existing invoice.
|
|
|
|
subscription, err := ctx.registry.SubscribeSingleInvoice(
|
|
|
|
testInvoicePaymentHash,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer subscription.Cancel()
|
|
|
|
|
|
|
|
require.Equal(
|
2021-03-25 03:48:32 +01:00
|
|
|
t, subscription.invoiceRef.PayHash(), &testInvoicePaymentHash,
|
2020-11-07 04:20:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Add the invoice, which requires the MPP payload to always be
|
|
|
|
// included due to its set of feature bits.
|
|
|
|
addIdx, err := ctx.registry.AddInvoice(
|
|
|
|
testPayAddrReqInvoice, testInvoicePaymentHash,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int(addIdx), 1)
|
|
|
|
|
|
|
|
// We expect the open state to be sent to the single invoice subscriber.
|
|
|
|
select {
|
|
|
|
case update := <-subscription.Updates:
|
|
|
|
if update.State != channeldb.ContractOpen {
|
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
|
|
|
update.State)
|
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect a new invoice notification to be sent out.
|
|
|
|
select {
|
|
|
|
case newInvoice := <-allSubscriptions.NewInvoices:
|
|
|
|
if newInvoice.State != channeldb.ContractOpen {
|
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
|
|
|
newInvoice.State)
|
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
|
|
|
hodlChan := make(chan interface{}, 1)
|
|
|
|
|
|
|
|
// Now try to settle the invoice, the testPayload doesn't have any mpp
|
|
|
|
// information, so it should be forced to the updateLegacy path then
|
|
|
|
// fail as a required feature bit exists.
|
|
|
|
resolution, err := ctx.registry.NotifyExitHopHtlc(
|
|
|
|
testInvoicePaymentHash, testInvoice.Terms.Value,
|
|
|
|
uint32(testCurrentHeight)+testInvoiceCltvDelta-1,
|
|
|
|
testCurrentHeight, getCircuitKey(10), hodlChan, testPayload,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
failResolution, ok := resolution.(*HtlcFailResolution)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected fail resolution, got: %T",
|
|
|
|
resolution)
|
|
|
|
}
|
|
|
|
require.Equal(t, failResolution.AcceptHeight, testCurrentHeight)
|
|
|
|
require.Equal(t, failResolution.Outcome, ResultAddressMismatch)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestSettleInvoicePaymentAddrRequiredOptionalGrace tests that if an invoice
|
|
|
|
// in the database has an optional payment addr required bit set, then we'll
|
|
|
|
// still allow it to be paid by an incoming HTLC that doesn't include the MPP
|
|
|
|
// payload. This ensures we don't break payment for any invoices in the wild.
|
|
|
|
func TestSettleInvoicePaymentAddrRequiredOptionalGrace(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
ctx := newTestContext(t)
|
|
|
|
defer ctx.cleanup()
|
|
|
|
|
|
|
|
allSubscriptions, err := ctx.registry.SubscribeNotifications(0, 0)
|
2021-03-03 19:00:28 +01:00
|
|
|
require.Nil(t, err)
|
2020-11-07 04:20:46 +01:00
|
|
|
defer allSubscriptions.Cancel()
|
|
|
|
|
|
|
|
// Subscribe to the not yet existing invoice.
|
|
|
|
subscription, err := ctx.registry.SubscribeSingleInvoice(
|
|
|
|
testInvoicePaymentHash,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer subscription.Cancel()
|
|
|
|
|
|
|
|
require.Equal(
|
2021-03-25 03:48:32 +01:00
|
|
|
t, subscription.invoiceRef.PayHash(), &testInvoicePaymentHash,
|
2020-11-07 04:20:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Add the invoice, which requires the MPP payload to always be
|
|
|
|
// included due to its set of feature bits.
|
|
|
|
addIdx, err := ctx.registry.AddInvoice(
|
|
|
|
testPayAddrOptionalInvoice, testInvoicePaymentHash,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int(addIdx), 1)
|
|
|
|
|
|
|
|
// We expect the open state to be sent to the single invoice
|
|
|
|
// subscriber.
|
|
|
|
select {
|
|
|
|
case update := <-subscription.Updates:
|
|
|
|
if update.State != channeldb.ContractOpen {
|
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
|
|
|
update.State)
|
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We expect a new invoice notification to be sent out.
|
|
|
|
select {
|
|
|
|
case newInvoice := <-allSubscriptions.NewInvoices:
|
|
|
|
if newInvoice.State != channeldb.ContractOpen {
|
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
|
|
|
newInvoice.State)
|
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll now attempt to settle the invoice as normal, this should work
|
|
|
|
// no problem as we should allow these existing invoices to be settled.
|
|
|
|
hodlChan := make(chan interface{}, 1)
|
|
|
|
resolution, err := ctx.registry.NotifyExitHopHtlc(
|
|
|
|
testInvoicePaymentHash, testInvoiceAmt,
|
|
|
|
testHtlcExpiry, testCurrentHeight,
|
|
|
|
getCircuitKey(10), hodlChan, testPayload,
|
|
|
|
)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
settleResolution, ok := resolution.(*HtlcSettleResolution)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("expected settle resolution, got: %T",
|
|
|
|
resolution)
|
|
|
|
}
|
|
|
|
require.Equal(t, settleResolution.Outcome, ResultSettled)
|
|
|
|
|
|
|
|
// We expect the settled state to be sent to the single invoice
|
|
|
|
// subscriber.
|
|
|
|
select {
|
|
|
|
case update := <-subscription.Updates:
|
|
|
|
if update.State != channeldb.ContractSettled {
|
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
|
|
|
update.State)
|
|
|
|
}
|
|
|
|
if update.AmtPaid != testInvoice.Terms.Value {
|
|
|
|
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:
|
|
|
|
if settledInvoice.State != channeldb.ContractSettled {
|
|
|
|
t.Fatalf("expected state ContractOpen, but got %v",
|
|
|
|
settledInvoice.State)
|
|
|
|
}
|
|
|
|
case <-time.After(testTimeout):
|
|
|
|
t.Fatal("no update received")
|
|
|
|
}
|
|
|
|
}
|