|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2019-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 we reject low difficulty headers to prevent our block tree from filling up with useless bloat""" |
| 6 | + |
| 7 | +from test_framework.test_framework import BitcoinTestFramework |
| 8 | + |
| 9 | +NODE1_BLOCKS_REQUIRED = 15 |
| 10 | +NODE2_BLOCKS_REQUIRED = 2047 |
| 11 | + |
| 12 | + |
| 13 | +class RejectLowDifficultyHeadersTest(BitcoinTestFramework): |
| 14 | + def set_test_params(self): |
| 15 | + self.setup_clean_chain = True |
| 16 | + self.num_nodes = 3 |
| 17 | + # Node0 has no required chainwork; node1 requires 15 blocks on top of the genesis block; node2 requires 2047 |
| 18 | + self.extra_args = [["-minimumchainwork=0x0"], ["-minimumchainwork=0x1f"], ["-minimumchainwork=0x1000"]] |
| 19 | + |
| 20 | + def setup_network(self): |
| 21 | + self.setup_nodes() |
| 22 | + self.connect_nodes(0, 1) |
| 23 | + self.connect_nodes(0, 2) |
| 24 | + self.sync_all() |
| 25 | + |
| 26 | + def test_chains_sync_when_long_enough(self): |
| 27 | + self.log.info("Generate blocks on the node with no required chainwork, and verify nodes 1 and 2 have no new headers in their headers tree") |
| 28 | + with self.nodes[1].assert_debug_log(expected_msgs=["[net] Ignoring low-work chain (height=14)"]), self.nodes[2].assert_debug_log(expected_msgs=["[net] Ignoring low-work chain (height=14)"]): |
| 29 | + self.generate(self.nodes[0], NODE1_BLOCKS_REQUIRED-1, sync_fun=self.no_op) |
| 30 | + |
| 31 | + for node in self.nodes[1:]: |
| 32 | + chaintips = node.getchaintips() |
| 33 | + assert(len(chaintips) == 1) |
| 34 | + assert { |
| 35 | + 'height': 0, |
| 36 | + 'hash': '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206', |
| 37 | + 'branchlen': 0, |
| 38 | + 'status': 'active', |
| 39 | + } in chaintips |
| 40 | + |
| 41 | + self.log.info("Generate more blocks to satisfy node1's minchainwork requirement, and verify node2 still has no new headers in headers tree") |
| 42 | + with self.nodes[2].assert_debug_log(expected_msgs=["[net] Ignoring low-work chain (height=15)"]): |
| 43 | + self.generate(self.nodes[0], NODE1_BLOCKS_REQUIRED - self.nodes[0].getblockcount(), sync_fun=self.no_op) |
| 44 | + self.sync_blocks(self.nodes[0:2]) |
| 45 | + |
| 46 | + assert { |
| 47 | + 'height': 0, |
| 48 | + 'hash': '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206', |
| 49 | + 'branchlen': 0, |
| 50 | + 'status': 'active', |
| 51 | + } in self.nodes[2].getchaintips() |
| 52 | + |
| 53 | + assert(len(self.nodes[2].getchaintips()) == 1) |
| 54 | + |
| 55 | + self.log.info("Generate long chain for node0/node1") |
| 56 | + self.generate(self.nodes[0], NODE2_BLOCKS_REQUIRED-self.nodes[0].getblockcount(), sync_fun=self.no_op) |
| 57 | + |
| 58 | + self.log.info("Verify that node2 will sync the chain when it gets long enough") |
| 59 | + self.sync_blocks() |
| 60 | + |
| 61 | + def run_test(self): |
| 62 | + self.test_chains_sync_when_long_enough() |
| 63 | + |
| 64 | + |
| 65 | +if __name__ == '__main__': |
| 66 | + RejectLowDifficultyHeadersTest().main() |
0 commit comments