Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/test_node.py
Show All 10 Lines | |||||
import logging | import logging | ||||
import os | import os | ||||
import subprocess | import subprocess | ||||
import time | import time | ||||
from .mininode import COIN, ToHex, FromHex, CTransaction | from .mininode import COIN, ToHex, FromHex, CTransaction | ||||
from .util import ( | from .util import ( | ||||
assert_equal, | assert_equal, | ||||
p2p_port, | |||||
rpc_port, | |||||
get_rpc_proxy, | get_rpc_proxy, | ||||
rpc_url, | rpc_url, | ||||
wait_until, | wait_until, | ||||
) | ) | ||||
from .authproxy import JSONRPCException | from .authproxy import JSONRPCException | ||||
BITCOIND_PROC_WAIT_TIMEOUT = 60 | BITCOIND_PROC_WAIT_TIMEOUT = 60 | ||||
class TestNode(): | class TestNode(): | ||||
"""A class for representing a bitcoind node under test. | """A class for representing a bitcoind node under test. | ||||
This class contains: | This class contains: | ||||
- state about the node (whether it's running, etc) | - state about the node (whether it's running, etc) | ||||
- a Python subprocess.Popen object representing the running process | - a Python subprocess.Popen object representing the running process | ||||
- an RPC connection to the node | - an RPC connection to the node | ||||
To make things easier for the test writer, a bit of magic is happening under the covers. | To make things easier for the test writer, a bit of magic is happening under the covers. | ||||
Any unrecognised messages will be dispatched to the RPC connection.""" | Any unrecognised messages will be dispatched to the RPC connection.""" | ||||
def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mocktime, coverage_dir): | def __init__(self, i, dirname, extra_args, host, rpc_port, p2p_port, timewait, binary, stderr, mocktime, coverage_dir): | ||||
self.index = i | self.index = i | ||||
self.datadir = os.path.join(dirname, "node" + str(i)) | self.datadir = os.path.join(dirname, "node" + str(i)) | ||||
self.rpchost = rpchost | self.host = host | ||||
self.rpc_port = rpc_port | |||||
self.p2p_port = p2p_port | |||||
self.nodename = "testnode{}".format(i) | |||||
if timewait: | if timewait: | ||||
self.rpc_timeout = timewait | self.rpc_timeout = timewait | ||||
else: | else: | ||||
# 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 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={}".format(self.nodename)] | ||||
self.cli = TestNodeCLI( | self.cli = TestNodeCLI( | ||||
os.getenv("BITCOINCLI", "bitcoin-cli"), self.datadir) | 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 | ||||
Show All 20 Lines | class TestNode(): | ||||
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): | ||||
assert self.process.poll( | assert self.process.poll( | ||||
) is None, "bitcoind exited with status %i during initialization" % self.process.returncode | ) is None, "bitcoind exited with status %i during initialization" % self.process.returncode | ||||
try: | try: | ||||
self.rpc = get_rpc_proxy(rpc_url(self.datadir, self.index, self.rpchost), | self.rpc = get_rpc_proxy(rpc_url(self.datadir, self.index, self.host), | ||||
self.index, timeout=self.rpc_timeout, coveragedir=self.coverage_dir) | self.index, timeout=self.rpc_timeout, coveragedir=self.coverage_dir) | ||||
self.rpc.getblockcount() | self.rpc.getblockcount() | ||||
# If the call to getblockcount() succeeds then the RPC connection is up | # If the call to getblockcount() succeeds then the RPC connection is up | ||||
self.rpc_connected = True | self.rpc_connected = True | ||||
self.url = self.rpc.url | self.url = self.rpc.url | ||||
self.log.debug("RPC successfully started") | self.log.debug("RPC successfully started") | ||||
return | return | ||||
except IOError as e: | except IOError as e: | ||||
▲ Show 20 Lines • Show All 122 Lines • Show Last 20 Lines |