From 393572532905010e4f6455dd00df8727d3ba4b59 Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 14 Oct 2021 18:44:33 +0200 Subject: [PATCH] invoices: update cancel logic for AMP invoices to be more efficient By passing in the set ID here, we ensure that we don't need to read ALL the invoices to update the state of a set of them. --- invoices/invoiceregistry.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index b5a42a5a9..c49535330 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -672,14 +672,39 @@ func (i *InvoiceRegistry) cancelSingleHtlc(invoiceRef channeldb.InvoiceRef, } // Lookup the current status of the htlc in the database. + var ( + htlcState channeldb.HtlcState + setID *channeldb.SetID + ) htlc, ok := invoice.Htlcs[key] if !ok { - return nil, fmt.Errorf("htlc %v not found", key) + // If this is an AMP invoice, then all the HTLCs won't + // be read out, so we'll consult the other mapping to + // try to find the HTLC state in question here. + var found bool + for ampSetID, htlcSet := range invoice.AMPState { + ampSetID := ampSetID + for htlcKey := range htlcSet.InvoiceKeys { + if htlcKey == key { + htlcState = htlcSet.State + setID = &SetID + + found = true + break + } + } + } + + if !found { + return nil, fmt.Errorf("htlc %v not found", key) + } + } else { + htlcState = htlc.State } // Cancelation is only possible if the htlc wasn't already // resolved. - if htlc.State != channeldb.HtlcStateAccepted { + if htlcState != channeldb.HtlcStateAccepted { log.Debugf("cancelSingleHtlc: htlc %v on invoice %v "+ "is already resolved", key, invoiceRef) @@ -697,6 +722,7 @@ func (i *InvoiceRegistry) cancelSingleHtlc(invoiceRef channeldb.InvoiceRef, return &channeldb.InvoiceUpdateDesc{ CancelHtlcs: canceledHtlcs, + SetID: setID, }, nil }