mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-29 15:11:09 +02:00
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.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user