2019-03-06 20:13:50 +00:00
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2020-04-24 07:27:25 +00:00
|
|
|
"context"
|
2019-03-06 20:13:50 +00:00
|
|
|
"errors"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
2020-06-17 20:25:57 +00:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-03-06 20:13:50 +00:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2019-10-01 15:21:17 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2023-01-09 15:36:52 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2019-11-16 00:16:30 +00:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
|
2019-10-01 15:21:17 +00:00
|
|
|
"github.com/lightningnetwork/lnd/zpay32"
|
2019-03-06 20:13:50 +00:00
|
|
|
)
|
|
|
|
|
2019-12-09 15:33:33 +00:00
|
|
|
var (
|
|
|
|
testStartingHeight = int32(600)
|
|
|
|
testNodePubkey = "03f5374b16f0b1f1b49101de1b9d89e0b460bc57ce9c2f9" +
|
|
|
|
"132b73dfc76d3704daa"
|
2019-12-09 15:47:23 +00:00
|
|
|
testSignature = []byte{55, 66, 77, 88, 99}
|
|
|
|
testSignatureMsg = "test"
|
2019-12-09 15:33:33 +00:00
|
|
|
)
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
// NewMockLnd returns a new instance of LndMockServices that can be used in unit
|
|
|
|
// tests.
|
|
|
|
func NewMockLnd() *LndMockServices {
|
|
|
|
lightningClient := &mockLightningClient{}
|
2019-10-01 15:21:17 +00:00
|
|
|
walletKit := &mockWalletKit{
|
2019-11-16 00:16:30 +00:00
|
|
|
feeEstimates: make(map[int32]chainfee.SatPerKWeight),
|
2019-10-01 15:21:17 +00:00
|
|
|
}
|
2019-03-06 20:13:50 +00:00
|
|
|
chainNotifier := &mockChainNotifier{}
|
|
|
|
signer := &mockSigner{}
|
|
|
|
invoices := &mockInvoices{}
|
2019-03-25 09:31:03 +00:00
|
|
|
router := &mockRouter{}
|
2020-04-24 07:20:42 +00:00
|
|
|
versioner := newMockVersioner()
|
2019-03-06 20:13:50 +00:00
|
|
|
|
|
|
|
lnd := LndMockServices{
|
|
|
|
LndServices: lndclient.LndServices{
|
|
|
|
WalletKit: walletKit,
|
|
|
|
Client: lightningClient,
|
|
|
|
ChainNotifier: chainNotifier,
|
|
|
|
Signer: signer,
|
|
|
|
Invoices: invoices,
|
2019-03-25 09:31:03 +00:00
|
|
|
Router: router,
|
2019-03-06 20:13:50 +00:00
|
|
|
ChainParams: &chaincfg.TestNet3Params,
|
2020-04-24 07:20:42 +00:00
|
|
|
Versioner: versioner,
|
2019-03-06 20:13:50 +00:00
|
|
|
},
|
|
|
|
SendPaymentChannel: make(chan PaymentChannelMessage),
|
|
|
|
ConfChannel: make(chan *chainntnfs.TxConfirmation),
|
|
|
|
RegisterConfChannel: make(chan *ConfRegistration),
|
|
|
|
RegisterSpendChannel: make(chan *SpendRegistration),
|
|
|
|
SpendChannel: make(chan *chainntnfs.SpendDetail),
|
|
|
|
TxPublishChannel: make(chan *wire.MsgTx),
|
|
|
|
SendOutputsChannel: make(chan wire.MsgTx),
|
|
|
|
SettleInvoiceChannel: make(chan lntypes.Preimage),
|
2020-05-29 09:27:47 +00:00
|
|
|
SingleInvoiceSubcribeChannel: make(chan *SingleInvoiceSubscription, 1),
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2019-03-25 09:31:03 +00:00
|
|
|
RouterSendPaymentChannel: make(chan RouterPaymentChannelMessage),
|
|
|
|
TrackPaymentChannel: make(chan TrackPaymentMessage),
|
|
|
|
|
2020-01-15 08:55:22 +00:00
|
|
|
SignOutputRawChannel: make(chan SignOutputRawRequest),
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
FailInvoiceChannel: make(chan lntypes.Hash, 2),
|
|
|
|
epochChannel: make(chan int32),
|
|
|
|
Height: testStartingHeight,
|
2019-12-09 15:33:33 +00:00
|
|
|
NodePubkey: testNodePubkey,
|
2019-12-09 15:34:02 +00:00
|
|
|
Signature: testSignature,
|
2019-12-09 15:47:23 +00:00
|
|
|
SignatureMsg: testSignatureMsg,
|
2020-05-20 11:42:20 +00:00
|
|
|
Invoices: make(map[lntypes.Hash]*lndclient.Invoice),
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lightningClient.lnd = &lnd
|
|
|
|
chainNotifier.lnd = &lnd
|
|
|
|
walletKit.lnd = &lnd
|
|
|
|
invoices.lnd = &lnd
|
2019-03-25 09:31:03 +00:00
|
|
|
router.lnd = &lnd
|
2019-12-09 15:34:02 +00:00
|
|
|
signer.lnd = &lnd
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2020-04-24 07:27:25 +00:00
|
|
|
// Also simulate the cached info that is loaded on startup.
|
|
|
|
info, _ := lightningClient.GetInfo(context.Background())
|
2020-04-22 13:48:11 +00:00
|
|
|
version, _ := versioner.GetVersion(context.Background())
|
2020-04-24 07:27:25 +00:00
|
|
|
lnd.LndServices.NodeAlias = info.Alias
|
|
|
|
lnd.LndServices.NodePubkey = info.IdentityPubkey
|
2020-04-22 13:48:11 +00:00
|
|
|
lnd.LndServices.Version = version
|
2020-04-24 07:27:25 +00:00
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
lnd.WaitForFinished = func() {
|
|
|
|
chainNotifier.WaitForFinished()
|
|
|
|
lightningClient.WaitForFinished()
|
|
|
|
invoices.WaitForFinished()
|
|
|
|
}
|
|
|
|
|
|
|
|
return &lnd
|
|
|
|
}
|
|
|
|
|
|
|
|
// PaymentChannelMessage is the data that passed through SendPaymentChannel.
|
|
|
|
type PaymentChannelMessage struct {
|
|
|
|
PaymentRequest string
|
2019-03-12 15:10:37 +00:00
|
|
|
Done chan lndclient.PaymentResult
|
2019-03-06 20:13:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-25 09:31:03 +00:00
|
|
|
// TrackPaymentMessage is the data that passed through TrackPaymentChannel.
|
|
|
|
type TrackPaymentMessage struct {
|
|
|
|
Hash lntypes.Hash
|
|
|
|
|
|
|
|
Updates chan lndclient.PaymentStatus
|
|
|
|
Errors chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
// RouterPaymentChannelMessage is the data that passed through RouterSendPaymentChannel.
|
|
|
|
type RouterPaymentChannelMessage struct {
|
|
|
|
lndclient.SendPaymentRequest
|
|
|
|
|
|
|
|
TrackPaymentMessage
|
|
|
|
}
|
|
|
|
|
2022-12-15 15:43:38 +00:00
|
|
|
// SingleInvoiceSubscription contains the single invoice subscribers.
|
2019-03-06 20:13:50 +00:00
|
|
|
type SingleInvoiceSubscription struct {
|
|
|
|
Hash lntypes.Hash
|
2019-03-12 15:10:37 +00:00
|
|
|
Update chan lndclient.InvoiceUpdate
|
2019-03-06 20:13:50 +00:00
|
|
|
Err chan error
|
|
|
|
}
|
|
|
|
|
2020-01-15 08:55:22 +00:00
|
|
|
// SignOutputRawRequest contains input data for a tx signing request.
|
|
|
|
type SignOutputRawRequest struct {
|
|
|
|
Tx *wire.MsgTx
|
2020-08-13 09:17:03 +00:00
|
|
|
SignDescriptors []*lndclient.SignDescriptor
|
2020-01-15 08:55:22 +00:00
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// LndMockServices provides a full set of mocked lnd services.
|
|
|
|
type LndMockServices struct {
|
|
|
|
lndclient.LndServices
|
|
|
|
|
|
|
|
SendPaymentChannel chan PaymentChannelMessage
|
|
|
|
SpendChannel chan *chainntnfs.SpendDetail
|
|
|
|
TxPublishChannel chan *wire.MsgTx
|
|
|
|
SendOutputsChannel chan wire.MsgTx
|
|
|
|
SettleInvoiceChannel chan lntypes.Preimage
|
|
|
|
FailInvoiceChannel chan lntypes.Hash
|
|
|
|
epochChannel chan int32
|
|
|
|
|
|
|
|
ConfChannel chan *chainntnfs.TxConfirmation
|
|
|
|
RegisterConfChannel chan *ConfRegistration
|
|
|
|
RegisterSpendChannel chan *SpendRegistration
|
|
|
|
|
|
|
|
SingleInvoiceSubcribeChannel chan *SingleInvoiceSubscription
|
|
|
|
|
2019-03-25 09:31:03 +00:00
|
|
|
RouterSendPaymentChannel chan RouterPaymentChannelMessage
|
|
|
|
TrackPaymentChannel chan TrackPaymentMessage
|
|
|
|
|
2020-01-15 08:55:22 +00:00
|
|
|
SignOutputRawChannel chan SignOutputRawRequest
|
|
|
|
|
2019-12-09 15:47:23 +00:00
|
|
|
Height int32
|
|
|
|
NodePubkey string
|
|
|
|
Signature []byte
|
|
|
|
SignatureMsg string
|
2019-03-06 20:13:50 +00:00
|
|
|
|
2023-01-09 15:36:52 +00:00
|
|
|
Transactions []lndclient.Transaction
|
|
|
|
Sweeps []string
|
|
|
|
SweepsVerbose []lnwallet.TransactionDetail
|
2019-12-10 00:16:41 +00:00
|
|
|
|
2020-05-20 11:42:20 +00:00
|
|
|
// Invoices is a set of invoices that have been created by the mock,
|
|
|
|
// keyed by hash string.
|
|
|
|
Invoices map[lntypes.Hash]*lndclient.Invoice
|
|
|
|
|
2021-12-22 18:11:50 +00:00
|
|
|
Channels []lndclient.ChannelInfo
|
|
|
|
ChannelEdges map[uint64]*lndclient.ChannelEdge
|
|
|
|
ClosedChannels []lndclient.ClosedChannel
|
|
|
|
ForwardingEvents []lndclient.ForwardingEvent
|
|
|
|
Payments []lndclient.Payment
|
|
|
|
MissionControlState []lndclient.MissionControlEntry
|
2020-05-18 14:23:02 +00:00
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
WaitForFinished func()
|
|
|
|
|
|
|
|
lock sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// NotifyHeight notifies a new block height.
|
|
|
|
func (s *LndMockServices) NotifyHeight(height int32) error {
|
|
|
|
s.Height = height
|
|
|
|
|
|
|
|
select {
|
|
|
|
case s.epochChannel <- height:
|
|
|
|
case <-time.After(Timeout):
|
|
|
|
return ErrTimeout
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-03-14 12:36:02 +00:00
|
|
|
// AddTx marks the given transaction as relevant.
|
2019-12-10 00:16:41 +00:00
|
|
|
func (s *LndMockServices) AddTx(tx *wire.MsgTx) {
|
|
|
|
s.lock.Lock()
|
2020-06-17 06:44:44 +00:00
|
|
|
s.Transactions = append(s.Transactions, lndclient.Transaction{
|
|
|
|
Tx: tx.Copy(),
|
|
|
|
})
|
2019-12-10 00:16:41 +00:00
|
|
|
s.lock.Unlock()
|
|
|
|
}
|
|
|
|
|
2019-03-06 20:13:50 +00:00
|
|
|
// IsDone checks whether all channels have been fully emptied. If not this may
|
|
|
|
// indicate unexpected behaviour of the code under test.
|
|
|
|
func (s *LndMockServices) IsDone() error {
|
|
|
|
select {
|
|
|
|
case <-s.SendPaymentChannel:
|
|
|
|
return errors.New("SendPaymentChannel not empty")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.SpendChannel:
|
|
|
|
return errors.New("SpendChannel not empty")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.TxPublishChannel:
|
|
|
|
return errors.New("TxPublishChannel not empty")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.SendOutputsChannel:
|
|
|
|
return errors.New("SendOutputsChannel not empty")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.SettleInvoiceChannel:
|
|
|
|
return errors.New("SettleInvoiceChannel not empty")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.ConfChannel:
|
|
|
|
return errors.New("ConfChannel not empty")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.RegisterConfChannel:
|
|
|
|
return errors.New("RegisterConfChannel not empty")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-s.RegisterSpendChannel:
|
|
|
|
return errors.New("RegisterSpendChannel not empty")
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeInvoice decodes a payment request string.
|
|
|
|
func (s *LndMockServices) DecodeInvoice(request string) (*zpay32.Invoice,
|
|
|
|
error) {
|
|
|
|
|
|
|
|
return zpay32.Decode(request, s.ChainParams)
|
|
|
|
}
|
2019-10-01 15:21:17 +00:00
|
|
|
|
|
|
|
func (s *LndMockServices) SetFeeEstimate(confTarget int32,
|
2019-11-16 00:16:30 +00:00
|
|
|
feeEstimate chainfee.SatPerKWeight) {
|
2019-10-01 15:21:17 +00:00
|
|
|
|
2023-03-29 13:56:40 +00:00
|
|
|
s.LndServices.WalletKit.(*mockWalletKit).setFeeEstimate(
|
|
|
|
confTarget, feeEstimate,
|
|
|
|
)
|
2019-10-01 15:21:17 +00:00
|
|
|
}
|