|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2021-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 that a node in blocksonly mode does not request compact blocks.""" |
| 6 | + |
| 7 | +from test_framework.messages import ( |
| 8 | + MSG_BLOCK, |
| 9 | + MSG_CMPCT_BLOCK, |
| 10 | + MSG_WITNESS_FLAG, |
| 11 | + CBlock, |
| 12 | + CBlockHeader, |
| 13 | + CInv, |
| 14 | + from_hex, |
| 15 | + msg_block, |
| 16 | + msg_headers, |
| 17 | + msg_sendcmpct, |
| 18 | +) |
| 19 | +from test_framework.p2p import P2PInterface |
| 20 | +from test_framework.test_framework import BitcoinTestFramework |
| 21 | +from test_framework.util import assert_equal |
| 22 | + |
| 23 | + |
| 24 | +class P2PCompactBlocksBlocksOnly(BitcoinTestFramework): |
| 25 | + def set_test_params(self): |
| 26 | + self.extra_args = [["-blocksonly"], [], []] |
| 27 | + self.num_nodes = 3 |
| 28 | + |
| 29 | + def setup_network(self): |
| 30 | + self.setup_nodes() |
| 31 | + # Start network with everyone disconnected |
| 32 | + self.sync_all() |
| 33 | + |
| 34 | + def build_block_on_tip(self): |
| 35 | + blockhash = self.nodes[2].generate(1)[0] |
| 36 | + block_hex = self.nodes[2].getblock(blockhash=blockhash, verbosity=0) |
| 37 | + block = from_hex(CBlock(), block_hex) |
| 38 | + block.rehash() |
| 39 | + return block |
| 40 | + |
| 41 | + def run_test(self): |
| 42 | + # Nodes will only request hb compact blocks mode when they're out of IBD |
| 43 | + for node in self.nodes: |
| 44 | + assert not node.getblockchaininfo()['initialblockdownload'] |
| 45 | + |
| 46 | + p2p_conn_blocksonly = self.nodes[0].add_p2p_connection(P2PInterface()) |
| 47 | + p2p_conn_high_bw = self.nodes[1].add_p2p_connection(P2PInterface()) |
| 48 | + for conn in [p2p_conn_blocksonly, p2p_conn_high_bw]: |
| 49 | + assert_equal(conn.message_count['sendcmpct'], 2) |
| 50 | + conn.send_and_ping(msg_sendcmpct(announce=False, version=2)) |
| 51 | + |
| 52 | + # Nodes: |
| 53 | + # 0 -> blocksonly |
| 54 | + # 1 -> high bandwidth |
| 55 | + # 2 -> miner |
| 56 | + # |
| 57 | + # Topology: |
| 58 | + # p2p_conn_blocksonly ---> node0 |
| 59 | + # p2p_conn_high_bw ---> node1 |
| 60 | + # node2 (no connections) |
| 61 | + # |
| 62 | + # node2 produces blocks that are passed to the rest of the nodes |
| 63 | + # through the respective p2p connections. |
| 64 | + |
| 65 | + self.log.info("Test that -blocksonly nodes do not select peers for BIP152 high bandwidth mode") |
| 66 | + |
| 67 | + block0 = self.build_block_on_tip() |
| 68 | + |
| 69 | + # A -blocksonly node should not request BIP152 high bandwidth mode upon |
| 70 | + # receiving a new valid block at the tip. |
| 71 | + p2p_conn_blocksonly.send_and_ping(msg_block(block0)) |
| 72 | + assert_equal(int(self.nodes[0].getbestblockhash(), 16), block0.sha256) |
| 73 | + assert_equal(p2p_conn_blocksonly.message_count['sendcmpct'], 2) |
| 74 | + assert_equal(p2p_conn_blocksonly.last_message['sendcmpct'].announce, False) |
| 75 | + |
| 76 | + # A normal node participating in transaction relay should request BIP152 |
| 77 | + # high bandwidth mode upon receiving a new valid block at the tip. |
| 78 | + p2p_conn_high_bw.send_and_ping(msg_block(block0)) |
| 79 | + assert_equal(int(self.nodes[1].getbestblockhash(), 16), block0.sha256) |
| 80 | + p2p_conn_high_bw.wait_until(lambda: p2p_conn_high_bw.message_count['sendcmpct'] == 3) |
| 81 | + assert_equal(p2p_conn_high_bw.last_message['sendcmpct'].announce, True) |
| 82 | + |
| 83 | +if __name__ == '__main__': |
| 84 | + P2PCompactBlocksBlocksOnly().main() |
0 commit comments