Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/test_node.py
Show First 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | def __init__(self, i, dirname, extra_args, host, rpc_port, p2p_port, timewait, binary, stderr, mocktime, coverage_dir, use_cli=False): | ||||
# Wait for up to 60 seconds for the RPC server to respond | # Wait for up to 60 seconds for the RPC server to respond | ||||
self.rpc_timeout = 60 | self.rpc_timeout = 60 | ||||
if binary is None: | if binary is None: | ||||
self.binary = os.getenv("BITCOIND", "bitcoind") | self.binary = os.getenv("BITCOIND", "bitcoind") | ||||
else: | else: | ||||
self.binary = binary | self.binary = binary | ||||
self.stderr = stderr | self.stderr = stderr | ||||
self.coverage_dir = coverage_dir | self.coverage_dir = coverage_dir | ||||
# Most callers will just need to add extra args to the standard list below. For those callers that need more flexibity, they can just set the args property directly. | # Most callers will just need to add extra args to the default list | ||||
# below. | |||||
# For those callers that need more flexibity, they can access the | |||||
# default args using the provided facilities | |||||
self.extra_args = extra_args | self.extra_args = extra_args | ||||
self.args = [self.binary, "-datadir=" + self.datadir, "-server", "-keypool=1", "-discover=0", "-rest", "-logtimemicros", | self.default_args = ["-datadir=" + self.datadir, "-server", "-keypool=1", "-discover=0", "-rest", "-logtimemicros", | ||||
"-debug", "-debugexclude=libevent", "-debugexclude=leveldb", "-mocktime=" + str(mocktime), "-uacomment=" + self.name] | "-debug", "-debugexclude=libevent", "-debugexclude=leveldb", "-mocktime=" + str(mocktime), "-uacomment=" + self.name] | ||||
self.cli = TestNodeCLI( | self.cli = TestNodeCLI( | ||||
os.getenv("BITCOINCLI", "bitcoin-cli"), self.datadir) | os.getenv("BITCOINCLI", "bitcoin-cli"), self.datadir) | ||||
self.use_cli = use_cli | self.use_cli = use_cli | ||||
self.running = False | self.running = False | ||||
self.process = None | self.process = None | ||||
self.rpc_connected = False | self.rpc_connected = False | ||||
self.rpc = None | self.rpc = None | ||||
self.url = None | self.url = None | ||||
self.relay_fee_cache = None | self.relay_fee_cache = None | ||||
self.log = logging.getLogger('TestFramework.node%d' % i) | self.log = logging.getLogger('TestFramework.node%d' % i) | ||||
self.p2ps = [] | self.p2ps = [] | ||||
def __getattr__(self, name): | def __getattr__(self, name): | ||||
"""Dispatches any unrecognised messages to the RPC connection or a CLI instance.""" | """Dispatches any unrecognised messages to the RPC connection or a CLI instance.""" | ||||
if self.use_cli: | if self.use_cli: | ||||
return getattr(self.cli, name) | return getattr(self.cli, name) | ||||
else: | else: | ||||
assert self.rpc is not None, "Error: RPC not initialized" | assert self.rpc is not None, "Error: RPC not initialized" | ||||
assert self.rpc_connected, "Error: No RPC connection" | assert self.rpc_connected, "Error: No RPC connection" | ||||
return getattr(self.rpc, name) | return getattr(self.rpc, name) | ||||
def clear_default_args(self): | |||||
self.default_args.clear() | |||||
def extend_default_args(self, args): | |||||
self.default_args.extend(args) | |||||
def remove_default_args(self, args): | |||||
for rm_arg in args: | |||||
# Remove all occurrences of rm_arg in self.default_args: | |||||
# - if the arg is a flag (-flag), then the names must match | |||||
# - if the arg is a value (-key=value) then the name must starts | |||||
# with "-key=" (the '"' char is to avoid removing "-key_suffix" | |||||
# arg is "-key" is the argument to remove). | |||||
self.default_args = [def_arg for def_arg in self.default_args | |||||
if rm_arg != def_arg and not def_arg.startswith(rm_arg + '=')] | |||||
def start(self, extra_args=None, stderr=None, *args, **kwargs): | def start(self, extra_args=None, stderr=None, *args, **kwargs): | ||||
"""Start the node.""" | """Start the node.""" | ||||
if extra_args is None: | if extra_args is None: | ||||
extra_args = self.extra_args | extra_args = self.extra_args | ||||
if stderr is None: | if stderr is None: | ||||
stderr = self.stderr | stderr = self.stderr | ||||
self.process = subprocess.Popen( | self.process = subprocess.Popen( | ||||
self.args + extra_args, stderr=stderr, *args, **kwargs) | [self.binary] + self.default_args + extra_args, | ||||
stderr=stderr, *args, **kwargs) | |||||
self.running = True | self.running = True | ||||
self.log.debug("bitcoind started, waiting for RPC to come up") | self.log.debug("bitcoind started, waiting for RPC to come up") | ||||
def wait_for_rpc_connection(self): | def wait_for_rpc_connection(self): | ||||
"""Sets up an RPC connection to the bitcoind process. Returns False if unable to connect.""" | """Sets up an RPC connection to the bitcoind process. Returns False if unable to connect.""" | ||||
# Poll at a rate of four times per second | # Poll at a rate of four times per second | ||||
poll_per_s = 4 | poll_per_s = 4 | ||||
for _ in range(poll_per_s * self.rpc_timeout): | for _ in range(poll_per_s * self.rpc_timeout): | ||||
▲ Show 20 Lines • Show All 198 Lines • Show Last 20 Lines |