Skip to content

Commit a904708

Browse files
committed
Enforce RPC methods to be async
1 parent 6b4872c commit a904708

File tree

7 files changed

+92
-79
lines changed

7 files changed

+92
-79
lines changed

tests/trinity/json-fixtures-over-rpc/test_rpc_fixtures.py

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,21 @@ def result_from_response(response_str):
199199
return (response.get('result', None), response.get('error', None))
200200

201201

202-
def call_rpc(rpc, method, params):
202+
async def call_rpc(rpc, method, params):
203203
request = build_request(method, params)
204-
response = rpc.execute(request)
204+
response = await rpc.execute(request)
205205
return result_from_response(response)
206206

207207

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)
210210
assert error is None
211211
assert result == expected
212212
return result
213213

214214

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])
217217
assert state_result == state[fixture_key], "Invalid state - %s" % state_error
218218

219219

@@ -224,19 +224,31 @@ def validate_account_attribute(fixture_key, rpc_method, rpc, state, addr, at_blo
224224
)
225225

226226

227-
def validate_account_state(rpc, state, addr, at_block):
227+
async def validate_account_state(rpc, state, addr, at_block):
228228
standardized_state = fixture_state_in_rpc_format(state)
229229
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+
)
231238
for key in state['storage']:
232239
position = '0x0' if key == '0x' else key
233240
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+
)
235247

236248

237-
def validate_accounts(rpc, states, at_block='latest'):
249+
async def validate_accounts(rpc, states, at_block='latest'):
238250
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)
240252

241253

242254
def validate_rpc_block_vs_fixture(block, block_fixture):
@@ -264,13 +276,13 @@ def is_by_hash(at_block):
264276
raise ValueError("Unrecognized 'at_block' value: %r" % at_block)
265277

266278

267-
def validate_transaction_count(rpc, block_fixture, at_block):
279+
async def validate_transaction_count(rpc, block_fixture, at_block):
268280
if is_by_hash(at_block):
269281
rpc_method = 'eth_getBlockTransactionCountByHash'
270282
else:
271283
rpc_method = 'eth_getBlockTransactionCountByNumber'
272284
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)
274286

275287

276288
def validate_rpc_transaction_vs_fixture(transaction, fixture):
@@ -282,74 +294,74 @@ def validate_rpc_transaction_vs_fixture(transaction, fixture):
282294
assert actual_transaction == expected
283295

284296

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):
286298
if is_by_hash(at_block):
287299
rpc_method = 'eth_getTransactionByBlockHashAndIndex'
288300
else:
289301
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)])
291303
assert error is None
292304
validate_rpc_transaction_vs_fixture(result, transaction_fixture)
293305

294306

295-
def validate_block(rpc, block_fixture, at_block):
307+
async def validate_block(rpc, block_fixture, at_block):
296308
if is_by_hash(at_block):
297309
rpc_method = 'eth_getBlockByHash'
298310
else:
299311
rpc_method = 'eth_getBlockByNumber'
300312

301313
# 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])
303315
assert error is None
304316
validate_rpc_block_vs_fixture(result, block_fixture)
305317
assert len(result['transactions']) == len(block_fixture['transactions'])
306318

307319
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)
309321

310-
validate_transaction_count(rpc, block_fixture, at_block)
322+
await validate_transaction_count(rpc, block_fixture, at_block)
311323

312324
# 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])
314326
# assert error is None
315327
# assert result['transactions'] == block_fixture['transactions']
316328

317-
validate_uncles(rpc, block_fixture, at_block)
329+
await validate_uncles(rpc, block_fixture, at_block)
318330

319331

320-
def validate_last_block(rpc, block_fixture):
332+
async def validate_last_block(rpc, block_fixture):
321333
header = block_fixture['blockHeader']
322334

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))
326338

327339

328-
def validate_uncle_count(rpc, block_fixture, at_block):
340+
async def validate_uncle_count(rpc, block_fixture, at_block):
329341
if is_by_hash(at_block):
330342
rpc_method = 'eth_getUncleCountByBlockHash'
331343
else:
332344
rpc_method = 'eth_getUncleCountByBlockNumber'
333345

334346
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))
336348

337349

338-
def validate_uncle_headers(rpc, block_fixture, at_block):
350+
async def validate_uncle_headers(rpc, block_fixture, at_block):
339351
if is_by_hash(at_block):
340352
rpc_method = 'eth_getUncleByBlockHashAndIndex'
341353
else:
342354
rpc_method = 'eth_getUncleByBlockNumberAndIndex'
343355

344356
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)])
346358
assert error is None
347359
validate_rpc_block_vs_fixture_header(result, uncle)
348360

349361

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)
353365

354366

355367
@pytest.fixture
@@ -369,13 +381,14 @@ def chain(chain_without_block_validation):
369381
return
370382

371383

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):
373386
rpc = RPCServer(None)
374387

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])
376389
assert setup_error is None and setup_result is True, "cannot load chain for %r" % fixture_data
377390

378-
validate_accounts(rpc, chain_fixture['pre'])
391+
await validate_accounts(rpc, chain_fixture['pre'])
379392

380393
for block_fixture in chain_fixture['blocks']:
381394
should_be_good_block = 'blockHeader' in block_fixture
@@ -384,21 +397,21 @@ def test_rpc_against_fixtures(chain, ipc_server, chain_fixture, fixture_data):
384397
assert not should_be_good_block
385398
continue
386399

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])
388401

389402
if should_be_good_block:
390403
assert block_error is None
391404
assert block_result == block_fixture['rlp']
392405

393-
validate_block(rpc, block_fixture, block_fixture['blockHeader']['hash'])
406+
await validate_block(rpc, block_fixture, block_fixture['blockHeader']['hash'])
394407
else:
395408
assert block_error is not None
396409

397410
if chain_fixture.get('lastblockhash', None):
398411
for block_fixture in chain_fixture['blocks']:
399412
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)
401414

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)

trinity/rpc/ipc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ async def connection_loop(execute_rpc: Callable[[Any], Any],
9999
continue
100100

101101
try:
102-
result = execute_rpc(request)
102+
result = await execute_rpc(request)
103103
except Exception as e:
104104
logger.exception("Unrecognized exception while executing RPC")
105105
await cancel_token.cancellable_wait(

trinity/rpc/main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@ def _lookup_method(self, rpc_method: str) -> Any:
9999
except AttributeError:
100100
raise ValueError("Method not implemented: %r" % rpc_method)
101101

102-
def _get_result(self,
103-
request: Dict[str, Any],
104-
debug: bool=False) -> Tuple[Any, Union[Exception, str]]:
102+
async def _get_result(self,
103+
request: Dict[str, Any],
104+
debug: bool=False) -> Tuple[Any, Union[Exception, str]]:
105105
'''
106106
:returns: (result, error) - result is None if error is provided. Error must be
107107
convertable to string with ``str(error)``.
@@ -114,7 +114,7 @@ def _get_result(self,
114114

115115
method = self._lookup_method(request['method'])
116116
params = request.get('params', [])
117-
result = method(*params)
117+
result = await method(*params)
118118

119119
if request['method'] == 'evm_resetToGenesisFixture':
120120
self.chain, result = result, True
@@ -133,11 +133,11 @@ def _get_result(self,
133133
else:
134134
return result, None
135135

136-
def execute(self, request: Dict[str, Any]) -> str:
136+
async def execute(self, request: Dict[str, Any]) -> str:
137137
'''
138138
The key entry point for all incoming requests
139139
'''
140-
result, error = self._get_result(request)
140+
result, error = await self._get_result(request)
141141
return generate_response(request, result, error)
142142

143143
@property

0 commit comments

Comments
 (0)