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:
Joost Jager
2019-08-09 15:30:33 +02:00
parent 416bc8c68c
commit 144856757d
2 changed files with 18 additions and 64 deletions

View File

@@ -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.