2021-08-23 10:35:48 +02:00
|
|
|
//go:build dev
|
2018-09-20 12:21:32 +02:00
|
|
|
// +build dev
|
2018-08-17 04:29:37 +02:00
|
|
|
|
|
|
|
package chainntnfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"math/rand"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
2018-08-17 04:29:37 +02:00
|
|
|
"github.com/btcsuite/btcd/btcjson"
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2018-08-17 04:29:37 +02:00
|
|
|
"github.com/btcsuite/btcd/chaincfg"
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
|
|
|
"github.com/btcsuite/btcd/integration/rpctest"
|
|
|
|
"github.com/btcsuite/btcd/txscript"
|
|
|
|
"github.com/btcsuite/btcd/wire"
|
|
|
|
"github.com/btcsuite/btcwallet/chain"
|
|
|
|
"github.com/btcsuite/btcwallet/walletdb"
|
|
|
|
"github.com/lightninglabs/neutrino"
|
2021-04-26 19:08:11 +02:00
|
|
|
"github.com/lightningnetwork/lnd/kvdb"
|
2018-08-17 04:29:37 +02:00
|
|
|
)
|
|
|
|
|
2018-08-24 13:50:47 +02:00
|
|
|
var (
|
2018-10-21 01:27:46 +02:00
|
|
|
// TrickleInterval is the interval at which the miner should trickle
|
2018-08-24 13:50:47 +02:00
|
|
|
// transactions to its peers. We'll set it small to ensure the miner
|
|
|
|
// propagates transactions quickly in the tests.
|
2018-10-21 01:27:46 +02:00
|
|
|
TrickleInterval = 10 * time.Millisecond
|
2018-08-24 13:50:47 +02:00
|
|
|
)
|
|
|
|
|
2018-08-17 04:29:37 +02:00
|
|
|
var (
|
|
|
|
NetParams = &chaincfg.RegressionNetParams
|
2018-12-07 06:14:34 +01:00
|
|
|
)
|
2018-08-17 04:29:37 +02:00
|
|
|
|
2018-12-07 06:14:34 +01:00
|
|
|
// randPubKeyHashScript generates a P2PKH script that pays to the public key of
|
|
|
|
// a randomly-generated private key.
|
|
|
|
func randPubKeyHashScript() ([]byte, *btcec.PrivateKey, error) {
|
2022-02-23 14:48:00 +01:00
|
|
|
privKey, err := btcec.NewPrivateKey()
|
2018-12-07 06:14:34 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2018-08-17 04:29:37 +02:00
|
|
|
}
|
2018-12-07 06:14:34 +01:00
|
|
|
|
|
|
|
pubKeyHash := btcutil.Hash160(privKey.PubKey().SerializeCompressed())
|
|
|
|
addrScript, err := btcutil.NewAddressPubKeyHash(pubKeyHash, NetParams)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pkScript, err := txscript.PayToAddrScript(addrScript)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pkScript, privKey, nil
|
|
|
|
}
|
2018-08-17 04:29:37 +02:00
|
|
|
|
|
|
|
// GetTestTxidAndScript generate a new test transaction and returns its txid and
|
|
|
|
// the script of the output being generated.
|
|
|
|
func GetTestTxidAndScript(h *rpctest.Harness) (*chainhash.Hash, []byte, error) {
|
2018-12-07 06:14:34 +01:00
|
|
|
pkScript, _, err := randPubKeyHashScript()
|
2018-08-17 04:29:37 +02:00
|
|
|
if err != nil {
|
2018-12-07 06:14:34 +01:00
|
|
|
return nil, nil, fmt.Errorf("unable to generate pkScript: %v", err)
|
2018-08-17 04:29:37 +02:00
|
|
|
}
|
2018-12-07 06:14:34 +01:00
|
|
|
output := &wire.TxOut{Value: 2e8, PkScript: pkScript}
|
2018-08-17 04:29:37 +02:00
|
|
|
txid, err := h.SendOutputs([]*wire.TxOut{output}, 10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-07 06:14:34 +01:00
|
|
|
return txid, pkScript, nil
|
2018-08-17 04:29:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForMempoolTx waits for the txid to be seen in the miner's mempool.
|
|
|
|
func WaitForMempoolTx(miner *rpctest.Harness, txid *chainhash.Hash) error {
|
|
|
|
timeout := time.After(10 * time.Second)
|
2018-10-21 01:27:46 +02:00
|
|
|
trickle := time.After(2 * TrickleInterval)
|
2018-08-17 04:29:37 +02:00
|
|
|
for {
|
|
|
|
// Check for the harness' knowledge of the txid.
|
2021-03-09 23:12:26 +01:00
|
|
|
tx, err := miner.Client.GetRawTransaction(txid)
|
2018-08-17 04:29:37 +02:00
|
|
|
if err != nil {
|
|
|
|
jsonErr, ok := err.(*btcjson.RPCError)
|
|
|
|
if ok && jsonErr.Code == btcjson.ErrRPCNoTxInfo {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if tx != nil && tx.Hash().IsEqual(txid) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
case <-timeout:
|
|
|
|
return errors.New("timed out waiting for tx")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-24 13:50:47 +02:00
|
|
|
// To ensure any transactions propagate from the miner to the peers
|
|
|
|
// before returning, ensure we have waited for at least
|
|
|
|
// 2*trickleInterval before returning.
|
|
|
|
select {
|
|
|
|
case <-trickle:
|
|
|
|
case <-timeout:
|
|
|
|
return errors.New("timeout waiting for trickle interval. " +
|
|
|
|
"Trickle interval to large?")
|
|
|
|
}
|
|
|
|
|
2018-08-17 04:29:37 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSpendableOutput creates and returns an output that can be spent later
|
|
|
|
// on.
|
2018-12-07 06:14:34 +01:00
|
|
|
func CreateSpendableOutput(t *testing.T,
|
|
|
|
miner *rpctest.Harness) (*wire.OutPoint, *wire.TxOut, *btcec.PrivateKey) {
|
|
|
|
|
2018-08-17 04:29:37 +02:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
// Create a transaction that only has one output, the one destined for
|
|
|
|
// the recipient.
|
2018-12-07 06:14:34 +01:00
|
|
|
pkScript, privKey, err := randPubKeyHashScript()
|
2018-08-17 04:29:37 +02:00
|
|
|
if err != nil {
|
2018-12-07 06:14:34 +01:00
|
|
|
t.Fatalf("unable to generate pkScript: %v", err)
|
2018-08-17 04:29:37 +02:00
|
|
|
}
|
2018-12-07 06:14:34 +01:00
|
|
|
output := &wire.TxOut{Value: 2e8, PkScript: pkScript}
|
2018-08-17 04:29:37 +02:00
|
|
|
txid, err := miner.SendOutputsWithoutChange([]*wire.TxOut{output}, 10)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create tx: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mine the transaction to mark the output as spendable.
|
|
|
|
if err := WaitForMempoolTx(miner, txid); err != nil {
|
|
|
|
t.Fatalf("tx not relayed to miner: %v", err)
|
|
|
|
}
|
2021-03-09 23:12:26 +01:00
|
|
|
if _, err := miner.Client.Generate(1); err != nil {
|
2018-08-17 04:29:37 +02:00
|
|
|
t.Fatalf("unable to generate single block: %v", err)
|
|
|
|
}
|
|
|
|
|
2018-12-07 06:14:34 +01:00
|
|
|
return wire.NewOutPoint(txid, 0), output, privKey
|
2018-08-17 04:29:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSpendTx creates a transaction spending the specified output.
|
2018-12-07 06:14:34 +01:00
|
|
|
func CreateSpendTx(t *testing.T, prevOutPoint *wire.OutPoint,
|
|
|
|
prevOutput *wire.TxOut, privKey *btcec.PrivateKey) *wire.MsgTx {
|
|
|
|
|
2018-08-17 04:29:37 +02:00
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
spendingTx := wire.NewMsgTx(1)
|
2018-12-07 06:14:34 +01:00
|
|
|
spendingTx.AddTxIn(&wire.TxIn{PreviousOutPoint: *prevOutPoint})
|
|
|
|
spendingTx.AddTxOut(&wire.TxOut{Value: 1e8, PkScript: prevOutput.PkScript})
|
2018-08-17 04:29:37 +02:00
|
|
|
|
|
|
|
sigScript, err := txscript.SignatureScript(
|
2018-12-07 06:14:34 +01:00
|
|
|
spendingTx, 0, prevOutput.PkScript, txscript.SigHashAll,
|
|
|
|
privKey, true,
|
2018-08-17 04:29:37 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to sign tx: %v", err)
|
|
|
|
}
|
|
|
|
spendingTx.TxIn[0].SignatureScript = sigScript
|
|
|
|
|
|
|
|
return spendingTx
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMiner spawns testing harness backed by a btcd node that can serve as a
|
|
|
|
// miner.
|
|
|
|
func NewMiner(t *testing.T, extraArgs []string, createChain bool,
|
|
|
|
spendableOutputs uint32) (*rpctest.Harness, func()) {
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
2018-08-24 13:50:47 +02:00
|
|
|
// Add the trickle interval argument to the extra args.
|
2018-10-21 01:27:46 +02:00
|
|
|
trickle := fmt.Sprintf("--trickleinterval=%v", TrickleInterval)
|
2018-08-24 13:50:47 +02:00
|
|
|
extraArgs = append(extraArgs, trickle)
|
|
|
|
|
2020-12-03 11:30:22 +01:00
|
|
|
node, err := rpctest.New(NetParams, nil, extraArgs, "")
|
2018-08-17 04:29:37 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create backend node: %v", err)
|
|
|
|
}
|
|
|
|
if err := node.SetUp(createChain, spendableOutputs); err != nil {
|
|
|
|
node.TearDown()
|
|
|
|
t.Fatalf("unable to set up backend node: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, func() { node.TearDown() }
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBitcoindBackend spawns a new bitcoind node that connects to a miner at the
|
|
|
|
// specified address. The txindex boolean can be set to determine whether the
|
|
|
|
// backend node should maintain a transaction index. A connection to the newly
|
|
|
|
// spawned bitcoind node is returned.
|
|
|
|
func NewBitcoindBackend(t *testing.T, minerAddr string,
|
|
|
|
txindex bool) (*chain.BitcoindConn, func()) {
|
|
|
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
tempBitcoindDir, err := ioutil.TempDir("", "bitcoind")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rpcPort := rand.Intn(65536-1024) + 1024
|
|
|
|
zmqBlockHost := "ipc:///" + tempBitcoindDir + "/blocks.socket"
|
|
|
|
zmqTxHost := "ipc:///" + tempBitcoindDir + "/tx.socket"
|
|
|
|
|
|
|
|
args := []string{
|
|
|
|
"-connect=" + minerAddr,
|
|
|
|
"-datadir=" + tempBitcoindDir,
|
|
|
|
"-regtest",
|
|
|
|
"-rpcauth=weks:469e9bb14ab2360f8e226efed5ca6fd$507c670e800a952" +
|
|
|
|
"84294edb5773b05544b220110063096c221be9933c82d38e1",
|
|
|
|
fmt.Sprintf("-rpcport=%d", rpcPort),
|
|
|
|
"-disablewallet",
|
|
|
|
"-zmqpubrawblock=" + zmqBlockHost,
|
|
|
|
"-zmqpubrawtx=" + zmqTxHost,
|
|
|
|
}
|
|
|
|
if txindex {
|
|
|
|
args = append(args, "-txindex")
|
|
|
|
}
|
|
|
|
|
|
|
|
bitcoind := exec.Command("bitcoind", args...)
|
|
|
|
if err := bitcoind.Start(); err != nil {
|
|
|
|
os.RemoveAll(tempBitcoindDir)
|
|
|
|
t.Fatalf("unable to start bitcoind: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the bitcoind instance to start up.
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
|
|
|
|
host := fmt.Sprintf("127.0.0.1:%d", rpcPort)
|
2021-03-09 23:12:26 +01:00
|
|
|
conn, err := chain.NewBitcoindConn(&chain.BitcoindConfig{
|
|
|
|
ChainParams: NetParams,
|
|
|
|
Host: host,
|
|
|
|
User: "weks",
|
|
|
|
Pass: "weks",
|
|
|
|
ZMQBlockHost: zmqBlockHost,
|
|
|
|
ZMQTxHost: zmqTxHost,
|
|
|
|
ZMQReadDeadline: 5 * time.Second,
|
|
|
|
// Fields only required for pruned nodes, not needed for these
|
|
|
|
// tests.
|
|
|
|
Dialer: nil,
|
|
|
|
PrunedModeMaxPeers: 0,
|
|
|
|
})
|
2018-08-17 04:29:37 +02:00
|
|
|
if err != nil {
|
|
|
|
bitcoind.Process.Kill()
|
|
|
|
bitcoind.Wait()
|
|
|
|
os.RemoveAll(tempBitcoindDir)
|
|
|
|
t.Fatalf("unable to establish connection to bitcoind: %v", err)
|
|
|
|
}
|
|
|
|
if err := conn.Start(); err != nil {
|
|
|
|
bitcoind.Process.Kill()
|
|
|
|
bitcoind.Wait()
|
|
|
|
os.RemoveAll(tempBitcoindDir)
|
|
|
|
t.Fatalf("unable to establish connection to bitcoind: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, func() {
|
|
|
|
conn.Stop()
|
|
|
|
bitcoind.Process.Kill()
|
|
|
|
bitcoind.Wait()
|
|
|
|
os.RemoveAll(tempBitcoindDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewNeutrinoBackend spawns a new neutrino node that connects to a miner at
|
|
|
|
// the specified address.
|
|
|
|
func NewNeutrinoBackend(t *testing.T, minerAddr string) (*neutrino.ChainService, func()) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
spvDir, err := ioutil.TempDir("", "neutrino")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("unable to create temp dir: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbName := filepath.Join(spvDir, "neutrino.db")
|
kvdb: add timeout options for bbolt (#4787)
* mod: bump btcwallet version to accept db timeout
* btcwallet: add DBTimeOut in config
* kvdb: add database timeout option for bbolt
This commit adds a DBTimeout option in bbolt config. The relevant
functions walletdb.Open/Create are updated to use this config. In
addition, the bolt compacter also applies the new timeout option.
* channeldb: add DBTimeout in db options
This commit adds the DBTimeout option for channeldb. A new unit
test file is created to test the default options. In addition,
the params used in kvdb.Create inside channeldb_test is updated
with a DefaultDBTimeout value.
* contractcourt+routing: use DBTimeout in kvdb
This commit touches multiple test files in contractcourt and routing.
The call of function kvdb.Create and kvdb.Open are now updated with
the new param DBTimeout, using the default value kvdb.DefaultDBTimeout.
* lncfg: add DBTimeout option in db config
The DBTimeout option is added to db config. A new unit test is
added to check the default DB config is created as expected.
* migration: add DBTimeout param in kvdb.Create/kvdb.Open
* keychain: update tests to use DBTimeout param
* htlcswitch+chainreg: add DBTimeout option
* macaroons: support DBTimeout config in creation
This commit adds the DBTimeout during the creation of macaroons.db.
The usage of kvdb.Create and kvdb.Open in its tests are updated with
a timeout value using kvdb.DefaultDBTimeout.
* walletunlocker: add dbTimeout option in UnlockerService
This commit adds a new param, dbTimeout, during the creation of
UnlockerService. This param is then passed to wallet.NewLoader
inside various service calls, specifying a timeout value to be
used when opening the bbolt. In addition, the macaroonService
is also called with this dbTimeout param.
* watchtower/wtdb: add dbTimeout param during creation
This commit adds the dbTimeout param for the creation of both
watchtower.db and wtclient.db.
* multi: add db timeout param for walletdb.Create
This commit adds the db timeout param for the function call
walletdb.Create. It touches only the test files found in chainntnfs,
lnwallet, and routing.
* lnd: pass DBTimeout config to relevant services
This commit enables lnd to pass the DBTimeout config to the following
services/config/functions,
- chainControlConfig
- walletunlocker
- wallet.NewLoader
- macaroons
- watchtower
In addition, the usage of wallet.Create is updated too.
* sample-config: add dbtimeout option
2020-12-08 00:31:49 +01:00
|
|
|
spvDatabase, err := walletdb.Create(
|
|
|
|
"bdb", dbName, true, kvdb.DefaultDBTimeout,
|
|
|
|
)
|
2018-08-17 04:29:37 +02:00
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(spvDir)
|
|
|
|
t.Fatalf("unable to create walletdb: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an instance of neutrino connected to the running btcd
|
|
|
|
// instance.
|
|
|
|
spvConfig := neutrino.Config{
|
|
|
|
DataDir: spvDir,
|
|
|
|
Database: spvDatabase,
|
|
|
|
ChainParams: *NetParams,
|
|
|
|
ConnectPeers: []string{minerAddr},
|
|
|
|
}
|
|
|
|
spvNode, err := neutrino.NewChainService(spvConfig)
|
|
|
|
if err != nil {
|
|
|
|
os.RemoveAll(spvDir)
|
|
|
|
spvDatabase.Close()
|
|
|
|
t.Fatalf("unable to create neutrino: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We'll also wait for the instance to sync up fully to the chain
|
|
|
|
// generated by the btcd instance.
|
|
|
|
spvNode.Start()
|
|
|
|
for !spvNode.IsCurrent() {
|
|
|
|
time.Sleep(time.Millisecond * 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
return spvNode, func() {
|
|
|
|
spvNode.Stop()
|
|
|
|
spvDatabase.Close()
|
|
|
|
os.RemoveAll(spvDir)
|
|
|
|
}
|
|
|
|
}
|