|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 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 compact blocks HB selection logic.""" |
| 6 | + |
| 7 | +from test_framework.test_framework import BitcoinTestFramework |
| 8 | +from test_framework.util import assert_equal |
| 9 | + |
| 10 | + |
| 11 | +class CompactBlocksConnectionTest(BitcoinTestFramework): |
| 12 | + """Test class for verifying selection of HB peer connections.""" |
| 13 | + |
| 14 | + def set_test_params(self): |
| 15 | + self.setup_clean_chain = True |
| 16 | + self.num_nodes = 6 |
| 17 | + |
| 18 | + def peer_info(self, from_node, to_node): |
| 19 | + """Query from_node for its getpeerinfo about to_node.""" |
| 20 | + for peerinfo in self.nodes[from_node].getpeerinfo(): |
| 21 | + if "(testnode%i)" % to_node in peerinfo['subver']: |
| 22 | + return peerinfo |
| 23 | + return None |
| 24 | + |
| 25 | + def setup_network(self): |
| 26 | + self.setup_nodes() |
| 27 | + # Start network with everyone disconnected |
| 28 | + self.sync_all() |
| 29 | + |
| 30 | + def relay_block_through(self, peer): |
| 31 | + """Relay a new block through peer peer, and return HB status between 1 and [2,3,4,5].""" |
| 32 | + self.connect_nodes(peer, 0) |
| 33 | + self.nodes[0].generate(1) |
| 34 | + self.sync_blocks() |
| 35 | + self.disconnect_nodes(peer, 0) |
| 36 | + status_to = [self.peer_info(1, i)['bip152_hb_to'] for i in range(2, 6)] |
| 37 | + status_from = [self.peer_info(i, 1)['bip152_hb_from'] for i in range(2, 6)] |
| 38 | + assert_equal(status_to, status_from) |
| 39 | + return status_to |
| 40 | + |
| 41 | + def run_test(self): |
| 42 | + self.log.info("Testing reserved high-bandwidth mode slot for outbound peer...") |
| 43 | + |
| 44 | + # Connect everyone to node 0, and mine some blocks to get all nodes out of IBD. |
| 45 | + for i in range(1, 6): |
| 46 | + self.connect_nodes(i, 0) |
| 47 | + self.nodes[0].generate(2) |
| 48 | + self.sync_blocks() |
| 49 | + for i in range(1, 6): |
| 50 | + self.disconnect_nodes(i, 0) |
| 51 | + |
| 52 | + # Construct network topology: |
| 53 | + # - Node 0 is the block producer |
| 54 | + # - Node 1 is the "target" node being tested |
| 55 | + # - Nodes 2-5 are intermediaries. |
| 56 | + # - Node 1 has an outbound connection to node 2 |
| 57 | + # - Node 1 has inbound connections from nodes 3-5 |
| 58 | + self.connect_nodes(3, 1) |
| 59 | + self.connect_nodes(4, 1) |
| 60 | + self.connect_nodes(5, 1) |
| 61 | + self.connect_nodes(1, 2) |
| 62 | + |
| 63 | + # Mine blocks subsequently relaying through nodes 3,4,5 (inbound to node 1) |
| 64 | + for nodeid in range(3, 6): |
| 65 | + status = self.relay_block_through(nodeid) |
| 66 | + assert_equal(status, [False, nodeid >= 3, nodeid >= 4, nodeid >= 5]) |
| 67 | + |
| 68 | + # And again through each. This should not change HB status. |
| 69 | + for nodeid in range(3, 6): |
| 70 | + status = self.relay_block_through(nodeid) |
| 71 | + assert_equal(status, [False, True, True, True]) |
| 72 | + |
| 73 | + # Now relay one block through peer 2 (outbound from node 1), so it should take HB status |
| 74 | + # from one of the inbounds. |
| 75 | + status = self.relay_block_through(2) |
| 76 | + assert_equal(status[0], True) |
| 77 | + assert_equal(sum(status), 3) |
| 78 | + |
| 79 | + # Now relay again through nodes 3,4,5. Since 2 is outbound, it should remain HB. |
| 80 | + for nodeid in range(3, 6): |
| 81 | + status = self.relay_block_through(nodeid) |
| 82 | + assert status[0] |
| 83 | + assert status[nodeid - 2] |
| 84 | + assert_equal(sum(status), 3) |
| 85 | + |
| 86 | + # Reconnect peer 2, and retry. Now the three inbounds should be HB again. |
| 87 | + self.disconnect_nodes(1, 2) |
| 88 | + self.connect_nodes(1, 2) |
| 89 | + for nodeid in range(3, 6): |
| 90 | + status = self.relay_block_through(nodeid) |
| 91 | + assert not status[0] |
| 92 | + assert status[nodeid - 2] |
| 93 | + assert_equal(status, [False, True, True, True]) |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == '__main__': |
| 97 | + CompactBlocksConnectionTest().main() |
0 commit comments