lntest/unittest: make sure miner is connected to bitcoind

We change how the `bitcoind` node connects to the miner by creating a
temp RPC client and use it to connect to the miner. In addition we also
assert the connection is made.
This commit is contained in:
yyforyongyu 2025-02-25 16:45:36 +08:00
parent 245ea85894
commit 175301628f
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868

View file

@ -10,6 +10,7 @@ import (
"github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/integration/rpctest" "github.com/btcsuite/btcd/integration/rpctest"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcwallet/chain" "github.com/btcsuite/btcwallet/chain"
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
"github.com/lightninglabs/neutrino" "github.com/lightninglabs/neutrino"
@ -101,7 +102,6 @@ func NewBitcoindBackend(t *testing.T, netParams *chaincfg.Params,
netParams.DefaultPort = fmt.Sprintf("%d", p2pPort) netParams.DefaultPort = fmt.Sprintf("%d", p2pPort)
args := []string{ args := []string{
"-connect=" + minerAddr,
"-datadir=" + tempBitcoindDir, "-datadir=" + tempBitcoindDir,
"-regtest", "-regtest",
"-rpcauth=weks:469e9bb14ab2360f8e226efed5ca6fd$507c670e800a95" + "-rpcauth=weks:469e9bb14ab2360f8e226efed5ca6fd$507c670e800a95" +
@ -178,6 +178,48 @@ func NewBitcoindBackend(t *testing.T, netParams *chaincfg.Params,
} }
t.Cleanup(conn.Stop) t.Cleanup(conn.Stop)
// Assert that the connection with the miner is made.
//
// Create a new RPC client.
rpcCfg := rpcclient.ConnConfig{
Host: cfg.Host,
User: cfg.User,
Pass: cfg.Pass,
DisableConnectOnNew: true,
DisableAutoReconnect: false,
DisableTLS: true,
HTTPPostMode: true,
}
rpcClient, err := rpcclient.New(&rpcCfg, nil)
require.NoError(t, err, "failed to create RPC client")
// Connect to the miner node.
err = rpcClient.AddNode(minerAddr, rpcclient.ANAdd)
require.NoError(t, err, "failed to connect to miner")
// Get the network info and assert the num of outbound connections is 1.
err = wait.NoError(func() error {
result, err := rpcClient.GetNetworkInfo()
require.NoError(t, err)
if int(result.Connections) != 1 {
return fmt.Errorf("want 1 conn, got %d",
result.Connections)
}
if int(result.ConnectionsOut) != 1 {
return fmt.Errorf("want 1 outbound conn, got %d",
result.Connections)
}
return nil
}, wait.DefaultTimeout)
require.NoError(t, err, "timeout connecting to the miner")
// Tear down the rpc client.
rpcClient.Shutdown()
return conn return conn
} }