8
8
#
9
9
10
10
from test_framework .test_framework import BitcoinTestFramework
11
- from test_framework .util import *
11
+ from test_framework .util import (
12
+ assert_equal ,
13
+ connect_nodes_bi ,
14
+ sync_chain ,
15
+ sync_blocks ,
16
+ )
12
17
13
18
def unidirectional_node_sync_via_rpc (node_src , node_dest ):
14
19
blocks_to_copy = []
@@ -33,84 +38,82 @@ def node_sync_via_rpc(nodes):
33
38
unidirectional_node_sync_via_rpc (node_src , node_dest )
34
39
35
40
class PreciousTest (BitcoinTestFramework ):
36
- def setup_chain (self ):
37
- print ("Initializing test directory " + self .options .tmpdir )
38
- initialize_chain_clean (self .options .tmpdir , 3 )
41
+ def __init__ (self ):
42
+ super ().__init__ ()
43
+ self .setup_clean_chain = True
44
+ self .num_nodes = 3
45
+ self .extra_args = [["-debug" ]] * self .num_nodes
39
46
40
47
def setup_network (self ):
41
- self .nodes = []
42
- self .is_network_split = False
43
- self .nodes .append (start_node (0 , self .options .tmpdir , ["-debug" ]))
44
- self .nodes .append (start_node (1 , self .options .tmpdir , ["-debug" ]))
45
- self .nodes .append (start_node (2 , self .options .tmpdir , ["-debug" ]))
48
+ self .nodes = self .setup_nodes ()
46
49
47
50
def run_test (self ):
48
51
print ("Ensure submitblock can in principle reorg to a competing chain" )
49
52
self .nodes [0 ].generate (1 )
50
- assert (self .nodes [0 ].getblockcount () == 1 )
53
+ assert_equal (self .nodes [0 ].getblockcount (), 1 )
51
54
(hashY , hashZ ) = self .nodes [1 ].generate (2 )
52
- assert (self .nodes [1 ].getblockcount () == 2 )
55
+ assert_equal (self .nodes [1 ].getblockcount (), 2 )
53
56
node_sync_via_rpc (self .nodes [0 :3 ])
54
- assert (self .nodes [0 ].getbestblockhash () == hashZ )
57
+ assert_equal (self .nodes [0 ].getbestblockhash (), hashZ )
55
58
56
59
print ("Mine blocks A-B-C on Node 0" )
57
60
(hashA , hashB , hashC ) = self .nodes [0 ].generate (3 )
58
- assert (self .nodes [0 ].getblockcount () == 5 )
61
+ assert_equal (self .nodes [0 ].getblockcount (), 5 )
59
62
print ("Mine competing blocks E-F-G on Node 1" )
60
63
(hashE , hashF , hashG ) = self .nodes [1 ].generate (3 )
61
- assert (self .nodes [1 ].getblockcount () == 5 )
64
+ assert_equal (self .nodes [1 ].getblockcount (), 5 )
62
65
assert (hashC != hashG )
63
66
print ("Connect nodes and check no reorg occurs" )
64
67
# Submit competing blocks via RPC so any reorg should occur before we proceed (no way to wait on inaction for p2p sync)
65
68
node_sync_via_rpc (self .nodes [0 :2 ])
66
69
connect_nodes_bi (self .nodes ,0 ,1 )
67
- assert (self .nodes [0 ].getbestblockhash () == hashC )
68
- assert (self .nodes [1 ].getbestblockhash () == hashG )
70
+ assert_equal (self .nodes [0 ].getbestblockhash (), hashC )
71
+ assert_equal (self .nodes [1 ].getbestblockhash (), hashG )
69
72
print ("Make Node0 prefer block G" )
70
73
self .nodes [0 ].preciousblock (hashG )
71
- assert (self .nodes [0 ].getbestblockhash () == hashG )
74
+ assert_equal (self .nodes [0 ].getbestblockhash (), hashG )
72
75
print ("Make Node0 prefer block C again" )
73
76
self .nodes [0 ].preciousblock (hashC )
74
- assert (self .nodes [0 ].getbestblockhash () == hashC )
77
+ assert_equal (self .nodes [0 ].getbestblockhash (), hashC )
75
78
print ("Make Node1 prefer block C" )
76
79
self .nodes [1 ].preciousblock (hashC )
77
80
sync_chain (self .nodes [0 :2 ]) # wait because node 1 may not have downloaded hashC
78
- assert (self .nodes [1 ].getbestblockhash () == hashC )
81
+ assert_equal (self .nodes [1 ].getbestblockhash (), hashC )
79
82
print ("Make Node1 prefer block G again" )
80
83
self .nodes [1 ].preciousblock (hashG )
81
- assert (self .nodes [1 ].getbestblockhash () == hashG )
84
+ assert_equal (self .nodes [1 ].getbestblockhash (), hashG )
82
85
print ("Make Node0 prefer block G again" )
83
86
self .nodes [0 ].preciousblock (hashG )
84
- assert (self .nodes [0 ].getbestblockhash () == hashG )
87
+ assert_equal (self .nodes [0 ].getbestblockhash (), hashG )
85
88
print ("Make Node1 prefer block C again" )
86
89
self .nodes [1 ].preciousblock (hashC )
87
- assert (self .nodes [1 ].getbestblockhash () == hashC )
90
+ assert_equal (self .nodes [1 ].getbestblockhash (), hashC )
88
91
print ("Mine another block (E-F-G-)H on Node 0 and reorg Node 1" )
89
92
self .nodes [0 ].generate (1 )
90
- assert (self .nodes [0 ].getblockcount () == 6 )
93
+ assert_equal (self .nodes [0 ].getblockcount (), 6 )
91
94
sync_blocks (self .nodes [0 :2 ])
92
95
hashH = self .nodes [0 ].getbestblockhash ()
93
- assert (self .nodes [1 ].getbestblockhash () == hashH )
96
+ assert_equal (self .nodes [1 ].getbestblockhash (), hashH )
94
97
print ("Node1 should not be able to prefer block C anymore" )
95
98
self .nodes [1 ].preciousblock (hashC )
96
- assert (self .nodes [1 ].getbestblockhash () == hashH )
99
+ assert_equal (self .nodes [1 ].getbestblockhash (), hashH )
97
100
print ("Mine competing blocks I-J-K-L on Node 2" )
98
101
self .nodes [2 ].generate (4 )
99
- assert (self .nodes [2 ].getblockcount () == 6 )
102
+ assert_equal (self .nodes [2 ].getblockcount (), 6 )
100
103
hashL = self .nodes [2 ].getbestblockhash ()
101
104
print ("Connect nodes and check no reorg occurs" )
102
105
node_sync_via_rpc (self .nodes [0 :3 ])
103
106
connect_nodes_bi (self .nodes ,1 ,2 )
104
107
connect_nodes_bi (self .nodes ,0 ,2 )
105
- assert (self .nodes [0 ].getbestblockhash () == hashH )
106
- assert (self .nodes [1 ].getbestblockhash () == hashH )
107
- assert (self .nodes [2 ].getbestblockhash () == hashL )
108
+ assert_equal (self .nodes [0 ].getbestblockhash (), hashH )
109
+ assert_equal (self .nodes [1 ].getbestblockhash (), hashH )
110
+ assert_equal (self .nodes [2 ].getbestblockhash (), hashL )
108
111
print ("Make Node1 prefer block L" )
109
112
self .nodes [1 ].preciousblock (hashL )
110
- assert (self .nodes [1 ].getbestblockhash () == hashL )
113
+ assert_equal (self .nodes [1 ].getbestblockhash (), hashL )
111
114
print ("Make Node2 prefer block H" )
112
115
self .nodes [2 ].preciousblock (hashH )
113
- assert (self .nodes [2 ].getbestblockhash () == hashH )
116
+ assert_equal (self .nodes [2 ].getbestblockhash (), hashH )
114
117
115
118
if __name__ == '__main__' :
116
119
PreciousTest ().main ()
0 commit comments