channeldb+invoices: move hold invoice settle 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-21 15:24:21 +02:00
parent ad3522f1a6
commit 416bc8c68c
3 changed files with 37 additions and 84 deletions

View File

@@ -456,6 +456,7 @@ func (i *InvoiceRegistry) updateInvoice(invoice *channeldb.Invoice,
if holdInvoice {
update.State = channeldb.ContractAccepted
} else {
update.Preimage = invoice.Terms.PaymentPreimage
update.State = channeldb.ContractSettled
}
return &update, nil
@@ -587,13 +588,32 @@ func (i *InvoiceRegistry) SettleHodlInvoice(preimage lntypes.Preimage) error {
i.Lock()
defer i.Unlock()
invoice, err := i.cdb.SettleHoldInvoice(preimage)
updateInvoice := func(invoice *channeldb.Invoice) (
*channeldb.InvoiceUpdateDesc, error) {
switch invoice.Terms.State {
case channeldb.ContractOpen:
return nil, channeldb.ErrInvoiceStillOpen
case channeldb.ContractCanceled:
return nil, channeldb.ErrInvoiceAlreadyCanceled
case channeldb.ContractSettled:
return nil, channeldb.ErrInvoiceAlreadySettled
}
return &channeldb.InvoiceUpdateDesc{
AmtPaid: invoice.AmtPaid,
State: channeldb.ContractSettled,
Preimage: preimage,
}, nil
}
hash := preimage.Hash()
invoice, err := i.cdb.UpdateInvoice(hash, updateInvoice)
if err != nil {
log.Errorf("SettleHodlInvoice with preimage %v: %v", preimage, err)
return err
}
hash := preimage.Hash()
log.Debugf("Invoice(%v): settled with preimage %v", hash,
invoice.Terms.PaymentPreimage)