[tests] Add -blocknotify functional test
[bitcoinplatinum.git] / test / functional / example_test.py
blob87d73ad14ac8ba50ea2917a61a7acbdd962c1894
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 """An example functional test
7 The module-level docstring should include a high-level description of
8 what the test is doing. It's the first thing people see when they open
9 the file and should give the reader information about *what* the test
10 is testing and *how* it's being tested
11 """
12 # Imports should be in PEP8 ordering (std library first, then third party
13 # libraries then local imports).
14 from collections import defaultdict
16 # Avoid wildcard * imports if possible
17 from test_framework.blocktools import (create_block, create_coinbase)
18 from test_framework.mininode import (
19 CInv,
20 NetworkThread,
21 NodeConn,
22 NodeConnCB,
23 mininode_lock,
24 msg_block,
25 msg_getdata,
27 from test_framework.test_framework import BitcoinTestFramework
28 from test_framework.util import (
29 assert_equal,
30 connect_nodes,
31 p2p_port,
32 wait_until,
35 # NodeConnCB is a class containing callbacks to be executed when a P2P
36 # message is received from the node-under-test. Subclass NodeConnCB and
37 # override the on_*() methods if you need custom behaviour.
38 class BaseNode(NodeConnCB):
39 def __init__(self):
40 """Initialize the NodeConnCB
42 Used to inialize custom properties for the Node that aren't
43 included by default in the base class. Be aware that the NodeConnCB
44 base class already stores a counter for each P2P message type and the
45 last received message of each type, which should be sufficient for the
46 needs of most tests.
48 Call super().__init__() first for standard initialization and then
49 initialize custom properties."""
50 super().__init__()
51 # Stores a dictionary of all blocks received
52 self.block_receive_map = defaultdict(int)
54 def on_block(self, conn, message):
55 """Override the standard on_block callback
57 Store the hash of a received block in the dictionary."""
58 message.block.calc_sha256()
59 self.block_receive_map[message.block.sha256] += 1
61 def on_inv(self, conn, message):
62 """Override the standard on_inv callback"""
63 pass
65 def custom_function():
66 """Do some custom behaviour
68 If this function is more generally useful for other tests, consider
69 moving it to a module in test_framework."""
70 # self.log.info("running custom_function") # Oops! Can't run self.log outside the BitcoinTestFramework
71 pass
73 class ExampleTest(BitcoinTestFramework):
74 # Each functional test is a subclass of the BitcoinTestFramework class.
76 # Override the set_test_params(), add_options(), setup_chain(), setup_network()
77 # and setup_nodes() methods to customize the test setup as required.
79 def set_test_params(self):
80 """Override test parameters for your individual test.
82 This method must be overridden and num_nodes must be exlicitly set."""
83 self.setup_clean_chain = True
84 self.num_nodes = 3
85 # Use self.extra_args to change command-line arguments for the nodes
86 self.extra_args = [[], ["-logips"], []]
88 # self.log.info("I've finished set_test_params") # Oops! Can't run self.log before run_test()
90 # Use add_options() to add specific command-line options for your test.
91 # In practice this is not used very much, since the tests are mostly written
92 # to be run in automated environments without command-line options.
93 # def add_options()
94 # pass
96 # Use setup_chain() to customize the node data directories. In practice
97 # this is not used very much since the default behaviour is almost always
98 # fine
99 # def setup_chain():
100 # pass
102 def setup_network(self):
103 """Setup the test network topology
105 Often you won't need to override this, since the standard network topology
106 (linear: node0 <-> node1 <-> node2 <-> ...) is fine for most tests.
108 If you do override this method, remember to start the nodes, assign
109 them to self.nodes, connect them and then sync."""
111 self.setup_nodes()
113 # In this test, we're not connecting node2 to node0 or node1. Calls to
114 # sync_all() should not include node2, since we're not expecting it to
115 # sync.
116 connect_nodes(self.nodes[0], 1)
117 self.sync_all([self.nodes[0:1]])
119 # Use setup_nodes() to customize the node start behaviour (for example if
120 # you don't want to start all nodes at the start of the test).
121 # def setup_nodes():
122 # pass
124 def custom_method(self):
125 """Do some custom behaviour for this test
127 Define it in a method here because you're going to use it repeatedly.
128 If you think it's useful in general, consider moving it to the base
129 BitcoinTestFramework class so other tests can use it."""
131 self.log.info("Running custom_method")
133 def run_test(self):
134 """Main test logic"""
136 # Create a P2P connection to one of the nodes
137 node0 = BaseNode()
138 connections = []
139 connections.append(NodeConn('127.0.0.1', p2p_port(0), self.nodes[0], node0))
140 node0.add_connection(connections[0])
142 # Start up network handling in another thread. This needs to be called
143 # after the P2P connections have been created.
144 NetworkThread().start()
145 # wait_for_verack ensures that the P2P connection is fully up.
146 node0.wait_for_verack()
148 # Generating a block on one of the nodes will get us out of IBD
149 blocks = [int(self.nodes[0].generate(nblocks=1)[0], 16)]
150 self.sync_all([self.nodes[0:1]])
152 # Notice above how we called an RPC by calling a method with the same
153 # name on the node object. Notice also how we used a keyword argument
154 # to specify a named RPC argument. Neither of those are defined on the
155 # node object. Instead there's some __getattr__() magic going on under
156 # the covers to dispatch unrecognised attribute calls to the RPC
157 # interface.
159 # Logs are nice. Do plenty of them. They can be used in place of comments for
160 # breaking the test into sub-sections.
161 self.log.info("Starting test!")
163 self.log.info("Calling a custom function")
164 custom_function()
166 self.log.info("Calling a custom method")
167 self.custom_method()
169 self.log.info("Create some blocks")
170 self.tip = int(self.nodes[0].getbestblockhash(), 16)
171 self.block_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time'] + 1
173 height = 1
175 for i in range(10):
176 # Use the mininode and blocktools functionality to manually build a block
177 # Calling the generate() rpc is easier, but this allows us to exactly
178 # control the blocks and transactions.
179 block = create_block(self.tip, create_coinbase(height), self.block_time)
180 block.solve()
181 block_message = msg_block(block)
182 # Send message is used to send a P2P message to the node over our NodeConn connection
183 node0.send_message(block_message)
184 self.tip = block.sha256
185 blocks.append(self.tip)
186 self.block_time += 1
187 height += 1
189 self.log.info("Wait for node1 to reach current tip (height 11) using RPC")
190 self.nodes[1].waitforblockheight(11)
192 self.log.info("Connect node2 and node1")
193 connect_nodes(self.nodes[1], 2)
195 self.log.info("Add P2P connection to node2")
196 node2 = BaseNode()
197 connections.append(NodeConn('127.0.0.1', p2p_port(2), self.nodes[2], node2))
198 node2.add_connection(connections[1])
199 node2.wait_for_verack()
201 self.log.info("Wait for node2 reach current tip. Test that it has propagated all the blocks to us")
203 getdata_request = msg_getdata()
204 for block in blocks:
205 getdata_request.inv.append(CInv(2, block))
206 node2.send_message(getdata_request)
208 # wait_until() will loop until a predicate condition is met. Use it to test properties of the
209 # NodeConnCB objects.
210 wait_until(lambda: sorted(blocks) == sorted(list(node2.block_receive_map.keys())), timeout=5, lock=mininode_lock)
212 self.log.info("Check that each block was received only once")
213 # The network thread uses a global lock on data access to the NodeConn objects when sending and receiving
214 # messages. The test thread should acquire the global lock before accessing any NodeConn data to avoid locking
215 # and synchronization issues. Note wait_until() acquires this global lock when testing the predicate.
216 with mininode_lock:
217 for block in node2.block_receive_map.values():
218 assert_equal(block, 1)
220 if __name__ == '__main__':
221 ExampleTest().main()