channeldb+invoices: move invoice cancel logic into registry
This commit is a continuation of the centralization of invoice state transition logic in the invoice registry.
This commit is contained in:
parent
416bc8c68c
commit
144856757d
@ -753,37 +753,6 @@ func (d *DB) UpdateInvoice(paymentHash lntypes.Hash,
|
|||||||
return updatedInvoice, err
|
return updatedInvoice, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// CancelInvoice attempts to cancel the invoice corresponding to the passed
|
|
||||||
// payment hash.
|
|
||||||
func (d *DB) CancelInvoice(paymentHash lntypes.Hash) (*Invoice, error) {
|
|
||||||
var canceledInvoice *Invoice
|
|
||||||
err := d.Update(func(tx *bbolt.Tx) error {
|
|
||||||
invoices, err := tx.CreateBucketIfNotExists(invoiceBucket)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
invoiceIndex, err := invoices.CreateBucketIfNotExists(
|
|
||||||
invoiceIndexBucket,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the invoice index to see if an invoice paying to this
|
|
||||||
// hash exists within the DB.
|
|
||||||
invoiceNum := invoiceIndex.Get(paymentHash[:])
|
|
||||||
if invoiceNum == nil {
|
|
||||||
return ErrInvoiceNotFound
|
|
||||||
}
|
|
||||||
|
|
||||||
canceledInvoice, err = cancelInvoice(invoices, invoiceNum)
|
|
||||||
|
|
||||||
return err
|
|
||||||
})
|
|
||||||
|
|
||||||
return canceledInvoice, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// InvoicesSettledSince can be used by callers to catch up any settled invoices
|
// InvoicesSettledSince can be used by callers to catch up any settled invoices
|
||||||
// they missed within the settled invoice time series. We'll return all known
|
// they missed within the settled invoice time series. We'll return all known
|
||||||
// settled invoice that have a settle index higher than the passed
|
// settled invoice that have a settle index higher than the passed
|
||||||
@ -1282,35 +1251,3 @@ func setSettleFields(settleIndex *bbolt.Bucket, invoiceNum []byte,
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func cancelInvoice(invoices *bbolt.Bucket, invoiceNum []byte) (
|
|
||||||
*Invoice, error) {
|
|
||||||
|
|
||||||
invoice, err := fetchInvoice(invoiceNum, invoices)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
switch invoice.Terms.State {
|
|
||||||
case ContractSettled:
|
|
||||||
return &invoice, ErrInvoiceAlreadySettled
|
|
||||||
case ContractCanceled:
|
|
||||||
return &invoice, ErrInvoiceAlreadyCanceled
|
|
||||||
}
|
|
||||||
|
|
||||||
invoice.Terms.State = ContractCanceled
|
|
||||||
|
|
||||||
// Set AmtPaid back to 0, in case the invoice was already accepted.
|
|
||||||
invoice.AmtPaid = 0
|
|
||||||
|
|
||||||
var buf bytes.Buffer
|
|
||||||
if err := serializeInvoice(&buf, &invoice); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := invoices.Put(invoiceNum[:], buf.Bytes()); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return &invoice, nil
|
|
||||||
}
|
|
||||||
|
@ -634,7 +634,24 @@ func (i *InvoiceRegistry) CancelInvoice(payHash lntypes.Hash) error {
|
|||||||
|
|
||||||
log.Debugf("Invoice(%v): canceling invoice", payHash)
|
log.Debugf("Invoice(%v): canceling invoice", payHash)
|
||||||
|
|
||||||
invoice, err := i.cdb.CancelInvoice(payHash)
|
updateInvoice := func(invoice *channeldb.Invoice) (
|
||||||
|
*channeldb.InvoiceUpdateDesc, error) {
|
||||||
|
|
||||||
|
switch invoice.Terms.State {
|
||||||
|
case channeldb.ContractSettled:
|
||||||
|
return nil, channeldb.ErrInvoiceAlreadySettled
|
||||||
|
case channeldb.ContractCanceled:
|
||||||
|
return nil, channeldb.ErrInvoiceAlreadyCanceled
|
||||||
|
}
|
||||||
|
|
||||||
|
// Move invoice to the canceled state.
|
||||||
|
return &channeldb.InvoiceUpdateDesc{
|
||||||
|
AmtPaid: 0,
|
||||||
|
State: channeldb.ContractCanceled,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
invoice, err := i.cdb.UpdateInvoice(payHash, updateInvoice)
|
||||||
|
|
||||||
// Implement idempotency by returning success if the invoice was already
|
// Implement idempotency by returning success if the invoice was already
|
||||||
// canceled.
|
// canceled.
|
||||||
|
Loading…
Reference in New Issue
Block a user