aboutsummaryrefslogtreecommitdiff
path: root/test/functional/test_framework/test_framework.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/functional/test_framework/test_framework.py')
-rwxr-xr-xtest/functional/test_framework/test_framework.py126
1 files changed, 62 insertions, 64 deletions
diff --git a/test/functional/test_framework/test_framework.py b/test/functional/test_framework/test_framework.py
index 09d7d877a7..aa674f9ebe 100755
--- a/test/functional/test_framework/test_framework.py
+++ b/test/functional/test_framework/test_framework.py
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
-# Copyright (c) 2014-2018 The Bitcoin Core developers
+# Copyright (c) 2014-2019 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Base class for RPC testing."""
@@ -10,6 +10,7 @@ import logging
import argparse
import os
import pdb
+import random
import shutil
import sys
import tempfile
@@ -28,7 +29,6 @@ from .util import (
disconnect_nodes,
get_datadir_path,
initialize_datadir,
- p2p_port,
sync_blocks,
sync_mempools,
)
@@ -129,6 +129,8 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
help="use bitcoin-cli instead of RPC for all commands")
parser.add_argument("--perf", dest="perf", default=False, action="store_true",
help="profile running nodes with perf for the duration of the test")
+ parser.add_argument("--randomseed", type=int,
+ help="set a random seed for deterministically reproducing a previous test run")
self.add_options(parser)
self.options = parser.parse_args()
@@ -158,6 +160,22 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
self.options.tmpdir = tempfile.mkdtemp(prefix=TMPDIR_PREFIX)
self._start_logging()
+ # Seed the PRNG. Note that test runs are reproducible if and only if
+ # a single thread accesses the PRNG. For more information, see
+ # https://docs.python.org/3/library/random.html#notes-on-reproducibility.
+ # The network thread shouldn't access random. If we need to change the
+ # network thread to access randomness, it should instantiate its own
+ # random.Random object.
+ seed = self.options.randomseed
+
+ if seed is None:
+ seed = random.randrange(sys.maxsize)
+ else:
+ self.log.debug("User supplied random seed {}".format(seed))
+
+ random.seed(seed)
+ self.log.debug("PRNG seed is: {}".format(seed))
+
self.log.debug('Setting up network thread')
self.network_thread = NetworkThread()
self.network_thread.start()
@@ -396,7 +414,8 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
"""
disconnect_nodes(self.nodes[1], 2)
disconnect_nodes(self.nodes[2], 1)
- self.sync_all([self.nodes[:2], self.nodes[2:]])
+ self.sync_all(self.nodes[:2])
+ self.sync_all(self.nodes[2:])
def join_network(self):
"""
@@ -405,13 +424,15 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
connect_nodes_bi(self.nodes, 1, 2)
self.sync_all()
- def sync_all(self, node_groups=None):
- if not node_groups:
- node_groups = [self.nodes]
+ def sync_blocks(self, nodes=None, **kwargs):
+ sync_blocks(nodes or self.nodes, **kwargs)
+
+ def sync_mempools(self, nodes=None, **kwargs):
+ sync_mempools(nodes or self.nodes, **kwargs)
- for group in node_groups:
- sync_blocks(group)
- sync_mempools(group)
+ def sync_all(self, nodes=None, **kwargs):
+ self.sync_blocks(nodes, **kwargs)
+ self.sync_mempools(nodes, **kwargs)
# Private helper methods. These should not be accessed by the subclass test scripts.
@@ -446,35 +467,23 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
def _initialize_chain(self):
"""Initialize a pre-mined blockchain for use by the test.
- Create a cache of a 199-block-long chain (with wallet) for MAX_NODES
+ Create a cache of a 199-block-long chain
Afterward, create num_nodes copies from the cache."""
+ CACHE_NODE_ID = 0 # Use node 0 to create the cache for all other nodes
+ cache_node_dir = get_datadir_path(self.options.cachedir, CACHE_NODE_ID)
assert self.num_nodes <= MAX_NODES
- create_cache = False
- for i in range(MAX_NODES):
- if not os.path.isdir(get_datadir_path(self.options.cachedir, i)):
- create_cache = True
- break
-
- if create_cache:
- self.log.debug("Creating data directories from cached datadir")
-
- # find and delete old cache directories if any exist
- for i in range(MAX_NODES):
- if os.path.isdir(get_datadir_path(self.options.cachedir, i)):
- shutil.rmtree(get_datadir_path(self.options.cachedir, i))
-
- # Create cache directories, run bitcoinds:
- for i in range(MAX_NODES):
- datadir = initialize_datadir(self.options.cachedir, i)
- args = [self.options.bitcoind, "-datadir=" + datadir, '-disablewallet']
- if i > 0:
- args.append("-connect=127.0.0.1:" + str(p2p_port(0)))
- self.nodes.append(TestNode(
- i,
- get_datadir_path(self.options.cachedir, i),
+
+ if not os.path.isdir(cache_node_dir):
+ self.log.debug("Creating cache directory {}".format(cache_node_dir))
+
+ initialize_datadir(self.options.cachedir, CACHE_NODE_ID)
+ self.nodes.append(
+ TestNode(
+ CACHE_NODE_ID,
+ cache_node_dir,
extra_conf=["bind=127.0.0.1"],
- extra_args=[],
+ extra_args=['-disablewallet'],
rpchost=None,
timewait=self.rpc_timeout,
bitcoind=self.options.bitcoind,
@@ -482,12 +491,10 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
coverage_dir=None,
cwd=self.options.tmpdir,
))
- self.nodes[i].args = args
- self.start_node(i)
+ self.start_node(CACHE_NODE_ID)
# Wait for RPC connections to be ready
- for node in self.nodes:
- node.wait_for_rpc_connection()
+ self.nodes[CACHE_NODE_ID].wait_for_rpc_connection()
# Create a 199-block-long chain; each of the 4 first nodes
# gets 25 mature blocks and 25 immature.
@@ -496,29 +503,29 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
# This is needed so that we are out of IBD when the test starts,
# see the tip age check in IsInitialBlockDownload().
for i in range(8):
- self.nodes[0].generatetoaddress(25 if i != 7 else 24, self.nodes[i % 4].get_deterministic_priv_key().address)
- sync_blocks(self.nodes)
+ self.nodes[CACHE_NODE_ID].generatetoaddress(
+ nblocks=25 if i != 7 else 24,
+ address=TestNode.PRIV_KEYS[i % 4].address,
+ )
- for n in self.nodes:
- assert_equal(n.getblockchaininfo()["blocks"], 199)
+ assert_equal(self.nodes[CACHE_NODE_ID].getblockchaininfo()["blocks"], 199)
- # Shut them down, and clean up cache directories:
+ # Shut it down, and clean up cache directories:
self.stop_nodes()
self.nodes = []
- def cache_path(n, *paths):
- return os.path.join(get_datadir_path(self.options.cachedir, n), "regtest", *paths)
+ def cache_path(*paths):
+ return os.path.join(cache_node_dir, "regtest", *paths)
- for i in range(MAX_NODES):
- os.rmdir(cache_path(i, 'wallets')) # Remove empty wallets dir
- for entry in os.listdir(cache_path(i)):
- if entry not in ['chainstate', 'blocks']:
- os.remove(cache_path(i, entry))
+ os.rmdir(cache_path('wallets')) # Remove empty wallets dir
+ for entry in os.listdir(cache_path()):
+ if entry not in ['chainstate', 'blocks']: # Only keep chainstate and blocks folder
+ os.remove(cache_path(entry))
for i in range(self.num_nodes):
- from_dir = get_datadir_path(self.options.cachedir, i)
+ self.log.debug("Copy cache directory {} to node {}".format(cache_node_dir, i))
to_dir = get_datadir_path(self.options.tmpdir, i)
- shutil.copytree(from_dir, to_dir)
+ shutil.copytree(cache_node_dir, to_dir)
initialize_datadir(self.options.tmpdir, i) # Overwrite port/rpcport in bitcoin.conf
def _initialize_chain_clean(self):
@@ -553,21 +560,12 @@ class BitcoinTestFramework(metaclass=BitcoinTestMetaClass):
def is_cli_compiled(self):
"""Checks whether bitcoin-cli was compiled."""
- config = configparser.ConfigParser()
- config.read_file(open(self.options.configfile))
-
- return config["components"].getboolean("ENABLE_CLI")
+ return self.config["components"].getboolean("ENABLE_CLI")
def is_wallet_compiled(self):
"""Checks whether the wallet module was compiled."""
- config = configparser.ConfigParser()
- config.read_file(open(self.options.configfile))
-
- return config["components"].getboolean("ENABLE_WALLET")
+ return self.config["components"].getboolean("ENABLE_WALLET")
def is_zmq_compiled(self):
"""Checks whether the zmq module was compiled."""
- config = configparser.ConfigParser()
- config.read_file(open(self.options.configfile))
-
- return config["components"].getboolean("ENABLE_ZMQ")
+ return self.config["components"].getboolean("ENABLE_ZMQ")