mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-26 20:26:34 +02:00
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:
@@ -17,6 +17,7 @@ import (
|
||||
"github.com/btcsuite/btcd/wire"
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/invoices"
|
||||
"github.com/lightningnetwork/lnd/lntypes"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/lightningnetwork/lnd/netann"
|
||||
@@ -46,7 +47,7 @@ const (
|
||||
// AddInvoiceConfig contains dependencies for invoice creation.
|
||||
type AddInvoiceConfig struct {
|
||||
// AddInvoice is called to add the invoice to the registry.
|
||||
AddInvoice func(invoice *channeldb.Invoice, paymentHash lntypes.Hash) (
|
||||
AddInvoice func(invoice *invoices.Invoice, paymentHash lntypes.Hash) (
|
||||
uint64, error)
|
||||
|
||||
// IsChannelActive is used to generate valid hop hints.
|
||||
@@ -234,7 +235,7 @@ func (d *AddInvoiceData) mppPaymentHashAndPreimage() (*lntypes.Preimage,
|
||||
// duplicated invoices are rejected, therefore all invoices *must* have a
|
||||
// unique payment preimage.
|
||||
func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
|
||||
invoice *AddInvoiceData) (*lntypes.Hash, *channeldb.Invoice, error) {
|
||||
invoice *AddInvoiceData) (*lntypes.Hash, *invoices.Invoice, error) {
|
||||
|
||||
paymentPreimage, paymentHash, err := invoice.paymentHashAndPreimage()
|
||||
if err != nil {
|
||||
@@ -243,10 +244,10 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
|
||||
|
||||
// The size of the memo, receipt and description hash attached must not
|
||||
// exceed the maximum values for either of the fields.
|
||||
if len(invoice.Memo) > channeldb.MaxMemoSize {
|
||||
if len(invoice.Memo) > invoices.MaxMemoSize {
|
||||
return nil, nil, fmt.Errorf("memo too large: %v bytes "+
|
||||
"(maxsize=%v)", len(invoice.Memo),
|
||||
channeldb.MaxMemoSize)
|
||||
invoices.MaxMemoSize)
|
||||
}
|
||||
if len(invoice.DescriptionHash) > 0 &&
|
||||
len(invoice.DescriptionHash) != 32 {
|
||||
@@ -448,11 +449,11 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
newInvoice := &channeldb.Invoice{
|
||||
newInvoice := &invoices.Invoice{
|
||||
CreationDate: creationDate,
|
||||
Memo: []byte(invoice.Memo),
|
||||
PaymentRequest: []byte(payReqString),
|
||||
Terms: channeldb.ContractTerm{
|
||||
Terms: invoices.ContractTerm{
|
||||
FinalCltvDelta: int32(payReq.MinFinalCLTVExpiry()),
|
||||
Expiry: payReq.Expiry(),
|
||||
Value: amtMSat,
|
||||
|
@@ -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
|
||||
|
@@ -6,7 +6,7 @@ import (
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/chaincfg"
|
||||
"github.com/lightningnetwork/lnd/channeldb"
|
||||
"github.com/lightningnetwork/lnd/invoices"
|
||||
"github.com/lightningnetwork/lnd/lnrpc"
|
||||
"github.com/lightningnetwork/lnd/lnwire"
|
||||
"github.com/lightningnetwork/lnd/zpay32"
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
// because not all information is stored in dedicated invoice fields. If there
|
||||
// is no payment request present, a dummy request will be returned. This can
|
||||
// happen with just-in-time inserted keysend invoices.
|
||||
func decodePayReq(invoice *channeldb.Invoice,
|
||||
func decodePayReq(invoice *invoices.Invoice,
|
||||
activeNetParams *chaincfg.Params) (*zpay32.Invoice, error) {
|
||||
|
||||
paymentRequest := string(invoice.PaymentRequest)
|
||||
@@ -40,8 +40,8 @@ func decodePayReq(invoice *channeldb.Invoice,
|
||||
return decoded, nil
|
||||
}
|
||||
|
||||
// CreateRPCInvoice creates an *lnrpc.Invoice from the *channeldb.Invoice.
|
||||
func CreateRPCInvoice(invoice *channeldb.Invoice,
|
||||
// CreateRPCInvoice creates an *lnrpc.Invoice from the *invoices.Invoice.
|
||||
func CreateRPCInvoice(invoice *invoices.Invoice,
|
||||
activeNetParams *chaincfg.Params) (*lnrpc.Invoice, error) {
|
||||
|
||||
decoded, err := decodePayReq(invoice, activeNetParams)
|
||||
@@ -76,18 +76,22 @@ func CreateRPCInvoice(invoice *channeldb.Invoice,
|
||||
satAmt := invoice.Terms.Value.ToSatoshis()
|
||||
satAmtPaid := invoice.AmtPaid.ToSatoshis()
|
||||
|
||||
isSettled := invoice.State == channeldb.ContractSettled
|
||||
isSettled := invoice.State == invoices.ContractSettled
|
||||
|
||||
var state lnrpc.Invoice_InvoiceState
|
||||
switch invoice.State {
|
||||
case channeldb.ContractOpen:
|
||||
case invoices.ContractOpen:
|
||||
state = lnrpc.Invoice_OPEN
|
||||
case channeldb.ContractSettled:
|
||||
|
||||
case invoices.ContractSettled:
|
||||
state = lnrpc.Invoice_SETTLED
|
||||
case channeldb.ContractCanceled:
|
||||
|
||||
case invoices.ContractCanceled:
|
||||
state = lnrpc.Invoice_CANCELED
|
||||
case channeldb.ContractAccepted:
|
||||
|
||||
case invoices.ContractAccepted:
|
||||
state = lnrpc.Invoice_ACCEPTED
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown invoice state %v",
|
||||
invoice.State)
|
||||
@@ -97,11 +101,11 @@ func CreateRPCInvoice(invoice *channeldb.Invoice,
|
||||
for key, htlc := range invoice.Htlcs {
|
||||
var state lnrpc.InvoiceHTLCState
|
||||
switch htlc.State {
|
||||
case channeldb.HtlcStateAccepted:
|
||||
case invoices.HtlcStateAccepted:
|
||||
state = lnrpc.InvoiceHTLCState_ACCEPTED
|
||||
case channeldb.HtlcStateSettled:
|
||||
case invoices.HtlcStateSettled:
|
||||
state = lnrpc.InvoiceHTLCState_SETTLED
|
||||
case channeldb.HtlcStateCanceled:
|
||||
case invoices.HtlcStateCanceled:
|
||||
state = lnrpc.InvoiceHTLCState_CANCELED
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown state %v", htlc.State)
|
||||
@@ -139,7 +143,7 @@ func CreateRPCInvoice(invoice *channeldb.Invoice,
|
||||
}
|
||||
|
||||
// Only report resolved times if htlc is resolved.
|
||||
if htlc.State != channeldb.HtlcStateAccepted {
|
||||
if htlc.State != invoices.HtlcStateAccepted {
|
||||
rpcHtlc.ResolveTime = htlc.ResolveTime.Unix()
|
||||
}
|
||||
|
||||
@@ -182,11 +186,11 @@ func CreateRPCInvoice(invoice *channeldb.Invoice,
|
||||
|
||||
var state lnrpc.InvoiceHTLCState
|
||||
switch ampState.State {
|
||||
case channeldb.HtlcStateAccepted:
|
||||
case invoices.HtlcStateAccepted:
|
||||
state = lnrpc.InvoiceHTLCState_ACCEPTED
|
||||
case channeldb.HtlcStateSettled:
|
||||
case invoices.HtlcStateSettled:
|
||||
state = lnrpc.InvoiceHTLCState_SETTLED
|
||||
case channeldb.HtlcStateCanceled:
|
||||
case invoices.HtlcStateCanceled:
|
||||
state = lnrpc.InvoiceHTLCState_CANCELED
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown state %v", ampState.State)
|
||||
@@ -202,7 +206,7 @@ func CreateRPCInvoice(invoice *channeldb.Invoice,
|
||||
// If at least one of the present HTLC sets show up as being
|
||||
// settled, then we'll mark the invoice itself as being
|
||||
// settled.
|
||||
if ampState.State == channeldb.HtlcStateSettled {
|
||||
if ampState.State == invoices.HtlcStateSettled {
|
||||
rpcInvoice.Settled = true // nolint:staticcheck
|
||||
rpcInvoice.State = lnrpc.Invoice_SETTLED
|
||||
}
|
||||
|
Reference in New Issue
Block a user