Skip to content

Commit c95d73c

Browse files
committed
invoices: remove obsolete code for AMP invoices.
We always fetch the HTLCs for the specific setID, so there is no need to keep this code. In earlier versions we would call the UpdateInvoice method with `nil` for the setID therefore we had to lookup the AMPState. However this was error prune because in case one partial payment times-out the AMPState would change to cancelled and that could lead to not resolve HTLCs.
1 parent 0532990 commit c95d73c

File tree

1 file changed

+14
-29
lines changed

1 file changed

+14
-29
lines changed

invoices/invoiceregistry.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,9 @@ func (i *InvoiceRegistry) startHtlcTimer(invoiceRef InvoiceRef,
654654
func (i *InvoiceRegistry) cancelSingleHtlc(invoiceRef InvoiceRef,
655655
key CircuitKey, result FailResolutionResult) error {
656656

657-
updateInvoice := func(invoice *Invoice) (*InvoiceUpdateDesc, error) {
657+
updateInvoice := func(invoice *Invoice, setID *SetID) (
658+
*InvoiceUpdateDesc, error) {
659+
658660
// Only allow individual htlc cancellation on open invoices.
659661
if invoice.State != ContractOpen {
660662
log.Debugf("cancelSingleHtlc: invoice %v no longer "+
@@ -663,37 +665,16 @@ func (i *InvoiceRegistry) cancelSingleHtlc(invoiceRef InvoiceRef,
663665
return nil, nil
664666
}
665667

666-
// Lookup the current status of the htlc in the database.
667-
var (
668-
htlcState HtlcState
669-
setID *SetID
670-
)
668+
// Also for AMP invoices we fetch the relevant HTLCs, so
669+
// the HTLC should be found, otherwise we return an error.
671670
htlc, ok := invoice.Htlcs[key]
672671
if !ok {
673-
// If this is an AMP invoice, then all the HTLCs won't
674-
// be read out, so we'll consult the other mapping to
675-
// try to find the HTLC state in question here.
676-
var found bool
677-
for ampSetID, htlcSet := range invoice.AMPState {
678-
ampSetID := ampSetID
679-
for htlcKey := range htlcSet.InvoiceKeys {
680-
if htlcKey == key {
681-
htlcState = htlcSet.State
682-
setID = &ampSetID
683-
684-
found = true
685-
break
686-
}
687-
}
688-
}
689-
690-
if !found {
691-
return nil, fmt.Errorf("htlc %v not found", key)
692-
}
693-
} else {
694-
htlcState = htlc.State
672+
return nil, fmt.Errorf("htlc %v not found on "+
673+
"invoice %v", key, invoiceRef)
695674
}
696675

676+
htlcState := htlc.State
677+
697678
// Cancellation is only possible if the htlc wasn't already
698679
// resolved.
699680
if htlcState != HtlcStateAccepted {
@@ -729,7 +710,7 @@ func (i *InvoiceRegistry) cancelSingleHtlc(invoiceRef InvoiceRef,
729710
func(invoice *Invoice) (
730711
*InvoiceUpdateDesc, error) {
731712

732-
updateDesc, err := updateInvoice(invoice)
713+
updateDesc, err := updateInvoice(invoice, setID)
733714
if err != nil {
734715
return nil, err
735716
}
@@ -756,8 +737,12 @@ func (i *InvoiceRegistry) cancelSingleHtlc(invoiceRef InvoiceRef,
756737
key, int32(htlc.AcceptHeight), result,
757738
)
758739

740+
log.Debugf("Cancelling htlc (%v) of invoice(%v) with "+
741+
"resolution: %v", key, invoiceRef, result)
742+
759743
i.notifyHodlSubscribers(resolution)
760744
}
745+
761746
return nil
762747
}
763748

0 commit comments

Comments
 (0)