You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_node.py 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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"""
  6. import decimal
  7. import errno
  8. import http.client
  9. import json
  10. import logging
  11. import os
  12. import re
  13. import subprocess
  14. import time
  15. from .authproxy import JSONRPCException
  16. from .util import (
  17. assert_equal,
  18. get_rpc_proxy,
  19. rpc_url,
  20. wait_until,
  21. p2p_port,
  22. )
  23. # For Python 3.4 compatibility
  24. JSONDecodeError = getattr(json, "JSONDecodeError", ValueError)
  25. BITCOIND_PROC_WAIT_TIMEOUT = 60
  26. class TestNode():
  27. """A class for representing a bitcoind node under test.
  28. This class contains:
  29. - state about the node (whether it's running, etc)
  30. - a Python subprocess.Popen object representing the running process
  31. - an RPC connection to the node
  32. - one or more P2P connections to the node
  33. To make things easier for the test writer, any unrecognised messages will
  34. be dispatched to the RPC connection."""
  35. def __init__(self, i, dirname, extra_args, rpchost, timewait, binary, stderr, mocktime, coverage_dir, use_cli=False):
  36. self.index = i
  37. self.datadir = os.path.join(dirname, "node" + str(i))
  38. self.rpchost = rpchost
  39. if timewait:
  40. self.rpc_timeout = timewait
  41. else:
  42. # Wait for up to 60 seconds for the RPC server to respond
  43. self.rpc_timeout = 60
  44. if binary is None:
  45. self.binary = os.getenv("BITCOIND", "bitcoind")
  46. else:
  47. self.binary = binary
  48. self.stderr = stderr
  49. self.coverage_dir = coverage_dir
  50. # 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.
  51. self.extra_args = extra_args
  52. 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]
  53. self.cli = TestNodeCLI(os.getenv("BITCOINCLI", "bitcoin-cli"), self.datadir)
  54. self.use_cli = use_cli
  55. self.running = False
  56. self.process = None
  57. self.rpc_connected = False
  58. self.rpc = None
  59. self.url = None
  60. self.log = logging.getLogger('TestFramework.node%d' % i)
  61. self.cleanup_on_exit = True # Whether to kill the node when this object goes away
  62. self.p2ps = []
  63. def __del__(self):
  64. # Ensure that we don't leave any bitcoind processes lying around after
  65. # the test ends
  66. if self.process and self.cleanup_on_exit:
  67. # Should only happen on test failure
  68. # Avoid using logger, as that may have already been shutdown when
  69. # this destructor is called.
  70. print("Cleaning up leftover process")
  71. self.process.kill()
  72. def __getattr__(self, name):
  73. """Dispatches any unrecognised messages to the RPC connection or a CLI instance."""
  74. if self.use_cli:
  75. return getattr(self.cli, name)
  76. else:
  77. assert self.rpc_connected and self.rpc is not None, "Error: no RPC connection"
  78. return getattr(self.rpc, name)
  79. def start(self, extra_args=None, stderr=None, *args, **kwargs):
  80. """Start the node."""
  81. if extra_args is None:
  82. extra_args = self.extra_args
  83. if stderr is None:
  84. stderr = self.stderr
  85. self.process = subprocess.Popen(self.args + extra_args, stderr=stderr, *args, **kwargs)
  86. self.running = True
  87. self.log.debug("bitcoind started, waiting for RPC to come up")
  88. def wait_for_rpc_connection(self):
  89. """Sets up an RPC connection to the bitcoind process. Returns False if unable to connect."""
  90. # Poll at a rate of four times per second
  91. poll_per_s = 4
  92. for _ in range(poll_per_s * self.rpc_timeout):
  93. assert self.process.poll() is None, "bitcoind exited with status %i during initialization" % self.process.returncode
  94. try:
  95. self.rpc = get_rpc_proxy(rpc_url(self.datadir, self.index, self.rpchost), self.index, timeout=self.rpc_timeout, coveragedir=self.coverage_dir)
  96. self.rpc.getblockcount()
  97. # If the call to getblockcount() succeeds then the RPC connection is up
  98. self.rpc_connected = True
  99. self.url = self.rpc.url
  100. self.log.debug("RPC successfully started")
  101. return
  102. except IOError as e:
  103. if e.errno != errno.ECONNREFUSED: # Port not yet open?
  104. raise # unknown IO error
  105. except JSONRPCException as e: # Initialization phase
  106. if e.error['code'] != -28: # RPC in warmup?
  107. raise # unknown JSON RPC exception
  108. except ValueError as e: # cookie file not found and no rpcuser or rpcassword. bitcoind still starting
  109. if "No RPC credentials" not in str(e):
  110. raise
  111. time.sleep(1.0 / poll_per_s)
  112. raise AssertionError("Unable to connect to bitcoind")
  113. def get_wallet_rpc(self, wallet_name):
  114. if self.use_cli:
  115. return self.cli("-rpcwallet={}".format(wallet_name))
  116. else:
  117. assert self.rpc_connected
  118. assert self.rpc
  119. wallet_path = "wallet/%s" % wallet_name
  120. return self.rpc / wallet_path
  121. def stop_node(self):
  122. """Stop the node."""
  123. if not self.running:
  124. return
  125. self.log.debug("Stopping node")
  126. try:
  127. self.stop()
  128. except http.client.CannotSendRequest:
  129. self.log.exception("Unable to stop node.")
  130. del self.p2ps[:]
  131. def is_node_stopped(self):
  132. """Checks whether the node has stopped.
  133. Returns True if the node has stopped. False otherwise.
  134. This method is responsible for freeing resources (self.process)."""
  135. if not self.running:
  136. return True
  137. return_code = self.process.poll()
  138. if return_code is None:
  139. 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
  148. def wait_until_stopped(self, timeout=BITCOIND_PROC_WAIT_TIMEOUT):
  149. wait_until(self.is_node_stopped, timeout=timeout)
  150. def node_encrypt_wallet(self, passphrase):
  151. """"Encrypts the wallet.
  152. This causes bitcoind to shutdown, so this method takes
  153. care of cleaning up resources."""
  154. self.encryptwallet(passphrase)
  155. self.wait_until_stopped()
  156. def add_p2p_connection(self, p2p_conn, *args, **kwargs):
  157. """Add a p2p connection to the node.
  158. This method adds the p2p connection to the self.p2ps list and also
  159. returns the connection to the caller."""
  160. if 'dstport' not in kwargs:
  161. kwargs['dstport'] = p2p_port(self.index)
  162. if 'dstaddr' not in kwargs:
  163. kwargs['dstaddr'] = '127.0.0.1'
  164. p2p_conn.peer_connect(*args, **kwargs)
  165. self.p2ps.append(p2p_conn)
  166. return p2p_conn
  167. @property
  168. def p2p(self):
  169. """Return the first p2p connection
  170. Convenience property - most tests only use a single p2p connection to each
  171. node, so this saves having to write node.p2ps[0] many times."""
  172. assert self.p2ps, "No p2p connection"
  173. return self.p2ps[0]
  174. def disconnect_p2ps(self):
  175. """Close all p2p connections to the node."""
  176. for p in self.p2ps:
  177. p.peer_disconnect()
  178. del self.p2ps[:]
  179. class TestNodeCLIAttr:
  180. def __init__(self, cli, command):
  181. self.cli = cli
  182. self.command = command
  183. def __call__(self, *args, **kwargs):
  184. return self.cli.send_cli(self.command, *args, **kwargs)
  185. def get_request(self, *args, **kwargs):
  186. return lambda: self(*args, **kwargs)
  187. class TestNodeCLI():
  188. """Interface to bitcoin-cli for an individual node"""
  189. def __init__(self, binary, datadir):
  190. self.options = []
  191. self.binary = binary
  192. self.datadir = datadir
  193. self.input = None
  194. self.log = logging.getLogger('TestFramework.bitcoincli')
  195. def __call__(self, *options, input=None):
  196. # TestNodeCLI is callable with bitcoin-cli command-line options
  197. cli = TestNodeCLI(self.binary, self.datadir)
  198. cli.options = [str(o) for o in options]
  199. cli.input = input
  200. return cli
  201. def __getattr__(self, command):
  202. return TestNodeCLIAttr(self, command)
  203. def batch(self, requests):
  204. results = []
  205. for request in requests:
  206. try:
  207. results.append(dict(result=request()))
  208. except JSONRPCException as e:
  209. results.append(dict(error=e))
  210. return results
  211. def send_cli(self, command=None, *args, **kwargs):
  212. """Run bitcoin-cli command. Deserializes returned string as python object."""
  213. pos_args = [str(arg) for arg in args]
  214. named_args = [str(key) + "=" + str(value) for (key, value) in kwargs.items()]
  215. assert not (pos_args and named_args), "Cannot use positional arguments and named arguments in the same bitcoin-cli call"
  216. p_args = [self.binary, "-datadir=" + self.datadir] + self.options
  217. if named_args:
  218. p_args += ["-named"]
  219. if command is not None:
  220. p_args += [command]
  221. p_args += pos_args + named_args
  222. self.log.debug("Running bitcoin-cli command: %s" % command)
  223. process = subprocess.Popen(p_args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
  224. cli_stdout, cli_stderr = process.communicate(input=self.input)
  225. returncode = process.poll()
  226. if returncode:
  227. match = re.match(r'error code: ([-0-9]+)\nerror message:\n(.*)', cli_stderr)
  228. if match:
  229. code, message = match.groups()
  230. raise JSONRPCException(dict(code=int(code), message=message))
  231. # Ignore cli_stdout, raise with cli_stderr
  232. raise subprocess.CalledProcessError(returncode, self.binary, output=cli_stderr)
  233. try:
  234. return json.loads(cli_stdout, parse_float=decimal.Decimal)
  235. except JSONDecodeError:
  236. return cli_stdout.rstrip("\n")