From da049ebcf7831c23145ce277ad988e08857e5315 Mon Sep 17 00:00:00 2001 From: Conner Fromknecht Date: Wed, 3 Mar 2021 09:56:28 -0800 Subject: [PATCH] channeldb: ensure invoices can't be added with HTLCs --- channeldb/invoice_test.go | 18 ++++++++++++++++++ channeldb/invoices.go | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/channeldb/invoice_test.go b/channeldb/invoice_test.go index 4e01f52e..a66d9f25 100644 --- a/channeldb/invoice_test.go +++ b/channeldb/invoice_test.go @@ -1304,6 +1304,24 @@ func TestHTLCSet(t *testing.T) { } } +// TestAddInvoiceWithHTLCs asserts that you can't insert an invoice that already +// has HTLCs. +func TestAddInvoiceWithHTLCs(t *testing.T) { + db, cleanUp, err := MakeTestDB() + defer cleanUp() + require.Nil(t, err) + + testInvoice, err := randInvoice(1000) + require.Nil(t, err) + + key := CircuitKey{HtlcID: 1} + testInvoice.Htlcs[key] = &InvoiceHTLC{} + + payHash := testInvoice.Terms.PaymentPreimage.Hash() + _, err = db.AddInvoice(testInvoice, payHash) + require.Equal(t, ErrInvoiceHasHtlcs, err) +} + // TestDeleteInvoices tests that deleting a list of invoices will succeed // if all delete references are valid, or will fail otherwise. func TestDeleteInvoices(t *testing.T) { diff --git a/channeldb/invoices.go b/channeldb/invoices.go index bcff0629..9f867ea8 100644 --- a/channeldb/invoices.go +++ b/channeldb/invoices.go @@ -106,6 +106,10 @@ var ( // ErrInvoicePreimageMismatch is returned when the preimage doesn't // match the invoice hash. ErrInvoicePreimageMismatch = errors.New("preimage does not match") + + // ErrInvoiceHasHtlcs is returned when attempting to insert an invoice + // that already has HTLCs. + ErrInvoiceHasHtlcs = errors.New("cannot add invoice with htlcs") ) const ( @@ -588,6 +592,11 @@ func validateInvoice(i *Invoice, paymentHash lntypes.Hash) error { if i.Terms.PaymentPreimage == nil && !i.HodlInvoice { return errors.New("non-hodl invoices must have a preimage") } + + if len(i.Htlcs) > 0 { + return ErrInvoiceHasHtlcs + } + return nil }