From 18c025151a934af046564e6c97dea32c8e830240 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Mon, 10 Jun 2019 12:02:06 +0200 Subject: [PATCH] invoices+channeldb: move invoice state check to invoiceregistry --- channeldb/invoice_test.go | 4 ++++ channeldb/invoices.go | 11 ----------- invoices/invoiceregistry.go | 9 +++++++++ 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/channeldb/invoice_test.go b/channeldb/invoice_test.go index 61e0b5d0..288edfef 100644 --- a/channeldb/invoice_test.go +++ b/channeldb/invoice_test.go @@ -662,5 +662,9 @@ func TestQueryInvoices(t *testing.T) { } func checkHtlcParameters(invoice *Invoice) error { + if invoice.Terms.State == ContractSettled { + return ErrInvoiceAlreadySettled + } + return nil } diff --git a/channeldb/invoices.go b/channeldb/invoices.go index 95e7c9fb..2fb490e9 100644 --- a/channeldb/invoices.go +++ b/channeldb/invoices.go @@ -1003,17 +1003,6 @@ func acceptOrSettleInvoice(invoices, settleIndex *bbolt.Bucket, return nil, err } - state := invoice.Terms.State - - switch { - case state == ContractAccepted: - return &invoice, ErrInvoiceAlreadyAccepted - case state == ContractSettled: - return &invoice, ErrInvoiceAlreadySettled - case state == ContractCanceled: - return &invoice, ErrInvoiceAlreadyCanceled - } - // If the invoice is still open, check the htlc parameters. if err := checkHtlcParameters(&invoice); err != nil { return &invoice, err diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index 5fe17d04..0b0b0bc9 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -526,6 +526,15 @@ func (i *InvoiceRegistry) LookupInvoice(rHash lntypes.Hash) (channeldb.Invoice, func (i *InvoiceRegistry) checkHtlcParameters(invoice *channeldb.Invoice, amtPaid lnwire.MilliSatoshi, htlcExpiry uint32, currentHeight int32) error { + switch invoice.Terms.State { + case channeldb.ContractAccepted: + return channeldb.ErrInvoiceAlreadyAccepted + case channeldb.ContractSettled: + return channeldb.ErrInvoiceAlreadySettled + case channeldb.ContractCanceled: + return channeldb.ErrInvoiceAlreadyCanceled + } + expiry, err := i.decodeFinalCltvExpiry(string(invoice.PaymentRequest)) if err != nil { return err