/ test / functional / p2p_invalid_locator.py
p2p_invalid_locator.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2015-2021 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  """Test node responses to invalid locators.
 6  """
 7  
 8  from test_framework.messages import msg_getheaders, msg_getblocks, MAX_LOCATOR_SZ
 9  from test_framework.p2p import P2PInterface
10  from test_framework.test_framework import BitcoinTestFramework
11  
12  
13  class InvalidLocatorTest(BitcoinTestFramework):
14      def set_test_params(self):
15          self.num_nodes = 1
16  
17      def run_test(self):
18          node = self.nodes[0]  # convenience reference to the node
19          self.generatetoaddress(node, 1, node.get_deterministic_priv_key().address)  # Get node out of IBD
20  
21          self.log.info('Test max locator size')
22          block_count = node.getblockcount()
23          for msg in [msg_getheaders(), msg_getblocks()]:
24              self.log.info('Wait for disconnect when sending {} hashes in locator'.format(MAX_LOCATOR_SZ + 1))
25              exceed_max_peer = node.add_p2p_connection(P2PInterface())
26              msg.locator.vHave = [int(node.getblockhash(i - 1), 16) for i in range(block_count, block_count - (MAX_LOCATOR_SZ + 1), -1)]
27              exceed_max_peer.send_message(msg)
28              exceed_max_peer.wait_for_disconnect()
29              node.disconnect_p2ps()
30  
31              self.log.info('Wait for response when sending {} hashes in locator'.format(MAX_LOCATOR_SZ))
32              within_max_peer = node.add_p2p_connection(P2PInterface())
33              msg.locator.vHave = [int(node.getblockhash(i - 1), 16) for i in range(block_count, block_count - (MAX_LOCATOR_SZ), -1)]
34              within_max_peer.send_message(msg)
35              if type(msg) is msg_getheaders:
36                  within_max_peer.wait_for_header(node.getbestblockhash())
37              else:
38                  within_max_peer.wait_for_block(int(node.getbestblockhash(), 16))
39  
40  
41  if __name__ == '__main__':
42      InvalidLocatorTest().main()