25
25
# on_getheaders: provide headers via BlockStore
26
26
# on_getdata: provide blocks via BlockStore
27
27
28
+ global mininode_lock
29
+
28
30
class TestNode (NodeConnCB ):
29
31
30
32
def __init__ (self , block_store , tx_store ):
@@ -148,10 +150,11 @@ def wait_for_verack(self):
148
150
max_tries = 10 / sleep_time # Wait at most 10 seconds
149
151
while max_tries > 0 :
150
152
done = True
151
- for c in self .connections :
152
- if c .cb .verack_received is False :
153
- done = False
154
- break
153
+ with mininode_lock :
154
+ for c in self .connections :
155
+ if c .cb .verack_received is False :
156
+ done = False
157
+ break
155
158
if done :
156
159
break
157
160
time .sleep (sleep_time )
@@ -161,10 +164,11 @@ def wait_for_pings(self, counter):
161
164
while received_pongs is not True :
162
165
time .sleep (0.05 )
163
166
received_pongs = True
164
- for c in self .connections :
165
- if c .cb .received_ping_response (counter ) is not True :
166
- received_pongs = False
167
- break
167
+ with mininode_lock :
168
+ for c in self .connections :
169
+ if c .cb .received_ping_response (counter ) is not True :
170
+ received_pongs = False
171
+ break
168
172
169
173
# sync_blocks: Wait for all connections to request the blockhash given
170
174
# then send get_headers to find out the tip of each node, and synchronize
@@ -173,8 +177,9 @@ def sync_blocks(self, blockhash, num_blocks):
173
177
# Wait for nodes to request block (50ms sleep * 20 tries * num_blocks)
174
178
max_tries = 20 * num_blocks
175
179
while max_tries > 0 :
176
- results = [ blockhash in c .cb .block_request_map and
177
- c .cb .block_request_map [blockhash ] for c in self .connections ]
180
+ with mininode_lock :
181
+ results = [ blockhash in c .cb .block_request_map and
182
+ c .cb .block_request_map [blockhash ] for c in self .connections ]
178
183
if False not in results :
179
184
break
180
185
time .sleep (0.05 )
@@ -199,8 +204,9 @@ def sync_transaction(self, txhash, num_events):
199
204
# Wait for nodes to request transaction (50ms sleep * 20 tries * num_events)
200
205
max_tries = 20 * num_events
201
206
while max_tries > 0 :
202
- results = [ txhash in c .cb .tx_request_map and
203
- c .cb .tx_request_map [txhash ] for c in self .connections ]
207
+ with mininode_lock :
208
+ results = [ txhash in c .cb .tx_request_map and
209
+ c .cb .tx_request_map [txhash ] for c in self .connections ]
204
210
if False not in results :
205
211
break
206
212
time .sleep (0.05 )
@@ -221,19 +227,21 @@ def sync_transaction(self, txhash, num_events):
221
227
self .ping_counter += 1
222
228
223
229
# Sort inv responses from each node
224
- [ c .cb .lastInv .sort () for c in self .connections ]
230
+ with mininode_lock :
231
+ [ c .cb .lastInv .sort () for c in self .connections ]
225
232
226
233
# Verify that the tip of each connection all agree with each other, and
227
234
# with the expected outcome (if given)
228
235
def check_results (self , blockhash , outcome ):
229
- for c in self .connections :
230
- if outcome is None :
231
- if c .cb .bestblockhash != self .connections [0 ].cb .bestblockhash :
236
+ with mininode_lock :
237
+ for c in self .connections :
238
+ if outcome is None :
239
+ if c .cb .bestblockhash != self .connections [0 ].cb .bestblockhash :
240
+ return False
241
+ elif ((c .cb .bestblockhash == blockhash ) != outcome ):
242
+ # print c.cb.bestblockhash, blockhash, outcome
232
243
return False
233
- elif ((c .cb .bestblockhash == blockhash ) != outcome ):
234
- # print c.cb.bestblockhash, blockhash, outcome
235
- return False
236
- return True
244
+ return True
237
245
238
246
# Either check that the mempools all agree with each other, or that
239
247
# txhash's presence in the mempool matches the outcome specified.
@@ -242,16 +250,17 @@ def check_results(self, blockhash, outcome):
242
250
# perhaps it would be useful to add the ability to check explicitly that
243
251
# a particular tx's existence in the mempool is the same across all nodes.
244
252
def check_mempool (self , txhash , outcome ):
245
- for c in self .connections :
246
- if outcome is None :
247
- # Make sure the mempools agree with each other
248
- if c .cb .lastInv != self .connections [0 ].cb .lastInv :
249
- # print c.rpc.getrawmempool()
253
+ with mininode_lock :
254
+ for c in self .connections :
255
+ if outcome is None :
256
+ # Make sure the mempools agree with each other
257
+ if c .cb .lastInv != self .connections [0 ].cb .lastInv :
258
+ # print c.rpc.getrawmempool()
259
+ return False
260
+ elif ((txhash in c .cb .lastInv ) != outcome ):
261
+ # print c.rpc.getrawmempool(), c.cb.lastInv
250
262
return False
251
- elif ((txhash in c .cb .lastInv ) != outcome ):
252
- # print c.rpc.getrawmempool(), c.cb.lastInv
253
- return False
254
- return True
263
+ return True
255
264
256
265
def run (self ):
257
266
# Wait until verack is received
@@ -272,9 +281,10 @@ def run(self):
272
281
block = b_or_t
273
282
block_outcome = outcome
274
283
# Add to shared block_store, set as current block
275
- self .block_store .add_block (block )
276
- for c in self .connections :
277
- c .cb .block_request_map [block .sha256 ] = False
284
+ with mininode_lock :
285
+ self .block_store .add_block (block )
286
+ for c in self .connections :
287
+ c .cb .block_request_map [block .sha256 ] = False
278
288
# Either send inv's to each node and sync, or add
279
289
# to invqueue for later inv'ing.
280
290
if (test_instance .sync_every_block ):
@@ -288,10 +298,11 @@ def run(self):
288
298
assert (isinstance (b_or_t , CTransaction ))
289
299
tx = b_or_t
290
300
tx_outcome = outcome
291
- # Add to shared tx store
292
- self .tx_store .add_transaction (tx )
293
- for c in self .connections :
294
- c .cb .tx_request_map [tx .sha256 ] = False
301
+ # Add to shared tx store and clear map entry
302
+ with mininode_lock :
303
+ self .tx_store .add_transaction (tx )
304
+ for c in self .connections :
305
+ c .cb .tx_request_map [tx .sha256 ] = False
295
306
# Again, either inv to all nodes or save for later
296
307
if (test_instance .sync_every_tx ):
297
308
[ c .cb .send_inv (tx ) for c in self .connections ]
@@ -302,7 +313,7 @@ def run(self):
302
313
invqueue .append (CInv (1 , tx .sha256 ))
303
314
# Ensure we're not overflowing the inv queue
304
315
if len (invqueue ) == MAX_INV_SZ :
305
- [ c .sb . send_message (msg_inv (invqueue )) for c in self .connections ]
316
+ [ c .send_message (msg_inv (invqueue )) for c in self .connections ]
306
317
invqueue = []
307
318
308
319
# Do final sync if we weren't syncing on every block or every tx.
0 commit comments