lbcd/rpctest/utils.go

172 lines
4.3 KiB
Go
Raw Normal View History

rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
// Copyright (c) 2016 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package rpctest
import (
"net"
"reflect"
"strconv"
"time"
"github.com/btcsuite/btcd/chaincfg/chainhash"
rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
"github.com/btcsuite/btcrpcclient"
)
// JoinType is an enum representing a particular type of "node join". A node
// join is a synchronization tool used to wait until a subset of nodes have a
// consistent state with respect to an attribute.
type JoinType uint8
const (
// Blocks is a JoinType which waits until all nodes share the same
// block height.
Blocks JoinType = iota
// Mempools is a JoinType which blocks until all nodes have identical
// mempool.
Mempools
)
// JoinNodes is a synchronization tool used to block until all passed nodes are
// fully synced with respect to an attribute. This function will block for a
// period of time, finally returning once all nodes are synced according to the
// passed JoinType. This function be used to to ensure all active test
// harnesses are at a consistent state before proceeding to an assertion or
// check within rpc tests.
func JoinNodes(nodes []*Harness, joinType JoinType) error {
switch joinType {
case Blocks:
return syncBlocks(nodes)
case Mempools:
return syncMempools(nodes)
}
return nil
}
// syncMempools blocks until all nodes have identical mempools.
func syncMempools(nodes []*Harness) error {
poolsMatch := false
retry:
rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
for !poolsMatch {
firstPool, err := nodes[0].Node.GetRawMempool()
if err != nil {
return err
}
// If all nodes have an identical mempool with respect to the
// first node, then we're done. Otherwise, drop back to the top
// of the loop and retry after a short wait period.
for _, node := range nodes[1:] {
rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
nodePool, err := node.Node.GetRawMempool()
if err != nil {
return err
}
if !reflect.DeepEqual(firstPool, nodePool) {
time.Sleep(time.Millisecond * 100)
continue retry
rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
}
}
poolsMatch = true
}
return nil
}
// syncBlocks blocks until all nodes report the same best chain.
rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
func syncBlocks(nodes []*Harness) error {
blocksMatch := false
retry:
rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
for !blocksMatch {
var prevHash *chainhash.Hash
var prevHeight int32
rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
for _, node := range nodes {
blockHash, blockHeight, err := node.Node.GetBestBlock()
rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
if err != nil {
return err
}
if prevHash != nil && (*blockHash != *prevHash ||
blockHeight != prevHeight) {
rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
time.Sleep(time.Millisecond * 100)
continue retry
rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
}
prevHash, prevHeight = blockHash, blockHeight
rpctest: create new rpctest package This commit adds a new package (rpctest) which provides functionality for writing automated black box tests to exercise the RPC interface. An instance of a rpctest consists of an active btcd process running in (typically) --simnet mode, a btcrpcclient instance connected to said node, and finally an embedded in-memory wallet instance (the memWallet) which manages any created coinbase outputs created by the mining btcd node. As part of the SetUp process for an RPC test, a test author can optionally opt to have a test blockchain created. The second argument to SetUp dictates the number of mature coinbase outputs desired. The btcd process will then be directed to generate a test chain of length: 100 + numMatureOutputs. The embedded memWallet instance acts as a minimal, simple wallet for each Harness instance. The memWallet itself is a BIP 32 HD wallet capable of creating new addresses, creating fully signed transactions, creating+broadcasting a transaction paying to an arbitrary set of outputs, and querying the currently confirmed balance. In order to test various scenarios of blocks containing arbitrary transactions, one can use the Generate rpc call via the exposed btcrpcclient connected to the active btcd node. Additionally, the Harness also exposes a secondary block generation API allowing callers to create blocks with a set of hand-selected transactions, and an arbitrary BlockVersion or Timestamp. After execution of test logic TearDown should be called, allowing the test instance to clean up created temporary directories, and shut down the running processes. Running multiple concurrent rpctest.Harness instances is supported in order to allow for test authors to exercise complex scenarios. As a result, the primary interface to create, and initialize an rpctest.Harness instance is concurrent safe, with shared package level private global variables protected by a sync.Mutex. Fixes #116.
2016-08-20 00:36:56 +02:00
}
blocksMatch = true
}
return nil
}
// ConnectNode establishes a new peer-to-peer connection between the "from"
// harness and the "to" harness. The connection made is flagged as persistent,
// therefore in the case of disconnects, "from" will attempt to reestablish a
// connection to the "to" harness.
func ConnectNode(from *Harness, to *Harness) error {
// Calculate the target p2p addr+port for the node to be connected to.
// p2p ports uses within the package are always even, so we multiply
// the node number by two before offsetting from the defaultP2pPort.
targetPort := defaultP2pPort + (2 * to.nodeNum)
targetAddr := net.JoinHostPort("127.0.0.1", strconv.Itoa(targetPort))
peerInfo, err := from.Node.GetPeerInfo()
if err != nil {
return err
}
numPeers := len(peerInfo)
if err := from.Node.AddNode(targetAddr, btcrpcclient.ANAdd); err != nil {
return err
}
// Block until a new connection has been established.
peerInfo, err = from.Node.GetPeerInfo()
if err != nil {
return err
}
for len(peerInfo) <= numPeers {
peerInfo, err = from.Node.GetPeerInfo()
if err != nil {
return err
}
}
return nil
}
// TearDownAll tears down all active test harnesses.
func TearDownAll() error {
harnessStateMtx.Lock()
defer harnessStateMtx.Unlock()
for _, harness := range testInstances {
if err := harness.TearDown(); err != nil {
return err
}
}
return nil
}
// ActiveHarnesses returns a slice of all currently active test harnesses. A
// test harness if considered "active" if it has been created, but not yet torn
// down.
func ActiveHarnesses() []*Harness {
harnessStateMtx.RLock()
defer harnessStateMtx.RUnlock()
activeNodes := make([]*Harness, 0, len(testInstances))
for _, harness := range testInstances {
activeNodes = append(activeNodes, harness)
}
return activeNodes
}