channeldb: fetch the invoice before calling into updateInvoice
This commit is contained in:
1 file changed
+28
-25
+28
-25
@@ -641,10 +641,25 @@ func (d *DB) UpdateInvoice(_ context.Context, ref invpkg.InvoiceRef,
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the set ID hint is non-nil, then we'll use that to filter
|
||||||
|
// out the HTLCs for AMP invoice so we don't need to read them
|
||||||
|
// all out to satisfy the invoice callback below. If it's nil,
|
||||||
|
// then we pass in the zero set ID which means no HTLCs will be
|
||||||
|
// read out.
|
||||||
|
var invSetID invpkg.SetID
|
||||||
|
|
||||||
|
if setIDHint != nil {
|
||||||
|
invSetID = *setIDHint
|
||||||
|
}
|
||||||
|
invoice, err := fetchInvoice(invoiceNum, invoices, &invSetID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
payHash := ref.PayHash()
|
payHash := ref.PayHash()
|
||||||
updatedInvoice, err = d.updateInvoice(
|
updatedInvoice, err = d.updateInvoice(
|
||||||
payHash, setIDHint, invoices, settleIndex, setIDIndex,
|
payHash, invoices, settleIndex, setIDIndex,
|
||||||
invoiceNum, callback,
|
&invoice, invoiceNum, callback,
|
||||||
)
|
)
|
||||||
|
|
||||||
return err
|
return err
|
||||||
@@ -1872,26 +1887,14 @@ func settleHtlcsAmp(invoice *invpkg.Invoice,
|
|||||||
|
|
||||||
// updateInvoice fetches the invoice, obtains the update descriptor from the
|
// updateInvoice fetches the invoice, obtains the update descriptor from the
|
||||||
// callback and applies the updates in a single db transaction.
|
// callback and applies the updates in a single db transaction.
|
||||||
func (d *DB) updateInvoice(hash *lntypes.Hash, refSetID *invpkg.SetID, invoices,
|
func (d *DB) updateInvoice(hash *lntypes.Hash, invoices,
|
||||||
settleIndex, setIDIndex kvdb.RwBucket, invoiceNum []byte,
|
settleIndex, setIDIndex kvdb.RwBucket, invoice *invpkg.Invoice,
|
||||||
callback invpkg.InvoiceUpdateCallback) (*invpkg.Invoice, error) {
|
invoiceNum []byte, callback invpkg.InvoiceUpdateCallback) (
|
||||||
|
*invpkg.Invoice, error) {
|
||||||
// If the set ID is non-nil, then we'll use that to filter out the
|
|
||||||
// HTLCs for AMP invoice so we don't need to read them all out to
|
|
||||||
// satisfy the invoice callback below. If it's nil, then we pass in the
|
|
||||||
// zero set ID which means no HTLCs will be read out.
|
|
||||||
var invSetID invpkg.SetID
|
|
||||||
if refSetID != nil {
|
|
||||||
invSetID = *refSetID
|
|
||||||
}
|
|
||||||
invoice, err := fetchInvoice(invoiceNum, invoices, &invSetID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create deep copy to prevent any accidental modification in the
|
// Create deep copy to prevent any accidental modification in the
|
||||||
// callback.
|
// callback.
|
||||||
invoiceCopy, err := invpkg.CopyInvoice(&invoice)
|
invoiceCopy, err := invpkg.CopyInvoice(invoice)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -1899,33 +1902,33 @@ func (d *DB) updateInvoice(hash *lntypes.Hash, refSetID *invpkg.SetID, invoices,
|
|||||||
// Call the callback and obtain the update descriptor.
|
// Call the callback and obtain the update descriptor.
|
||||||
update, err := callback(invoiceCopy)
|
update, err := callback(invoiceCopy)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &invoice, err
|
return invoice, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If there is nothing to update, return early.
|
// If there is nothing to update, return early.
|
||||||
if update == nil {
|
if update == nil {
|
||||||
return &invoice, nil
|
return invoice, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
switch update.UpdateType {
|
switch update.UpdateType {
|
||||||
case invpkg.CancelHTLCsUpdate:
|
case invpkg.CancelHTLCsUpdate:
|
||||||
return d.cancelHTLCs(invoices, invoiceNum, &invoice, update)
|
return d.cancelHTLCs(invoices, invoiceNum, invoice, update)
|
||||||
|
|
||||||
case invpkg.AddHTLCsUpdate:
|
case invpkg.AddHTLCsUpdate:
|
||||||
return d.addHTLCs(
|
return d.addHTLCs(
|
||||||
invoices, settleIndex, setIDIndex, invoiceNum, &invoice,
|
invoices, settleIndex, setIDIndex, invoiceNum, invoice,
|
||||||
hash, update,
|
hash, update,
|
||||||
)
|
)
|
||||||
|
|
||||||
case invpkg.SettleHodlInvoiceUpdate:
|
case invpkg.SettleHodlInvoiceUpdate:
|
||||||
return d.settleHodlInvoice(
|
return d.settleHodlInvoice(
|
||||||
invoices, settleIndex, invoiceNum, &invoice, hash,
|
invoices, settleIndex, invoiceNum, invoice, hash,
|
||||||
update.State,
|
update.State,
|
||||||
)
|
)
|
||||||
|
|
||||||
case invpkg.CancelInvoiceUpdate:
|
case invpkg.CancelInvoiceUpdate:
|
||||||
return d.cancelInvoice(
|
return d.cancelInvoice(
|
||||||
invoices, invoiceNum, &invoice, hash, update.State,
|
invoices, invoiceNum, invoice, hash, update.State,
|
||||||
)
|
)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
Reference in new issue
Block a user