Skip to content

Commit

Permalink
invoices: refactor cancelHTLCs to apply all updates in one place
Browse files Browse the repository at this point in the history
This commit changes cancelHTLCs such that all updates are gathered and
applied in the same function. This is part of a larger refactor to
gather all invoice changes later to be used in the invoice updater.
  • Loading branch information
bhandras committed Nov 14, 2023
1 parent 048106f commit bd604c9
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions invoices/update_invoice.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,30 @@ func cancelHTLCs(invoice *Invoice, updateTime time.Time,
// 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() {
cancelHtlcsAmp(invoice, htlcsAmpUpdate, htlc, key)
// Process the cancellation of the HTLC that belongs to
// an AMP HTLC set. We'll need to update the meta data
// in the main invoice, and also apply the new update
// to the update map.

setID := htlc.AMP.Record.SetID()
// First, we'll update the state of the entire HTLC set
// to cancelled.
newAmpState := getUpdatedInvoiceAmpState(
invoice, setID, key, HtlcStateCanceled,
htlc.Amt,
)

invoice.AMPState[setID] = newAmpState

// We'll only decrement the total amount paid if the
// invoice was already in the accepted state.
if invoice.AmtPaid != 0 {
invoice.AmtPaid -= htlc.Amt
}

applyCancelHtlcsAmp(
invoice, setID, key, htlc, htlcsAmpUpdate,
)
}
}

Expand Down Expand Up @@ -582,24 +605,12 @@ func getUpdatedInvoiceAmpState(invoice *Invoice, setID SetID,
return ampState
}

// cancelHtlcsAmp processes a cancellation of an HTLC that belongs to an AMP
// HTLC set. We'll need to update the meta data in the main invoice, and also
// apply the new update to the update MAP, since all the HTLCs for a given HTLC
// set need to be written in-line with each other.
func cancelHtlcsAmp(invoice *Invoice,
updateMap map[SetID]map[models.CircuitKey]*InvoiceHTLC,
htlc *InvoiceHTLC, circuitKey models.CircuitKey) {

setID := htlc.AMP.Record.SetID()

// First, we'll update the state of the entire HTLC set
// to cancelled.
newAmpState := getUpdatedInvoiceAmpState(
invoice, setID, circuitKey, HtlcStateCanceled,
htlc.Amt,
)

invoice.AMPState[setID] = newAmpState
// applyCancelHtlcsAmp applies the cancellation update to the update map, since
// all the HTLCs for a given HTLC set need to be written in-line with each
// other.
func applyCancelHtlcsAmp(invoice *Invoice, setID [32]byte,
circuitKey models.CircuitKey, htlc *InvoiceHTLC,
updateMap map[SetID]map[models.CircuitKey]*InvoiceHTLC) {

if _, ok := updateMap[setID]; !ok {
// Only HTLCs in the accepted state, can be cancelled, but we
Expand All @@ -620,12 +631,6 @@ func cancelHtlcsAmp(invoice *Invoice,
// Finally, include the newly cancelled HTLC in the set of HTLCs we
// need to cancel.
updateMap[setID][circuitKey] = htlc

// We'll only decrement the total amount paid if the invoice was
// already in the accepted state.
if invoice.AmtPaid != 0 {
invoice.AmtPaid -= htlc.Amt
}
}

// canCancelSingleHtlc validates cancellation of a single HTLC. If nil is
Expand Down

0 comments on commit bd604c9

Please sign in to comment.