Remove unused Python imports
[bitcoinplatinum.git] / test / functional / test_framework / test_node.py
bloba9248c764e3d440f7cab4cf7269cc17ea4dd66d5
1 #!/usr/bin/env python3
2 # Copyright (c) 2017 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Class for bitcoind node under test"""
7 import decimal
8 import errno
9 import http.client
10 import json
11 import logging
12 import os
13 import subprocess
14 import time
16 from .authproxy import JSONRPCException
17 from .util import (
18 assert_equal,
19 get_rpc_proxy,
20 rpc_url,
21 wait_until,
22 p2p_port,
25 BITCOIND_PROC_WAIT_TIMEOUT = 60
27 class TestNode():
28 """A class for representing a bitcoind node under test.
30 This class contains:
32 - state about the node (whether it's running, etc)
33 - a Python subprocess.Popen object representing the running process
34 - an RPC connection to the node
35 - one or more P2P connections to the node
38 To make things easier for the test writer, any unrecognised messages will
39 be dispatched to the RPC connection."""
41 def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mocktime, coverage_dir):
42 self.index = i
43 self.datadir = os.path.join(dirname, "node" + str(i))
44 self.rpchost = rpchost
45 if timewait:
46 self.rpc_timeout = timewait
47 else:
48 # Wait for up to 60 seconds for the RPC server to respond
49 self.rpc_timeout = 60
50 if binary is None:
51 self.binary = os.getenv("BITCOIND", "bitcoind")
52 else:
53 self.binary = binary
54 self.stderr = stderr
55 self.coverage_dir = coverage_dir
56 # 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.
57 self.extra_args = extra_args
58 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]
60 self.cli = TestNodeCLI(os.getenv("BITCOINCLI", "bitcoin-cli"), self.datadir)
62 self.running = False
63 self.process = None
64 self.rpc_connected = False
65 self.rpc = None
66 self.url = None
67 self.log = logging.getLogger('TestFramework.node%d' % i)
69 self.p2ps = []
71 def __getattr__(self, name):
72 """Dispatches any unrecognised messages to the RPC connection."""
73 assert self.rpc_connected and self.rpc is not None, "Error: no RPC connection"
74 return getattr(self.rpc, name)
76 def start(self, extra_args=None, stderr=None):
77 """Start the node."""
78 if extra_args is None:
79 extra_args = self.extra_args
80 if stderr is None:
81 stderr = self.stderr
82 self.process = subprocess.Popen(self.args + extra_args, stderr=stderr)
83 self.running = True
84 self.log.debug("bitcoind started, waiting for RPC to come up")
86 def wait_for_rpc_connection(self):
87 """Sets up an RPC connection to the bitcoind process. Returns False if unable to connect."""
88 # Poll at a rate of four times per second
89 poll_per_s = 4
90 for _ in range(poll_per_s * self.rpc_timeout):
91 assert self.process.poll() is None, "bitcoind exited with status %i during initialization" % self.process.returncode
92 try:
93 self.rpc = get_rpc_proxy(rpc_url(self.datadir, self.index, self.rpchost), self.index, timeout=self.rpc_timeout, coveragedir=self.coverage_dir)
94 self.rpc.getblockcount()
95 # If the call to getblockcount() succeeds then the RPC connection is up
96 self.rpc_connected = True
97 self.url = self.rpc.url
98 self.log.debug("RPC successfully started")
99 return
100 except IOError as e:
101 if e.errno != errno.ECONNREFUSED: # Port not yet open?
102 raise # unknown IO error
103 except JSONRPCException as e: # Initialization phase
104 if e.error['code'] != -28: # RPC in warmup?
105 raise # unknown JSON RPC exception
106 except ValueError as e: # cookie file not found and no rpcuser or rpcassword. bitcoind still starting
107 if "No RPC credentials" not in str(e):
108 raise
109 time.sleep(1.0 / poll_per_s)
110 raise AssertionError("Unable to connect to bitcoind")
112 def get_wallet_rpc(self, wallet_name):
113 assert self.rpc_connected
114 assert self.rpc
115 wallet_path = "wallet/%s" % wallet_name
116 return self.rpc / wallet_path
118 def stop_node(self):
119 """Stop the node."""
120 if not self.running:
121 return
122 self.log.debug("Stopping node")
123 try:
124 self.stop()
125 except http.client.CannotSendRequest:
126 self.log.exception("Unable to stop node.")
127 del self.p2ps[:]
129 def is_node_stopped(self):
130 """Checks whether the node has stopped.
132 Returns True if the node has stopped. False otherwise.
133 This method is responsible for freeing resources (self.process)."""
134 if not self.running:
135 return True
136 return_code = self.process.poll()
137 if return_code is None:
138 return False
140 # process has stopped. Assert that it didn't return an error code.
141 assert_equal(return_code, 0)
142 self.running = False
143 self.process = None
144 self.rpc_connected = False
145 self.rpc = None
146 self.log.debug("Node stopped")
147 return True
149 def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_TIMEOUT):
150 wait_until(self.is_node_stopped, timeout=timeout)
152 def node_encrypt_wallet(self, passphrase):
153 """"Encrypts the wallet.
155 This causes bitcoind to shutdown, so this method takes
156 care of cleaning up resources."""
157 self.encryptwallet(passphrase)
158 self.wait_until_stopped()
160 def add_p2p_connection(self, p2p_conn, *args, **kwargs):
161 """Add a p2p connection to the node.
163 This method adds the p2p connection to the self.p2ps list and also
164 returns the connection to the caller."""
165 if 'dstport' not in kwargs:
166 kwargs['dstport'] = p2p_port(self.index)
167 if 'dstaddr' not in kwargs:
168 kwargs['dstaddr'] = '127.0.0.1'
170 p2p_conn.peer_connect(*args, **kwargs)
171 self.p2ps.append(p2p_conn)
173 return p2p_conn
175 @property
176 def p2p(self):
177 """Return the first p2p connection
179 Convenience property - most tests only use a single p2p connection to each
180 node, so this saves having to write node.p2ps[0] many times."""
181 assert self.p2ps, "No p2p connection"
182 return self.p2ps[0]
184 def disconnect_p2ps(self):
185 """Close all p2p connections to the node."""
186 for p in self.p2ps:
187 p.peer_disconnect()
188 del self.p2ps[:]
191 class TestNodeCLI():
192 """Interface to bitcoin-cli for an individual node"""
194 def __init__(self, binary, datadir):
195 self.args = []
196 self.binary = binary
197 self.datadir = datadir
198 self.input = None
200 def __call__(self, *args, input=None):
201 # TestNodeCLI is callable with bitcoin-cli command-line args
202 self.args = [str(arg) for arg in args]
203 self.input = input
204 return self
206 def __getattr__(self, command):
207 def dispatcher(*args, **kwargs):
208 return self.send_cli(command, *args, **kwargs)
209 return dispatcher
211 def send_cli(self, command, *args, **kwargs):
212 """Run bitcoin-cli command. Deserializes returned string as python object."""
214 pos_args = [str(arg) for arg in args]
215 named_args = [str(key) + "=" + str(value) for (key, value) in kwargs.items()]
216 assert not (pos_args and named_args), "Cannot use positional arguments and named arguments in the same bitcoin-cli call"
217 p_args = [self.binary, "-datadir=" + self.datadir] + self.args
218 if named_args:
219 p_args += ["-named"]
220 p_args += [command] + pos_args + named_args
221 process = subprocess.Popen(p_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
222 cli_stdout, cli_stderr = process.communicate(input=self.input)
223 returncode = process.poll()
224 if returncode:
225 # Ignore cli_stdout, raise with cli_stderr
226 raise subprocess.CalledProcessError(returncode, self.binary, output=cli_stderr)
227 return json.loads(cli_stdout, parse_float=decimal.Decimal)