Skip to content

Commit fac1141

Browse files
author
MarcoFalke
committed
[qa] preciousblock: Use assert_equal and BitcoinTestFramework.__init__
1 parent 1253f86 commit fac1141

File tree

1 file changed

+34
-31
lines changed

1 file changed

+34
-31
lines changed

qa/rpc-tests/preciousblock.py

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
#
99

1010
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+
)
1217

1318
def unidirectional_node_sync_via_rpc(node_src, node_dest):
1419
blocks_to_copy = []
@@ -33,84 +38,82 @@ def node_sync_via_rpc(nodes):
3338
unidirectional_node_sync_via_rpc(node_src, node_dest)
3439

3540
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
3946

4047
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()
4649

4750
def run_test(self):
4851
print("Ensure submitblock can in principle reorg to a competing chain")
4952
self.nodes[0].generate(1)
50-
assert(self.nodes[0].getblockcount() == 1)
53+
assert_equal(self.nodes[0].getblockcount(), 1)
5154
(hashY, hashZ) = self.nodes[1].generate(2)
52-
assert(self.nodes[1].getblockcount() == 2)
55+
assert_equal(self.nodes[1].getblockcount(), 2)
5356
node_sync_via_rpc(self.nodes[0:3])
54-
assert(self.nodes[0].getbestblockhash() == hashZ)
57+
assert_equal(self.nodes[0].getbestblockhash(), hashZ)
5558

5659
print("Mine blocks A-B-C on Node 0")
5760
(hashA, hashB, hashC) = self.nodes[0].generate(3)
58-
assert(self.nodes[0].getblockcount() == 5)
61+
assert_equal(self.nodes[0].getblockcount(), 5)
5962
print("Mine competing blocks E-F-G on Node 1")
6063
(hashE, hashF, hashG) = self.nodes[1].generate(3)
61-
assert(self.nodes[1].getblockcount() == 5)
64+
assert_equal(self.nodes[1].getblockcount(), 5)
6265
assert(hashC != hashG)
6366
print("Connect nodes and check no reorg occurs")
6467
# Submit competing blocks via RPC so any reorg should occur before we proceed (no way to wait on inaction for p2p sync)
6568
node_sync_via_rpc(self.nodes[0:2])
6669
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)
6972
print("Make Node0 prefer block G")
7073
self.nodes[0].preciousblock(hashG)
71-
assert(self.nodes[0].getbestblockhash() == hashG)
74+
assert_equal(self.nodes[0].getbestblockhash(), hashG)
7275
print("Make Node0 prefer block C again")
7376
self.nodes[0].preciousblock(hashC)
74-
assert(self.nodes[0].getbestblockhash() == hashC)
77+
assert_equal(self.nodes[0].getbestblockhash(), hashC)
7578
print("Make Node1 prefer block C")
7679
self.nodes[1].preciousblock(hashC)
7780
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)
7982
print("Make Node1 prefer block G again")
8083
self.nodes[1].preciousblock(hashG)
81-
assert(self.nodes[1].getbestblockhash() == hashG)
84+
assert_equal(self.nodes[1].getbestblockhash(), hashG)
8285
print("Make Node0 prefer block G again")
8386
self.nodes[0].preciousblock(hashG)
84-
assert(self.nodes[0].getbestblockhash() == hashG)
87+
assert_equal(self.nodes[0].getbestblockhash(), hashG)
8588
print("Make Node1 prefer block C again")
8689
self.nodes[1].preciousblock(hashC)
87-
assert(self.nodes[1].getbestblockhash() == hashC)
90+
assert_equal(self.nodes[1].getbestblockhash(), hashC)
8891
print("Mine another block (E-F-G-)H on Node 0 and reorg Node 1")
8992
self.nodes[0].generate(1)
90-
assert(self.nodes[0].getblockcount() == 6)
93+
assert_equal(self.nodes[0].getblockcount(), 6)
9194
sync_blocks(self.nodes[0:2])
9295
hashH = self.nodes[0].getbestblockhash()
93-
assert(self.nodes[1].getbestblockhash() == hashH)
96+
assert_equal(self.nodes[1].getbestblockhash(), hashH)
9497
print("Node1 should not be able to prefer block C anymore")
9598
self.nodes[1].preciousblock(hashC)
96-
assert(self.nodes[1].getbestblockhash() == hashH)
99+
assert_equal(self.nodes[1].getbestblockhash(), hashH)
97100
print("Mine competing blocks I-J-K-L on Node 2")
98101
self.nodes[2].generate(4)
99-
assert(self.nodes[2].getblockcount() == 6)
102+
assert_equal(self.nodes[2].getblockcount(), 6)
100103
hashL = self.nodes[2].getbestblockhash()
101104
print("Connect nodes and check no reorg occurs")
102105
node_sync_via_rpc(self.nodes[0:3])
103106
connect_nodes_bi(self.nodes,1,2)
104107
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)
108111
print("Make Node1 prefer block L")
109112
self.nodes[1].preciousblock(hashL)
110-
assert(self.nodes[1].getbestblockhash() == hashL)
113+
assert_equal(self.nodes[1].getbestblockhash(), hashL)
111114
print("Make Node2 prefer block H")
112115
self.nodes[2].preciousblock(hashH)
113-
assert(self.nodes[2].getbestblockhash() == hashH)
116+
assert_equal(self.nodes[2].getbestblockhash(), hashH)
114117

115118
if __name__ == '__main__':
116119
PreciousTest().main()

0 commit comments

Comments
 (0)