p2p_ibd_stalling.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2022-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  """
   6  Test stalling logic during IBD
   7  """
   8  
   9  import time
  10  
  11  from test_framework.blocktools import (
  12          create_block,
  13  )
  14  from test_framework.messages import (
  15          MSG_BLOCK,
  16          MSG_TYPE_MASK,
  17  )
  18  from test_framework.p2p import (
  19          CBlockHeader,
  20          msg_block,
  21          msg_headers,
  22          P2PDataStore,
  23  )
  24  from test_framework.test_framework import BitcoinTestFramework
  25  from test_framework.util import (
  26          assert_equal,
  27  )
  28  
  29  
  30  class P2PStaller(P2PDataStore):
  31      def __init__(self, stall_blocks):
  32          self.stall_blocks = stall_blocks
  33          super().__init__()
  34  
  35      def on_getdata(self, message):
  36          for inv in message.inv:
  37              self.getdata_requests.append(inv.hash)
  38              if (inv.type & MSG_TYPE_MASK) == MSG_BLOCK:
  39                  if (inv.hash not in self.stall_blocks):
  40                      self.send_without_ping(msg_block(self.block_store[inv.hash]))
  41  
  42      def on_getheaders(self, message):
  43          pass
  44  
  45  
  46  class P2PIBDStallingTest(BitcoinTestFramework):
  47      def set_test_params(self):
  48          self.setup_clean_chain = True
  49          self.num_nodes = 1
  50  
  51      def run_test(self):
  52          NUM_BLOCKS = 1025
  53          NUM_PEERS = 5
  54          node = self.nodes[0]
  55          tip = int(node.getbestblockhash(), 16)
  56          blocks = []
  57          height = 1
  58          block_time = node.getblock(node.getbestblockhash())['time'] + 1
  59          self.log.info("Prepare blocks without sending them to the node")
  60          block_dict = {}
  61          for _ in range(NUM_BLOCKS):
  62              blocks.append(create_block(tip, height=height, ntime=block_time))
  63              blocks[-1].solve()
  64              tip = blocks[-1].hash_int
  65              block_time += 1
  66              height += 1
  67              block_dict[blocks[-1].hash_int] = blocks[-1]
  68          stall_index = 0
  69          second_stall_index = 500
  70          stall_blocks = [blocks[stall_index].hash_int, blocks[second_stall_index].hash_int]
  71  
  72          headers_message = msg_headers()
  73          headers_message.headers = [CBlockHeader(b) for b in blocks[:NUM_BLOCKS-1]]
  74          peers = []
  75  
  76          self.log.info("Check that a staller does not get disconnected if the 1024 block lookahead buffer is filled")
  77          self.mocktime = int(time.time()) + 1
  78          node.setmocktime(self.mocktime)
  79          for id in range(NUM_PEERS):
  80              peers.append(node.add_outbound_p2p_connection(P2PStaller(stall_blocks), p2p_idx=id, connection_type="outbound-full-relay"))
  81              peers[-1].block_store = block_dict
  82              peers[-1].send_and_ping(headers_message)
  83  
  84          # Wait until all blocks are received (except for the stall blocks), so that no other blocks are in flight.
  85          self.wait_until(lambda: sum(len(peer['inflight']) for peer in node.getpeerinfo()) == len(stall_blocks))
  86  
  87          self.all_sync_send_with_ping(peers)
  88          # If there was a peer marked for stalling, it would get disconnected
  89          self.mocktime += 3
  90          node.setmocktime(self.mocktime)
  91          self.all_sync_send_with_ping(peers)
  92          assert_equal(node.num_test_p2p_connections(), NUM_PEERS)
  93  
  94          self.log.info("Check that increasing the window beyond 1024 blocks triggers stalling logic")
  95          headers_message.headers = [CBlockHeader(b) for b in blocks]
  96          with node.assert_debug_log(expected_msgs=['Stall started']):
  97              for p in peers:
  98                  p.send_without_ping(headers_message)
  99              self.all_sync_send_with_ping(peers)
 100  
 101          self.log.info("Check that the stalling peer is disconnected after 2 seconds")
 102          self.mocktime += 3
 103          node.setmocktime(self.mocktime)
 104          peers[0].wait_for_disconnect()
 105          assert_equal(node.num_test_p2p_connections(), NUM_PEERS - 1)
 106          self.wait_until(lambda: self.is_block_requested(peers, stall_blocks[0]))
 107          # Make sure that SendMessages() is invoked, which assigns the missing block
 108          # to another peer and starts the stalling logic for them
 109          self.all_sync_send_with_ping(peers)
 110  
 111          self.log.info("Check that the stalling timeout gets doubled to 4 seconds for the next staller")
 112          # No disconnect after just 3 seconds
 113          self.mocktime += 3
 114          node.setmocktime(self.mocktime)
 115          self.all_sync_send_with_ping(peers)
 116          assert_equal(node.num_test_p2p_connections(), NUM_PEERS - 1)
 117  
 118          self.mocktime += 2
 119          node.setmocktime(self.mocktime)
 120          self.wait_until(lambda: sum(x.is_connected for x in node.p2ps) == NUM_PEERS - 2)
 121          self.wait_until(lambda: self.is_block_requested(peers, stall_blocks[0]))
 122          self.all_sync_send_with_ping(peers)
 123  
 124          self.log.info("Check that the stalling timeout gets doubled to 8 seconds for the next staller")
 125          # No disconnect after just 7 seconds
 126          self.mocktime += 7
 127          node.setmocktime(self.mocktime)
 128          self.all_sync_send_with_ping(peers)
 129          assert_equal(node.num_test_p2p_connections(), NUM_PEERS - 2)
 130  
 131          self.mocktime += 2
 132          node.setmocktime(self.mocktime)
 133          self.wait_until(lambda: sum(x.is_connected for x in node.p2ps) == NUM_PEERS - 3)
 134          self.wait_until(lambda: self.is_block_requested(peers, stall_blocks[0]))
 135          self.all_sync_send_with_ping(peers)
 136  
 137          self.log.info("Provide the first withheld block and check that stalling timeout gets reduced back to 2 seconds")
 138          with node.assert_debug_log(expected_msgs=['Decreased stalling timeout to 2 seconds'], unexpected_msgs=['Stall started']):
 139              for p in peers:
 140                  if p.is_connected and (stall_blocks[0] in p.getdata_requests):
 141                      p.send_without_ping(msg_block(block_dict[stall_blocks[0]]))
 142              self.all_sync_send_with_ping(peers)
 143  
 144          self.log.info("Check that all outstanding blocks up to the second stall block get connected")
 145          self.wait_until(lambda: node.getblockcount() == second_stall_index)
 146  
 147  
 148      def all_sync_send_with_ping(self, peers):
 149          for p in peers:
 150              if p.is_connected:
 151                  p.sync_with_ping()
 152  
 153      def is_block_requested(self, peers, hash):
 154          for p in peers:
 155              if p.is_connected and (hash in p.getdata_requests):
 156                  return True
 157          return False
 158  
 159  
 160  if __name__ == '__main__':
 161      P2PIBDStallingTest(__file__).main()
 162