multi: break invoice depenency on channeldb

Now that we have the new package `lnd/channeldb/models` we can invert the
depenency between `channeldb` and `invoices`.

- Move all the invoice related types and errors to the
`invoices` package.

- Ensure that all the packages dealing with invoices use the types and
  interfaces defined in the `invoices` package.

- Implement the InvoiceDB interface (defined in `lnd/invoices`) in
  channeldb.

- Add new mock for InterfaceDB.

- `InvoiceRegistery` tests are now in its own subpacakge (they need to
  import both invoices & channeldb). This is temporary until we can
  decouple them.
This commit is contained in:
positiveblue committed 2023-01-16 07:31:09 -08:00
1 parent 383cb40f8d
commit 5ff5225245
26 files changed
+2839 -2253

No files matched your search

+10 -9
View File
@@ -5,13 +5,14 @@ package invoicesrpc
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/invoices"
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/macaroons"
@@ -287,7 +288,7 @@ func (s *Server) SettleInvoice(ctx context.Context,
}
err = s.cfg.InvoiceRegistry.SettleHodlInvoice(preimage)
if err != nil && err != channeldb.ErrInvoiceAlreadySettled {
if err != nil && !errors.Is(err, invoices.ErrInvoiceAlreadySettled) {
return nil, err
}
@@ -380,7 +381,7 @@ func (s *Server) AddHoldInvoice(ctx context.Context,
func (s *Server) LookupInvoiceV2(ctx context.Context,
req *LookupInvoiceMsg) (*lnrpc.Invoice, error) {
var invoiceRef channeldb.InvoiceRef
var invoiceRef invoices.InvoiceRef
// First, we'll attempt to parse out the invoice ref from the proto
// oneof. If none of the three currently supported types was
@@ -395,7 +396,7 @@ func (s *Server) LookupInvoiceV2(ctx context.Context,
)
}
invoiceRef = channeldb.InvoiceRefByHash(payHash)
invoiceRef = invoices.InvoiceRefByHash(payHash)
case req.GetPaymentAddr() != nil &&
req.LookupModifier == LookupModifier_HTLC_SET_BLANK:
@@ -403,13 +404,13 @@ func (s *Server) LookupInvoiceV2(ctx context.Context,
var payAddr [32]byte
copy(payAddr[:], req.GetPaymentAddr())
invoiceRef = channeldb.InvoiceRefByAddrBlankHtlc(payAddr)
invoiceRef = invoices.InvoiceRefByAddrBlankHtlc(payAddr)
case req.GetPaymentAddr() != nil:
var payAddr [32]byte
copy(payAddr[:], req.GetPaymentAddr())
invoiceRef = channeldb.InvoiceRefByAddr(payAddr)
invoiceRef = invoices.InvoiceRefByAddr(payAddr)
case req.GetSetId() != nil &&
req.LookupModifier == LookupModifier_HTLC_SET_ONLY:
@@ -417,13 +418,13 @@ func (s *Server) LookupInvoiceV2(ctx context.Context,
var setID [32]byte
copy(setID[:], req.GetSetId())
invoiceRef = channeldb.InvoiceRefBySetIDFiltered(setID)
invoiceRef = invoices.InvoiceRefBySetIDFiltered(setID)
case req.GetSetId() != nil:
var setID [32]byte
copy(setID[:], req.GetSetId())
invoiceRef = channeldb.InvoiceRefBySetID(setID)
invoiceRef = invoices.InvoiceRefBySetID(setID)
default:
return nil, status.Error(codes.InvalidArgument,
@@ -434,7 +435,7 @@ func (s *Server) LookupInvoiceV2(ctx context.Context,
// we can't find it in the database.
invoice, err := s.cfg.InvoiceRegistry.LookupInvoiceByRef(invoiceRef)
switch {
case err == channeldb.ErrInvoiceNotFound:
case errors.Is(err, invoices.ErrInvoiceNotFound):
return nil, status.Error(codes.NotFound, err.Error())
case err != nil:
return nil, err