Skip to content

Commit 04b5d23

Browse files
committed
Replace sleep with syncing using pings
1 parent 6b1066f commit 04b5d23

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

qa/rpc-tests/p2p-acceptblock.py

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ def __init__(self):
6464
NodeConnCB.__init__(self)
6565
self.create_callback_map()
6666
self.connection = None
67+
self.ping_counter = 1
68+
self.last_pong = msg_pong()
6769

6870
def add_connection(self, conn):
6971
self.connection = conn
@@ -87,6 +89,24 @@ def wait_for_verack(self):
8789
def send_message(self, message):
8890
self.connection.send_message(message)
8991

92+
def on_pong(self, conn, message):
93+
self.last_pong = message
94+
95+
# Sync up with the node after delivery of a block
96+
def sync_with_ping(self, timeout=30):
97+
self.connection.send_message(msg_ping(nonce=self.ping_counter))
98+
received_pong = False
99+
sleep_time = 0.05
100+
while not received_pong and timeout > 0:
101+
time.sleep(sleep_time)
102+
timeout -= sleep_time
103+
with mininode_lock:
104+
if self.last_pong.nonce == self.ping_counter:
105+
received_pong = True
106+
self.ping_counter += 1
107+
return received_pong
108+
109+
90110
class AcceptBlockTest(BitcoinTestFramework):
91111
def add_options(self, parser):
92112
parser.add_option("--testbinary", dest="testbinary",
@@ -139,7 +159,7 @@ def run_test(self):
139159
test_node.send_message(msg_block(blocks_h2[0]))
140160
white_node.send_message(msg_block(blocks_h2[1]))
141161

142-
time.sleep(0.5)
162+
[ x.sync_with_ping() for x in [test_node, white_node] ]
143163
assert_equal(self.nodes[0].getblockcount(), 2)
144164
assert_equal(self.nodes[1].getblockcount(), 2)
145165
print "First height 2 block accepted by both nodes"
@@ -152,7 +172,7 @@ def run_test(self):
152172
test_node.send_message(msg_block(blocks_h2f[0]))
153173
white_node.send_message(msg_block(blocks_h2f[1]))
154174

155-
time.sleep(0.5) # Give time to process the block
175+
[ x.sync_with_ping() for x in [test_node, white_node] ]
156176
for x in self.nodes[0].getchaintips():
157177
if x['hash'] == blocks_h2f[0].hash:
158178
assert_equal(x['status'], "headers-only")
@@ -171,7 +191,7 @@ def run_test(self):
171191
test_node.send_message(msg_block(blocks_h3[0]))
172192
white_node.send_message(msg_block(blocks_h3[1]))
173193

174-
time.sleep(0.5)
194+
[ x.sync_with_ping() for x in [test_node, white_node] ]
175195
# Since the earlier block was not processed by node0, the new block
176196
# can't be fully validated.
177197
for x in self.nodes[0].getchaintips():
@@ -222,7 +242,7 @@ def run_test(self):
222242
white_node.send_message(headers_message) # Send headers leading to tip
223243
white_node.send_message(msg_block(tips[1])) # Now deliver the tip
224244
try:
225-
time.sleep(0.5)
245+
white_node.sync_with_ping()
226246
self.nodes[1].getblock(tips[1].hash)
227247
print "Unrequested block far ahead of tip accepted from whitelisted peer"
228248
except:
@@ -238,7 +258,7 @@ def run_test(self):
238258
# the node processes it and incorrectly advances the tip).
239259
# But this would be caught later on, when we verify that an inv triggers
240260
# a getdata request for this block.
241-
time.sleep(1)
261+
test_node.sync_with_ping()
242262
assert_equal(self.nodes[0].getblockcount(), 2)
243263
print "Unrequested block that would complete more-work chain was ignored"
244264

@@ -250,7 +270,7 @@ def run_test(self):
250270
test_node.last_getdata = None
251271
test_node.send_message(msg_inv([CInv(2, blocks_h3[0].sha256)]))
252272

253-
time.sleep(0.5)
273+
test_node.sync_with_ping()
254274
with mininode_lock:
255275
getdata = test_node.last_getdata
256276

@@ -261,7 +281,7 @@ def run_test(self):
261281
# 7. Send the missing block for the third time (now it is requested)
262282
test_node.send_message(msg_block(blocks_h2f[0]))
263283

264-
time.sleep(2)
284+
test_node.sync_with_ping()
265285
assert_equal(self.nodes[0].getblockcount(), 290)
266286
print "Successfully reorged to longer chain from non-whitelisted peer"
267287

0 commit comments

Comments
 (0)