@@ -16,15 +16,17 @@ class InvalidateTest(BitcoinTestFramework):
16
16
17
17
def setup_chain (self ):
18
18
print ("Initializing test directory " + self .options .tmpdir )
19
- initialize_chain_clean (self .options .tmpdir , 2 )
19
+ initialize_chain_clean (self .options .tmpdir , 3 )
20
20
21
21
def setup_network (self ):
22
22
self .nodes = []
23
23
self .is_network_split = False
24
24
self .nodes .append (start_node (0 , self .options .tmpdir , ["-debug" ]))
25
25
self .nodes .append (start_node (1 , self .options .tmpdir , ["-debug" ]))
26
+ self .nodes .append (start_node (2 , self .options .tmpdir , ["-debug" ]))
26
27
27
28
def run_test (self ):
29
+ print "Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:"
28
30
print "Mine 4 blocks on Node 0"
29
31
self .nodes [0 ].setgenerate (True , 4 )
30
32
assert (self .nodes [0 ].getblockcount () == 4 )
@@ -36,7 +38,7 @@ def run_test(self):
36
38
37
39
print "Connect nodes to force a reorg"
38
40
connect_nodes_bi (self .nodes ,0 ,1 )
39
- sync_blocks (self .nodes )
41
+ sync_blocks (self .nodes [ 0 : 2 ] )
40
42
assert (self .nodes [0 ].getblockcount () == 6 )
41
43
badhash = self .nodes [1 ].getblockhash (2 )
42
44
@@ -47,5 +49,28 @@ def run_test(self):
47
49
if (newheight != 4 or newhash != besthash ):
48
50
raise AssertionError ("Wrong tip for node0, hash %s, height %d" % (newhash ,newheight ))
49
51
52
+ print "\n Make 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"
64
+ self .nodes [2 ].setgenerate (True , 1 )
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
+
50
75
if __name__ == '__main__' :
51
76
InvalidateTest ().main ()
0 commit comments