From 3bedffcc1a73eeaec15542f6d442a00060a6980e Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Thu, 28 Jun 2018 21:47:41 -0700 Subject: [PATCH] invoiceregistry: serialize all invoice modifications, eliminate extra db call for settle In this commit, we now ensure that all modifications to the invoice DB are properly serialized. This ensures that our time series within the database will be properly coherent. Additionally, within SettleInvoice, we remove an extra DB call by taking advantage of the new SettleInvoice method which will return the invoice being settled as well. --- invoiceregistry.go | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/invoiceregistry.go b/invoiceregistry.go index a49a8581..ab38ea77 100644 --- a/invoiceregistry.go +++ b/invoiceregistry.go @@ -223,6 +223,9 @@ func (i *invoiceRegistry) AddDebugInvoice(amt btcutil.Amount, preimage chainhash // daemon add/forward HTLCs are able to obtain the proper preimage required for // redemption in the case that we're the final destination. func (i *invoiceRegistry) AddInvoice(invoice *channeldb.Invoice) error { + i.Lock() + defer i.Unlock() + ltndLog.Debugf("Adding invoice %v", newLogClosure(func() string { return spew.Sdump(invoice) })) @@ -280,39 +283,29 @@ func (i *invoiceRegistry) LookupInvoice(rHash chainhash.Hash) (channeldb.Invoice func (i *invoiceRegistry) SettleInvoice(rHash chainhash.Hash, amtPaid lnwire.MilliSatoshi) error { + i.Lock() + defer i.Unlock() + ltndLog.Debugf("Settling invoice %x", rHash[:]) // First check the in-memory debug invoice index to see if this is an // existing invoice added for debugging. - i.RLock() if _, ok := i.debugInvoices[rHash]; ok { // Debug invoices are never fully settled, so we simply return // immediately in this case. - i.RUnlock() - return nil } - i.RUnlock() // If this isn't a debug invoice, then we'll attempt to settle an // invoice matching this rHash on disk (if one exists). - if err := i.cdb.SettleInvoice(rHash, amtPaid); err != nil { + invoice, err := i.cdb.SettleInvoice(rHash, amtPaid) + if err != nil { return err } - // Launch a new goroutine to notify any/all registered invoice - // notification clients. - go func() { - invoice, err := i.cdb.LookupInvoice(rHash) - if err != nil { - ltndLog.Errorf("unable to find invoice: %v", err) - return - } + ltndLog.Infof("Payment received: %v", spew.Sdump(invoice)) - ltndLog.Infof("Payment received: %v", spew.Sdump(invoice)) - - i.notifyClients(&invoice, true) - }() + i.notifyClients(invoice, true) return nil }