Skip to content

Commit fa97ccb

Browse files
author
MarcoFalke
committed
[qa] util: Rework sync_*()
* Only allow named args in sync_*() * Make sync_* fails more verbose * Add timeout to sync_chain()
1 parent fac1141 commit fa97ccb

File tree

2 files changed

+20
-18
lines changed

2 files changed

+20
-18
lines changed

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)