2019-05-15 10:02:53 +03:00
|
|
|
package itest
|
2016-08-30 08:07:54 +03:00
|
|
|
|
|
|
|
import (
|
2019-09-29 01:43:42 +03:00
|
|
|
"context"
|
2020-11-04 13:03:29 +03:00
|
|
|
"flag"
|
2016-08-30 08:07:54 +03:00
|
|
|
"fmt"
|
2016-11-22 06:16:44 +03:00
|
|
|
"os"
|
2017-01-08 07:25:45 +03:00
|
|
|
"strings"
|
2016-10-24 05:00:09 +03:00
|
|
|
"testing"
|
2016-09-14 05:00:47 +03:00
|
|
|
"time"
|
2016-10-24 05:00:09 +03:00
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
2018-06-15 22:33:49 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lntest"
|
2020-07-07 11:32:13 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-08-30 08:07:54 +03:00
|
|
|
)
|
|
|
|
|
2020-11-04 13:03:29 +03:00
|
|
|
const (
|
|
|
|
// defaultSplitTranches is the default number of tranches we split the
|
|
|
|
// test cases into.
|
|
|
|
defaultSplitTranches uint = 1
|
|
|
|
|
|
|
|
// defaultRunTranche is the default index of the test cases tranche that
|
|
|
|
// we run.
|
|
|
|
defaultRunTranche uint = 0
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// testCasesSplitParts is the number of tranches the test cases should
|
|
|
|
// be split into. By default this is set to 1, so no splitting happens.
|
|
|
|
// If this value is increased, then the -runtranche flag must be
|
|
|
|
// specified as well to indicate which part should be run in the current
|
|
|
|
// invocation.
|
|
|
|
testCasesSplitTranches = flag.Uint(
|
|
|
|
"splittranches", defaultSplitTranches, "split the test cases "+
|
|
|
|
"in this many tranches and run the tranche at "+
|
|
|
|
"0-based index specified by the -runtranche flag",
|
|
|
|
)
|
|
|
|
|
|
|
|
// testCasesRunTranche is the 0-based index of the split test cases
|
|
|
|
// tranche to run in the current invocation.
|
|
|
|
testCasesRunTranche = flag.Uint(
|
|
|
|
"runtranche", defaultRunTranche, "run the tranche of the "+
|
|
|
|
"split test cases with the given (0-based) index",
|
|
|
|
)
|
2020-06-22 20:36:12 +03:00
|
|
|
|
2021-06-14 13:31:08 +03:00
|
|
|
// dbBackendFlag specifies the backend to use
|
|
|
|
dbBackendFlag = flag.String("dbbackend", "bbolt", "Database backend (bbolt, etcd)")
|
2020-11-04 13:03:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// getTestCaseSplitTranche returns the sub slice of the test cases that should
|
|
|
|
// be run as the current split tranche as well as the index and slice offset of
|
|
|
|
// the tranche.
|
|
|
|
func getTestCaseSplitTranche() ([]*testCase, uint, uint) {
|
|
|
|
numTranches := defaultSplitTranches
|
|
|
|
if testCasesSplitTranches != nil {
|
|
|
|
numTranches = *testCasesSplitTranches
|
|
|
|
}
|
|
|
|
runTranche := defaultRunTranche
|
|
|
|
if testCasesRunTranche != nil {
|
|
|
|
runTranche = *testCasesRunTranche
|
|
|
|
}
|
|
|
|
|
2020-11-07 15:23:30 +03:00
|
|
|
// There's a special flake-hunt mode where we run the same test multiple
|
|
|
|
// times in parallel. In that case the tranche index is equal to the
|
|
|
|
// thread ID, but we need to actually run all tests for the regex
|
|
|
|
// selection to work.
|
|
|
|
threadID := runTranche
|
|
|
|
if numTranches == 1 {
|
|
|
|
runTranche = 0
|
|
|
|
}
|
|
|
|
|
2020-11-04 13:03:29 +03:00
|
|
|
numCases := uint(len(allTestCases))
|
|
|
|
testsPerTranche := numCases / numTranches
|
|
|
|
trancheOffset := runTranche * testsPerTranche
|
|
|
|
trancheEnd := trancheOffset + testsPerTranche
|
|
|
|
if trancheEnd > numCases || runTranche == numTranches-1 {
|
|
|
|
trancheEnd = numCases
|
|
|
|
}
|
|
|
|
|
2020-11-07 15:23:30 +03:00
|
|
|
return allTestCases[trancheOffset:trancheEnd], threadID, trancheOffset
|
2020-11-04 13:03:29 +03:00
|
|
|
}
|
|
|
|
|
2016-08-30 08:07:54 +03:00
|
|
|
// TestLightningNetworkDaemon performs a series of integration tests amongst a
|
2016-09-15 21:59:51 +03:00
|
|
|
// programmatically driven network of lnd nodes.
|
2016-08-30 08:07:54 +03:00
|
|
|
func TestLightningNetworkDaemon(t *testing.T) {
|
2020-10-26 16:21:05 +03:00
|
|
|
// If no tests are registered, then we can exit early.
|
2020-11-04 13:03:29 +03:00
|
|
|
if len(allTestCases) == 0 {
|
2020-01-10 17:27:48 +03:00
|
|
|
t.Skip("integration tests not selected with flag 'rpctest'")
|
|
|
|
}
|
|
|
|
|
2020-11-04 13:03:29 +03:00
|
|
|
// Parse testing flags that influence our test execution.
|
2020-11-04 13:03:30 +03:00
|
|
|
logDir := lntest.GetLogDir()
|
|
|
|
require.NoError(t, os.MkdirAll(logDir, 0700))
|
2020-11-04 13:03:29 +03:00
|
|
|
testCases, trancheIndex, trancheOffset := getTestCaseSplitTranche()
|
|
|
|
lntest.ApplyPortOffset(uint32(trancheIndex) * 1000)
|
|
|
|
|
2020-12-03 13:30:23 +03:00
|
|
|
// Before we start any node, we need to make sure that any btcd node
|
|
|
|
// that is started through the RPC harness uses a unique port as well to
|
|
|
|
// avoid any port collisions.
|
|
|
|
rpctest.ListenAddressGenerator = lntest.GenerateBtcdListenerAddresses
|
2016-08-30 08:07:54 +03:00
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// Declare the network harness here to gain access to its
|
|
|
|
// 'OnTxAccepted' call back.
|
2017-11-17 04:31:41 +03:00
|
|
|
var lndHarness *lntest.NetworkHarness
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// Create 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.
|
|
|
|
//
|
|
|
|
// We will also connect it to our chain backend.
|
2020-11-04 13:03:30 +03:00
|
|
|
minerLogDir := fmt.Sprintf("%s/.minerlogs", logDir)
|
2020-09-30 01:15:05 +03:00
|
|
|
miner, minerCleanUp, err := lntest.NewMiner(
|
2020-12-03 13:30:22 +03:00
|
|
|
minerLogDir, "output_btcd_miner.log", harnessNetParams,
|
|
|
|
&rpcclient.NotificationHandlers{}, lntest.GetBtcdBinary(),
|
2020-09-30 01:15:05 +03:00
|
|
|
)
|
|
|
|
require.NoError(t, err, "failed to create new miner")
|
2018-10-10 14:04:13 +03:00
|
|
|
defer func() {
|
2020-09-30 01:15:05 +03:00
|
|
|
require.NoError(t, minerCleanUp(), "failed to clean up miner")
|
2018-10-10 14:04:13 +03:00
|
|
|
}()
|
2017-11-17 04:31:41 +03:00
|
|
|
|
2019-05-24 15:17:48 +03:00
|
|
|
// Start a chain backend.
|
2019-07-11 14:51:22 +03:00
|
|
|
chainBackend, cleanUp, err := lntest.NewBackend(
|
|
|
|
miner.P2PAddress(), harnessNetParams,
|
|
|
|
)
|
2020-12-03 13:30:23 +03:00
|
|
|
require.NoError(t, err, "new backend")
|
2020-09-02 22:15:04 +03:00
|
|
|
defer func() {
|
2020-12-03 13:30:23 +03:00
|
|
|
require.NoError(t, cleanUp(), "cleanup")
|
2020-09-02 22:15:04 +03:00
|
|
|
}()
|
2019-05-24 15:17:48 +03:00
|
|
|
|
2020-12-03 13:30:23 +03:00
|
|
|
// 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
|
2018-12-14 11:24:00 +03:00
|
|
|
|
2020-12-03 13:30:23 +03:00
|
|
|
// Set up miner and connect chain backend to it.
|
|
|
|
require.NoError(t, miner.SetUp(true, 50))
|
2021-03-10 01:12:26 +03:00
|
|
|
require.NoError(t, miner.Client.NotifyNewTransactions(false))
|
2020-12-03 13:30:23 +03:00
|
|
|
require.NoError(t, chainBackend.ConnectMiner(), "connect miner")
|
2020-09-03 13:59:08 +03:00
|
|
|
|
2021-06-14 13:31:08 +03:00
|
|
|
// Parse database backend
|
|
|
|
var dbBackend lntest.DatabaseBackend
|
|
|
|
switch *dbBackendFlag {
|
|
|
|
case "bbolt":
|
|
|
|
dbBackend = lntest.BackendBbolt
|
|
|
|
|
|
|
|
case "etcd":
|
|
|
|
dbBackend = lntest.BackendEtcd
|
|
|
|
|
|
|
|
default:
|
|
|
|
require.Fail(t, "unknown db backend")
|
|
|
|
}
|
|
|
|
|
2018-12-14 11:24:00 +03:00
|
|
|
// Now we can set up our test harness (LND instance), with the chain
|
|
|
|
// backend we just created.
|
2020-12-03 13:30:23 +03:00
|
|
|
ht := newHarnessTest(t, nil)
|
2020-11-04 13:03:31 +03:00
|
|
|
binary := ht.getLndBinary()
|
2020-06-22 20:36:12 +03:00
|
|
|
lndHarness, err = lntest.NewNetworkHarness(
|
2021-06-14 13:31:08 +03:00
|
|
|
miner, chainBackend, binary, dbBackend,
|
2020-06-22 20:36:12 +03:00
|
|
|
)
|
2016-08-31 05:34:13 +03:00
|
|
|
if err != nil {
|
2016-10-26 15:28:05 +03:00
|
|
|
ht.Fatalf("unable to create lightning network harness: %v", err)
|
2016-08-31 05:34:13 +03:00
|
|
|
}
|
2020-10-29 17:37:17 +03:00
|
|
|
defer lndHarness.Stop()
|
2016-08-31 05:34:13 +03:00
|
|
|
|
2016-11-24 11:49:18 +03:00
|
|
|
// Spawn a new goroutine to watch for any fatal errors that any of the
|
|
|
|
// running lnd processes encounter. If an error occurs, then the test
|
2017-10-25 05:35:54 +03:00
|
|
|
// case should naturally as a result and we log the server error here to
|
|
|
|
// help debug.
|
2016-11-24 11:49:18 +03:00
|
|
|
go func() {
|
2017-10-25 05:35:54 +03:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err, more := <-lndHarness.ProcessErrors():
|
|
|
|
if !more {
|
|
|
|
return
|
|
|
|
}
|
2020-11-04 13:03:29 +03:00
|
|
|
ht.Logf("lnd finished with error (stderr):\n%v",
|
|
|
|
err)
|
2017-10-25 05:35:54 +03:00
|
|
|
}
|
2016-11-24 11:49:18 +03:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-01-06 00:17:49 +03:00
|
|
|
// Next mine enough blocks in order for segwit and the CSV package
|
|
|
|
// soft-fork to activate on SimNet.
|
2019-07-11 14:51:22 +03:00
|
|
|
numBlocks := harnessNetParams.MinerConfirmationWindow * 2
|
2021-03-10 01:12:26 +03:00
|
|
|
if _, err := miner.Client.Generate(numBlocks); err != nil {
|
2017-01-06 00:17:49 +03:00
|
|
|
ht.Fatalf("unable to generate blocks: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-31 05:34:13 +03:00
|
|
|
// With the btcd harness created, we can now complete the
|
2016-09-15 22:24:52 +03:00
|
|
|
// initialization of the network. args - list of lnd arguments,
|
|
|
|
// example: "--debuglevel=debug"
|
2016-09-26 20:40:58 +03:00
|
|
|
// TODO(roasbeef): create master balanced channel with all the monies?
|
2020-08-13 01:45:57 +03:00
|
|
|
aliceBobArgs := []string{
|
|
|
|
"--default-remote-max-htlcs=483",
|
|
|
|
}
|
2016-08-30 08:07:54 +03:00
|
|
|
|
2020-11-04 13:03:29 +03:00
|
|
|
// Run the subset of the test cases selected in this tranche.
|
|
|
|
for idx, testCase := range testCases {
|
|
|
|
testCase := testCase
|
2020-10-29 17:37:17 +03:00
|
|
|
name := fmt.Sprintf("%02d-of-%d/%s/%s",
|
|
|
|
trancheOffset+uint(idx)+1, len(allTestCases),
|
|
|
|
chainBackend.Name(), testCase.name)
|
2018-06-15 22:35:12 +03:00
|
|
|
|
2020-10-29 17:37:17 +03:00
|
|
|
success := t.Run(name, func(t1 *testing.T) {
|
|
|
|
cleanTestCaseName := strings.ReplaceAll(
|
|
|
|
testCase.name, " ", "_",
|
|
|
|
)
|
|
|
|
|
2021-06-07 23:05:12 +03:00
|
|
|
err = lndHarness.SetUp(
|
|
|
|
t1, cleanTestCaseName, aliceBobArgs,
|
|
|
|
)
|
2020-10-29 17:37:17 +03:00
|
|
|
require.NoError(t1,
|
|
|
|
err, "unable to set up test lightning network",
|
|
|
|
)
|
|
|
|
defer func() {
|
|
|
|
require.NoError(t1, lndHarness.TearDown())
|
|
|
|
}()
|
2018-06-15 22:35:12 +03:00
|
|
|
|
2021-06-11 07:35:30 +03:00
|
|
|
lndHarness.EnsureConnected(
|
|
|
|
context.Background(), t1,
|
|
|
|
lndHarness.Alice, lndHarness.Bob,
|
2020-10-29 17:37:17 +03:00
|
|
|
)
|
2020-10-29 17:35:02 +03:00
|
|
|
|
2020-10-29 17:37:17 +03:00
|
|
|
logLine := fmt.Sprintf(
|
|
|
|
"STARTING ============ %v ============\n",
|
|
|
|
testCase.name,
|
|
|
|
)
|
2018-01-09 14:59:32 +03:00
|
|
|
|
2020-11-21 14:52:02 +03:00
|
|
|
AddToNodeLog(t, lndHarness.Alice, logLine)
|
|
|
|
AddToNodeLog(t, lndHarness.Bob, logLine)
|
2020-10-29 17:37:17 +03:00
|
|
|
|
|
|
|
// Start every test with the default static fee estimate.
|
|
|
|
lndHarness.SetFeeEstimate(12500)
|
|
|
|
|
|
|
|
// Create a separate harness test for the testcase to
|
|
|
|
// avoid overwriting the external harness test that is
|
|
|
|
// tied to the parent test.
|
2019-04-15 17:19:47 +03:00
|
|
|
ht := newHarnessTest(t1, lndHarness)
|
|
|
|
ht.RunTestCase(testCase)
|
2017-06-19 16:53:52 +03:00
|
|
|
})
|
2017-06-19 17:39:07 +03:00
|
|
|
|
2017-06-19 16:53:52 +03:00
|
|
|
// Stop at the first failure. Mimic behavior of original test
|
2017-06-19 17:39:07 +03:00
|
|
|
// framework.
|
2017-06-19 16:53:52 +03:00
|
|
|
if !success {
|
2020-04-06 22:32:26 +03:00
|
|
|
// Log failure time to help relate the lnd logs to the
|
|
|
|
// failure.
|
2020-11-04 13:03:29 +03:00
|
|
|
t.Logf("Failure time: %v", time.Now().Format(
|
|
|
|
"2006-01-02 15:04:05.000",
|
|
|
|
))
|
2017-06-19 16:53:52 +03:00
|
|
|
break
|
|
|
|
}
|
2016-08-30 08:07:54 +03:00
|
|
|
}
|
|
|
|
}
|