multi: extend InvoiceDB methods with a context argument (#8066)

* multi: extend InvoiceDB methods with a context argument

This commit adds a context to InvoiceDB's methods. Along this refactor
we also extend InvoiceRegistry methods with contexts where it makes
sense. This change is essential to be able to provide kvdb and sqldb
implementations for InvoiceDB.

* channeldb: restrict invoice tests to only use an InvoiceDB instance

* docs: update release notes for 0.18.0
This commit is contained in:
András Bánki-Horváth authored and GitHub committed 2023-10-11 13:42:59 +02:00
1 parent 9478b85e59
commit ad5cd9c8bb
18 files changed
+458 -328

No files matched your search

+5 -3
View File
@@ -1,6 +1,8 @@
package htlcswitch
import (
"context"
"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
@@ -17,7 +19,7 @@ import (
type InvoiceDatabase interface {
// LookupInvoice attempts to look up an invoice according to its 32
// byte payment hash.
LookupInvoice(lntypes.Hash) (invoices.Invoice, error)
LookupInvoice(context.Context, lntypes.Hash) (invoices.Invoice, error)
// NotifyExitHopHtlc attempts to mark an invoice as settled. If the
// invoice is a debug invoice, then this method is a noop as debug
@@ -33,10 +35,10 @@ type InvoiceDatabase interface {
// CancelInvoice attempts to cancel the invoice corresponding to the
// passed payment hash.
CancelInvoice(payHash lntypes.Hash) error
CancelInvoice(ctx context.Context, payHash lntypes.Hash) error
// SettleHodlInvoice settles a hold invoice.
SettleHodlInvoice(preimage lntypes.Preimage) error
SettleHodlInvoice(ctx context.Context, preimage lntypes.Preimage) error
// HodlUnsubscribeAll unsubscribes from all htlc resolutions.
HodlUnsubscribeAll(subscriber chan<- interface{})
+55 -25
View File
@@ -2,6 +2,7 @@ package htlcswitch
import (
"bytes"
"context"
"crypto/rand"
"crypto/sha256"
"encoding/binary"
@@ -483,7 +484,9 @@ func TestChannelLinkSingleHopPayment(t *testing.T) {
// Check that alice invoice was settled and bandwidth of HTLC
// links was changed.
invoice, err := receiver.registry.LookupInvoice(rhash)
invoice, err := receiver.registry.LookupInvoice(
context.Background(), rhash,
)
require.NoError(t, err, "unable to get invoice")
if invoice.State != invpkg.ContractSettled {
t.Fatal("alice invoice wasn't settled")
@@ -601,7 +604,9 @@ func testChannelLinkMultiHopPayment(t *testing.T,
// Check that Carol invoice was settled and bandwidth of HTLC
// links were changed.
invoice, err := receiver.registry.LookupInvoice(rhash)
invoice, err := receiver.registry.LookupInvoice(
context.Background(), rhash,
)
require.NoError(t, err, "unable to get invoice")
if invoice.State != invpkg.ContractSettled {
t.Fatal("carol invoice haven't been settled")
@@ -710,7 +715,9 @@ func TestChannelLinkCancelFullCommitment(t *testing.T) {
// invoice registry at this point, so we poll until we are able
// to settle.
err = wait.NoError(func() error {
return n.bobServer.registry.SettleHodlInvoice(preimage)
return n.bobServer.registry.SettleHodlInvoice(
context.Background(), preimage,
)
}, time.Minute)
if err != nil {
t.Fatal(err)
@@ -1177,7 +1184,9 @@ func TestUpdateForwardingPolicy(t *testing.T) {
// Carol's invoice should now be shown as settled as the payment
// succeeded.
invoice, err := n.carolServer.registry.LookupInvoice(payResp)
invoice, err := n.carolServer.registry.LookupInvoice(
context.Background(), payResp,
)
require.NoError(t, err, "unable to get invoice")
if invoice.State != invpkg.ContractSettled {
t.Fatal("carol invoice haven't been settled")
@@ -1331,7 +1340,9 @@ func TestChannelLinkMultiHopInsufficientPayment(t *testing.T) {
// Check that alice invoice wasn't settled and bandwidth of htlc
// links hasn't been changed.
invoice, err := receiver.registry.LookupInvoice(rhash)
invoice, err := receiver.registry.LookupInvoice(
context.Background(), rhash,
)
require.NoError(t, err, "unable to get invoice")
if invoice.State == invpkg.ContractSettled {
t.Fatal("carol invoice have been settled")
@@ -1509,7 +1520,9 @@ func TestChannelLinkMultiHopUnknownNextHop(t *testing.T) {
// Check that alice invoice wasn't settled and bandwidth of htlc
// links hasn't been changed.
invoice, err := receiver.registry.LookupInvoice(rhash)
invoice, err := receiver.registry.LookupInvoice(
context.Background(), rhash,
)
require.NoError(t, err, "unable to get invoice")
if invoice.State == invpkg.ContractSettled {
t.Fatal("carol invoice have been settled")
@@ -1617,7 +1630,9 @@ func TestChannelLinkMultiHopDecodeError(t *testing.T) {
// Check that alice invoice wasn't settled and bandwidth of htlc
// links hasn't been changed.
invoice, err := receiver.registry.LookupInvoice(rhash)
invoice, err := receiver.registry.LookupInvoice(
context.Background(), rhash,
)
require.NoError(t, err, "unable to get invoice")
if invoice.State == invpkg.ContractSettled {
t.Fatal("carol invoice have been settled")
@@ -2406,10 +2421,11 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) {
)
require.NoError(t, err, "unable to create payment")
ctxb := context.Background()
// We must add the invoice to the registry, such that Alice expects
// this payment.
err = coreLink.cfg.Registry.(*mockInvoiceRegistry).AddInvoice(
*invoice, htlc.PaymentHash,
ctxb, *invoice, htlc.PaymentHash,
)
require.NoError(t, err, "unable to add invoice to registry")
@@ -2501,7 +2517,7 @@ func TestChannelLinkBandwidthConsistency(t *testing.T) {
)
require.NoError(t, err, "unable to create payment")
err = coreLink.cfg.Registry.(*mockInvoiceRegistry).AddInvoice(
*invoice, htlc.PaymentHash,
ctxb, *invoice, htlc.PaymentHash,
)
require.NoError(t, err, "unable to add invoice to registry")
@@ -3625,7 +3641,9 @@ func TestChannelRetransmission(t *testing.T) {
// Check that alice invoice wasn't settled and
// bandwidth of htlc links hasn't been changed.
invoice, err = receiver.registry.LookupInvoice(rhash)
invoice, err = receiver.registry.LookupInvoice(
context.Background(), rhash,
)
if err != nil {
err = fmt.Errorf(
"unable to get invoice: %w", err,
@@ -4098,7 +4116,9 @@ func TestChannelLinkAcceptDuplicatePayment(t *testing.T) {
t.Fatal(err)
}
err = n.carolServer.registry.AddInvoice(*invoice, htlc.PaymentHash)
err = n.carolServer.registry.AddInvoice(
context.Background(), *invoice, htlc.PaymentHash,
)
require.NoError(t, err, "unable to add invoice in carol registry")
// With the invoice now added to Carol's registry, we'll send the
@@ -4186,7 +4206,9 @@ func TestChannelLinkAcceptOverpay(t *testing.T) {
// Even though we sent 2x what was asked for, Carol should still have
// accepted the payment and marked it as settled.
invoice, err := receiver.registry.LookupInvoice(rhash)
invoice, err := receiver.registry.LookupInvoice(
context.Background(), rhash,
)
require.NoError(t, err, "unable to get invoice")
if invoice.State != invpkg.ContractSettled {
t.Fatal("carol invoice haven't been settled")
@@ -4503,7 +4525,7 @@ func generateHtlc(t *testing.T, coreLink *channelLink,
// We must add the invoice to the registry, such that Alice
// expects this payment.
err := coreLink.cfg.Registry.(*mockInvoiceRegistry).AddInvoice(
*invoice, htlc.PaymentHash,
context.Background(), *invoice, htlc.PaymentHash,
)
require.NoError(t, err, "unable to add invoice to registry")
@@ -5847,7 +5869,7 @@ func TestChannelLinkCanceledInvoice(t *testing.T) {
// Cancel the invoice at bob's end.
hash := invoice.Terms.PaymentPreimage.Hash()
err = n.bobServer.registry.CancelInvoice(hash)
err = n.bobServer.registry.CancelInvoice(context.Background(), hash)
if err != nil {
t.Fatal(err)
}
@@ -5964,7 +5986,9 @@ func TestChannelLinkHoldInvoiceSettle(t *testing.T) {
t.Fatal(err)
}
err = ctx.n.bobServer.registry.SettleHodlInvoice(ctx.preimage)
err = ctx.n.bobServer.registry.SettleHodlInvoice(
context.Background(), ctx.preimage,
)
if err != nil {
t.Fatal(err)
}
@@ -6008,7 +6032,9 @@ func TestChannelLinkHoldInvoiceCancel(t *testing.T) {
t.Fatal(err)
}
err = ctx.n.bobServer.registry.CancelInvoice(ctx.hash)
err = ctx.n.bobServer.registry.CancelInvoice(
context.Background(), ctx.hash,
)
if err != nil {
t.Fatal(err)
}
@@ -6062,7 +6088,7 @@ func TestChannelLinkHoldInvoiceRestart(t *testing.T) {
// We must add the invoice to the registry, such that Alice
// expects this payment.
err = registry.AddInvoice(
*invoice, htlc.PaymentHash,
context.Background(), *invoice, htlc.PaymentHash,
)
require.NoError(t, err, "unable to add invoice to registry")
@@ -6097,7 +6123,7 @@ func TestChannelLinkHoldInvoiceRestart(t *testing.T) {
<-registry.settleChan
// Settle the invoice with the preimage.
err = registry.SettleHodlInvoice(*preimage)
err = registry.SettleHodlInvoice(context.Background(), *preimage)
require.NoError(t, err, "settle hodl invoice")
// Expect alice to send a settle and commitsig message to bob.
@@ -6154,11 +6180,12 @@ func TestChannelLinkRevocationWindowRegular(t *testing.T) {
htlc1, invoice1 := generateHtlcAndInvoice(t, 0)
htlc2, invoice2 := generateHtlcAndInvoice(t, 1)
ctxb := context.Background()
// We must add the invoice to the registry, such that Alice
// expects this payment.
err = registry.AddInvoice(*invoice1, htlc1.PaymentHash)
err = registry.AddInvoice(ctxb, *invoice1, htlc1.PaymentHash)
require.NoError(t, err, "unable to add invoice to registry")
err = registry.AddInvoice(*invoice2, htlc2.PaymentHash)
err = registry.AddInvoice(ctxb, *invoice2, htlc2.PaymentHash)
require.NoError(t, err, "unable to add invoice to registry")
// Lock in htlc 1 on both sides.
@@ -6238,11 +6265,12 @@ func TestChannelLinkRevocationWindowHodl(t *testing.T) {
invoice2.Terms.PaymentPreimage = nil
invoice2.HodlInvoice = true
ctxb := context.Background()
// We must add the invoices to the registry, such that Alice
// expects the payments.
err = registry.AddInvoice(*invoice1, htlc1.PaymentHash)
err = registry.AddInvoice(ctxb, *invoice1, htlc1.PaymentHash)
require.NoError(t, err, "unable to add invoice to registry")
err = registry.AddInvoice(*invoice2, htlc2.PaymentHash)
err = registry.AddInvoice(ctxb, *invoice2, htlc2.PaymentHash)
require.NoError(t, err, "unable to add invoice to registry")
ctx := linkTestContext{
@@ -6281,7 +6309,7 @@ func TestChannelLinkRevocationWindowHodl(t *testing.T) {
}
// Settle invoice 1 with the preimage.
err = registry.SettleHodlInvoice(*preimage1)
err = registry.SettleHodlInvoice(ctxb, *preimage1)
require.NoError(t, err, "settle hodl invoice")
// Expect alice to send a settle and commitsig message to bob. Bob does
@@ -6290,7 +6318,7 @@ func TestChannelLinkRevocationWindowHodl(t *testing.T) {
ctx.receiveCommitSigAliceToBob(1)
// Settle invoice 2 with the preimage.
err = registry.SettleHodlInvoice(*preimage2)
err = registry.SettleHodlInvoice(ctxb, *preimage2)
require.NoError(t, err, "settle hodl invoice")
// Expect alice to send a settle for htlc 2.
@@ -6634,7 +6662,9 @@ func TestPipelineSettle(t *testing.T) {
// Add the invoice to Alice's registry so she expects it.
aliceReg := alice.coreLink.cfg.Registry.(*mockInvoiceRegistry)
err = aliceReg.AddInvoice(*invoice1, htlc1.PaymentHash)
err = aliceReg.AddInvoice(
context.Background(), *invoice1, htlc1.PaymentHash,
)
require.NoError(t, err)
// <---add-----
+13 -10
View File
@@ -2,6 +2,7 @@ package htlcswitch
import (
"bytes"
"context"
"crypto/sha256"
"encoding/binary"
"fmt"
@@ -979,16 +980,16 @@ func newMockRegistry(minDelta uint32) *mockInvoiceRegistry {
}
}
func (i *mockInvoiceRegistry) LookupInvoice(rHash lntypes.Hash) (
invoices.Invoice, error) {
func (i *mockInvoiceRegistry) LookupInvoice(ctx context.Context,
rHash lntypes.Hash) (invoices.Invoice, error) {
return i.registry.LookupInvoice(rHash)
return i.registry.LookupInvoice(ctx, rHash)
}
func (i *mockInvoiceRegistry) SettleHodlInvoice(
preimage lntypes.Preimage) error {
ctx context.Context, preimage lntypes.Preimage) error {
return i.registry.SettleHodlInvoice(preimage)
return i.registry.SettleHodlInvoice(ctx, preimage)
}
func (i *mockInvoiceRegistry) NotifyExitHopHtlc(rhash lntypes.Hash,
@@ -1010,14 +1011,16 @@ func (i *mockInvoiceRegistry) NotifyExitHopHtlc(rhash lntypes.Hash,
return event, nil
}
func (i *mockInvoiceRegistry) CancelInvoice(payHash lntypes.Hash) error {
return i.registry.CancelInvoice(payHash)
func (i *mockInvoiceRegistry) CancelInvoice(ctx context.Context,
payHash lntypes.Hash) error {
return i.registry.CancelInvoice(ctx, payHash)
}
func (i *mockInvoiceRegistry) AddInvoice(invoice invoices.Invoice,
paymentHash lntypes.Hash) error {
func (i *mockInvoiceRegistry) AddInvoice(ctx context.Context,
invoice invoices.Invoice, paymentHash lntypes.Hash) error {
_, err := i.registry.AddInvoice(&invoice, paymentHash)
_, err := i.registry.AddInvoice(ctx, &invoice, paymentHash)
return err
}
+4 -1
View File
@@ -1,6 +1,7 @@
package htlcswitch
import (
"context"
"crypto/rand"
"crypto/sha256"
"fmt"
@@ -3564,7 +3565,9 @@ func (n *threeHopNetwork) sendThreeHopPayment(t *testing.T) (*lnwire.UpdateAddHT
t.Fatal(err)
}
err = n.carolServer.registry.AddInvoice(*invoice, htlc.PaymentHash)
err = n.carolServer.registry.AddInvoice(
context.Background(), *invoice, htlc.PaymentHash,
)
require.NoError(t, err, "unable to add invoice in carol registry")
if err := n.aliceServer.htlcSwitch.SendHTLC(
+7 -2
View File
@@ -2,6 +2,7 @@ package htlcswitch
import (
"bytes"
"context"
crand "crypto/rand"
"crypto/sha256"
"encoding/binary"
@@ -780,7 +781,9 @@ func preparePayment(sendingPeer, receivingPeer lnpeer.Peer,
// Check who is last in the route and add invoice to server registry.
hash := invoice.Terms.PaymentPreimage.Hash()
if err := receiver.registry.AddInvoice(*invoice, hash); err != nil {
if err := receiver.registry.AddInvoice(
context.Background(), *invoice, hash,
); err != nil {
return nil, nil, err
}
@@ -1338,7 +1341,9 @@ func (n *twoHopNetwork) makeHoldPayment(sendingPeer, receivingPeer lnpeer.Peer,
}
// Check who is last in the route and add invoice to server registry.
if err := receiver.registry.AddInvoice(*invoice, rhash); err != nil {
if err := receiver.registry.AddInvoice(
context.Background(), *invoice, rhash,
); err != nil {
paymentErr <- err
return paymentErr
}