Skip to content

Commit 92782c1

Browse files
committed
linting and formatting
1 parent 2d40774 commit 92782c1

File tree

5 files changed

+32
-49
lines changed

5 files changed

+32
-49
lines changed

chainbench/profile/solana/get_block.py

Lines changed: 0 additions & 22 deletions
This file was deleted.

chainbench/user/jsonrpc.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,10 @@ def make_rpc_call(
9898
path: str = "",
9999
) -> None:
100100
"""Make a JSON-RPC call."""
101-
if rpc_call is None:
101+
if rpc_call is None and method is not None:
102102
rpc_call = RpcCall(method, params)
103+
else:
104+
raise ValueError("Either rpc_call or method must be provided")
103105

104106
if name == "" and method is not None:
105107
name = method

chainbench/user/protocol/evm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
class EvmBaseUser:
1818
abstract = True
19-
test_data = EvmTestData()
19+
test_data: EvmTestData = EvmTestData()
2020
rng = RNGManager()
2121

2222
_default_trace_timeout = "120s"

chainbench/user/wss.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,15 @@
55
import orjson as json
66
from gevent import Greenlet, Timeout
77
from locust import User, task
8+
from locust.env import Environment
89
from orjson import JSONDecodeError
910
from websocket import WebSocket, WebSocketConnectionClosedException, create_connection
11+
1012
from chainbench.util.jsonrpc import RpcCall
1113

1214

1315
class WSSubscription:
14-
def __init__(
15-
self,
16-
subscribe_method: str,
17-
subscribe_params: dict | list,
18-
unsubscribe_method: str
19-
):
16+
def __init__(self, subscribe_method: str, subscribe_params: dict | list, unsubscribe_method: str):
2017
self.subscribe_rpc_call: RpcCall = RpcCall(subscribe_method, subscribe_params)
2118
self.unsubscribe_method: str = unsubscribe_method
2219
self.subscribed: bool = False
@@ -38,7 +35,7 @@ def subscription_id(self):
3835

3936

4037
class WSRequest:
41-
def __init__(self, rpc_call: RpcCall, start_time: int, subscription_index: int = None):
38+
def __init__(self, rpc_call: RpcCall, start_time: int, subscription_index: int | None = None):
4239
self.rpc_call = rpc_call
4340
self.start_time = start_time
4441
self.subscription_index = subscription_index
@@ -52,7 +49,7 @@ class WssJrpcUser(User):
5249
subscriptions: list[WSSubscription] = []
5350
subscription_ids_to_index: dict[str | int, int] = {}
5451

55-
def __init__(self, environment):
52+
def __init__(self, environment: Environment):
5653
super().__init__(environment)
5754
self._ws: WebSocket | None = None
5855
self._ws_greenlet: Greenlet | None = None
@@ -108,7 +105,7 @@ def get_notification_name(self, parsed_response: dict):
108105
# Override this method to return the name of the notification if this is not correct
109106
return parsed_response["method"]
110107

111-
def on_message(self, message):
108+
def on_message(self, message: str | bytes):
112109
try:
113110
parsed_json: dict = json.loads(message)
114111
if "error" in parsed_json:
@@ -181,15 +178,24 @@ def receive_loop(self):
181178
self.logger.error("Connection closed by server, trying to reconnect...")
182179
self.on_start()
183180

184-
def send(self, rpc_call: RpcCall = None, method: str = None, params: dict | list = None, subscription_index: int = None):
181+
def send(
182+
self,
183+
rpc_call: RpcCall | None = None,
184+
method: str | None = None,
185+
params: dict | list | None = None,
186+
subscription_index: int | None = None,
187+
):
188+
def _get_args():
189+
if rpc_call:
190+
return rpc_call
191+
elif method:
192+
return RpcCall(method, params)
193+
else:
194+
raise ValueError("Either rpc_call or method must be provided")
195+
196+
rpc_call = _get_args()
185197
self.logger.debug(f"Sending: {rpc_call or method}")
186-
rpc = {
187-
(None, None): None,
188-
(None, method): RpcCall(method, params),
189-
(rpc_call, None): rpc_call,
190-
}
191198

192-
rpc_call = rpc[(rpc_call, method)]
193199
if rpc_call is None:
194200
raise ValueError("Either rpc_call or method must be provided")
195201

@@ -202,4 +208,5 @@ def send(self, rpc_call: RpcCall = None, method: str = None, params: dict | list
202208
)
203209
json_body = json.dumps(rpc_call.request_body())
204210
self.logger.debug(f"WSReq: {json_body}")
205-
self._ws.send(json_body)
211+
if self._ws:
212+
self._ws.send(json_body)

chainbench/util/jsonrpc.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import random
22
import typing as t
3+
34
import orjson as json
45

56

67
class RpcCall:
7-
def __init__(self, method: str, params: list[t.Any] | dict | None = None, request_id: int = None) -> None:
8+
def __init__(self, method: str, params: list[t.Any] | dict | None = None, request_id: int | None = None) -> None:
89
self._request_id = request_id
910
self.method = method
1011
self.params = params
@@ -15,7 +16,7 @@ def request_id(self) -> int:
1516
self._request_id = random.Random().randint(1, 100000000)
1617
return self._request_id
1718

18-
def request_body(self, request_id: int = None) -> dict:
19+
def request_body(self, request_id: int | None = None) -> dict:
1920
"""Generate a JSON-RPC request body."""
2021
if self.params is None:
2122
self.params = []
@@ -36,12 +37,7 @@ def request_body(self, request_id: int = None) -> dict:
3637

3738
def generate_batch_request_body(rpc_calls: list[RpcCall]) -> str:
3839
"""Generate a batch JSON-RPC request body."""
39-
return json.dumps(
40-
[
41-
rpc_calls[i].request_body(i)
42-
for i in range(1, len(rpc_calls))
43-
]
44-
).decode("utf-8")
40+
return json.dumps([rpc_calls[i].request_body(i) for i in range(1, len(rpc_calls))]).decode("utf-8")
4541

4642

4743
def expand_rpc_calls(rpc_calls_weighted: dict[t.Callable[[], RpcCall], int]) -> list[RpcCall]:

0 commit comments

Comments
 (0)