@@ -199,21 +199,21 @@ def result_from_response(response_str):
199
199
return (response .get ('result' , None ), response .get ('error' , None ))
200
200
201
201
202
- def call_rpc (rpc , method , params ):
202
+ async def call_rpc (rpc , method , params ):
203
203
request = build_request (method , params )
204
- response = rpc .execute (request )
204
+ response = await rpc .execute (request )
205
205
return result_from_response (response )
206
206
207
207
208
- def assert_rpc_result (rpc , method , params , expected ):
209
- result , error = call_rpc (rpc , method , params )
208
+ async def assert_rpc_result (rpc , method , params , expected ):
209
+ result , error = await call_rpc (rpc , method , params )
210
210
assert error is None
211
211
assert result == expected
212
212
return result
213
213
214
214
215
- def validate_account_attribute (fixture_key , rpc_method , rpc , state , addr , at_block ):
216
- state_result , state_error = call_rpc (rpc , rpc_method , [addr , at_block ])
215
+ async def validate_account_attribute (fixture_key , rpc_method , rpc , state , addr , at_block ):
216
+ state_result , state_error = await call_rpc (rpc , rpc_method , [addr , at_block ])
217
217
assert state_result == state [fixture_key ], "Invalid state - %s" % state_error
218
218
219
219
@@ -224,19 +224,31 @@ def validate_account_attribute(fixture_key, rpc_method, rpc, state, addr, at_blo
224
224
)
225
225
226
226
227
- def validate_account_state (rpc , state , addr , at_block ):
227
+ async def validate_account_state (rpc , state , addr , at_block ):
228
228
standardized_state = fixture_state_in_rpc_format (state )
229
229
for fixture_key , rpc_method in RPC_STATE_LOOKUPS :
230
- validate_account_attribute (fixture_key , rpc_method , rpc , standardized_state , addr , at_block )
230
+ await validate_account_attribute (
231
+ fixture_key ,
232
+ rpc_method ,
233
+ rpc ,
234
+ standardized_state ,
235
+ addr ,
236
+ at_block
237
+ )
231
238
for key in state ['storage' ]:
232
239
position = '0x0' if key == '0x' else key
233
240
expected_storage = state ['storage' ][key ]
234
- assert_rpc_result (rpc , 'eth_getStorageAt' , [addr , position , at_block ], expected_storage )
241
+ await assert_rpc_result (
242
+ rpc ,
243
+ 'eth_getStorageAt' ,
244
+ [addr , position , at_block ],
245
+ expected_storage
246
+ )
235
247
236
248
237
- def validate_accounts (rpc , states , at_block = 'latest' ):
249
+ async def validate_accounts (rpc , states , at_block = 'latest' ):
238
250
for addr in states :
239
- validate_account_state (rpc , states [addr ], addr , at_block )
251
+ await validate_account_state (rpc , states [addr ], addr , at_block )
240
252
241
253
242
254
def validate_rpc_block_vs_fixture (block , block_fixture ):
@@ -264,13 +276,13 @@ def is_by_hash(at_block):
264
276
raise ValueError ("Unrecognized 'at_block' value: %r" % at_block )
265
277
266
278
267
- def validate_transaction_count (rpc , block_fixture , at_block ):
279
+ async def validate_transaction_count (rpc , block_fixture , at_block ):
268
280
if is_by_hash (at_block ):
269
281
rpc_method = 'eth_getBlockTransactionCountByHash'
270
282
else :
271
283
rpc_method = 'eth_getBlockTransactionCountByNumber'
272
284
expected_transaction_count = hex (len (block_fixture ['transactions' ]))
273
- assert_rpc_result (rpc , rpc_method , [at_block ], expected_transaction_count )
285
+ await assert_rpc_result (rpc , rpc_method , [at_block ], expected_transaction_count )
274
286
275
287
276
288
def validate_rpc_transaction_vs_fixture (transaction , fixture ):
@@ -282,74 +294,74 @@ def validate_rpc_transaction_vs_fixture(transaction, fixture):
282
294
assert actual_transaction == expected
283
295
284
296
285
- def validate_transaction_by_index (rpc , transaction_fixture , at_block , index ):
297
+ async def validate_transaction_by_index (rpc , transaction_fixture , at_block , index ):
286
298
if is_by_hash (at_block ):
287
299
rpc_method = 'eth_getTransactionByBlockHashAndIndex'
288
300
else :
289
301
rpc_method = 'eth_getTransactionByBlockNumberAndIndex'
290
- result , error = call_rpc (rpc , rpc_method , [at_block , hex (index )])
302
+ result , error = await call_rpc (rpc , rpc_method , [at_block , hex (index )])
291
303
assert error is None
292
304
validate_rpc_transaction_vs_fixture (result , transaction_fixture )
293
305
294
306
295
- def validate_block (rpc , block_fixture , at_block ):
307
+ async def validate_block (rpc , block_fixture , at_block ):
296
308
if is_by_hash (at_block ):
297
309
rpc_method = 'eth_getBlockByHash'
298
310
else :
299
311
rpc_method = 'eth_getBlockByNumber'
300
312
301
313
# validate without transaction bodies
302
- result , error = call_rpc (rpc , rpc_method , [at_block , False ])
314
+ result , error = await call_rpc (rpc , rpc_method , [at_block , False ])
303
315
assert error is None
304
316
validate_rpc_block_vs_fixture (result , block_fixture )
305
317
assert len (result ['transactions' ]) == len (block_fixture ['transactions' ])
306
318
307
319
for index , transaction_fixture in enumerate (block_fixture ['transactions' ]):
308
- validate_transaction_by_index (rpc , transaction_fixture , at_block , index )
320
+ await validate_transaction_by_index (rpc , transaction_fixture , at_block , index )
309
321
310
- validate_transaction_count (rpc , block_fixture , at_block )
322
+ await validate_transaction_count (rpc , block_fixture , at_block )
311
323
312
324
# TODO validate transaction bodies
313
- result , error = call_rpc (rpc , rpc_method , [at_block , True ])
325
+ result , error = await call_rpc (rpc , rpc_method , [at_block , True ])
314
326
# assert error is None
315
327
# assert result['transactions'] == block_fixture['transactions']
316
328
317
- validate_uncles (rpc , block_fixture , at_block )
329
+ await validate_uncles (rpc , block_fixture , at_block )
318
330
319
331
320
- def validate_last_block (rpc , block_fixture ):
332
+ async def validate_last_block (rpc , block_fixture ):
321
333
header = block_fixture ['blockHeader' ]
322
334
323
- validate_block (rpc , block_fixture , 'latest' )
324
- validate_block (rpc , block_fixture , header ['hash' ])
325
- validate_block (rpc , block_fixture , int (header ['number' ], 16 ))
335
+ await validate_block (rpc , block_fixture , 'latest' )
336
+ await validate_block (rpc , block_fixture , header ['hash' ])
337
+ await validate_block (rpc , block_fixture , int (header ['number' ], 16 ))
326
338
327
339
328
- def validate_uncle_count (rpc , block_fixture , at_block ):
340
+ async def validate_uncle_count (rpc , block_fixture , at_block ):
329
341
if is_by_hash (at_block ):
330
342
rpc_method = 'eth_getUncleCountByBlockHash'
331
343
else :
332
344
rpc_method = 'eth_getUncleCountByBlockNumber'
333
345
334
346
num_uncles = len (block_fixture ['uncleHeaders' ])
335
- assert_rpc_result (rpc , rpc_method , [at_block ], hex (num_uncles ))
347
+ await assert_rpc_result (rpc , rpc_method , [at_block ], hex (num_uncles ))
336
348
337
349
338
- def validate_uncle_headers (rpc , block_fixture , at_block ):
350
+ async def validate_uncle_headers (rpc , block_fixture , at_block ):
339
351
if is_by_hash (at_block ):
340
352
rpc_method = 'eth_getUncleByBlockHashAndIndex'
341
353
else :
342
354
rpc_method = 'eth_getUncleByBlockNumberAndIndex'
343
355
344
356
for idx , uncle in enumerate (block_fixture ['uncleHeaders' ]):
345
- result , error = call_rpc (rpc , rpc_method , [at_block , hex (idx )])
357
+ result , error = await call_rpc (rpc , rpc_method , [at_block , hex (idx )])
346
358
assert error is None
347
359
validate_rpc_block_vs_fixture_header (result , uncle )
348
360
349
361
350
- def validate_uncles (rpc , block_fixture , at_block ):
351
- validate_uncle_count (rpc , block_fixture , at_block )
352
- validate_uncle_headers (rpc , block_fixture , at_block )
362
+ async def validate_uncles (rpc , block_fixture , at_block ):
363
+ await validate_uncle_count (rpc , block_fixture , at_block )
364
+ await validate_uncle_headers (rpc , block_fixture , at_block )
353
365
354
366
355
367
@pytest .fixture
@@ -369,13 +381,14 @@ def chain(chain_without_block_validation):
369
381
return
370
382
371
383
372
- def test_rpc_against_fixtures (chain , ipc_server , chain_fixture , fixture_data ):
384
+ @pytest .mark .asyncio
385
+ async def test_rpc_against_fixtures (chain , ipc_server , chain_fixture , fixture_data ):
373
386
rpc = RPCServer (None )
374
387
375
- setup_result , setup_error = call_rpc (rpc , 'evm_resetToGenesisFixture' , [chain_fixture ])
388
+ setup_result , setup_error = await call_rpc (rpc , 'evm_resetToGenesisFixture' , [chain_fixture ])
376
389
assert setup_error is None and setup_result is True , "cannot load chain for %r" % fixture_data
377
390
378
- validate_accounts (rpc , chain_fixture ['pre' ])
391
+ await validate_accounts (rpc , chain_fixture ['pre' ])
379
392
380
393
for block_fixture in chain_fixture ['blocks' ]:
381
394
should_be_good_block = 'blockHeader' in block_fixture
@@ -384,21 +397,21 @@ def test_rpc_against_fixtures(chain, ipc_server, chain_fixture, fixture_data):
384
397
assert not should_be_good_block
385
398
continue
386
399
387
- block_result , block_error = call_rpc (rpc , 'evm_applyBlockFixture' , [block_fixture ])
400
+ block_result , block_error = await call_rpc (rpc , 'evm_applyBlockFixture' , [block_fixture ])
388
401
389
402
if should_be_good_block :
390
403
assert block_error is None
391
404
assert block_result == block_fixture ['rlp' ]
392
405
393
- validate_block (rpc , block_fixture , block_fixture ['blockHeader' ]['hash' ])
406
+ await validate_block (rpc , block_fixture , block_fixture ['blockHeader' ]['hash' ])
394
407
else :
395
408
assert block_error is not None
396
409
397
410
if chain_fixture .get ('lastblockhash' , None ):
398
411
for block_fixture in chain_fixture ['blocks' ]:
399
412
if get_in (['blockHeader' , 'hash' ], block_fixture ) == chain_fixture ['lastblockhash' ]:
400
- validate_last_block (rpc , block_fixture )
413
+ await validate_last_block (rpc , block_fixture )
401
414
402
- validate_accounts (rpc , chain_fixture ['postState' ])
403
- validate_accounts (rpc , chain_fixture ['pre' ], 'earliest' )
404
- validate_accounts (rpc , chain_fixture ['pre' ], 0 )
415
+ await validate_accounts (rpc , chain_fixture ['postState' ])
416
+ await validate_accounts (rpc , chain_fixture ['pre' ], 'earliest' )
417
+ await validate_accounts (rpc , chain_fixture ['pre' ], 0 )
0 commit comments