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