channeldb: ensure invoices can't be added with HTLCs

This commit is contained in:
Conner Fromknecht 2021-03-03 09:56:28 -08:00
parent b2234703f6
commit da049ebcf7
No known key found for this signature in database
GPG Key ID: E7D737B67FA592C7
2 changed files with 27 additions and 0 deletions

@ -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) {

@ -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
}