Skip to content

Commit 05e57cc

Browse files
committed
[qa] Fix sync_blocks timeout argument
Motivation for this change is mainly to make sync_blocks behavior easier to understand. Behavior is unchanged in the normal case when there are only 2 nodes in the rpc_connections set. When there are more than 2 nodes, the previous "timeout -= wait" statement wouldn't take into account time spent waiting for all nodes and as a result could lead to blocking for longer than the requested timeout.
1 parent fd6bb70 commit 05e57cc

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

qa/rpc-tests/test_framework/util.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,19 +123,24 @@ def str_to_b64str(string):
123123

124124
def sync_blocks(rpc_connections, *, wait=1, timeout=60):
125125
"""
126-
Wait until everybody has the same tip
126+
Wait until everybody has the same tip.
127+
128+
sync_blocks needs to be called with an rpc_connections set that has least
129+
one node already synced to the latest, stable tip, otherwise there's a
130+
chance it might return before all nodes are stably synced.
127131
"""
128132
maxheight = 0
129-
while timeout > 0:
133+
start_time = cur_time = time.time()
134+
while cur_time <= start_time + timeout:
130135
tips = [r.waitforblockheight(maxheight, int(wait * 1000)) for r in rpc_connections]
131136
heights = [t["height"] for t in tips]
132137
if tips == [tips[0]] * len(tips):
133138
return
134139
if heights == [heights[0]] * len(heights):
135140
raise AssertionError("Block sync failed, mismatched block hashes:{}".format(
136141
"".join("\n {!r}".format(tip) for tip in tips)))
137-
timeout -= wait
138142
maxheight = max(heights)
143+
cur_time = time.time()
139144
raise AssertionError("Block sync to height {} timed out:{}".format(
140145
maxheight, "".join("\n {!r}".format(tip) for tip in tips)))
141146

0 commit comments

Comments
 (0)