|
| 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_block): |
| 33 | + self.stall_block = stall_block |
| 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 != self.stall_block): |
| 41 | + self.send_message(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 = 4 |
| 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].sha256 |
| 66 | + block_time += 1 |
| 67 | + height += 1 |
| 68 | + block_dict[blocks[-1].sha256] = blocks[-1] |
| 69 | + stall_block = blocks[0].sha256 |
| 70 | + |
| 71 | + headers_message = msg_headers() |
| 72 | + headers_message.headers = [CBlockHeader(b) for b in blocks[:NUM_BLOCKS-1]] |
| 73 | + peers = [] |
| 74 | + |
| 75 | + self.log.info("Check that a staller does not get disconnected if the 1024 block lookahead buffer is filled") |
| 76 | + for id in range(NUM_PEERS): |
| 77 | + peers.append(node.add_outbound_p2p_connection(P2PStaller(stall_block), p2p_idx=id, connection_type="outbound-full-relay")) |
| 78 | + peers[-1].block_store = block_dict |
| 79 | + peers[-1].send_message(headers_message) |
| 80 | + |
| 81 | + # Need to wait until 1023 blocks are received - the magic total bytes number is a workaround in lack of an rpc |
| 82 | + # returning the number of downloaded (but not connected) blocks. |
| 83 | + self.wait_until(lambda: self.total_bytes_recv_for_blocks() == 172761) |
| 84 | + |
| 85 | + self.all_sync_send_with_ping(peers) |
| 86 | + # If there was a peer marked for stalling, it would get disconnected |
| 87 | + self.mocktime = int(time.time()) + 3 |
| 88 | + node.setmocktime(self.mocktime) |
| 89 | + self.all_sync_send_with_ping(peers) |
| 90 | + assert_equal(node.num_test_p2p_connections(), NUM_PEERS) |
| 91 | + |
| 92 | + self.log.info("Check that increasing the window beyond 1024 blocks triggers stalling logic") |
| 93 | + headers_message.headers = [CBlockHeader(b) for b in blocks] |
| 94 | + with node.assert_debug_log(expected_msgs=['Stall started']): |
| 95 | + for p in peers: |
| 96 | + p.send_message(headers_message) |
| 97 | + self.all_sync_send_with_ping(peers) |
| 98 | + |
| 99 | + self.log.info("Check that the stalling peer is disconnected after 2 seconds") |
| 100 | + self.mocktime += 3 |
| 101 | + node.setmocktime(self.mocktime) |
| 102 | + peers[0].wait_for_disconnect() |
| 103 | + assert_equal(node.num_test_p2p_connections(), NUM_PEERS - 1) |
| 104 | + self.wait_until(lambda: self.is_block_requested(peers, stall_block)) |
| 105 | + # Make sure that SendMessages() is invoked, which assigns the missing block |
| 106 | + # to another peer and starts the stalling logic for them |
| 107 | + self.all_sync_send_with_ping(peers) |
| 108 | + |
| 109 | + self.log.info("Check that the stalling timeout gets doubled to 4 seconds for the next staller") |
| 110 | + # No disconnect after just 3 seconds |
| 111 | + self.mocktime += 3 |
| 112 | + node.setmocktime(self.mocktime) |
| 113 | + self.all_sync_send_with_ping(peers) |
| 114 | + assert_equal(node.num_test_p2p_connections(), NUM_PEERS - 1) |
| 115 | + |
| 116 | + self.mocktime += 2 |
| 117 | + node.setmocktime(self.mocktime) |
| 118 | + self.wait_until(lambda: node.num_test_p2p_connections() == NUM_PEERS - 2) |
| 119 | + self.wait_until(lambda: self.is_block_requested(peers, stall_block)) |
| 120 | + self.all_sync_send_with_ping(peers) |
| 121 | + |
| 122 | + self.log.info("Check that the stalling timeout gets doubled to 8 seconds for the next staller") |
| 123 | + # No disconnect after just 7 seconds |
| 124 | + self.mocktime += 7 |
| 125 | + node.setmocktime(self.mocktime) |
| 126 | + self.all_sync_send_with_ping(peers) |
| 127 | + assert_equal(node.num_test_p2p_connections(), NUM_PEERS - 2) |
| 128 | + |
| 129 | + self.mocktime += 2 |
| 130 | + node.setmocktime(self.mocktime) |
| 131 | + self.wait_until(lambda: node.num_test_p2p_connections() == NUM_PEERS - 3) |
| 132 | + self.wait_until(lambda: self.is_block_requested(peers, stall_block)) |
| 133 | + self.all_sync_send_with_ping(peers) |
| 134 | + |
| 135 | + self.log.info("Provide the withheld block and check that stalling timeout gets reduced back to 2 seconds") |
| 136 | + with node.assert_debug_log(expected_msgs=['Decreased stalling timeout to 2 seconds']): |
| 137 | + for p in peers: |
| 138 | + if p.is_connected and (stall_block in p.getdata_requests): |
| 139 | + p.send_message(msg_block(block_dict[stall_block])) |
| 140 | + |
| 141 | + self.log.info("Check that all outstanding blocks get connected") |
| 142 | + self.wait_until(lambda: node.getblockcount() == NUM_BLOCKS) |
| 143 | + |
| 144 | + def total_bytes_recv_for_blocks(self): |
| 145 | + total = 0 |
| 146 | + for info in self.nodes[0].getpeerinfo(): |
| 147 | + if ("block" in info["bytesrecv_per_msg"].keys()): |
| 148 | + total += info["bytesrecv_per_msg"]["block"] |
| 149 | + return total |
| 150 | + |
| 151 | + def all_sync_send_with_ping(self, peers): |
| 152 | + for p in peers: |
| 153 | + if p.is_connected: |
| 154 | + p.sync_send_with_ping() |
| 155 | + |
| 156 | + def is_block_requested(self, peers, hash): |
| 157 | + for p in peers: |
| 158 | + if p.is_connected and (hash in p.getdata_requests): |
| 159 | + return True |
| 160 | + return False |
| 161 | + |
| 162 | + |
| 163 | +if __name__ == '__main__': |
| 164 | + P2PIBDStallingTest().main() |
0 commit comments