4
4
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
5
"""Test the invalidateblock RPC."""
6
6
7
- import time
8
-
9
7
from 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
+
11
16
12
17
class InvalidateTest (BitcoinTestFramework ):
13
18
def set_test_params (self ):
@@ -21,46 +26,66 @@ def run_test(self):
21
26
self .log .info ("Make sure we repopulate setBlockIndexCandidates after InvalidateBlock:" )
22
27
self .log .info ("Mine 4 blocks on Node 0" )
23
28
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 ()
26
31
27
32
self .log .info ("Mine competing 6 blocks on Node 1" )
28
33
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 )
30
35
31
36
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 )
33
38
sync_blocks (self .nodes [0 :2 ])
34
- assert (self .nodes [0 ].getblockcount () == 6 )
39
+ assert_equal (self .nodes [0 ].getblockcount (), 6 )
35
40
badhash = self .nodes [1 ].getblockhash (2 )
36
41
37
42
self .log .info ("Invalidate block 2 on node 0 and verify we reorg to node 0's original chain" )
38
43
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 )
43
46
44
47
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 )
46
49
self .log .info ("Sync node 2 to node 1 so both have 6 blocks" )
47
50
sync_blocks (self .nodes [1 :3 ])
48
- assert (self .nodes [2 ].getblockcount () == 6 )
51
+ assert_equal (self .nodes [2 ].getblockcount (), 6 )
49
52
self .log .info ("Invalidate block 5 on node 1 so its tip is now at 4" )
50
53
self .nodes [1 ].invalidateblock (self .nodes [1 ].getblockhash (5 ))
51
- assert (self .nodes [1 ].getblockcount () == 4 )
54
+ assert_equal (self .nodes [1 ].getblockcount (), 4 )
52
55
self .log .info ("Invalidate block 3 on node 2, so its tip is now 2" )
53
56
self .nodes [2 ].invalidateblock (self .nodes [2 ].getblockhash (3 ))
54
- assert (self .nodes [2 ].getblockcount () == 2 )
57
+ assert_equal (self .nodes [2 ].getblockcount (), 2 )
55
58
self .log .info ("..and then mine a block" )
56
59
self .nodes [2 ].generatetoaddress (1 , self .nodes [2 ].get_deterministic_priv_key ().address )
57
60
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
+
64
89
65
90
if __name__ == '__main__' :
66
91
InvalidateTest ().main ()
0 commit comments