Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/test_node.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2017 The Bitcoin Core developers | # Copyright (c) 2017 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Class for bitcoind node under test""" | """Class for bitcoind node under test""" | ||||
import decimal | |||||
import errno | import errno | ||||
import http.client | import http.client | ||||
import json | |||||
import logging | import logging | ||||
import os | import os | ||||
import subprocess | import subprocess | ||||
import time | import time | ||||
from .util import ( | from .util import ( | ||||
assert_equal, | assert_equal, | ||||
get_rpc_proxy, | get_rpc_proxy, | ||||
Show All 29 Lines | def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mocktime, coverage_dir): | ||||
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 standard list below. For those callers that need more flexibity, they can just set the args property directly. | ||||
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.args = [self.binary, "-datadir=" + self.datadir, "-server", "-keypool=1", "-discover=0", "-rest", "-logtimemicros", | ||||
"-debug", "-debugexclude=libevent", "-debugexclude=leveldb", "-mocktime=" + str(mocktime), "-uacomment=testnode%d" % i] | "-debug", "-debugexclude=libevent", "-debugexclude=leveldb", "-mocktime=" + str(mocktime), "-uacomment=testnode%d" % i] | ||||
self.cli = TestNodeCLI( | |||||
os.getenv("BITCOINCLI", "bitcoin-cli"), self.datadir) | |||||
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.log = logging.getLogger('TestFramework.node%d' % i) | self.log = logging.getLogger('TestFramework.node%d' % i) | ||||
def __getattr__(self, *args, **kwargs): | def __getattr__(self, *args, **kwargs): | ||||
▲ Show 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | def node_encrypt_wallet(self, passphrase): | ||||
This causes bitcoind to shutdown, so this method takes | This causes bitcoind to shutdown, so this method takes | ||||
care of cleaning up resources.""" | care of cleaning up resources.""" | ||||
self.encryptwallet(passphrase) | self.encryptwallet(passphrase) | ||||
while not self.is_node_stopped(): | while not self.is_node_stopped(): | ||||
time.sleep(0.1) | time.sleep(0.1) | ||||
self.rpc = None | self.rpc = None | ||||
self.rpc_connected = False | self.rpc_connected = False | ||||
class TestNodeCLI(): | |||||
"""Interface to bitcoin-cli for an individual node""" | |||||
def __init__(self, binary, datadir): | |||||
self.binary = binary | |||||
self.datadir = datadir | |||||
def __getattr__(self, command): | |||||
def dispatcher(*args, **kwargs): | |||||
return self.send_cli(command, *args, **kwargs) | |||||
return dispatcher | |||||
def send_cli(self, command, *args, **kwargs): | |||||
"""Run bitcoin-cli command. Deserializes returned string as python object.""" | |||||
pos_args = [str(arg) for arg in args] | |||||
named_args = [str(key) + "=" + str(value) | |||||
for (key, value) in kwargs.items()] | |||||
assert not ( | |||||
pos_args and named_args), "Cannot use positional arguments and named arguments in the same bitcoin-cli call" | |||||
p_args = [self.binary, "-datadir=" + self.datadir] | |||||
if named_args: | |||||
p_args += ["-named"] | |||||
p_args += [command] + pos_args + named_args | |||||
cli_output = subprocess.check_output(p_args, universal_newlines=True) | |||||
return json.loads(cli_output, parse_float=decimal.Decimal) |