harness_setup.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package lntest
  2. import (
  3. "context"
  4. "os"
  5. "testing"
  6. "github.com/btcsuite/btcd/integration/rpctest"
  7. "github.com/lightningnetwork/lnd/lntest/node"
  8. "github.com/lightningnetwork/lnd/lntest/wait"
  9. "github.com/stretchr/testify/require"
  10. )
  11. // SetupHarness creates a new HarnessTest with a series of setups such that the
  12. // instance is ready for usage. The setups are,
  13. // 1. create the directories to hold lnd files.
  14. // 2. start a btcd miner.
  15. // 3. start a chain backend(btcd, bitcoind, or neutrino).
  16. // 4. connect the miner and the chain backend.
  17. // 5. start the HarnessTest.
  18. func SetupHarness(t *testing.T, binaryPath, dbBackendName string,
  19. nativeSQL bool, feeService WebFeeService) *HarnessTest {
  20. t.Log("Setting up HarnessTest...")
  21. // Parse testing flags that influence our test execution.
  22. logDir := node.GetLogDir()
  23. require.NoError(t, os.MkdirAll(logDir, 0700), "create log dir failed")
  24. // Parse database backend
  25. dbBackend := prepareDBBackend(t, dbBackendName)
  26. // Create a new HarnessTest.
  27. ht := NewHarnessTest(t, binaryPath, feeService, dbBackend, nativeSQL)
  28. // Init the miner.
  29. t.Log("Prepare the miner and mine blocks to activate segwit...")
  30. miner := prepareMiner(ht.runCtx, ht.T)
  31. // Start a chain backend.
  32. chainBackend, cleanUp := prepareChainBackend(t, miner.P2PAddress())
  33. ht.stopChainBackend = cleanUp
  34. // Connect our chainBackend to our miner.
  35. t.Logf("Connecting the miner at %v with the chain backend...",
  36. miner.P2PAddress())
  37. // Give the chain backend some time to fully start up, re-trying if any
  38. // errors in connecting to the miner are encountered.
  39. err := wait.NoError(func() error {
  40. return chainBackend.ConnectMiner()
  41. }, DefaultTimeout)
  42. require.NoError(t, err, "connect miner")
  43. // Start the HarnessTest with the chainBackend and miner.
  44. ht.Start(chainBackend, miner)
  45. return ht
  46. }
  47. // prepareMiner creates an instance of the btcd's rpctest.Harness that will act
  48. // as the miner for all tests. This will be used to fund the wallets of the
  49. // nodes within the test network and to drive blockchain related events within
  50. // the network. Revert the default setting of accepting non-standard
  51. // transactions on simnet to reject them. Transactions on the lightning network
  52. // should always be standard to get better guarantees of getting included in to
  53. // blocks.
  54. func prepareMiner(ctxt context.Context, t *testing.T) *HarnessMiner {
  55. miner := NewMiner(ctxt, t)
  56. // Before we start anything, we want to overwrite some of the
  57. // connection settings to make the tests more robust. We might need to
  58. // restart the miner while there are already blocks present, which will
  59. // take a bit longer than the 1 second the default settings amount to.
  60. // Doubling both values will give us retries up to 4 seconds.
  61. miner.MaxConnRetries = rpctest.DefaultMaxConnectionRetries * 2
  62. miner.ConnectionRetryTimeout = rpctest.DefaultConnectionRetryTimeout * 2
  63. // Set up miner and connect chain backend to it.
  64. require.NoError(t, miner.SetUp(true, 50))
  65. require.NoError(t, miner.Client.NotifyNewTransactions(false))
  66. // Next mine enough blocks in order for segwit and the CSV package
  67. // soft-fork to activate on SimNet.
  68. numBlocks := harnessNetParams.MinerConfirmationWindow * 2
  69. miner.GenerateBlocks(numBlocks)
  70. return miner
  71. }
  72. // prepareChainBackend creates a new chain backend.
  73. func prepareChainBackend(t *testing.T,
  74. minerAddr string) (node.BackendConfig, func()) {
  75. chainBackend, cleanUp, err := NewBackend(
  76. minerAddr, harnessNetParams,
  77. )
  78. require.NoError(t, err, "new backend")
  79. return chainBackend, func() {
  80. require.NoError(t, cleanUp(), "cleanup")
  81. }
  82. }
  83. // prepareDBBackend parses a DatabaseBackend based on the name given.
  84. func prepareDBBackend(t *testing.T,
  85. dbBackendName string) node.DatabaseBackend {
  86. var dbBackend node.DatabaseBackend
  87. switch dbBackendName {
  88. case "bbolt":
  89. dbBackend = node.BackendBbolt
  90. case "etcd":
  91. dbBackend = node.BackendEtcd
  92. case "postgres":
  93. dbBackend = node.BackendPostgres
  94. case "sqlite":
  95. dbBackend = node.BackendSqlite
  96. default:
  97. require.Fail(t, "unknown db backend")
  98. }
  99. return dbBackend
  100. }