@@ -64,6 +64,8 @@ def __init__(self):
64
64
NodeConnCB .__init__ (self )
65
65
self .create_callback_map ()
66
66
self .connection = None
67
+ self .ping_counter = 1
68
+ self .last_pong = msg_pong ()
67
69
68
70
def add_connection (self , conn ):
69
71
self .connection = conn
@@ -87,6 +89,24 @@ def wait_for_verack(self):
87
89
def send_message (self , message ):
88
90
self .connection .send_message (message )
89
91
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
+
90
110
class AcceptBlockTest (BitcoinTestFramework ):
91
111
def add_options (self , parser ):
92
112
parser .add_option ("--testbinary" , dest = "testbinary" ,
@@ -139,7 +159,7 @@ def run_test(self):
139
159
test_node .send_message (msg_block (blocks_h2 [0 ]))
140
160
white_node .send_message (msg_block (blocks_h2 [1 ]))
141
161
142
- time . sleep ( 0.5 )
162
+ [ x . sync_with_ping () for x in [ test_node , white_node ] ]
143
163
assert_equal (self .nodes [0 ].getblockcount (), 2 )
144
164
assert_equal (self .nodes [1 ].getblockcount (), 2 )
145
165
print "First height 2 block accepted by both nodes"
@@ -152,7 +172,7 @@ def run_test(self):
152
172
test_node .send_message (msg_block (blocks_h2f [0 ]))
153
173
white_node .send_message (msg_block (blocks_h2f [1 ]))
154
174
155
- time . sleep ( 0.5 ) # Give time to process the block
175
+ [ x . sync_with_ping () for x in [ test_node , white_node ] ]
156
176
for x in self .nodes [0 ].getchaintips ():
157
177
if x ['hash' ] == blocks_h2f [0 ].hash :
158
178
assert_equal (x ['status' ], "headers-only" )
@@ -171,7 +191,7 @@ def run_test(self):
171
191
test_node .send_message (msg_block (blocks_h3 [0 ]))
172
192
white_node .send_message (msg_block (blocks_h3 [1 ]))
173
193
174
- time . sleep ( 0.5 )
194
+ [ x . sync_with_ping () for x in [ test_node , white_node ] ]
175
195
# Since the earlier block was not processed by node0, the new block
176
196
# can't be fully validated.
177
197
for x in self .nodes [0 ].getchaintips ():
@@ -222,7 +242,7 @@ def run_test(self):
222
242
white_node .send_message (headers_message ) # Send headers leading to tip
223
243
white_node .send_message (msg_block (tips [1 ])) # Now deliver the tip
224
244
try :
225
- time . sleep ( 0.5 )
245
+ white_node . sync_with_ping ( )
226
246
self .nodes [1 ].getblock (tips [1 ].hash )
227
247
print "Unrequested block far ahead of tip accepted from whitelisted peer"
228
248
except :
@@ -238,7 +258,7 @@ def run_test(self):
238
258
# the node processes it and incorrectly advances the tip).
239
259
# But this would be caught later on, when we verify that an inv triggers
240
260
# a getdata request for this block.
241
- time . sleep ( 1 )
261
+ test_node . sync_with_ping ( )
242
262
assert_equal (self .nodes [0 ].getblockcount (), 2 )
243
263
print "Unrequested block that would complete more-work chain was ignored"
244
264
@@ -250,7 +270,7 @@ def run_test(self):
250
270
test_node .last_getdata = None
251
271
test_node .send_message (msg_inv ([CInv (2 , blocks_h3 [0 ].sha256 )]))
252
272
253
- time . sleep ( 0.5 )
273
+ test_node . sync_with_ping ( )
254
274
with mininode_lock :
255
275
getdata = test_node .last_getdata
256
276
@@ -261,7 +281,7 @@ def run_test(self):
261
281
# 7. Send the missing block for the third time (now it is requested)
262
282
test_node .send_message (msg_block (blocks_h2f [0 ]))
263
283
264
- time . sleep ( 2 )
284
+ test_node . sync_with_ping ( )
265
285
assert_equal (self .nodes [0 ].getblockcount (), 290 )
266
286
print "Successfully reorged to longer chain from non-whitelisted peer"
267
287
0 commit comments