Skip to content

Commit

Permalink
Return responses sorted by response ids in each provider case
Browse files Browse the repository at this point in the history
  • Loading branch information
fselmo committed Apr 30, 2024
1 parent f7114f4 commit 6eea4db
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 7 deletions.
9 changes: 7 additions & 2 deletions web3/providers/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,12 +218,17 @@ def make_batch_request(
timeout.sleep(0)
elif has_valid_json_rpc_ending(raw_response):
try:
response = self.decode_rpc_response(raw_response)
response = cast(
List[RPCResponse],
self.decode_rpc_response(raw_response),
)
except JSONDecodeError:
timeout.sleep(0)
continue
else:
return cast(List[RPCResponse], response)
# sort by response `id` since the JSON-RPC 2.0 spec doesn't
# guarantee order
return sorted(response, key=lambda resp: int(resp["id"]))
else:
timeout.sleep(0)
continue
Expand Down
4 changes: 3 additions & 1 deletion web3/providers/legacy_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,6 @@ def make_batch_request(
future = asyncio.run_coroutine_threadsafe(
self.coro_make_request(request_data), LegacyWebSocketProvider._loop
)
return cast(List[RPCResponse], future.result())
response = cast(List[RPCResponse], future.result())
# sort by response `id` since the JSON-RPC 2.0 spec doesn't guarantee order
return sorted(response, key=lambda resp: int(resp["id"]))
7 changes: 5 additions & 2 deletions web3/providers/persistent/async_ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,11 @@ async def make_batch_request(

# generate a cache key with all the request ids hashed
request_ids = [rpc_request["id"] for rpc_request in json.loads(request_data)]
response = await self._get_response_for_request_id(request_ids)
return cast(List[RPCResponse], response)
response = cast(
List[RPCResponse], await self._get_response_for_request_id(request_ids)
)
# sort by response `id` since the JSON-RPC 2.0 spec doesn't guarantee order
return sorted(response, key=lambda resp: int(resp["id"]))

async def _message_listener(self) -> None:
self.logger.info(
Expand Down
7 changes: 5 additions & 2 deletions web3/providers/persistent/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,11 @@ async def make_batch_request(

# generate a cache key with all the request ids hashed
request_ids = [rpc_request["id"] for rpc_request in json.loads(request_data)]
response = await self._get_response_for_request_id(request_ids)
return cast(List[RPCResponse], response)
response = cast(
List[RPCResponse], await self._get_response_for_request_id(request_ids)
)
# sort by response `id` since the JSON-RPC 2.0 spec doesn't guarantee order
return sorted(response, key=lambda resp: int(resp["id"]))

async def _message_listener(self) -> None:
self.logger.info(
Expand Down

0 comments on commit 6eea4db

Please sign in to comment.