From 144856757dd692769ca1752dd3a97aed887abff6 Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Fri, 9 Aug 2019 15:30:33 +0200 Subject: [PATCH] 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. --- channeldb/invoices.go | 63 ------------------------------------- invoices/invoiceregistry.go | 19 ++++++++++- 2 files changed, 18 insertions(+), 64 deletions(-) diff --git a/channeldb/invoices.go b/channeldb/invoices.go index de480f36..ff4d60f6 100644 --- a/channeldb/invoices.go +++ b/channeldb/invoices.go @@ -753,37 +753,6 @@ func (d *DB) UpdateInvoice(paymentHash lntypes.Hash, 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 // they missed within the settled invoice time series. We'll return all known // settled invoice that have a settle index higher than the passed @@ -1282,35 +1251,3 @@ func setSettleFields(settleIndex *bbolt.Bucket, invoiceNum []byte, 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 -} diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index 1681bae5..1d258e5b 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -634,7 +634,24 @@ func (i *InvoiceRegistry) CancelInvoice(payHash lntypes.Hash) error { 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 // canceled.