From 08df7f417568d26cc17b1b1baf19df6ad9f9ca49 Mon Sep 17 00:00:00 2001 From: Andras Banki-Horvath Date: Fri, 24 Nov 2023 17:31:11 +0100 Subject: [PATCH] invoices: change cancelSingleHtlc to be purely used for validation This change moves the HTLC state change out of the cancelSingleHtlc function. This is part of the larger refactor of collecting all changes to be later applied by the invoice updater. --- channeldb/invoices.go | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/channeldb/invoices.go b/channeldb/invoices.go index e6c96806b..5e66ac3c2 100644 --- a/channeldb/invoices.go +++ b/channeldb/invoices.go @@ -1953,11 +1953,14 @@ func (d *DB) cancelHTLCs(invoices kvdb.RwBucket, invoiceNum []byte, return nil, fmt.Errorf("cancel of non-existent htlc") } - err := cancelSingleHtlc(timestamp, htlc, invoice.State) + err := canCancelSingleHtlc(htlc, invoice.State) if err != nil { return nil, err } + htlc.State = invpkg.HtlcStateCanceled + htlc.ResolveTime = timestamp + // Tally this into the set of HTLCs that need to be updated on // disk, but once again, only if this is an AMP invoice. if invoice.IsAMP() { @@ -2600,26 +2603,22 @@ func updateInvoiceAmpState(invoice *invpkg.Invoice, setID invpkg.SetID, return nil } -// cancelSingleHtlc validates cancellation of a single htlc and update its -// state. -func cancelSingleHtlc(resolveTime time.Time, htlc *invpkg.InvoiceHTLC, - invState invpkg.ContractState) error { +// canCancelSingleHtlc validates cancellation of a single HTLC. If nil is +// returned, then the HTLC can be cancelled. +func canCancelSingleHtlc(htlc *invpkg.InvoiceHTLC, + invoiceState invpkg.ContractState) error { // It is only possible to cancel individual htlcs on an open invoice. - if invState != invpkg.ContractOpen { - return fmt.Errorf("htlc canceled on invoice in "+ - "state %v", invState) + if invoiceState != invpkg.ContractOpen { + return fmt.Errorf("htlc canceled on invoice in state %v", + invoiceState) } // It is only possible if the htlc is still pending. if htlc.State != invpkg.HtlcStateAccepted { - return fmt.Errorf("htlc canceled in state %v", - htlc.State) + return fmt.Errorf("htlc canceled in state %v", htlc.State) } - htlc.State = invpkg.HtlcStateCanceled - htlc.ResolveTime = resolveTime - return nil }