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 11KB

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