|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2017 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 logic for setting nMinimumChainWork on command line. |
| 6 | +
|
| 7 | +Nodes don't consider themselves out of "initial block download" until |
| 8 | +their active chain has more work than nMinimumChainWork. |
| 9 | +
|
| 10 | +Nodes don't download blocks from a peer unless the peer's best known block |
| 11 | +has more work than nMinimumChainWork. |
| 12 | +
|
| 13 | +While in initial block download, nodes won't relay blocks to their peers, so |
| 14 | +test that this parameter functions as intended by verifying that block relay |
| 15 | +only succeeds past a given node once its nMinimumChainWork has been exceeded. |
| 16 | +""" |
| 17 | + |
| 18 | +import time |
| 19 | + |
| 20 | +from test_framework.test_framework import BitcoinTestFramework |
| 21 | +from test_framework.util import sync_blocks, connect_nodes, assert_equal |
| 22 | + |
| 23 | +# 2 hashes required per regtest block (with no difficulty adjustment) |
| 24 | +REGTEST_WORK_PER_BLOCK = 2 |
| 25 | + |
| 26 | +class MinimumChainWorkTest(BitcoinTestFramework): |
| 27 | + def set_test_params(self): |
| 28 | + self.setup_clean_chain = True |
| 29 | + self.num_nodes = 3 |
| 30 | + self.extra_args = [[], ["-minimumchainwork=0x65"], ["-minimumchainwork=0x65"]] |
| 31 | + self.node_min_work = [0, 101, 101] |
| 32 | + |
| 33 | + def setup_network(self): |
| 34 | + # This test relies on the chain setup being: |
| 35 | + # node0 <- node1 <- node2 |
| 36 | + # Before leaving IBD, nodes prefer to download blocks from outbound |
| 37 | + # peers, so ensure that we're mining on an outbound peer and testing |
| 38 | + # block relay to inbound peers. |
| 39 | + self.setup_nodes() |
| 40 | + for i in range(self.num_nodes-1): |
| 41 | + connect_nodes(self.nodes[i+1], i) |
| 42 | + |
| 43 | + def run_test(self): |
| 44 | + # Start building a chain on node0. node2 shouldn't be able to sync until node1's |
| 45 | + # minchainwork is exceeded |
| 46 | + starting_chain_work = REGTEST_WORK_PER_BLOCK # Genesis block's work |
| 47 | + self.log.info("Testing relay across node %d (minChainWork = %d)", 1, self.node_min_work[1]) |
| 48 | + |
| 49 | + starting_blockcount = self.nodes[2].getblockcount() |
| 50 | + |
| 51 | + num_blocks_to_generate = int((self.node_min_work[1] - starting_chain_work) / REGTEST_WORK_PER_BLOCK) |
| 52 | + self.log.info("Generating %d blocks on node0", num_blocks_to_generate) |
| 53 | + hashes = self.nodes[0].generate(num_blocks_to_generate) |
| 54 | + |
| 55 | + self.log.info("Node0 current chain work: %s", self.nodes[0].getblockheader(hashes[-1])['chainwork']) |
| 56 | + |
| 57 | + # Sleep a few seconds and verify that node2 didn't get any new blocks |
| 58 | + # or headers. We sleep, rather than sync_blocks(node0, node1) because |
| 59 | + # it's reasonable either way for node1 to get the blocks, or not get |
| 60 | + # them (since they're below node1's minchainwork). |
| 61 | + time.sleep(3) |
| 62 | + |
| 63 | + self.log.info("Verifying node 2 has no more blocks than before") |
| 64 | + self.log.info("Blockcounts: %s", [n.getblockcount() for n in self.nodes]) |
| 65 | + # Node2 shouldn't have any new headers yet, because node1 should not |
| 66 | + # have relayed anything. |
| 67 | + assert_equal(len(self.nodes[2].getchaintips()), 1) |
| 68 | + assert_equal(self.nodes[2].getchaintips()[0]['height'], 0) |
| 69 | + |
| 70 | + assert self.nodes[1].getbestblockhash() != self.nodes[0].getbestblockhash() |
| 71 | + assert_equal(self.nodes[2].getblockcount(), starting_blockcount) |
| 72 | + |
| 73 | + self.log.info("Generating one more block") |
| 74 | + self.nodes[0].generate(1) |
| 75 | + |
| 76 | + self.log.info("Verifying nodes are all synced") |
| 77 | + self.sync_all() |
| 78 | + self.log.info("Blockcounts: %s", [n.getblockcount() for n in self.nodes]) |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + MinimumChainWorkTest().main() |
0 commit comments