Merge #11923: Wallet : remove unused fNoncriticalErrors variable from CWalletDB:...
[bitcoinplatinum.git] / test / functional / minchainwork.py
blob90a3de0e0d9d00c28eb975975b66888200553d3f
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.
7 Nodes don't consider themselves out of "initial block download" until
8 their active chain has more work than nMinimumChainWork.
10 Nodes don't download blocks from a peer unless the peer's best known block
11 has more work than nMinimumChainWork.
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 """
18 import time
20 from test_framework.test_framework import BitcoinTestFramework
21 from test_framework.util import connect_nodes, assert_equal
23 # 2 hashes required per regtest block (with no difficulty adjustment)
24 REGTEST_WORK_PER_BLOCK = 2
26 class MinimumChainWorkTest(BitcoinTestFramework):
27 def set_test_params(self):
28 self.setup_clean_chain = True
29 self.num_nodes = 3
31 self.extra_args = [[], ["-minimumchainwork=0x65"], ["-minimumchainwork=0x65"]]
32 self.node_min_work = [0, 101, 101]
34 def setup_network(self):
35 # This test relies on the chain setup being:
36 # node0 <- node1 <- node2
37 # Before leaving IBD, nodes prefer to download blocks from outbound
38 # peers, so ensure that we're mining on an outbound peer and testing
39 # block relay to inbound peers.
40 self.setup_nodes()
41 for i in range(self.num_nodes-1):
42 connect_nodes(self.nodes[i+1], i)
44 def run_test(self):
45 # Start building a chain on node0. node2 shouldn't be able to sync until node1's
46 # minchainwork is exceeded
47 starting_chain_work = REGTEST_WORK_PER_BLOCK # Genesis block's work
48 self.log.info("Testing relay across node %d (minChainWork = %d)", 1, self.node_min_work[1])
50 starting_blockcount = self.nodes[2].getblockcount()
52 num_blocks_to_generate = int((self.node_min_work[1] - starting_chain_work) / REGTEST_WORK_PER_BLOCK)
53 self.log.info("Generating %d blocks on node0", num_blocks_to_generate)
54 hashes = self.nodes[0].generate(num_blocks_to_generate)
56 self.log.info("Node0 current chain work: %s", self.nodes[0].getblockheader(hashes[-1])['chainwork'])
58 # Sleep a few seconds and verify that node2 didn't get any new blocks
59 # or headers. We sleep, rather than sync_blocks(node0, node1) because
60 # it's reasonable either way for node1 to get the blocks, or not get
61 # them (since they're below node1's minchainwork).
62 time.sleep(3)
64 self.log.info("Verifying node 2 has no more blocks than before")
65 self.log.info("Blockcounts: %s", [n.getblockcount() for n in self.nodes])
66 # Node2 shouldn't have any new headers yet, because node1 should not
67 # have relayed anything.
68 assert_equal(len(self.nodes[2].getchaintips()), 1)
69 assert_equal(self.nodes[2].getchaintips()[0]['height'], 0)
71 assert self.nodes[1].getbestblockhash() != self.nodes[0].getbestblockhash()
72 assert_equal(self.nodes[2].getblockcount(), starting_blockcount)
74 self.log.info("Generating one more block")
75 self.nodes[0].generate(1)
77 self.log.info("Verifying nodes are all synced")
79 # Because nodes in regtest are all manual connections (eg using
80 # addnode), node1 should not have disconnected node0. If not for that,
81 # we'd expect node1 to have disconnected node0 for serving an
82 # insufficient work chain, in which case we'd need to reconnect them to
83 # continue the test.
85 self.sync_all()
86 self.log.info("Blockcounts: %s", [n.getblockcount() for n in self.nodes])
88 if __name__ == '__main__':
89 MinimumChainWorkTest().main()