From c19c872cffe19dfd26cfbf041797acfc0800111f Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Fri, 29 Jun 2018 16:03:46 -0700 Subject: [PATCH] lnd: update invoiceRegistry to match new switch requirements --- invoiceregistry.go | 24 ++++++++++++++++++------ rpcserver.go | 2 +- witness_beacon.go | 2 +- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/invoiceregistry.go b/invoiceregistry.go index 58fb0515..186089c3 100644 --- a/invoiceregistry.go +++ b/invoiceregistry.go @@ -9,6 +9,7 @@ import ( "github.com/davecgh/go-spew/spew" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/lnwire" + "github.com/lightningnetwork/lnd/zpay32" "github.com/roasbeef/btcd/chaincfg/chainhash" "github.com/roasbeef/btcutil" ) @@ -95,10 +96,14 @@ func (i *invoiceRegistry) AddInvoice(invoice *channeldb.Invoice) error { //go i.notifyClients(invoice, false) } -// lookupInvoice looks up an invoice by its payment hash (R-Hash), if found -// then we're able to pull the funds pending within an HTLC. +// LookupInvoice looks up an invoice by its payment hash (R-Hash), if found +// then we're able to pull the funds pending within an HTLC. We'll also return +// what the expected min final CLTV delta is, pre-parsed from the payment +// request. This may be used by callers to determine if an HTLC is well formed +// according to the cltv delta. +// // TODO(roasbeef): ignore if settled? -func (i *invoiceRegistry) LookupInvoice(rHash chainhash.Hash) (channeldb.Invoice, error) { +func (i *invoiceRegistry) LookupInvoice(rHash chainhash.Hash) (channeldb.Invoice, uint32, error) { // First check the in-memory debug invoice index to see if this is an // existing invoice added for debugging. i.RLock() @@ -107,17 +112,24 @@ func (i *invoiceRegistry) LookupInvoice(rHash chainhash.Hash) (channeldb.Invoice // If found, then simply return the invoice directly. if ok { - return *invoice, nil + return *invoice, 0, nil } // Otherwise, we'll check the database to see if there's an existing // matching invoice. invoice, err := i.cdb.LookupInvoice(rHash) if err != nil { - return channeldb.Invoice{}, err + return channeldb.Invoice{}, 0, err } - return *invoice, nil + payReq, err := zpay32.Decode( + string(invoice.PaymentRequest), activeNetParams.Params, + ) + if err != nil { + return channeldb.Invoice{}, 0, err + } + + return *invoice, uint32(payReq.MinFinalCLTVExpiry()), nil } // SettleInvoice attempts to mark an invoice as settled. If the invoice is a diff --git a/rpcserver.go b/rpcserver.go index 3f7e92f4..99de1e34 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -2799,7 +2799,7 @@ func (r *rpcServer) LookupInvoice(ctx context.Context, rpcsLog.Tracef("[lookupinvoice] searching for invoice %x", payHash[:]) - invoice, err := r.server.invoices.LookupInvoice(payHash) + invoice, _, err := r.server.invoices.LookupInvoice(payHash) if err != nil { return nil, err } diff --git a/witness_beacon.go b/witness_beacon.go index cb0d63cd..842321b7 100644 --- a/witness_beacon.go +++ b/witness_beacon.go @@ -73,7 +73,7 @@ func (p *preimageBeacon) LookupPreimage(payHash []byte) ([]byte, bool) { // the preimage as it's on that we created ourselves. var invoiceKey chainhash.Hash copy(invoiceKey[:], payHash) - invoice, err := p.invoices.LookupInvoice(invoiceKey) + invoice, _, err := p.invoices.LookupInvoice(invoiceKey) switch { case err == channeldb.ErrInvoiceNotFound: // If we get this error, then it simply means that this invoice