|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2019 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 | +"""Tests NODE_COMPACT_FILTERS (BIP 157/158). |
| 6 | +
|
| 7 | +Tests that a node configured with -blockfilterindex and -peerblockfilters can serve |
| 8 | +cfcheckpts. |
| 9 | +""" |
| 10 | + |
| 11 | +from test_framework.messages import ( |
| 12 | + FILTER_TYPE_BASIC, |
| 13 | + msg_getcfcheckpt, |
| 14 | +) |
| 15 | +from test_framework.mininode import P2PInterface |
| 16 | +from test_framework.test_framework import BitcoinTestFramework |
| 17 | +from test_framework.util import ( |
| 18 | + assert_equal, |
| 19 | + connect_nodes, |
| 20 | + disconnect_nodes, |
| 21 | + wait_until, |
| 22 | +) |
| 23 | + |
| 24 | +class CompactFiltersTest(BitcoinTestFramework): |
| 25 | + def set_test_params(self): |
| 26 | + self.setup_clean_chain = True |
| 27 | + self.rpc_timeout = 480 |
| 28 | + self.num_nodes = 2 |
| 29 | + self.extra_args = [ |
| 30 | + ["-blockfilterindex", "-peerblockfilters"], |
| 31 | + ["-blockfilterindex"], |
| 32 | + ] |
| 33 | + |
| 34 | + def run_test(self): |
| 35 | + # Node 0 supports COMPACT_FILTERS, node 1 does not. |
| 36 | + node0 = self.nodes[0].add_p2p_connection(P2PInterface()) |
| 37 | + node1 = self.nodes[1].add_p2p_connection(P2PInterface()) |
| 38 | + |
| 39 | + # Nodes 0 & 1 share the same first 999 blocks in the chain. |
| 40 | + self.nodes[0].generate(999) |
| 41 | + self.sync_blocks(timeout=600) |
| 42 | + |
| 43 | + # Stale blocks by disconnecting nodes 0 & 1, mining, then reconnecting |
| 44 | + disconnect_nodes(self.nodes[0], 1) |
| 45 | + |
| 46 | + self.nodes[0].generate(1) |
| 47 | + wait_until(lambda: self.nodes[0].getblockcount() == 1000) |
| 48 | + stale_block_hash = self.nodes[0].getblockhash(1000) |
| 49 | + |
| 50 | + self.nodes[1].generate(1001) |
| 51 | + wait_until(lambda: self.nodes[1].getblockcount() == 2000) |
| 52 | + |
| 53 | + self.log.info("get cfcheckpt on chain to be re-orged out.") |
| 54 | + request = msg_getcfcheckpt( |
| 55 | + filter_type=FILTER_TYPE_BASIC, |
| 56 | + stop_hash=int(stale_block_hash, 16) |
| 57 | + ) |
| 58 | + node0.send_and_ping(message=request) |
| 59 | + response = node0.last_message['cfcheckpt'] |
| 60 | + assert_equal(response.filter_type, request.filter_type) |
| 61 | + assert_equal(response.stop_hash, request.stop_hash) |
| 62 | + assert_equal(len(response.headers), 1) |
| 63 | + |
| 64 | + self.log.info("Reorg node 0 to a new chain.") |
| 65 | + connect_nodes(self.nodes[0], 1) |
| 66 | + self.sync_blocks(timeout=600) |
| 67 | + |
| 68 | + main_block_hash = self.nodes[0].getblockhash(1000) |
| 69 | + assert main_block_hash != stale_block_hash, "node 0 chain did not reorganize" |
| 70 | + |
| 71 | + self.log.info("Check that peers can fetch cfcheckpt on active chain.") |
| 72 | + tip_hash = self.nodes[0].getbestblockhash() |
| 73 | + request = msg_getcfcheckpt( |
| 74 | + filter_type=FILTER_TYPE_BASIC, |
| 75 | + stop_hash=int(tip_hash, 16) |
| 76 | + ) |
| 77 | + node0.send_and_ping(request) |
| 78 | + response = node0.last_message['cfcheckpt'] |
| 79 | + assert_equal(response.filter_type, request.filter_type) |
| 80 | + assert_equal(response.stop_hash, request.stop_hash) |
| 81 | + |
| 82 | + main_cfcheckpt = self.nodes[0].getblockfilter(main_block_hash, 'basic')['header'] |
| 83 | + tip_cfcheckpt = self.nodes[0].getblockfilter(tip_hash, 'basic')['header'] |
| 84 | + assert_equal( |
| 85 | + response.headers, |
| 86 | + [int(header, 16) for header in (main_cfcheckpt, tip_cfcheckpt)] |
| 87 | + ) |
| 88 | + |
| 89 | + self.log.info("Check that peers can fetch cfcheckpt on stale chain.") |
| 90 | + request = msg_getcfcheckpt( |
| 91 | + filter_type=FILTER_TYPE_BASIC, |
| 92 | + stop_hash=int(stale_block_hash, 16) |
| 93 | + ) |
| 94 | + node0.send_and_ping(request) |
| 95 | + response = node0.last_message['cfcheckpt'] |
| 96 | + |
| 97 | + stale_cfcheckpt = self.nodes[0].getblockfilter(stale_block_hash, 'basic')['header'] |
| 98 | + assert_equal( |
| 99 | + response.headers, |
| 100 | + [int(header, 16) for header in (stale_cfcheckpt,)] |
| 101 | + ) |
| 102 | + |
| 103 | + self.log.info("Requests to node 1 without NODE_COMPACT_FILTERS results in disconnection.") |
| 104 | + requests = [ |
| 105 | + msg_getcfcheckpt( |
| 106 | + filter_type=FILTER_TYPE_BASIC, |
| 107 | + stop_hash=int(main_block_hash, 16) |
| 108 | + ), |
| 109 | + ] |
| 110 | + for request in requests: |
| 111 | + node1 = self.nodes[1].add_p2p_connection(P2PInterface()) |
| 112 | + node1.send_message(request) |
| 113 | + node1.wait_for_disconnect() |
| 114 | + |
| 115 | + self.log.info("Check that invalid requests result in disconnection.") |
| 116 | + requests = [ |
| 117 | + # Requesting unknown filter type results in disconnection. |
| 118 | + msg_getcfcheckpt( |
| 119 | + filter_type=255, |
| 120 | + stop_hash=int(main_block_hash, 16) |
| 121 | + ), |
| 122 | + # Requesting unknown hash results in disconnection. |
| 123 | + msg_getcfcheckpt( |
| 124 | + filter_type=FILTER_TYPE_BASIC, |
| 125 | + stop_hash=123456789, |
| 126 | + ), |
| 127 | + ] |
| 128 | + for request in requests: |
| 129 | + node0 = self.nodes[0].add_p2p_connection(P2PInterface()) |
| 130 | + node0.send_message(request) |
| 131 | + node0.wait_for_disconnect() |
| 132 | + |
| 133 | +if __name__ == '__main__': |
| 134 | + CompactFiltersTest().main() |
0 commit comments