Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

test_framework.py 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2014-2016 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. """Base class for RPC testing."""
  6. from collections import deque
  7. import logging
  8. import optparse
  9. import os
  10. import shutil
  11. import subprocess
  12. import sys
  13. import tempfile
  14. import time
  15. from .util import (
  16. PortSeed,
  17. MAX_NODES,
  18. bitcoind_processes,
  19. check_json_precision,
  20. connect_nodes_bi,
  21. disable_mocktime,
  22. disconnect_nodes,
  23. enable_coverage,
  24. enable_mocktime,
  25. get_mocktime,
  26. get_rpc_proxy,
  27. initialize_datadir,
  28. log_filename,
  29. p2p_port,
  30. rpc_url,
  31. set_node_times,
  32. start_node,
  33. start_nodes,
  34. stop_node,
  35. stop_nodes,
  36. sync_blocks,
  37. sync_mempools,
  38. wait_for_bitcoind_start,
  39. )
  40. from .authproxy import JSONRPCException
  41. class BitcoinTestFramework(object):
  42. """Base class for a bitcoin test script.
  43. Individual bitcoin test scripts should subclass this class and override the following methods:
  44. - __init__()
  45. - add_options()
  46. - setup_chain()
  47. - setup_network()
  48. - run_test()
  49. The main() method should not be overridden.
  50. This class also contains various public and private helper methods."""
  51. # Methods to override in subclass test scripts.
  52. TEST_EXIT_PASSED = 0
  53. TEST_EXIT_FAILED = 1
  54. TEST_EXIT_SKIPPED = 77
  55. def __init__(self):
  56. self.num_nodes = 4
  57. self.setup_clean_chain = False
  58. self.nodes = None
  59. def add_options(self, parser):
  60. pass
  61. def setup_chain(self):
  62. self.log.info("Initializing test directory "+self.options.tmpdir)
  63. if self.setup_clean_chain:
  64. self._initialize_chain_clean(self.options.tmpdir, self.num_nodes)
  65. else:
  66. self._initialize_chain(self.options.tmpdir, self.num_nodes, self.options.cachedir)
  67. def setup_network(self):
  68. self.setup_nodes()
  69. # Connect the nodes as a "chain". This allows us
  70. # to split the network between nodes 1 and 2 to get
  71. # two halves that can work on competing chains.
  72. for i in range(self.num_nodes - 1):
  73. connect_nodes_bi(self.nodes, i, i + 1)
  74. self.sync_all()
  75. def setup_nodes(self):
  76. extra_args = None
  77. if hasattr(self, "extra_args"):
  78. extra_args = self.extra_args
  79. self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, extra_args)
  80. def run_test(self):
  81. raise NotImplementedError
  82. # Main function. This should not be overridden by the subclass test scripts.
  83. def main(self):
  84. parser = optparse.OptionParser(usage="%prog [options]")
  85. parser.add_option("--nocleanup", dest="nocleanup", default=False, action="store_true",
  86. help="Leave bitcoinds and test.* datadir on exit or error")
  87. parser.add_option("--noshutdown", dest="noshutdown", default=False, action="store_true",
  88. help="Don't stop bitcoinds after the test execution")
  89. parser.add_option("--srcdir", dest="srcdir", default=os.path.normpath(os.path.dirname(os.path.realpath(__file__))+"/../../../src"),
  90. help="Source directory containing bitcoind/bitcoin-cli (default: %default)")
  91. parser.add_option("--cachedir", dest="cachedir", default=os.path.normpath(os.path.dirname(os.path.realpath(__file__))+"/../../cache"),
  92. help="Directory for caching pregenerated datadirs")
  93. parser.add_option("--tmpdir", dest="tmpdir", default=tempfile.mkdtemp(prefix="test"),
  94. help="Root directory for datadirs")
  95. parser.add_option("-l", "--loglevel", dest="loglevel", default="INFO",
  96. help="log events at this level and higher to the console. Can be set to DEBUG, INFO, WARNING, ERROR or CRITICAL. Passing --loglevel DEBUG will output all logs to console. Note that logs at all levels are always written to the test_framework.log file in the temporary test directory.")
  97. parser.add_option("--tracerpc", dest="trace_rpc", default=False, action="store_true",
  98. help="Print out all RPC calls as they are made")
  99. parser.add_option("--portseed", dest="port_seed", default=os.getpid(), type='int',
  100. help="The seed to use for assigning port numbers (default: current process id)")
  101. parser.add_option("--coveragedir", dest="coveragedir",
  102. help="Write tested RPC commands into this directory")
  103. self.add_options(parser)
  104. (self.options, self.args) = parser.parse_args()
  105. # backup dir variable for removal at cleanup
  106. self.options.root, self.options.tmpdir = self.options.tmpdir, self.options.tmpdir + '/' + str(self.options.port_seed)
  107. if self.options.coveragedir:
  108. enable_coverage(self.options.coveragedir)
  109. PortSeed.n = self.options.port_seed
  110. os.environ['PATH'] = self.options.srcdir+":"+self.options.srcdir+"/qt:"+os.environ['PATH']
  111. check_json_precision()
  112. # Set up temp directory and start logging
  113. os.makedirs(self.options.tmpdir, exist_ok=False)
  114. self._start_logging()
  115. success = False
  116. try:
  117. self.setup_chain()
  118. self.setup_network()
  119. self.run_test()
  120. success = True
  121. except JSONRPCException as e:
  122. self.log.exception("JSONRPC error")
  123. except AssertionError as e:
  124. self.log.exception("Assertion failed")
  125. except KeyError as e:
  126. self.log.exception("Key error")
  127. except Exception as e:
  128. self.log.exception("Unexpected exception caught during testing")
  129. except KeyboardInterrupt as e:
  130. self.log.warning("Exiting after keyboard interrupt")
  131. if not self.options.noshutdown:
  132. self.log.info("Stopping nodes")
  133. self.stop_nodes()
  134. else:
  135. self.log.info("Note: bitcoinds were not stopped and may still be running")
  136. if not self.options.nocleanup and not self.options.noshutdown and success:
  137. self.log.info("Cleaning up")
  138. shutil.rmtree(self.options.tmpdir)
  139. if not os.listdir(self.options.root):
  140. os.rmdir(self.options.root)
  141. else:
  142. self.log.warning("Not cleaning up dir %s" % self.options.tmpdir)
  143. if os.getenv("PYTHON_DEBUG", ""):
  144. # Dump the end of the debug logs, to aid in debugging rare
  145. # travis failures.
  146. import glob
  147. filenames = [self.options.tmpdir + "/test_framework.log"]
  148. filenames += glob.glob(self.options.tmpdir + "/node*/regtest/debug.log")
  149. MAX_LINES_TO_PRINT = 1000
  150. for fn in filenames:
  151. try:
  152. with open(fn, 'r') as f:
  153. print("From" , fn, ":")
  154. print("".join(deque(f, MAX_LINES_TO_PRINT)))
  155. except OSError:
  156. print("Opening file %s failed." % fn)
  157. traceback.print_exc()
  158. if success:
  159. self.log.info("Tests successful")
  160. sys.exit(self.TEST_EXIT_PASSED)
  161. else:
  162. self.log.error("Test failed. Test logging available at %s/test_framework.log", self.options.tmpdir)
  163. logging.shutdown()
  164. sys.exit(self.TEST_EXIT_FAILED)
  165. # Public helper methods. These can be accessed by the subclass test scripts.
  166. def start_node(self, i, dirname, extra_args=None, rpchost=None, timewait=None, binary=None, stderr=None):
  167. return start_node(i, dirname, extra_args, rpchost, timewait, binary, stderr)
  168. def start_nodes(self, num_nodes, dirname, extra_args=None, rpchost=None, timewait=None, binary=None):
  169. return start_nodes(num_nodes, dirname, extra_args, rpchost, timewait, binary)
  170. def stop_node(self, num_node):
  171. stop_node(self.nodes[num_node], num_node)
  172. def stop_nodes(self):
  173. stop_nodes(self.nodes)
  174. def split_network(self):
  175. """
  176. Split the network of four nodes into nodes 0/1 and 2/3.
  177. """
  178. disconnect_nodes(self.nodes[1], 2)
  179. disconnect_nodes(self.nodes[2], 1)
  180. self.sync_all([self.nodes[:2], self.nodes[2:]])
  181. def join_network(self):
  182. """
  183. Join the (previously split) network halves together.
  184. """
  185. connect_nodes_bi(self.nodes, 1, 2)
  186. self.sync_all()
  187. def sync_all(self, node_groups=None):
  188. if not node_groups:
  189. node_groups = [self.nodes]
  190. for group in node_groups:
  191. sync_blocks(group)
  192. sync_mempools(group)
  193. # Private helper methods. These should not be accessed by the subclass test scripts.
  194. def _start_logging(self):
  195. # Add logger and logging handlers
  196. self.log = logging.getLogger('TestFramework')
  197. self.log.setLevel(logging.DEBUG)
  198. # Create file handler to log all messages
  199. fh = logging.FileHandler(self.options.tmpdir + '/test_framework.log')
  200. fh.setLevel(logging.DEBUG)
  201. # Create console handler to log messages to stderr. By default this logs only error messages, but can be configured with --loglevel.
  202. ch = logging.StreamHandler(sys.stdout)
  203. # User can provide log level as a number or string (eg DEBUG). loglevel was caught as a string, so try to convert it to an int
  204. ll = int(self.options.loglevel) if self.options.loglevel.isdigit() else self.options.loglevel.upper()
  205. ch.setLevel(ll)
  206. # Format logs the same as bitcoind's debug.log with microprecision (so log files can be concatenated and sorted)
  207. formatter = logging.Formatter(fmt = '%(asctime)s.%(msecs)03d000 %(name)s (%(levelname)s): %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
  208. formatter.converter = time.gmtime
  209. fh.setFormatter(formatter)
  210. ch.setFormatter(formatter)
  211. # add the handlers to the logger
  212. self.log.addHandler(fh)
  213. self.log.addHandler(ch)
  214. if self.options.trace_rpc:
  215. rpc_logger = logging.getLogger("BitcoinRPC")
  216. rpc_logger.setLevel(logging.DEBUG)
  217. rpc_handler = logging.StreamHandler(sys.stdout)
  218. rpc_handler.setLevel(logging.DEBUG)
  219. rpc_logger.addHandler(rpc_handler)
  220. def _initialize_chain(self, test_dir, num_nodes, cachedir):
  221. """Initialize a pre-mined blockchain for use by the test.
  222. Create a cache of a 200-block-long chain (with wallet) for MAX_NODES
  223. Afterward, create num_nodes copies from the cache."""
  224. assert num_nodes <= MAX_NODES
  225. create_cache = False
  226. for i in range(MAX_NODES):
  227. if not os.path.isdir(os.path.join(cachedir, 'node' + str(i))):
  228. create_cache = True
  229. break
  230. if create_cache:
  231. self.log.debug("Creating data directories from cached datadir")
  232. # find and delete old cache directories if any exist
  233. for i in range(MAX_NODES):
  234. if os.path.isdir(os.path.join(cachedir, "node" + str(i))):
  235. shutil.rmtree(os.path.join(cachedir, "node" + str(i)))
  236. # Create cache directories, run bitcoinds:
  237. for i in range(MAX_NODES):
  238. datadir = initialize_datadir(cachedir, i)
  239. args = [os.getenv("BITCOIND", "bitcoind"), "-server", "-keypool=1", "-datadir=" + datadir, "-discover=0"]
  240. if i > 0:
  241. args.append("-connect=127.0.0.1:" + str(p2p_port(0)))
  242. bitcoind_processes[i] = subprocess.Popen(args)
  243. self.log.debug("initialize_chain: bitcoind started, waiting for RPC to come up")
  244. wait_for_bitcoind_start(bitcoind_processes[i], rpc_url(i), i)
  245. self.log.debug("initialize_chain: RPC successfully started")
  246. self.nodes = []
  247. for i in range(MAX_NODES):
  248. try:
  249. self.nodes.append(get_rpc_proxy(rpc_url(i), i))
  250. except:
  251. self.log.exception("Error connecting to node %d" % i)
  252. sys.exit(1)
  253. # Create a 200-block-long chain; each of the 4 first nodes
  254. # gets 25 mature blocks and 25 immature.
  255. # Note: To preserve compatibility with older versions of
  256. # initialize_chain, only 4 nodes will generate coins.
  257. #
  258. # blocks are created with timestamps 10 minutes apart
  259. # starting from 2010 minutes in the past
  260. enable_mocktime()
  261. block_time = get_mocktime() - (201 * 10 * 60)
  262. for i in range(2):
  263. for peer in range(4):
  264. for j in range(25):
  265. set_node_times(self.nodes, block_time)
  266. self.nodes[peer].generate(1)
  267. block_time += 10 * 60
  268. # Must sync before next peer starts generating blocks
  269. sync_blocks(self.nodes)
  270. # Shut them down, and clean up cache directories:
  271. self.stop_nodes()
  272. self.nodes = []
  273. disable_mocktime()
  274. for i in range(MAX_NODES):
  275. os.remove(log_filename(cachedir, i, "debug.log"))
  276. os.remove(log_filename(cachedir, i, "db.log"))
  277. os.remove(log_filename(cachedir, i, "peers.dat"))
  278. os.remove(log_filename(cachedir, i, "fee_estimates.dat"))
  279. for i in range(num_nodes):
  280. from_dir = os.path.join(cachedir, "node" + str(i))
  281. to_dir = os.path.join(test_dir, "node" + str(i))
  282. shutil.copytree(from_dir, to_dir)
  283. initialize_datadir(test_dir, i) # Overwrite port/rpcport in bitcoin.conf
  284. def _initialize_chain_clean(self, test_dir, num_nodes):
  285. """Initialize empty blockchain for use by the test.
  286. Create an empty blockchain and num_nodes wallets.
  287. Useful if a test case wants complete control over initialization."""
  288. for i in range(num_nodes):
  289. initialize_datadir(test_dir, i)
  290. # Test framework for doing p2p comparison testing, which sets up some bitcoind
  291. # binaries:
  292. # 1 binary: test binary
  293. # 2 binaries: 1 test binary, 1 ref binary
  294. # n>2 binaries: 1 test binary, n-1 ref binaries
  295. class ComparisonTestFramework(BitcoinTestFramework):
  296. def __init__(self):
  297. super().__init__()
  298. self.num_nodes = 2
  299. self.setup_clean_chain = True
  300. def add_options(self, parser):
  301. parser.add_option("--testbinary", dest="testbinary",
  302. default=os.getenv("BITCOIND", "bitcoind"),
  303. help="bitcoind binary to test")
  304. parser.add_option("--refbinary", dest="refbinary",
  305. default=os.getenv("BITCOIND", "bitcoind"),
  306. help="bitcoind binary to use for reference nodes (if any)")
  307. def setup_network(self):
  308. self.nodes = self.start_nodes(
  309. self.num_nodes, self.options.tmpdir,
  310. extra_args=[['-whitelist=127.0.0.1']] * self.num_nodes,
  311. binary=[self.options.testbinary] +
  312. [self.options.refbinary]*(self.num_nodes-1))