2022-08-12 11:03:44 +02:00
|
|
|
package lntest
|
2022-07-22 14:57:23 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
2022-08-12 11:03:44 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lntest/node"
|
2022-07-22 14:57:23 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SetupHarness creates a new HarnessTest with a series of setups such that the
|
|
|
|
// instance is ready for usage. The setups are,
|
|
|
|
// 1. create the directories to hold lnd files.
|
|
|
|
// 2. start a btcd miner.
|
|
|
|
// 3. start a chain backend(btcd, bitcoind, or neutrino).
|
|
|
|
// 4. connect the miner and the chain backend.
|
|
|
|
// 5. start the HarnessTest.
|
2022-08-16 11:31:20 +02:00
|
|
|
func SetupHarness(t *testing.T, binaryPath, dbBackendName string,
|
|
|
|
feeService WebFeeService) *HarnessTest {
|
|
|
|
|
2022-07-22 14:57:23 +02:00
|
|
|
t.Log("Setting up HarnessTest...")
|
|
|
|
|
|
|
|
// Parse testing flags that influence our test execution.
|
2022-08-12 07:07:16 +02:00
|
|
|
logDir := node.GetLogDir()
|
2022-07-22 14:57:23 +02:00
|
|
|
require.NoError(t, os.MkdirAll(logDir, 0700), "create log dir failed")
|
|
|
|
|
|
|
|
// Parse database backend
|
2023-01-31 17:32:03 +01:00
|
|
|
dbBackend := prepareDBBackend(t, dbBackendName)
|
2022-07-22 14:57:23 +02:00
|
|
|
|
|
|
|
// Create a new HarnessTest.
|
2022-08-16 11:31:20 +02:00
|
|
|
ht := NewHarnessTest(t, binaryPath, feeService, dbBackend)
|
2022-07-22 14:57:23 +02:00
|
|
|
|
|
|
|
// Init the miner.
|
|
|
|
t.Log("Prepare the miner and mine blocks to activate segwit...")
|
|
|
|
miner := prepareMiner(ht.runCtx, ht.T)
|
|
|
|
|
|
|
|
// Start a chain backend.
|
|
|
|
chainBackend, cleanUp := prepareChainBackend(t, miner.P2PAddress())
|
|
|
|
ht.stopChainBackend = cleanUp
|
|
|
|
|
|
|
|
// Connect our chainBackend to our miner.
|
|
|
|
t.Log("Connecting the miner with the chain backend...")
|
|
|
|
require.NoError(t, chainBackend.ConnectMiner(), "connect miner")
|
|
|
|
|
|
|
|
// Start the HarnessTest with the chainBackend and miner.
|
|
|
|
ht.Start(chainBackend, miner)
|
|
|
|
|
|
|
|
return ht
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareMiner creates an instance of the btcd's rpctest.Harness that will act
|
|
|
|
// as the miner for all tests. This will be used to fund the wallets of the
|
|
|
|
// nodes within the test network and to drive blockchain related events within
|
|
|
|
// the network. Revert the default setting of accepting non-standard
|
|
|
|
// transactions on simnet to reject them. Transactions on the lightning network
|
|
|
|
// should always be standard to get better guarantees of getting included in to
|
|
|
|
// blocks.
|
|
|
|
func prepareMiner(ctxt context.Context, t *testing.T) *HarnessMiner {
|
|
|
|
miner := NewMiner(ctxt, t)
|
|
|
|
|
|
|
|
// Before we start anything, we want to overwrite some of the
|
|
|
|
// connection settings to make the tests more robust. We might need to
|
|
|
|
// restart the miner 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.
|
|
|
|
miner.MaxConnRetries = rpctest.DefaultMaxConnectionRetries * 2
|
|
|
|
miner.ConnectionRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2
|
|
|
|
|
|
|
|
// Set up miner and connect chain backend to it.
|
|
|
|
require.NoError(t, miner.SetUp(true, 50))
|
|
|
|
require.NoError(t, miner.Client.NotifyNewTransactions(false))
|
|
|
|
|
|
|
|
// Next mine enough blocks in order for segwit and the CSV package
|
|
|
|
// soft-fork to activate on SimNet.
|
|
|
|
numBlocks := harnessNetParams.MinerConfirmationWindow * 2
|
|
|
|
miner.GenerateBlocks(numBlocks)
|
|
|
|
|
|
|
|
return miner
|
|
|
|
}
|
|
|
|
|
|
|
|
// prepareChainBackend creates a new chain backend.
|
|
|
|
func prepareChainBackend(t *testing.T,
|
2022-08-12 07:07:16 +02:00
|
|
|
minerAddr string) (node.BackendConfig, func()) {
|
2022-07-22 14:57:23 +02:00
|
|
|
|
2022-08-12 11:03:44 +02:00
|
|
|
chainBackend, cleanUp, err := NewBackend(
|
2022-07-22 14:57:23 +02:00
|
|
|
minerAddr, harnessNetParams,
|
|
|
|
)
|
|
|
|
require.NoError(t, err, "new backend")
|
|
|
|
|
|
|
|
return chainBackend, func() {
|
|
|
|
require.NoError(t, cleanUp(), "cleanup")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-31 17:32:03 +01:00
|
|
|
// prepareDBBackend parses a DatabaseBackend based on the name given.
|
|
|
|
func prepareDBBackend(t *testing.T,
|
2022-08-12 07:07:16 +02:00
|
|
|
dbBackendName string) node.DatabaseBackend {
|
2022-07-22 14:57:23 +02:00
|
|
|
|
2022-08-12 07:07:16 +02:00
|
|
|
var dbBackend node.DatabaseBackend
|
2022-07-22 14:57:23 +02:00
|
|
|
switch dbBackendName {
|
|
|
|
case "bbolt":
|
2022-08-12 07:07:16 +02:00
|
|
|
dbBackend = node.BackendBbolt
|
2022-07-22 14:57:23 +02:00
|
|
|
|
|
|
|
case "etcd":
|
2022-08-12 07:07:16 +02:00
|
|
|
dbBackend = node.BackendEtcd
|
2022-07-22 14:57:23 +02:00
|
|
|
|
|
|
|
case "postgres":
|
2022-08-12 07:07:16 +02:00
|
|
|
dbBackend = node.BackendPostgres
|
2022-07-22 14:57:23 +02:00
|
|
|
|
2022-12-14 10:54:47 +01:00
|
|
|
case "sqlite":
|
2022-08-12 07:07:16 +02:00
|
|
|
dbBackend = node.BackendSqlite
|
2022-12-14 10:54:47 +01:00
|
|
|
|
2022-07-22 14:57:23 +02:00
|
|
|
default:
|
|
|
|
require.Fail(t, "unknown db backend")
|
|
|
|
}
|
|
|
|
|
|
|
|
return dbBackend
|
|
|
|
}
|