From a339065fdcec38be273bf75431757f7de1c1872c Mon Sep 17 00:00:00 2001 From: Joost Jager Date: Tue, 4 Feb 2020 15:15:48 +0100 Subject: [PATCH] invoices: add hash to update context --- invoices/invoiceregistry.go | 10 +++++----- invoices/update.go | 2 ++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/invoices/invoiceregistry.go b/invoices/invoiceregistry.go index 4b3a1b2d..9ab1a65b 100644 --- a/invoices/invoiceregistry.go +++ b/invoices/invoiceregistry.go @@ -693,8 +693,7 @@ func (i *InvoiceRegistry) cancelSingleHtlc(hash lntypes.Hash, // processKeySend just-in-time inserts an invoice if this htlc is a keysend // htlc. -func (i *InvoiceRegistry) processKeySend(ctx invoiceUpdateCtx, - hash lntypes.Hash) error { +func (i *InvoiceRegistry) processKeySend(ctx invoiceUpdateCtx) error { // Retrieve keysend record if present. preimageSlice, ok := ctx.customRecords[record.KeySendType] @@ -704,7 +703,7 @@ func (i *InvoiceRegistry) processKeySend(ctx invoiceUpdateCtx, // Cancel htlc is preimage is invalid. preimage, err := lntypes.MakePreimage(preimageSlice) - if err != nil || preimage.Hash() != hash { + if err != nil || preimage.Hash() != ctx.hash { return errors.New("invalid keysend preimage") } @@ -751,7 +750,7 @@ func (i *InvoiceRegistry) processKeySend(ctx invoiceUpdateCtx, // Insert invoice into database. Ignore duplicates, because this // may be a replay. - _, err = i.AddInvoice(invoice, hash) + _, err = i.AddInvoice(invoice, ctx.hash) if err != nil && err != channeldb.ErrDuplicateInvoice { return err } @@ -789,6 +788,7 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash, // Create the update context containing the relevant details of the // incoming htlc. updateCtx := invoiceUpdateCtx{ + hash: rHash, circuitKey: circuitKey, amtPaid: amtPaid, expiry: expiry, @@ -802,7 +802,7 @@ func (i *InvoiceRegistry) NotifyExitHopHtlc(rHash lntypes.Hash, // AddInvoice obtains its own lock. This is no problem, because the // operation is idempotent. if i.cfg.AcceptKeySend { - err := i.processKeySend(updateCtx, rHash) + err := i.processKeySend(updateCtx) if err != nil { debugLog(fmt.Sprintf("keysend error: %v", err)) diff --git a/invoices/update.go b/invoices/update.go index 57a41de9..2e05b398 100644 --- a/invoices/update.go +++ b/invoices/update.go @@ -4,6 +4,7 @@ import ( "errors" "github.com/lightningnetwork/lnd/channeldb" + "github.com/lightningnetwork/lnd/lntypes" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/record" ) @@ -169,6 +170,7 @@ func (u ResolutionResult) String() string { // invoiceUpdateCtx is an object that describes the context for the invoice // update to be carried out. type invoiceUpdateCtx struct { + hash lntypes.Hash circuitKey channeldb.CircuitKey amtPaid lnwire.MilliSatoshi expiry uint32