From 7e2f5a184be09ab9265aecd183becd4e180ea333 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Wed, 24 Mar 2021 19:48:51 -0700 Subject: [PATCH] channeldb: validate feature dependencies when adding invoice --- channeldb/invoice_test.go | 28 ++++++++++++++++++++++++++++ channeldb/invoices.go | 6 ++++++ invoices/test_utils_test.go | 10 ++++++++-- 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/channeldb/invoice_test.go b/channeldb/invoice_test.go index af1d967b..d1731402 100644 --- a/channeldb/invoice_test.go +++ b/channeldb/invoice_test.go @@ -8,6 +8,7 @@ import ( "time" "github.com/lightningnetwork/lnd/clock" + "github.com/lightningnetwork/lnd/feature" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/record" @@ -1658,3 +1659,30 @@ func TestDeleteInvoices(t *testing.T) { assertInvoiceCount(0) } + +// TestAddInvoiceInvalidFeatureDeps asserts that inserting an invoice with +// invalid transitive feature dependencies fails with the appropriate error. +func TestAddInvoiceInvalidFeatureDeps(t *testing.T) { + t.Parallel() + + db, cleanup, err := MakeTestDB() + require.NoError(t, err, "unable to make test db") + defer cleanup() + + invoice, err := randInvoice(500) + require.NoError(t, err) + + invoice.Terms.Features = lnwire.NewFeatureVector( + lnwire.NewRawFeatureVector( + lnwire.TLVOnionPayloadOptional, + lnwire.MPPOptional, + ), + lnwire.Features, + ) + + hash := invoice.Terms.PaymentPreimage.Hash() + _, err = db.AddInvoice(invoice, hash) + require.Error(t, err, feature.NewErrMissingFeatureDep( + lnwire.PaymentAddrOptional, + )) +} diff --git a/channeldb/invoices.go b/channeldb/invoices.go index 5a607ce9..a4ddd950 100644 --- a/channeldb/invoices.go +++ b/channeldb/invoices.go @@ -10,6 +10,7 @@ import ( "time" "github.com/lightningnetwork/lnd/channeldb/kvdb" + "github.com/lightningnetwork/lnd/feature" "github.com/lightningnetwork/lnd/htlcswitch/hop" "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwire" @@ -674,6 +675,11 @@ func validateInvoice(i *Invoice, paymentHash lntypes.Hash) error { return errors.New("invoice must have a feature vector") } + err := feature.ValidateDeps(i.Terms.Features) + if err != nil { + return err + } + if i.Terms.PaymentPreimage == nil && !i.HodlInvoice { return errors.New("non-hodl invoices must have a preimage") } diff --git a/invoices/test_utils_test.go b/invoices/test_utils_test.go index 73d41346..0a2d8fa3 100644 --- a/invoices/test_utils_test.go +++ b/invoices/test_utils_test.go @@ -111,7 +111,10 @@ var ( Value: testInvoiceAmt, Expiry: time.Hour, Features: lnwire.NewFeatureVector( - lnwire.NewRawFeatureVector(lnwire.PaymentAddrRequired), + lnwire.NewRawFeatureVector( + lnwire.TLVOnionPayloadOptional, + lnwire.PaymentAddrRequired, + ), lnwire.Features, ), }, @@ -124,7 +127,10 @@ var ( Value: testInvoiceAmt, Expiry: time.Hour, Features: lnwire.NewFeatureVector( - lnwire.NewRawFeatureVector(lnwire.PaymentAddrOptional), + lnwire.NewRawFeatureVector( + lnwire.TLVOnionPayloadOptional, + lnwire.PaymentAddrOptional, + ), lnwire.Features, ), },