2 # Copyright (c) 2014 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or https://www.opensource.org/licenses/mit-license.php .
6 # Base class for RPC testing
8 # Add python-bitcoinrpc to module search path:
16 from authproxy import JSONRPCException
17 from util import assert_equal, check_json_precision, \
18 initialize_chain, initialize_chain_clean, \
19 start_nodes, connect_nodes_bi, stop_nodes, \
20 sync_blocks, sync_mempools, wait_bitcoinds
23 class BitcoinTestFramework(object):
25 # These may be over-ridden by subclasses:
27 for node in self.nodes:
28 assert_equal(node.getblockcount(), 200)
29 assert_equal(node.getbalance(), 25*10)
31 def add_options(self, parser):
34 def setup_chain(self):
35 print("Initializing test directory "+self.options.tmpdir)
36 initialize_chain(self.options.tmpdir)
38 def setup_nodes(self):
39 return start_nodes(4, self.options.tmpdir)
41 def setup_network(self, split = False):
42 self.nodes = self.setup_nodes()
44 # Connect the nodes as a "chain". This allows us
45 # to split the network between nodes 1 and 2 to get
46 # two halves that can work on competing chains.
48 # If we joined network halves, connect the nodes from the joint
49 # on outward. This ensures that chains are properly reorganised.
51 connect_nodes_bi(self.nodes, 1, 2)
52 sync_blocks(self.nodes[1:3])
53 sync_mempools(self.nodes[1:3])
55 connect_nodes_bi(self.nodes, 0, 1)
56 connect_nodes_bi(self.nodes, 2, 3)
57 self.is_network_split = split
60 def split_network(self):
62 Split the network of four nodes into nodes 0/1 and 2/3.
64 assert not self.is_network_split
65 stop_nodes(self.nodes)
67 self.setup_network(True)
70 if self.is_network_split:
71 sync_blocks(self.nodes[:2])
72 sync_blocks(self.nodes[2:])
73 sync_mempools(self.nodes[:2])
74 sync_mempools(self.nodes[2:])
76 sync_blocks(self.nodes)
77 sync_mempools(self.nodes)
79 def join_network(self):
81 Join the (previously split) network halves together.
83 assert self.is_network_split
84 stop_nodes(self.nodes)
86 self.setup_network(False)
91 parser = optparse.OptionParser(usage="%prog [options]")
92 parser.add_option("--nocleanup", dest="nocleanup", default=False, action="store_true",
93 help="Leave komodods and test.* datadir on exit or error")
94 parser.add_option("--noshutdown", dest="noshutdown", default=False, action="store_true",
95 help="Don't stop komodods after the test execution")
96 parser.add_option("--srcdir", dest="srcdir", default="../../src",
97 help="Source directory containing komodod/komodo-cli (default: %default)")
98 parser.add_option("--tmpdir", dest="tmpdir", default=tempfile.mkdtemp(prefix="test"),
99 help="Root directory for datadirs")
100 parser.add_option("--tracerpc", dest="trace_rpc", default=False, action="store_true",
101 help="Print out all RPC calls as they are made")
102 self.add_options(parser)
103 (self.options, self.args) = parser.parse_args()
105 if self.options.trace_rpc:
107 logging.basicConfig(level=logging.DEBUG)
109 os.environ['PATH'] = self.options.srcdir+":"+os.environ['PATH']
111 check_json_precision()
115 if not os.path.isdir(self.options.tmpdir):
116 os.makedirs(self.options.tmpdir)
125 except JSONRPCException as e:
126 print("JSONRPC error: "+e.error['message'])
127 traceback.print_tb(sys.exc_info()[2])
128 except AssertionError as e:
129 print("Assertion failed: "+e.message)
130 traceback.print_tb(sys.exc_info()[2])
131 except Exception as e:
132 print("Unexpected exception caught during testing: "+str(e))
133 traceback.print_tb(sys.exc_info()[2])
135 if not self.options.noshutdown:
136 print("Stopping nodes")
137 stop_nodes(self.nodes)
140 print("Note: komodods were not stopped and may still be running")
142 if not self.options.nocleanup and not self.options.noshutdown:
144 shutil.rmtree(self.options.tmpdir)
147 print("Tests successful")
154 # Test framework for doing p2p comparison testing, which sets up some komodod
156 # 1 binary: test binary
157 # 2 binaries: 1 test binary, 1 ref binary
158 # n>2 binaries: 1 test binary, n-1 ref binaries
160 class ComparisonTestFramework(BitcoinTestFramework):
162 # Can override the num_nodes variable to indicate how many nodes to run.
166 def add_options(self, parser):
167 parser.add_option("--testbinary", dest="testbinary",
168 default=os.getenv("BITCOIND", "komodod"),
169 help="bitcoind binary to test")
170 parser.add_option("--refbinary", dest="refbinary",
171 default=os.getenv("BITCOIND", "komodod"),
172 help="bitcoind binary to use for reference nodes (if any)")
174 def setup_chain(self):
175 print "Initializing test directory "+self.options.tmpdir
176 initialize_chain_clean(self.options.tmpdir, self.num_nodes)
178 def setup_network(self):
179 self.nodes = start_nodes(self.num_nodes, self.options.tmpdir,
180 extra_args=[['-debug', '-whitelist=127.0.0.1']] * self.num_nodes,
181 binary=[self.options.testbinary] +
182 [self.options.refbinary]*(self.num_nodes-1))