multi: move unit test backend funcs to new package
To avoid circular dependency issues between packages, we move the unit test backend creation function to a new package in the lntest parent package.
This commit is contained in:
8 files changed
+250
-215
No files matched your search
@@ -15,6 +15,7 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/blockcache"
|
"github.com/lightningnetwork/lnd/blockcache"
|
||||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
|
"github.com/lightningnetwork/lnd/lntest/unittest"
|
||||||
"github.com/lightningnetwork/lnd/lntest/wait"
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -61,7 +62,7 @@ func setUpNotifier(t *testing.T, bitcoindConn *chain.BitcoindConn,
|
|||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
notifier := New(
|
notifier := New(
|
||||||
bitcoindConn, chainntnfs.NetParams, spendHintCache,
|
bitcoindConn, unittest.NetParams, spendHintCache,
|
||||||
confirmHintCache, blockCache,
|
confirmHintCache, blockCache,
|
||||||
)
|
)
|
||||||
if err := notifier.Start(); err != nil {
|
if err := notifier.Start(); err != nil {
|
||||||
@@ -119,12 +120,12 @@ func TestHistoricalConfDetailsTxIndex(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testHistoricalConfDetailsTxIndex(t *testing.T, rpcPolling bool) {
|
func testHistoricalConfDetailsTxIndex(t *testing.T, rpcPolling bool) {
|
||||||
miner := chainntnfs.NewMiner(
|
miner := unittest.NewMiner(
|
||||||
t, chainntnfs.NetParams, []string{"--txindex"}, true, 25,
|
t, unittest.NetParams, []string{"--txindex"}, true, 25,
|
||||||
)
|
)
|
||||||
|
|
||||||
bitcoindConn := chainntnfs.NewBitcoindBackend(
|
bitcoindConn := unittest.NewBitcoindBackend(
|
||||||
t, chainntnfs.NetParams, miner.P2PAddress(), true, rpcPolling,
|
t, unittest.NetParams, miner.P2PAddress(), true, rpcPolling,
|
||||||
)
|
)
|
||||||
|
|
||||||
hintCache := initHintCache(t)
|
hintCache := initHintCache(t)
|
||||||
@@ -217,10 +218,10 @@ func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func testHistoricalConfDetailsNoTxIndex(t *testing.T, rpcpolling bool) {
|
func testHistoricalConfDetailsNoTxIndex(t *testing.T, rpcpolling bool) {
|
||||||
miner := chainntnfs.NewMiner(t, chainntnfs.NetParams, nil, true, 25)
|
miner := unittest.NewMiner(t, unittest.NetParams, nil, true, 25)
|
||||||
|
|
||||||
bitcoindConn := chainntnfs.NewBitcoindBackend(
|
bitcoindConn := unittest.NewBitcoindBackend(
|
||||||
t, chainntnfs.NetParams, miner.P2PAddress(), false, rpcpolling,
|
t, unittest.NetParams, miner.P2PAddress(), false, rpcpolling,
|
||||||
)
|
)
|
||||||
|
|
||||||
hintCache := initHintCache(t)
|
hintCache := initHintCache(t)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/blockcache"
|
"github.com/lightningnetwork/lnd/blockcache"
|
||||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
|
"github.com/lightningnetwork/lnd/lntest/unittest"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -55,7 +56,7 @@ func setUpNotifier(t *testing.T, h *rpctest.Harness) *BtcdNotifier {
|
|||||||
|
|
||||||
rpcCfg := h.RPCConfig()
|
rpcCfg := h.RPCConfig()
|
||||||
notifier, err := New(
|
notifier, err := New(
|
||||||
&rpcCfg, chainntnfs.NetParams, hintCache, hintCache, blockCache,
|
&rpcCfg, unittest.NetParams, hintCache, hintCache, blockCache,
|
||||||
)
|
)
|
||||||
require.NoError(t, err, "unable to create notifier")
|
require.NoError(t, err, "unable to create notifier")
|
||||||
if err := notifier.Start(); err != nil {
|
if err := notifier.Start(); err != nil {
|
||||||
@@ -73,8 +74,8 @@ func setUpNotifier(t *testing.T, h *rpctest.Harness) *BtcdNotifier {
|
|||||||
func TestHistoricalConfDetailsTxIndex(t *testing.T) {
|
func TestHistoricalConfDetailsTxIndex(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
harness := chainntnfs.NewMiner(
|
harness := unittest.NewMiner(
|
||||||
t, chainntnfs.NetParams, []string{"--txindex"}, true, 25,
|
t, unittest.NetParams, []string{"--txindex"}, true, 25,
|
||||||
)
|
)
|
||||||
|
|
||||||
notifier := setUpNotifier(t, harness)
|
notifier := setUpNotifier(t, harness)
|
||||||
@@ -145,7 +146,7 @@ func TestHistoricalConfDetailsTxIndex(t *testing.T) {
|
|||||||
func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
|
func TestHistoricalConfDetailsNoTxIndex(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
harness := chainntnfs.NewMiner(t, chainntnfs.NetParams, nil, true, 25)
|
harness := unittest.NewMiner(t, unittest.NetParams, nil, true, 25)
|
||||||
|
|
||||||
notifier := setUpNotifier(t, harness)
|
notifier := setUpNotifier(t, harness)
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
|
"github.com/lightningnetwork/lnd/chainntnfs/btcdnotify"
|
||||||
"github.com/lightningnetwork/lnd/chainntnfs/neutrinonotify"
|
"github.com/lightningnetwork/lnd/chainntnfs/neutrinonotify"
|
||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
|
"github.com/lightningnetwork/lnd/lntest/unittest"
|
||||||
"github.com/lightningnetwork/lnd/lnutils"
|
"github.com/lightningnetwork/lnd/lnutils"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -1043,8 +1044,8 @@ func testReorgConf(miner *rpctest.Harness,
|
|||||||
notifier chainntnfs.TestChainNotifier, scriptDispatch bool, t *testing.T) {
|
notifier chainntnfs.TestChainNotifier, scriptDispatch bool, t *testing.T) {
|
||||||
|
|
||||||
// Set up a new miner that we can use to cause a reorg.
|
// Set up a new miner that we can use to cause a reorg.
|
||||||
miner2 := chainntnfs.NewMiner(
|
miner2 := unittest.NewMiner(
|
||||||
t, chainntnfs.NetParams, []string{"--txindex"}, false, 0,
|
t, unittest.NetParams, []string{"--txindex"}, false, 0,
|
||||||
)
|
)
|
||||||
|
|
||||||
// We start by connecting the new miner to our original miner,
|
// We start by connecting the new miner to our original miner,
|
||||||
@@ -1199,8 +1200,8 @@ func testReorgSpend(miner *rpctest.Harness,
|
|||||||
require.NoError(t, err, "unable to register for spend")
|
require.NoError(t, err, "unable to register for spend")
|
||||||
|
|
||||||
// Set up a new miner that we can use to cause a reorg.
|
// Set up a new miner that we can use to cause a reorg.
|
||||||
miner2 := chainntnfs.NewMiner(
|
miner2 := unittest.NewMiner(
|
||||||
t, chainntnfs.NetParams, []string{"--txindex"}, false, 0,
|
t, unittest.NetParams, []string{"--txindex"}, false, 0,
|
||||||
)
|
)
|
||||||
|
|
||||||
// We start by connecting the new miner to our original miner, in order
|
// We start by connecting the new miner to our original miner, in order
|
||||||
@@ -1517,8 +1518,8 @@ func testCatchUpOnMissedBlocksWithReorg(miner1 *rpctest.Harness,
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
// Set up a new miner that we can use to cause a reorg.
|
// Set up a new miner that we can use to cause a reorg.
|
||||||
miner2 := chainntnfs.NewMiner(
|
miner2 := unittest.NewMiner(
|
||||||
t, chainntnfs.NetParams, []string{"--txindex"}, false, 0,
|
t, unittest.NetParams, []string{"--txindex"}, false, 0,
|
||||||
)
|
)
|
||||||
|
|
||||||
// We start by connecting the new miner to our original miner,
|
// We start by connecting the new miner to our original miner,
|
||||||
@@ -1890,7 +1891,7 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
|
|||||||
// dedicated miner to generate blocks, cause re-orgs, etc. We'll set up
|
// dedicated miner to generate blocks, cause re-orgs, etc. We'll set up
|
||||||
// this node with a chain length of 125, so we have plenty of BTC to
|
// this node with a chain length of 125, so we have plenty of BTC to
|
||||||
// play around with.
|
// play around with.
|
||||||
miner := chainntnfs.NewMiner(t, chainntnfs.NetParams, nil, true, 25)
|
miner := unittest.NewMiner(t, unittest.NetParams, nil, true, 25)
|
||||||
|
|
||||||
rpcConfig := miner.RPCConfig()
|
rpcConfig := miner.RPCConfig()
|
||||||
p2pAddr := miner.P2PAddress()
|
p2pAddr := miner.P2PAddress()
|
||||||
@@ -1929,24 +1930,24 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
|
|||||||
switch notifierType {
|
switch notifierType {
|
||||||
case "bitcoind":
|
case "bitcoind":
|
||||||
var bitcoindConn *chain.BitcoindConn
|
var bitcoindConn *chain.BitcoindConn
|
||||||
bitcoindConn = chainntnfs.NewBitcoindBackend(
|
bitcoindConn = unittest.NewBitcoindBackend(
|
||||||
t, chainntnfs.NetParams, p2pAddr, true, false,
|
t, unittest.NetParams, p2pAddr, true, false,
|
||||||
)
|
)
|
||||||
newNotifier = func() (chainntnfs.TestChainNotifier, error) {
|
newNotifier = func() (chainntnfs.TestChainNotifier, error) {
|
||||||
return bitcoindnotify.New(
|
return bitcoindnotify.New(
|
||||||
bitcoindConn, chainntnfs.NetParams,
|
bitcoindConn, unittest.NetParams,
|
||||||
hintCache, hintCache, blockCache,
|
hintCache, hintCache, blockCache,
|
||||||
), nil
|
), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
case "bitcoind-rpc-polling":
|
case "bitcoind-rpc-polling":
|
||||||
var bitcoindConn *chain.BitcoindConn
|
var bitcoindConn *chain.BitcoindConn
|
||||||
bitcoindConn = chainntnfs.NewBitcoindBackend(
|
bitcoindConn = unittest.NewBitcoindBackend(
|
||||||
t, chainntnfs.NetParams, p2pAddr, true, true,
|
t, unittest.NetParams, p2pAddr, true, true,
|
||||||
)
|
)
|
||||||
newNotifier = func() (chainntnfs.TestChainNotifier, error) {
|
newNotifier = func() (chainntnfs.TestChainNotifier, error) {
|
||||||
return bitcoindnotify.New(
|
return bitcoindnotify.New(
|
||||||
bitcoindConn, chainntnfs.NetParams,
|
bitcoindConn, unittest.NetParams,
|
||||||
hintCache, hintCache, blockCache,
|
hintCache, hintCache, blockCache,
|
||||||
), nil
|
), nil
|
||||||
}
|
}
|
||||||
@@ -1954,15 +1955,15 @@ func TestInterfaces(t *testing.T, targetBackEnd string) {
|
|||||||
case "btcd":
|
case "btcd":
|
||||||
newNotifier = func() (chainntnfs.TestChainNotifier, error) {
|
newNotifier = func() (chainntnfs.TestChainNotifier, error) {
|
||||||
return btcdnotify.New(
|
return btcdnotify.New(
|
||||||
&rpcConfig, chainntnfs.NetParams,
|
&rpcConfig, unittest.NetParams,
|
||||||
hintCache, hintCache, blockCache,
|
hintCache, hintCache, blockCache,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
case "neutrino":
|
case "neutrino":
|
||||||
var spvNode *neutrino.ChainService
|
var spvNode *neutrino.ChainService
|
||||||
spvNode = chainntnfs.NewNeutrinoBackend(
|
spvNode = unittest.NewNeutrinoBackend(
|
||||||
t, chainntnfs.NetParams, p2pAddr,
|
t, unittest.NetParams, p2pAddr,
|
||||||
)
|
)
|
||||||
newNotifier = func() (chainntnfs.TestChainNotifier, error) {
|
newNotifier = func() (chainntnfs.TestChainNotifier, error) {
|
||||||
return neutrinonotify.New(
|
return neutrinonotify.New(
|
||||||
|
|||||||
+4
-180
@@ -6,25 +6,17 @@ package chainntnfs
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/btcec/v2"
|
"github.com/btcsuite/btcd/btcec/v2"
|
||||||
"github.com/btcsuite/btcd/btcjson"
|
"github.com/btcsuite/btcd/btcjson"
|
||||||
"github.com/btcsuite/btcd/btcutil"
|
"github.com/btcsuite/btcd/btcutil"
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
|
||||||
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
"github.com/btcsuite/btcd/integration/rpctest"
|
"github.com/btcsuite/btcd/integration/rpctest"
|
||||||
"github.com/btcsuite/btcd/txscript"
|
"github.com/btcsuite/btcd/txscript"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
"github.com/btcsuite/btcwallet/chain"
|
"github.com/lightningnetwork/lnd/lntest/unittest"
|
||||||
"github.com/btcsuite/btcwallet/walletdb"
|
|
||||||
"github.com/lightninglabs/neutrino"
|
|
||||||
"github.com/lightningnetwork/lnd/kvdb"
|
|
||||||
"github.com/lightningnetwork/lnd/lntest/port"
|
|
||||||
"github.com/lightningnetwork/lnd/lntest/wait"
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -35,10 +27,6 @@ var (
|
|||||||
TrickleInterval = 10 * time.Millisecond
|
TrickleInterval = 10 * time.Millisecond
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
NetParams = &chaincfg.RegressionNetParams
|
|
||||||
)
|
|
||||||
|
|
||||||
// randPubKeyHashScript generates a P2PKH script that pays to the public key of
|
// randPubKeyHashScript generates a P2PKH script that pays to the public key of
|
||||||
// a randomly-generated private key.
|
// a randomly-generated private key.
|
||||||
func randPubKeyHashScript() ([]byte, *btcec.PrivateKey, error) {
|
func randPubKeyHashScript() ([]byte, *btcec.PrivateKey, error) {
|
||||||
@@ -48,7 +36,9 @@ func randPubKeyHashScript() ([]byte, *btcec.PrivateKey, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pubKeyHash := btcutil.Hash160(privKey.PubKey().SerializeCompressed())
|
pubKeyHash := btcutil.Hash160(privKey.PubKey().SerializeCompressed())
|
||||||
addrScript, err := btcutil.NewAddressPubKeyHash(pubKeyHash, NetParams)
|
addrScript, err := btcutil.NewAddressPubKeyHash(
|
||||||
|
pubKeyHash, unittest.NetParams,
|
||||||
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -162,169 +152,3 @@ func CreateSpendTx(t *testing.T, prevOutPoint *wire.OutPoint,
|
|||||||
|
|
||||||
return spendingTx
|
return spendingTx
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMiner spawns testing harness backed by a btcd node that can serve as a
|
|
||||||
// miner.
|
|
||||||
func NewMiner(t *testing.T, netParams *chaincfg.Params, extraArgs []string,
|
|
||||||
createChain bool, spendableOutputs uint32) *rpctest.Harness {
|
|
||||||
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
// Add the trickle interval argument to the extra args.
|
|
||||||
trickle := fmt.Sprintf("--trickleinterval=%v", TrickleInterval)
|
|
||||||
extraArgs = append(extraArgs, trickle)
|
|
||||||
|
|
||||||
node, err := rpctest.New(netParams, nil, extraArgs, "")
|
|
||||||
require.NoError(t, err, "unable to create backend node")
|
|
||||||
t.Cleanup(func() {
|
|
||||||
require.NoError(t, node.TearDown())
|
|
||||||
})
|
|
||||||
|
|
||||||
// We want to overwrite some of the connection settings to make the
|
|
||||||
// tests more robust. We might need to restart the backend while there
|
|
||||||
// are already blocks present, which will take a bit longer than the
|
|
||||||
// 1 second the default settings amount to. Doubling both values will
|
|
||||||
// give us retries up to 4 seconds.
|
|
||||||
node.MaxConnRetries = rpctest.DefaultMaxConnectionRetries * 2
|
|
||||||
node.ConnectionRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2
|
|
||||||
|
|
||||||
if err := node.SetUp(createChain, spendableOutputs); err != nil {
|
|
||||||
t.Fatalf("unable to set up backend node: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return node
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewBitcoindBackend spawns a new bitcoind node that connects to a miner at the
|
|
||||||
// specified address. The txindex boolean can be set to determine whether the
|
|
||||||
// backend node should maintain a transaction index. The rpcpolling boolean
|
|
||||||
// can be set to determine whether bitcoind's RPC polling interface should be
|
|
||||||
// used for block and tx notifications or if its ZMQ interface should be used.
|
|
||||||
// A connection to the newly spawned bitcoind node is returned.
|
|
||||||
func NewBitcoindBackend(t *testing.T, netParams *chaincfg.Params,
|
|
||||||
minerAddr string, txindex, rpcpolling bool) *chain.BitcoindConn {
|
|
||||||
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
tempBitcoindDir := t.TempDir()
|
|
||||||
|
|
||||||
rpcPort := port.NextAvailablePort()
|
|
||||||
zmqBlockPort := port.NextAvailablePort()
|
|
||||||
zmqTxPort := port.NextAvailablePort()
|
|
||||||
zmqBlockHost := fmt.Sprintf("tcp://127.0.0.1:%d", zmqBlockPort)
|
|
||||||
zmqTxHost := fmt.Sprintf("tcp://127.0.0.1:%d", zmqTxPort)
|
|
||||||
|
|
||||||
args := []string{
|
|
||||||
"-connect=" + minerAddr,
|
|
||||||
"-datadir=" + tempBitcoindDir,
|
|
||||||
"-regtest",
|
|
||||||
"-rpcauth=weks:469e9bb14ab2360f8e226efed5ca6fd$507c670e800a952" +
|
|
||||||
"84294edb5773b05544b220110063096c221be9933c82d38e1",
|
|
||||||
fmt.Sprintf("-rpcport=%d", rpcPort),
|
|
||||||
"-disablewallet",
|
|
||||||
"-zmqpubrawblock=" + zmqBlockHost,
|
|
||||||
"-zmqpubrawtx=" + zmqTxHost,
|
|
||||||
}
|
|
||||||
if txindex {
|
|
||||||
args = append(args, "-txindex")
|
|
||||||
}
|
|
||||||
|
|
||||||
bitcoind := exec.Command("bitcoind", args...)
|
|
||||||
if err := bitcoind.Start(); err != nil {
|
|
||||||
t.Fatalf("unable to start bitcoind: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
_ = bitcoind.Process.Kill()
|
|
||||||
_ = bitcoind.Wait()
|
|
||||||
})
|
|
||||||
|
|
||||||
// Wait for the bitcoind instance to start up.
|
|
||||||
host := fmt.Sprintf("127.0.0.1:%d", rpcPort)
|
|
||||||
cfg := &chain.BitcoindConfig{
|
|
||||||
ChainParams: netParams,
|
|
||||||
Host: host,
|
|
||||||
User: "weks",
|
|
||||||
Pass: "weks",
|
|
||||||
// Fields only required for pruned nodes, not needed for these
|
|
||||||
// tests.
|
|
||||||
Dialer: nil,
|
|
||||||
PrunedModeMaxPeers: 0,
|
|
||||||
}
|
|
||||||
|
|
||||||
if rpcpolling {
|
|
||||||
cfg.PollingConfig = &chain.PollingConfig{
|
|
||||||
BlockPollingInterval: time.Millisecond * 20,
|
|
||||||
TxPollingInterval: time.Millisecond * 20,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
cfg.ZMQConfig = &chain.ZMQConfig{
|
|
||||||
ZMQBlockHost: zmqBlockHost,
|
|
||||||
ZMQTxHost: zmqTxHost,
|
|
||||||
ZMQReadDeadline: 5 * time.Second,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var conn *chain.BitcoindConn
|
|
||||||
err := wait.NoError(func() error {
|
|
||||||
var err error
|
|
||||||
conn, err = chain.NewBitcoindConn(cfg)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return conn.Start()
|
|
||||||
}, 10*time.Second)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to establish connection to bitcoind at %v: "+
|
|
||||||
"%v", tempBitcoindDir, err)
|
|
||||||
}
|
|
||||||
t.Cleanup(conn.Stop)
|
|
||||||
|
|
||||||
return conn
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewNeutrinoBackend spawns a new neutrino node that connects to a miner at
|
|
||||||
// the specified address.
|
|
||||||
func NewNeutrinoBackend(t *testing.T, netParams *chaincfg.Params,
|
|
||||||
minerAddr string) *neutrino.ChainService {
|
|
||||||
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
spvDir := t.TempDir()
|
|
||||||
|
|
||||||
dbName := filepath.Join(spvDir, "neutrino.db")
|
|
||||||
spvDatabase, err := walletdb.Create(
|
|
||||||
"bdb", dbName, true, kvdb.DefaultDBTimeout,
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create walletdb: %v", err)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
spvDatabase.Close()
|
|
||||||
})
|
|
||||||
|
|
||||||
// Create an instance of neutrino connected to the running btcd
|
|
||||||
// instance.
|
|
||||||
spvConfig := neutrino.Config{
|
|
||||||
DataDir: spvDir,
|
|
||||||
Database: spvDatabase,
|
|
||||||
ChainParams: *netParams,
|
|
||||||
ConnectPeers: []string{minerAddr},
|
|
||||||
}
|
|
||||||
spvNode, err := neutrino.NewChainService(spvConfig)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("unable to create neutrino: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// We'll also wait for the instance to sync up fully to the chain
|
|
||||||
// generated by the btcd instance.
|
|
||||||
spvNode.Start()
|
|
||||||
for !spvNode.IsCurrent() {
|
|
||||||
time.Sleep(time.Millisecond * 100)
|
|
||||||
}
|
|
||||||
t.Cleanup(func() {
|
|
||||||
spvNode.Stop()
|
|
||||||
})
|
|
||||||
|
|
||||||
return spvNode
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,207 @@
|
|||||||
|
package unittest
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
|
"github.com/btcsuite/btcd/integration/rpctest"
|
||||||
|
"github.com/btcsuite/btcwallet/chain"
|
||||||
|
"github.com/btcsuite/btcwallet/walletdb"
|
||||||
|
"github.com/lightninglabs/neutrino"
|
||||||
|
"github.com/lightningnetwork/lnd/kvdb"
|
||||||
|
"github.com/lightningnetwork/lnd/lntest/port"
|
||||||
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// TrickleInterval is the interval at which the miner should trickle
|
||||||
|
// transactions to its peers. We'll set it small to ensure the miner
|
||||||
|
// propagates transactions quickly in the tests.
|
||||||
|
TrickleInterval = 10 * time.Millisecond
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// NetParams are the default network parameters for the tests.
|
||||||
|
NetParams = &chaincfg.RegressionNetParams
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewMiner spawns testing harness backed by a btcd node that can serve as a
|
||||||
|
// miner.
|
||||||
|
func NewMiner(t *testing.T, netParams *chaincfg.Params, extraArgs []string,
|
||||||
|
createChain bool, spendableOutputs uint32) *rpctest.Harness {
|
||||||
|
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
// Add the trickle interval argument to the extra args.
|
||||||
|
trickle := fmt.Sprintf("--trickleinterval=%v", TrickleInterval)
|
||||||
|
extraArgs = append(extraArgs, trickle)
|
||||||
|
|
||||||
|
node, err := rpctest.New(netParams, nil, extraArgs, "")
|
||||||
|
require.NoError(t, err, "unable to create backend node")
|
||||||
|
t.Cleanup(func() {
|
||||||
|
require.NoError(t, node.TearDown())
|
||||||
|
})
|
||||||
|
|
||||||
|
// We want to overwrite some of the connection settings to make the
|
||||||
|
// tests more robust. We might need to restart the backend while there
|
||||||
|
// are already blocks present, which will take a bit longer than the
|
||||||
|
// 1 second the default settings amount to. Doubling both values will
|
||||||
|
// give us retries up to 4 seconds.
|
||||||
|
node.MaxConnRetries = rpctest.DefaultMaxConnectionRetries * 2
|
||||||
|
node.ConnectionRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2
|
||||||
|
|
||||||
|
if err := node.SetUp(createChain, spendableOutputs); err != nil {
|
||||||
|
t.Fatalf("unable to set up backend node: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return node
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewBitcoindBackend spawns a new bitcoind node that connects to a miner at the
|
||||||
|
// specified address. The txindex boolean can be set to determine whether the
|
||||||
|
// backend node should maintain a transaction index. The rpcpolling boolean
|
||||||
|
// can be set to determine whether bitcoind's RPC polling interface should be
|
||||||
|
// used for block and tx notifications or if its ZMQ interface should be used.
|
||||||
|
// A connection to the newly spawned bitcoind node is returned.
|
||||||
|
func NewBitcoindBackend(t *testing.T, netParams *chaincfg.Params,
|
||||||
|
minerAddr string, txindex, rpcpolling bool) *chain.BitcoindConn {
|
||||||
|
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
tempBitcoindDir := t.TempDir()
|
||||||
|
|
||||||
|
rpcPort := port.NextAvailablePort()
|
||||||
|
zmqBlockPort := port.NextAvailablePort()
|
||||||
|
zmqTxPort := port.NextAvailablePort()
|
||||||
|
zmqBlockHost := fmt.Sprintf("tcp://127.0.0.1:%d", zmqBlockPort)
|
||||||
|
zmqTxHost := fmt.Sprintf("tcp://127.0.0.1:%d", zmqTxPort)
|
||||||
|
|
||||||
|
args := []string{
|
||||||
|
"-connect=" + minerAddr,
|
||||||
|
"-datadir=" + tempBitcoindDir,
|
||||||
|
"-regtest",
|
||||||
|
"-rpcauth=weks:469e9bb14ab2360f8e226efed5ca6fd$507c670e800a95" +
|
||||||
|
"284294edb5773b05544b220110063096c221be9933c82d38e1",
|
||||||
|
fmt.Sprintf("-rpcport=%d", rpcPort),
|
||||||
|
"-disablewallet",
|
||||||
|
"-zmqpubrawblock=" + zmqBlockHost,
|
||||||
|
"-zmqpubrawtx=" + zmqTxHost,
|
||||||
|
}
|
||||||
|
if txindex {
|
||||||
|
args = append(args, "-txindex")
|
||||||
|
}
|
||||||
|
|
||||||
|
bitcoind := exec.Command("bitcoind", args...)
|
||||||
|
if err := bitcoind.Start(); err != nil {
|
||||||
|
t.Fatalf("unable to start bitcoind: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
_ = bitcoind.Process.Kill()
|
||||||
|
_ = bitcoind.Wait()
|
||||||
|
})
|
||||||
|
|
||||||
|
// Wait for the bitcoind instance to start up.
|
||||||
|
time.Sleep(time.Second)
|
||||||
|
|
||||||
|
host := fmt.Sprintf("127.0.0.1:%d", rpcPort)
|
||||||
|
cfg := &chain.BitcoindConfig{
|
||||||
|
ChainParams: netParams,
|
||||||
|
Host: host,
|
||||||
|
User: "weks",
|
||||||
|
Pass: "weks",
|
||||||
|
// Fields only required for pruned nodes, not needed for these
|
||||||
|
// tests.
|
||||||
|
Dialer: nil,
|
||||||
|
PrunedModeMaxPeers: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
if rpcpolling {
|
||||||
|
cfg.PollingConfig = &chain.PollingConfig{
|
||||||
|
BlockPollingInterval: time.Millisecond * 20,
|
||||||
|
TxPollingInterval: time.Millisecond * 20,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
cfg.ZMQConfig = &chain.ZMQConfig{
|
||||||
|
ZMQBlockHost: zmqBlockHost,
|
||||||
|
ZMQTxHost: zmqTxHost,
|
||||||
|
ZMQReadDeadline: 5 * time.Second,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var conn *chain.BitcoindConn
|
||||||
|
err := wait.NoError(func() error {
|
||||||
|
var err error
|
||||||
|
conn, err = chain.NewBitcoindConn(cfg)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return conn.Start()
|
||||||
|
}, 10*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to establish connection to bitcoind at %v: "+
|
||||||
|
"%v", tempBitcoindDir, err)
|
||||||
|
}
|
||||||
|
t.Cleanup(conn.Stop)
|
||||||
|
|
||||||
|
return conn
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewNeutrinoBackend spawns a new neutrino node that connects to a miner at
|
||||||
|
// the specified address.
|
||||||
|
func NewNeutrinoBackend(t *testing.T, netParams *chaincfg.Params,
|
||||||
|
minerAddr string) *neutrino.ChainService {
|
||||||
|
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
spvDir := t.TempDir()
|
||||||
|
|
||||||
|
dbName := filepath.Join(spvDir, "neutrino.db")
|
||||||
|
spvDatabase, err := walletdb.Create(
|
||||||
|
"bdb", dbName, true, kvdb.DefaultDBTimeout,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to create walletdb: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
spvDatabase.Close()
|
||||||
|
})
|
||||||
|
|
||||||
|
// Create an instance of neutrino connected to the running btcd
|
||||||
|
// instance.
|
||||||
|
spvConfig := neutrino.Config{
|
||||||
|
DataDir: spvDir,
|
||||||
|
Database: spvDatabase,
|
||||||
|
ChainParams: *netParams,
|
||||||
|
ConnectPeers: []string{minerAddr},
|
||||||
|
}
|
||||||
|
spvNode, err := neutrino.NewChainService(spvConfig)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unable to create neutrino: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We'll also wait for the instance to sync up fully to the chain
|
||||||
|
// generated by the btcd instance.
|
||||||
|
_ = spvNode.Start()
|
||||||
|
for !spvNode.IsCurrent() {
|
||||||
|
time.Sleep(time.Millisecond * 100)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() {
|
||||||
|
_ = spvNode.Stop()
|
||||||
|
})
|
||||||
|
|
||||||
|
return spvNode
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
// Before we start any node, we need to make sure that any btcd or
|
||||||
|
// bitcoind node that is started through the RPC harness uses a unique
|
||||||
|
// port as well to avoid any port collisions.
|
||||||
|
rpctest.ListenAddressGenerator =
|
||||||
|
port.GenerateSystemUniqueListenerAddresses
|
||||||
|
}
|
||||||
@@ -18,8 +18,8 @@ import (
|
|||||||
"github.com/btcsuite/btcwallet/chain"
|
"github.com/btcsuite/btcwallet/chain"
|
||||||
"github.com/btcsuite/btcwallet/waddrmgr"
|
"github.com/btcsuite/btcwallet/waddrmgr"
|
||||||
"github.com/lightningnetwork/lnd/blockcache"
|
"github.com/lightningnetwork/lnd/blockcache"
|
||||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
|
||||||
"github.com/lightningnetwork/lnd/input"
|
"github.com/lightningnetwork/lnd/input"
|
||||||
|
"github.com/lightningnetwork/lnd/lntest/unittest"
|
||||||
"github.com/lightningnetwork/lnd/lnwallet"
|
"github.com/lightningnetwork/lnd/lnwallet"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -326,7 +326,7 @@ func newTestWallet(t *testing.T, netParams *chaincfg.Params,
|
|||||||
func getChainBackend(t *testing.T, netParams *chaincfg.Params) (chain.Interface,
|
func getChainBackend(t *testing.T, netParams *chaincfg.Params) (chain.Interface,
|
||||||
*rpctest.Harness) {
|
*rpctest.Harness) {
|
||||||
|
|
||||||
miningNode := chainntnfs.NewMiner(
|
miningNode := unittest.NewMiner(
|
||||||
t, netParams, []string{"--txindex"}, true, 25,
|
t, netParams, []string{"--txindex"}, true, 25,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import (
|
|||||||
"github.com/lightningnetwork/lnd/keychain"
|
"github.com/lightningnetwork/lnd/keychain"
|
||||||
"github.com/lightningnetwork/lnd/kvdb"
|
"github.com/lightningnetwork/lnd/kvdb"
|
||||||
"github.com/lightningnetwork/lnd/labels"
|
"github.com/lightningnetwork/lnd/labels"
|
||||||
|
"github.com/lightningnetwork/lnd/lntest/unittest"
|
||||||
"github.com/lightningnetwork/lnd/lntest/wait"
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
||||||
"github.com/lightningnetwork/lnd/lnwallet"
|
"github.com/lightningnetwork/lnd/lnwallet"
|
||||||
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
|
"github.com/lightningnetwork/lnd/lnwallet/btcwallet"
|
||||||
@@ -2120,7 +2121,7 @@ func testReorgWalletBalance(r *rpctest.Harness, w *lnwallet.LightningWallet,
|
|||||||
|
|
||||||
// Now we cause a reorganization as follows.
|
// Now we cause a reorganization as follows.
|
||||||
// Step 1: create a new miner and start it.
|
// Step 1: create a new miner and start it.
|
||||||
r2 := chainntnfs.NewMiner(
|
r2 := unittest.NewMiner(
|
||||||
t, r.ActiveNet, []string{"--txindex"}, false, 0,
|
t, r.ActiveNet, []string{"--txindex"}, false, 0,
|
||||||
)
|
)
|
||||||
newBalance, err := w.ConfirmedBalance(1, lnwallet.DefaultAccountName)
|
newBalance, err := w.ConfirmedBalance(1, lnwallet.DefaultAccountName)
|
||||||
@@ -3101,7 +3102,7 @@ func TestLightningWallet(t *testing.T, targetBackEnd string) {
|
|||||||
// dedicated miner to generate blocks, cause re-orgs, etc. We'll set
|
// dedicated miner to generate blocks, cause re-orgs, etc. We'll set
|
||||||
// up this node with a chain length of 125, so we have plenty of BTC
|
// up this node with a chain length of 125, so we have plenty of BTC
|
||||||
// to play around with.
|
// to play around with.
|
||||||
miningNode := chainntnfs.NewMiner(
|
miningNode := unittest.NewMiner(
|
||||||
t, netParams, []string{"--txindex"}, true, 25,
|
t, netParams, []string{"--txindex"}, true, 25,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -25,9 +25,9 @@ import (
|
|||||||
_ "github.com/btcsuite/btcwallet/walletdb/bdb" // Required to register the boltdb walletdb implementation.
|
_ "github.com/btcsuite/btcwallet/walletdb/bdb" // Required to register the boltdb walletdb implementation.
|
||||||
"github.com/lightninglabs/neutrino"
|
"github.com/lightninglabs/neutrino"
|
||||||
"github.com/lightningnetwork/lnd/blockcache"
|
"github.com/lightningnetwork/lnd/blockcache"
|
||||||
"github.com/lightningnetwork/lnd/chainntnfs"
|
|
||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
"github.com/lightningnetwork/lnd/kvdb"
|
"github.com/lightningnetwork/lnd/kvdb"
|
||||||
|
"github.com/lightningnetwork/lnd/lntest/unittest"
|
||||||
"github.com/lightningnetwork/lnd/lntest/wait"
|
"github.com/lightningnetwork/lnd/lntest/wait"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -482,7 +482,7 @@ func testFilterBlockDisconnected(node *rpctest.Harness,
|
|||||||
|
|
||||||
// Create a node that has a shorter chain than the main chain, so we
|
// Create a node that has a shorter chain than the main chain, so we
|
||||||
// can trigger a reorg.
|
// can trigger a reorg.
|
||||||
reorgNode := chainntnfs.NewMiner(
|
reorgNode := unittest.NewMiner(
|
||||||
t, netParams, []string{"--txindex"}, true, 5,
|
t, netParams, []string{"--txindex"}, true, 5,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -984,7 +984,7 @@ func TestFilteredChainView(t *testing.T) {
|
|||||||
// dedicated miner to generate blocks, cause re-orgs, etc. We'll set up
|
// dedicated miner to generate blocks, cause re-orgs, etc. We'll set up
|
||||||
// this node with a chain length of 125, so we have plenty of BTC to
|
// this node with a chain length of 125, so we have plenty of BTC to
|
||||||
// play around with.
|
// play around with.
|
||||||
miner := chainntnfs.NewMiner(
|
miner := unittest.NewMiner(
|
||||||
t, netParams, []string{"--txindex"}, true, 25,
|
t, netParams, []string{"--txindex"}, true, 25,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user