Skip to content

Commit e536499

Browse files
author
MarcoFalke
committed
Merge #9097: [qa] Rework sync_* and preciousblock.py
fa97ccb [qa] util: Rework sync_*() (MarcoFalke) fac1141 [qa] preciousblock: Use assert_equal and BitcoinTestFramework.__init__ (MarcoFalke)
2 parents 4a2b170 + fa97ccb commit e536499

File tree

3 files changed

+54
-49
lines changed

3 files changed

+54
-49
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()

qa/rpc-tests/smartfees.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,9 @@ def transact_and_mine(self, numblocks, mining_node):
225225
self.memutxo, Decimal("0.005"), min_fee, min_fee)
226226
tx_kbytes = (len(txhex) // 2) / 1000.0
227227
self.fees_per_kb.append(float(fee)/tx_kbytes)
228-
sync_mempools(self.nodes[0:3],.1)
228+
sync_mempools(self.nodes[0:3], wait=.1)
229229
mined = mining_node.getblock(mining_node.generate(1)[0],True)["tx"]
230-
sync_blocks(self.nodes[0:3],.1)
230+
sync_blocks(self.nodes[0:3], wait=.1)
231231
# update which txouts are confirmed
232232
newmem = []
233233
for utx in self.memutxo:
@@ -259,7 +259,7 @@ def run_test(self):
259259
while len(self.nodes[1].getrawmempool()) > 0:
260260
self.nodes[1].generate(1)
261261

262-
sync_blocks(self.nodes[0:3],.1)
262+
sync_blocks(self.nodes[0:3], wait=.1)
263263
print("Final estimates after emptying mempools")
264264
check_estimates(self.nodes[1], self.fees_per_kb, 2)
265265

qa/rpc-tests/test_framework/util.py

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -121,33 +121,35 @@ def hex_str_to_bytes(hex_str):
121121
def str_to_b64str(string):
122122
return b64encode(string.encode('utf-8')).decode('ascii')
123123

124-
def sync_blocks(rpc_connections, wait=1, timeout=60):
124+
def sync_blocks(rpc_connections, *, wait=1, timeout=60):
125125
"""
126126
Wait until everybody has the same tip
127127
"""
128128
maxheight = 0
129129
while timeout > 0:
130-
tips = [ x.waitforblockheight(maxheight, int(wait * 1000)) for x in rpc_connections ]
131-
heights = [ x["height"] for x in tips ]
132-
if tips == [ tips[0] ]*len(tips):
133-
return True
134-
if heights == [ heights[0] ]*len(heights): #heights are the same but hashes are not
135-
raise AssertionError("Block sync failed")
130+
tips = [r.waitforblockheight(maxheight, int(wait * 1000)) for r in rpc_connections]
131+
heights = [t["height"] for t in tips]
132+
if tips == [tips[0]] * len(tips):
133+
return
134+
if heights == [heights[0]] * len(heights):
135+
raise AssertionError("Block sync failed: (Hashes don't match)")
136136
timeout -= wait
137137
maxheight = max(heights)
138-
raise AssertionError("Block sync failed")
138+
raise AssertionError("Block sync failed with heights: {}".format(heights))
139139

140-
def sync_chain(rpc_connections, wait=1):
140+
def sync_chain(rpc_connections, *, wait=1, timeout=60):
141141
"""
142142
Wait until everybody has the same best block
143143
"""
144-
while True:
145-
counts = [ x.getbestblockhash() for x in rpc_connections ]
146-
if counts == [ counts[0] ]*len(counts):
147-
break
144+
while timeout > 0:
145+
best_hash = [x.getbestblockhash() for x in rpc_connections]
146+
if best_hash == [best_hash[0]]*len(best_hash):
147+
return
148148
time.sleep(wait)
149+
timeout -= wait
150+
raise AssertionError("Chain sync failed: Best block hashes don't match")
149151

150-
def sync_mempools(rpc_connections, wait=1, timeout=60):
152+
def sync_mempools(rpc_connections, *, wait=1, timeout=60):
151153
"""
152154
Wait until everybody has the same transactions in their memory
153155
pools
@@ -159,7 +161,7 @@ def sync_mempools(rpc_connections, wait=1, timeout=60):
159161
if set(rpc_connections[i].getrawmempool()) == pool:
160162
num_match = num_match+1
161163
if num_match == len(rpc_connections):
162-
return True
164+
return
163165
time.sleep(wait)
164166
timeout -= wait
165167
raise AssertionError("Mempool sync failed")

0 commit comments

Comments
 (0)