/ test / functional / example_test.py
example_test.py
  1  #!/usr/bin/env python3
  2  # Copyright (c) 2017-present 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
  6  
  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
 15  
 16  # Avoid wildcard * imports
 17  # Use lexicographically sorted multi-line imports
 18  from test_framework.blocktools import (
 19      create_block,
 20  )
 21  from test_framework.messages import (
 22      CInv,
 23      MSG_BLOCK,
 24  )
 25  from test_framework.p2p import (
 26      P2PInterface,
 27      msg_block,
 28      msg_getdata,
 29      p2p_lock,
 30  )
 31  from test_framework.test_framework import BitcoinTestFramework
 32  from test_framework.util import (
 33      assert_equal,
 34  )
 35  
 36  # P2PInterface is a class containing callbacks to be executed when a P2P
 37  # message is received from the node-under-test. Subclass P2PInterface and
 38  # override the on_*() methods if you need custom behaviour.
 39  class BaseNode(P2PInterface):
 40      def __init__(self):
 41          """Initialize the P2PInterface
 42  
 43          Used to initialize custom properties for the Node that aren't
 44          included by default in the base class. Be aware that the P2PInterface
 45          base class already stores a counter for each P2P message type and the
 46          last received message of each type, which should be sufficient for the
 47          needs of most tests.
 48  
 49          Call super().__init__() first for standard initialization and then
 50          initialize custom properties."""
 51          super().__init__()
 52          # Stores a dictionary of all blocks received
 53          self.block_receive_map = defaultdict(int)
 54  
 55      def on_block(self, message):
 56          """Override the standard on_block callback
 57  
 58          Store the hash of a received block in the dictionary."""
 59          self.block_receive_map[message.block.hash_int] += 1
 60  
 61      def on_inv(self, message):
 62          """Override the standard on_inv callback"""
 63          pass
 64  
 65  def custom_function():
 66      """Do some custom behaviour
 67  
 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
 72  
 73  
 74  class ExampleTest(BitcoinTestFramework):
 75      # Each functional test is a subclass of the BitcoinTestFramework class.
 76  
 77      # Override the set_test_params(), skip_test_if_missing_module(), add_options(), setup_chain(), setup_network()
 78      # and setup_nodes() methods to customize the test setup as required.
 79  
 80      def set_test_params(self):
 81          """Override test parameters for your individual test.
 82  
 83          This method must be overridden and num_nodes must be explicitly set."""
 84          # By default every test loads a pre-mined chain of 200 blocks from cache.
 85          # Set setup_clean_chain to True to skip this and start from the Genesis
 86          # block.
 87          self.setup_clean_chain = True
 88          self.num_nodes = 3
 89          # Use self.extra_args to change command-line arguments for the nodes
 90          self.extra_args = [[], ["-logips"], []]
 91  
 92          # self.log.info("I've finished set_test_params")  # Oops! Can't run self.log before run_test()
 93  
 94      # Use skip_test_if_missing_module() to skip the test if your test requires certain modules to be present.
 95      # This test uses generate which requires wallet to be compiled
 96      def skip_test_if_missing_module(self):
 97          self.skip_if_no_wallet()
 98  
 99      # Use add_options() to add specific command-line options for your test.
100      # In practice this is not used very much, since the tests are mostly written
101      # to be run in automated environments without command-line options.
102      # def add_options()
103      #     pass
104  
105      # Use setup_chain() to customize the node data directories. In practice
106      # this is not used very much since the default behaviour is almost always
107      # fine
108      # def setup_chain():
109      #     pass
110  
111      def setup_network(self):
112          """Setup the test network topology
113  
114          Often you won't need to override this, since the standard network topology
115          (linear: node0 <-> node1 <-> node2 <-> ...) is fine for most tests.
116  
117          If you do override this method, remember to start the nodes, assign
118          them to self.nodes, connect them and then sync."""
119  
120          self.setup_nodes()
121  
122          # In this test, we're not connecting node2 to node0 or node1. Calls to
123          # sync_all() should not include node2, since we're not expecting it to
124          # sync.
125          self.connect_nodes(0, 1)
126          self.sync_all(self.nodes[0:2])
127  
128      # Use setup_nodes() to customize the node start behaviour (for example if
129      # you don't want to start all nodes at the start of the test).
130      # def setup_nodes():
131      #     pass
132  
133      def custom_method(self):
134          """Do some custom behaviour for this test
135  
136          Define it in a method here because you're going to use it repeatedly.
137          If you think it's useful in general, consider moving it to the base
138          BitcoinTestFramework class so other tests can use it."""
139  
140          self.log.info("Running custom_method")
141  
142      def run_test(self):
143          """Main test logic"""
144  
145          # Create P2P connections will wait for a verack to make sure the connection is fully up
146          peer_messaging = self.nodes[0].add_p2p_connection(BaseNode())
147  
148          # Generating a block on one of the nodes will get us out of IBD
149          blocks = [int(self.generate(self.nodes[0], sync_fun=lambda: self.sync_all(self.nodes[0:2]), nblocks=1)[0], 16)]
150  
151          # Notice above how we called an RPC by calling a method with the same
152          # name on the node object. Notice also how we used a keyword argument
153          # to specify a named RPC argument. Neither of those are defined on the
154          # node object. Instead there's some __getattr__() magic going on under
155          # the covers to dispatch unrecognised attribute calls to the RPC
156          # interface.
157  
158          # Logs are nice. Do plenty of them. They can be used in place of comments for
159          # breaking the test into sub-sections.
160          self.log.info("Starting test!")
161  
162          self.log.info("Calling a custom function")
163          custom_function()
164  
165          self.log.info("Calling a custom method")
166          self.custom_method()
167  
168          self.log.info("Create some blocks")
169          self.tip = int(self.nodes[0].getbestblockhash(), 16)
170          self.block_time = self.nodes[0].getblock(self.nodes[0].getbestblockhash())['time'] + 1
171  
172          height = self.nodes[0].getblockcount()
173  
174          for _ in range(10):
175              # Use the blocktools functionality to manually build a block.
176              # Calling the generate() rpc is easier, but this allows us to exactly
177              # control the blocks and transactions.
178              block = create_block(self.tip, height=height+1, ntime=self.block_time)
179              block.solve()
180              block_message = msg_block(block)
181              # Send message is used to send a P2P message to the node over our P2PInterface
182              peer_messaging.send_without_ping(block_message)
183              self.tip = block.hash_int
184              blocks.append(self.tip)
185              self.block_time += 1
186              height += 1
187  
188          self.log.info("Wait for node1 to reach current tip (height 11) using RPC")
189          self.nodes[1].waitforblockheight(11)
190  
191          self.log.info("Connect node2 and node1")
192          self.connect_nodes(1, 2)
193  
194          self.log.info("Wait for node2 to receive all the blocks from node1")
195          self.sync_all()
196  
197          self.log.info("Add P2P connection to node2")
198          self.nodes[0].disconnect_p2ps()
199  
200          peer_receiving = self.nodes[2].add_p2p_connection(BaseNode())
201  
202          self.log.info("Test that node2 propagates all the blocks to us")
203  
204          getdata_request = msg_getdata()
205          for block in blocks:
206              getdata_request.inv.append(CInv(MSG_BLOCK, block))
207          peer_receiving.send_without_ping(getdata_request)
208  
209          # wait_until() will loop until a predicate condition is met. Use it to test properties of the
210          # P2PInterface objects.
211          peer_receiving.wait_until(lambda: sorted(blocks) == sorted(list(peer_receiving.block_receive_map.keys())), timeout=5)
212  
213          self.log.info("Check that each block was received only once")
214          # The network thread uses a global lock on data access to the P2PConnection objects when sending and receiving
215          # messages. The test thread should acquire the global lock before accessing any P2PConnection data to avoid locking
216          # and synchronization issues. Note p2p.wait_until() acquires this global lock internally when testing the predicate.
217          with p2p_lock:
218              for block in peer_receiving.block_receive_map.values():
219                  assert_equal(block, 1)
220  
221  
222  if __name__ == '__main__':
223      ExampleTest(__file__).main()