|
| 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 | +"""Test that we reject low difficulty headers to prevent our block tree from filling up with useless bloat""" |
| 6 | + |
| 7 | +from test_framework.messages import ( |
| 8 | + CBlockHeader, |
| 9 | + FromHex, |
| 10 | +) |
| 11 | +from test_framework.mininode import ( |
| 12 | + P2PInterface, |
| 13 | + msg_headers, |
| 14 | +) |
| 15 | +from test_framework.test_framework import BitcoinTestFramework |
| 16 | + |
| 17 | +import os |
| 18 | + |
| 19 | + |
| 20 | +class RejectLowDifficultyHeadersTest(BitcoinTestFramework): |
| 21 | + def set_test_params(self): |
| 22 | + self.setup_clean_chain = True |
| 23 | + self.chain = 'testnet3' # Use testnet chain because it has an early checkpoint |
| 24 | + self.num_nodes = 2 |
| 25 | + |
| 26 | + def add_options(self, parser): |
| 27 | + parser.add_argument( |
| 28 | + '--datafile', |
| 29 | + default='data/blockheader_testnet3.hex', |
| 30 | + help='Test data file (default: %(default)s)', |
| 31 | + ) |
| 32 | + |
| 33 | + def run_test(self): |
| 34 | + self.log.info("Read headers data") |
| 35 | + self.headers_file_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), self.options.datafile) |
| 36 | + with open(self.headers_file_path, encoding='utf-8') as headers_data: |
| 37 | + h_lines = [l.strip() for l in headers_data.readlines()] |
| 38 | + |
| 39 | + # The headers data is taken from testnet3 for early blocks from genesis until the first checkpoint. There are |
| 40 | + # two headers with valid POW at height 1 and 2, forking off from genesis. They are indicated by the FORK_PREFIX. |
| 41 | + FORK_PREFIX = 'fork:' |
| 42 | + self.headers = [l for l in h_lines if not l.startswith(FORK_PREFIX)] |
| 43 | + self.headers_fork = [l[len(FORK_PREFIX):] for l in h_lines if l.startswith(FORK_PREFIX)] |
| 44 | + |
| 45 | + self.headers = [FromHex(CBlockHeader(), h) for h in self.headers] |
| 46 | + self.headers_fork = [FromHex(CBlockHeader(), h) for h in self.headers_fork] |
| 47 | + |
| 48 | + self.log.info("Feed all non-fork headers, including and up to the first checkpoint") |
| 49 | + self.nodes[0].add_p2p_connection(P2PInterface()) |
| 50 | + self.nodes[0].p2p.send_message(msg_headers(self.headers)) |
| 51 | + self.nodes[0].p2p.sync_with_ping() |
| 52 | + assert { |
| 53 | + 'height': 546, |
| 54 | + 'hash': '000000002a936ca763904c3c35fce2f3556c559c0214345d31b1bcebf76acb70', |
| 55 | + 'branchlen': 546, |
| 56 | + 'status': 'headers-only', |
| 57 | + } in self.nodes[0].getchaintips() |
| 58 | + |
| 59 | + self.log.info("Feed all fork headers (fails due to checkpoint)") |
| 60 | + with self.nodes[0].assert_debug_log(['bad-fork-prior-to-checkpoint (code 67)']): |
| 61 | + self.nodes[0].p2p.send_message(msg_headers(self.headers_fork)) |
| 62 | + self.nodes[0].p2p.wait_for_disconnect() |
| 63 | + |
| 64 | + self.log.info("Feed all fork headers (succeeds without checkpoint)") |
| 65 | + # On node 0 it succeeds because checkpoints are disabled |
| 66 | + self.restart_node(0, extra_args=['-nocheckpoints']) |
| 67 | + self.nodes[0].add_p2p_connection(P2PInterface()) |
| 68 | + self.nodes[0].p2p.send_message(msg_headers(self.headers_fork)) |
| 69 | + self.nodes[0].p2p.sync_with_ping() |
| 70 | + assert { |
| 71 | + "height": 2, |
| 72 | + "hash": "00000000b0494bd6c3d5ff79c497cfce40831871cbf39b1bc28bd1dac817dc39", |
| 73 | + "branchlen": 2, |
| 74 | + "status": "headers-only", |
| 75 | + } in self.nodes[0].getchaintips() |
| 76 | + |
| 77 | + # On node 1 it succeeds because no checkpoint has been reached yet by a chain tip |
| 78 | + self.nodes[1].add_p2p_connection(P2PInterface()) |
| 79 | + self.nodes[1].p2p.send_message(msg_headers(self.headers_fork)) |
| 80 | + self.nodes[1].p2p.sync_with_ping() |
| 81 | + assert { |
| 82 | + "height": 2, |
| 83 | + "hash": "00000000b0494bd6c3d5ff79c497cfce40831871cbf39b1bc28bd1dac817dc39", |
| 84 | + "branchlen": 2, |
| 85 | + "status": "headers-only", |
| 86 | + } in self.nodes[1].getchaintips() |
| 87 | + |
| 88 | + |
| 89 | +if __name__ == '__main__': |
| 90 | + RejectLowDifficultyHeadersTest().main() |
0 commit comments