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:
26 files changed
+2839
-2253
No files matched your search
+21
-17
@@ -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