From d29f2fe4f9ddadf43a7fb447fa214cad13b4b0f8 Mon Sep 17 00:00:00 2001 From: carla Date: Fri, 23 Apr 2021 08:19:55 +0200 Subject: [PATCH] invoices: refactor - move cancel check into separate function Useful for tests which need to mock cancelInvoiceImpl --- invoices/invoiceregistry.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index 3ef83a98..c457acd5 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -1176,6 +1176,20 @@ func (i *InvoiceRegistry) CancelInvoice(payHash lntypes.Hash) error { return i.cancelInvoiceImpl(payHash, true) } +// shouldCancel examines the state of an invoice and whether we want to +// cancel already accepted invoices, taking our force cancel boolean into +// account. This is pulled out into its own function so that tests that mock +// cancelInvoiceImpl can reuse this logic. +func shouldCancel(state channeldb.ContractState, cancelAccepted bool) bool { + if state != channeldb.ContractAccepted { + return true + } + + // If the invoice is accepted, we should only cancel if we want to + // force cancelation of accepted invoices. + return cancelAccepted +} + // cancelInvoice attempts to cancel the invoice corresponding to the passed // payment hash. Accepted invoices will only be canceled if explicitly // requested to do so. It notifies subscribing links and resolvers that @@ -1192,9 +1206,7 @@ func (i *InvoiceRegistry) cancelInvoiceImpl(payHash lntypes.Hash, updateInvoice := func(invoice *channeldb.Invoice) ( *channeldb.InvoiceUpdateDesc, error) { - // Only cancel the invoice in ContractAccepted state if explicitly - // requested to do so. - if invoice.State == channeldb.ContractAccepted && !cancelAccepted { + if !shouldCancel(invoice.State, cancelAccepted) { return nil, nil }