chainntnfs: fix issues with NewBitcoindBackend

This commit fixes a number of issues with our temporary bitcoind nodes
that we spin up for unit tests:
 - We didn't remove the node's temporary data dir after tests finished.
 - We used random ports which lead to unwanted port collisions.
 - We used ipc:// unix sockets for ZMQ which currently isn't supported
   in bitcoind v26.0 due to a regression. Since we can reliably create
   new non-colliding ports now, we should use TCP for ZMQ anyway.
This commit is contained in:
Oliver Gugger 2024-03-15 12:54:34 +01:00
parent 2cc28baa84
commit 2884389ce4
No known key found for this signature in database
GPG key ID: 8E4256593F177720

View file

@ -6,8 +6,6 @@ package chainntnfs
import ( import (
"errors" "errors"
"fmt" "fmt"
"math/rand"
"os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"testing" "testing"
@ -25,6 +23,7 @@ import (
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
"github.com/lightninglabs/neutrino" "github.com/lightninglabs/neutrino"
"github.com/lightningnetwork/lnd/kvdb" "github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lntest/port"
"github.com/lightningnetwork/lnd/lntest/wait" "github.com/lightningnetwork/lnd/lntest/wait"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -207,15 +206,13 @@ func NewBitcoindBackend(t *testing.T, netParams *chaincfg.Params,
t.Helper() t.Helper()
// We use ioutil.TempDir here instead of t.TempDir because some versions tempBitcoindDir := t.TempDir()
// of bitcoind complain about the zmq connection string formats when the
// t.TempDir directory string is used.
tempBitcoindDir, err := os.MkdirTemp("", "bitcoind")
require.NoError(t, err, "unable to create temp dir")
rpcPort := rand.Intn(65536-1024) + 1024 rpcPort := port.NextAvailablePort()
zmqBlockHost := "ipc:///" + tempBitcoindDir + "/blocks.socket" zmqBlockPort := port.NextAvailablePort()
zmqTxHost := "ipc:///" + tempBitcoindDir + "/tx.socket" 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{ args := []string{
"-connect=" + minerAddr, "-connect=" + minerAddr,
@ -268,7 +265,7 @@ func NewBitcoindBackend(t *testing.T, netParams *chaincfg.Params,
} }
var conn *chain.BitcoindConn var conn *chain.BitcoindConn
err = wait.NoError(func() error { err := wait.NoError(func() error {
var err error var err error
conn, err = chain.NewBitcoindConn(cfg) conn, err = chain.NewBitcoindConn(cfg)
if err != nil { if err != nil {
@ -278,7 +275,8 @@ func NewBitcoindBackend(t *testing.T, netParams *chaincfg.Params,
return conn.Start() return conn.Start()
}, 10*time.Second) }, 10*time.Second)
if err != nil { if err != nil {
t.Fatalf("unable to establish connection to bitcoind: %v", err) t.Fatalf("unable to establish connection to bitcoind at %v: "+
"%v", tempBitcoindDir, err)
} }
t.Cleanup(conn.Stop) t.Cleanup(conn.Stop)