]>
Commit | Line | Data |
---|---|---|
88f6c8c3 AM |
1 | #!/usr/bin/env python2 |
2 | # Copyright (c) 2014 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 | ||
6 | # | |
7 | # Test InvalidateBlock code | |
8 | # | |
9 | ||
64937fe5 | 10 | from test_framework.test_framework import BitcoinTestFramework |
aff0bf7f DH |
11 | from test_framework.util import initialize_chain_clean, start_node, \ |
12 | connect_nodes_bi, sync_blocks | |
13 | ||
14 | import time | |
88f6c8c3 AM |
15 | |
16 | class InvalidateTest(BitcoinTestFramework): | |
88f6c8c3 AM |
17 | def setup_chain(self): |
18 | print("Initializing test directory "+self.options.tmpdir) | |
cd3d67cf | 19 | initialize_chain_clean(self.options.tmpdir, 3) |
ff0f3054 | 20 | |
88f6c8c3 AM |
21 | def setup_network(self): |
22 | self.nodes = [] | |
ff0f3054 | 23 | self.is_network_split = False |
88f6c8c3 AM |
24 | self.nodes.append(start_node(0, self.options.tmpdir, ["-debug"])) |
25 | self.nodes.append(start_node(1, self.options.tmpdir, ["-debug"])) | |
cd3d67cf | 26 | self.nodes.append(start_node(2, self.options.tmpdir, ["-debug"])) |
ff0f3054 | 27 | |
88f6c8c3 | 28 | def run_test(self): |
cd3d67cf | 29 | print "Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:" |
88f6c8c3 | 30 | print "Mine 4 blocks on Node 0" |
6b04508e | 31 | self.nodes[0].generate(4) |
88f6c8c3 AM |
32 | assert(self.nodes[0].getblockcount() == 4) |
33 | besthash = self.nodes[0].getbestblockhash() | |
34 | ||
35 | print "Mine competing 6 blocks on Node 1" | |
6b04508e | 36 | self.nodes[1].generate(6) |
88f6c8c3 AM |
37 | assert(self.nodes[1].getblockcount() == 6) |
38 | ||
39 | print "Connect nodes to force a reorg" | |
40 | connect_nodes_bi(self.nodes,0,1) | |
cd3d67cf | 41 | sync_blocks(self.nodes[0:2]) |
88f6c8c3 AM |
42 | assert(self.nodes[0].getblockcount() == 6) |
43 | badhash = self.nodes[1].getblockhash(2) | |
44 | ||
45 | print "Invalidate block 2 on node 0 and verify we reorg to node 0's original chain" | |
46 | self.nodes[0].invalidateblock(badhash) | |
47 | newheight = self.nodes[0].getblockcount() | |
48 | newhash = self.nodes[0].getbestblockhash() | |
49 | if (newheight != 4 or newhash != besthash): | |
50 | raise AssertionError("Wrong tip for node0, hash %s, height %d"%(newhash,newheight)) | |
51 | ||
cd3d67cf AM |
52 | print "\nMake sure we won't reorg to a lower work chain:" |
53 | connect_nodes_bi(self.nodes,1,2) | |
54 | print "Sync node 2 to node 1 so both have 6 blocks" | |
55 | sync_blocks(self.nodes[1:3]) | |
56 | assert(self.nodes[2].getblockcount() == 6) | |
57 | print "Invalidate block 5 on node 1 so its tip is now at 4" | |
58 | self.nodes[1].invalidateblock(self.nodes[1].getblockhash(5)) | |
59 | assert(self.nodes[1].getblockcount() == 4) | |
60 | print "Invalidate block 3 on node 2, so its tip is now 2" | |
61 | self.nodes[2].invalidateblock(self.nodes[2].getblockhash(3)) | |
62 | assert(self.nodes[2].getblockcount() == 2) | |
63 | print "..and then mine a block" | |
6b04508e | 64 | self.nodes[2].generate(1) |
cd3d67cf AM |
65 | print "Verify all nodes are at the right height" |
66 | time.sleep(5) | |
67 | for i in xrange(3): | |
68 | print i,self.nodes[i].getblockcount() | |
69 | assert(self.nodes[2].getblockcount() == 3) | |
70 | assert(self.nodes[0].getblockcount() == 4) | |
71 | node1height = self.nodes[1].getblockcount() | |
72 | if node1height < 4: | |
73 | raise AssertionError("Node 1 reorged to a lower height: %d"%node1height) | |
74 | ||
88f6c8c3 AM |
75 | if __name__ == '__main__': |
76 | InvalidateTest().main() |