44# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55"""Test the invalidateblock RPC."""
66
7- import time
8-
97from test_framework .test_framework import BitcoinTestFramework
10- from test_framework .util import assert_equal , connect_nodes_bi , sync_blocks
8+ from test_framework .address import ADDRESS_BCRT1_UNSPENDABLE
9+ from test_framework .util import (
10+ assert_equal ,
11+ connect_nodes_bi ,
12+ sync_blocks ,
13+ wait_until ,
14+ )
15+
1116
1217class InvalidateTest (BitcoinTestFramework ):
1318 def set_test_params (self ):
@@ -21,46 +26,66 @@ def run_test(self):
2126 self .log .info ("Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:" )
2227 self .log .info ("Mine 4 blocks on Node 0" )
2328 self .nodes [0 ].generatetoaddress (4 , self .nodes [0 ].get_deterministic_priv_key ().address )
24- assert (self .nodes [0 ].getblockcount () == 4 )
25- besthash = self .nodes [0 ].getbestblockhash ()
29+ assert_equal (self .nodes [0 ].getblockcount (), 4 )
30+ besthash_n0 = self .nodes [0 ].getbestblockhash ()
2631
2732 self .log .info ("Mine competing 6 blocks on Node 1" )
2833 self .nodes [1 ].generatetoaddress (6 , self .nodes [1 ].get_deterministic_priv_key ().address )
29- assert (self .nodes [1 ].getblockcount () == 6 )
34+ assert_equal (self .nodes [1 ].getblockcount (), 6 )
3035
3136 self .log .info ("Connect nodes to force a reorg" )
32- connect_nodes_bi (self .nodes ,0 , 1 )
37+ connect_nodes_bi (self .nodes , 0 , 1 )
3338 sync_blocks (self .nodes [0 :2 ])
34- assert (self .nodes [0 ].getblockcount () == 6 )
39+ assert_equal (self .nodes [0 ].getblockcount (), 6 )
3540 badhash = self .nodes [1 ].getblockhash (2 )
3641
3742 self .log .info ("Invalidate block 2 on node 0 and verify we reorg to node 0's original chain" )
3843 self .nodes [0 ].invalidateblock (badhash )
39- newheight = self .nodes [0 ].getblockcount ()
40- newhash = self .nodes [0 ].getbestblockhash ()
41- if (newheight != 4 or newhash != besthash ):
42- raise AssertionError ("Wrong tip for node0, hash %s, height %d" % (newhash ,newheight ))
44+ assert_equal (self .nodes [0 ].getblockcount (), 4 )
45+ assert_equal (self .nodes [0 ].getbestblockhash (), besthash_n0 )
4346
4447 self .log .info ("Make sure we won't reorg to a lower work chain:" )
45- connect_nodes_bi (self .nodes ,1 , 2 )
48+ connect_nodes_bi (self .nodes , 1 , 2 )
4649 self .log .info ("Sync node 2 to node 1 so both have 6 blocks" )
4750 sync_blocks (self .nodes [1 :3 ])
48- assert (self .nodes [2 ].getblockcount () == 6 )
51+ assert_equal (self .nodes [2 ].getblockcount (), 6 )
4952 self .log .info ("Invalidate block 5 on node 1 so its tip is now at 4" )
5053 self .nodes [1 ].invalidateblock (self .nodes [1 ].getblockhash (5 ))
51- assert (self .nodes [1 ].getblockcount () == 4 )
54+ assert_equal (self .nodes [1 ].getblockcount (), 4 )
5255 self .log .info ("Invalidate block 3 on node 2, so its tip is now 2" )
5356 self .nodes [2 ].invalidateblock (self .nodes [2 ].getblockhash (3 ))
54- assert (self .nodes [2 ].getblockcount () == 2 )
57+ assert_equal (self .nodes [2 ].getblockcount (), 2 )
5558 self .log .info ("..and then mine a block" )
5659 self .nodes [2 ].generatetoaddress (1 , self .nodes [2 ].get_deterministic_priv_key ().address )
5760 self .log .info ("Verify all nodes are at the right height" )
58- time .sleep (5 )
59- assert_equal (self .nodes [2 ].getblockcount (), 3 )
60- assert_equal (self .nodes [0 ].getblockcount (), 4 )
61- node1height = self .nodes [1 ].getblockcount ()
62- if node1height < 4 :
63- raise AssertionError ("Node 1 reorged to a lower height: %d" % node1height )
61+ wait_until (lambda : self .nodes [2 ].getblockcount () == 3 , timeout = 5 )
62+ wait_until (lambda : self .nodes [0 ].getblockcount () == 4 , timeout = 5 )
63+ wait_until (lambda : self .nodes [1 ].getblockcount () == 4 , timeout = 5 )
64+
65+ self .log .info ("Verify that we reconsider all ancestors as well" )
66+ blocks = self .nodes [1 ].generatetoaddress (10 , ADDRESS_BCRT1_UNSPENDABLE )
67+ assert_equal (self .nodes [1 ].getbestblockhash (), blocks [- 1 ])
68+ # Invalidate the two blocks at the tip
69+ self .nodes [1 ].invalidateblock (blocks [- 1 ])
70+ self .nodes [1 ].invalidateblock (blocks [- 2 ])
71+ assert_equal (self .nodes [1 ].getbestblockhash (), blocks [- 3 ])
72+ # Reconsider only the previous tip
73+ self .nodes [1 ].reconsiderblock (blocks [- 1 ])
74+ # Should be back at the tip by now
75+ assert_equal (self .nodes [1 ].getbestblockhash (), blocks [- 1 ])
76+
77+ self .log .info ("Verify that we reconsider all descendants" )
78+ blocks = self .nodes [1 ].generatetoaddress (10 , ADDRESS_BCRT1_UNSPENDABLE )
79+ assert_equal (self .nodes [1 ].getbestblockhash (), blocks [- 1 ])
80+ # Invalidate the two blocks at the tip
81+ self .nodes [1 ].invalidateblock (blocks [- 2 ])
82+ self .nodes [1 ].invalidateblock (blocks [- 4 ])
83+ assert_equal (self .nodes [1 ].getbestblockhash (), blocks [- 5 ])
84+ # Reconsider only the previous tip
85+ self .nodes [1 ].reconsiderblock (blocks [- 4 ])
86+ # Should be back at the tip by now
87+ assert_equal (self .nodes [1 ].getbestblockhash (), blocks [- 1 ])
88+
6489
6590if __name__ == '__main__' :
6691 InvalidateTest ().main ()
0 commit comments