web3 7.0.0b7__py3-none-any.whl → 7.0.0b8__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -187,10 +187,28 @@ def apply_list_to_array_formatter(formatter: Any) -> Callable[..., Any]:
187
187
  return to_list(apply_formatter_to_array(formatter))
188
188
 
189
189
 
190
+ def storage_key_to_hexstr(value: Union[bytes, int, str]) -> HexStr:
191
+ if not isinstance(value, (bytes, int, str)):
192
+ raise Web3ValueError(
193
+ f"Storage key must be one of bytes, int, str, got {type(value)}"
194
+ )
195
+ if isinstance(value, str):
196
+ if value.startswith("0x") and len(value) == 66:
197
+ return HexStr(value)
198
+ elif len(value) == 64:
199
+ return HexStr(f"0x{value}")
200
+ elif isinstance(value, bytes):
201
+ if len(value) == 32:
202
+ return HexBytes(value).to_0x_hex()
203
+ elif isinstance(value, int):
204
+ return storage_key_to_hexstr(hex(value))
205
+ raise Web3ValueError(f"Storage key must be a 32-byte value, got {value!r}")
206
+
207
+
190
208
  ACCESS_LIST_FORMATTER = type_aware_apply_formatters_to_dict(
191
209
  {
192
210
  "address": to_checksum_address,
193
- "storageKeys": apply_list_to_array_formatter(to_hexbytes(64)),
211
+ "storageKeys": apply_list_to_array_formatter(storage_key_to_hexstr),
194
212
  }
195
213
  )
196
214
 
@@ -1210,7 +1210,7 @@ class AsyncEthModuleTest:
1210
1210
  assert len(access_list) > 0
1211
1211
  assert access_list[0]["address"] is not None
1212
1212
  assert is_checksum_address(access_list[0]["address"])
1213
- assert len(access_list[0]["storageKeys"][0]) == 32
1213
+ assert len(access_list[0]["storageKeys"][0]) == 66
1214
1214
  assert int(response["gasUsed"]) >= 0
1215
1215
 
1216
1216
  # assert the result can be used directly in a transaction dict
@@ -2786,7 +2786,7 @@ class EthModuleTest:
2786
2786
  assert len(access_list) > 0
2787
2787
  assert access_list[0]["address"] is not None
2788
2788
  assert is_checksum_address(access_list[0]["address"])
2789
- assert len(access_list[0]["storageKeys"][0]) == 32
2789
+ assert len(access_list[0]["storageKeys"][0]) == 66
2790
2790
  assert int(response["gasUsed"]) >= 0
2791
2791
 
2792
2792
  # assert the result can be used directly in a transaction dict
@@ -1,6 +1,4 @@
1
- from collections import (
2
- deque,
3
- )
1
+ import asyncio
4
2
  from typing import (
5
3
  TYPE_CHECKING,
6
4
  Any,
@@ -179,7 +177,9 @@ class WebSocketMessageStreamMock:
179
177
  def __init__(
180
178
  self, messages: Collection[bytes] = None, raise_exception: Exception = None
181
179
  ) -> None:
182
- self.messages = deque(messages) if messages else deque()
180
+ self.queue = asyncio.Queue() # type: ignore # py38 issue
181
+ for msg in messages or []:
182
+ self.queue.put_nowait(msg)
183
183
  self.raise_exception = raise_exception
184
184
 
185
185
  def __await__(self) -> Generator[Any, Any, "Self"]:
@@ -192,13 +192,12 @@ class WebSocketMessageStreamMock:
192
192
  return self
193
193
 
194
194
  async def __anext__(self) -> bytes:
195
+ return await self.queue.get()
196
+
197
+ async def recv(self) -> bytes:
195
198
  if self.raise_exception:
196
199
  raise self.raise_exception
197
-
198
- elif len(self.messages) == 0:
199
- raise StopAsyncIteration
200
-
201
- return self.messages.popleft()
200
+ return await self.queue.get()
202
201
 
203
202
  @staticmethod
204
203
  async def pong() -> Literal[False]:
@@ -23,6 +23,7 @@ from web3.middleware import (
23
23
  )
24
24
  from web3.types import (
25
25
  FormattedEthSubscriptionResponse,
26
+ RPCEndpoint,
26
27
  )
27
28
 
28
29
  if TYPE_CHECKING:
@@ -31,6 +32,22 @@ if TYPE_CHECKING:
31
32
  )
32
33
 
33
34
 
35
+ SOME_BLOCK_KEYS = [
36
+ "number",
37
+ "hash",
38
+ "parentHash",
39
+ "transactionsRoot",
40
+ "stateRoot",
41
+ "receiptsRoot",
42
+ "size",
43
+ "gasLimit",
44
+ "gasUsed",
45
+ "timestamp",
46
+ "transactions",
47
+ "baseFeePerGas",
48
+ ]
49
+
50
+
34
51
  class PersistentConnectionProviderTest:
35
52
  @pytest.mark.asyncio
36
53
  @pytest.mark.parametrize(
@@ -372,22 +389,8 @@ class PersistentConnectionProviderTest:
372
389
  assert isinstance(pending, AttributeDict)
373
390
 
374
391
  # assert block values
375
- some_block_keys = [
376
- "number",
377
- "hash",
378
- "parentHash",
379
- "transactionsRoot",
380
- "stateRoot",
381
- "receiptsRoot",
382
- "size",
383
- "gasLimit",
384
- "gasUsed",
385
- "timestamp",
386
- "transactions",
387
- "baseFeePerGas",
388
- ]
389
- assert all(k in latest.keys() for k in some_block_keys)
390
- assert all(k in pending.keys() for k in some_block_keys)
392
+ assert all(k in latest.keys() for k in SOME_BLOCK_KEYS)
393
+ assert all(k in pending.keys() for k in SOME_BLOCK_KEYS)
391
394
 
392
395
  assert isinstance(block_num, int)
393
396
  assert latest["number"] == block_num
@@ -395,3 +398,28 @@ class PersistentConnectionProviderTest:
395
398
  assert isinstance(chain_id, int)
396
399
  assert isinstance(chain_id2, int)
397
400
  assert isinstance(chain_id3, int)
401
+
402
+ @pytest.mark.asyncio
403
+ async def test_public_socket_api(self, async_w3: "AsyncWeb3") -> None:
404
+ # send a request over the socket
405
+ await async_w3.socket.send(
406
+ RPCEndpoint("eth_getBlockByNumber"), ["latest", True]
407
+ )
408
+
409
+ # recv and validate the unprocessed response
410
+ response = await async_w3.socket.recv()
411
+ assert "id" in response, "Expected 'id' key in response."
412
+ assert "jsonrpc" in response, "Expected 'jsonrpc' key in response."
413
+ assert "result" in response, "Expected 'result' key in response."
414
+ assert all(k in response["result"].keys() for k in SOME_BLOCK_KEYS)
415
+ assert not isinstance(response["result"]["number"], int) # assert not processed
416
+
417
+ # make a request over the socket
418
+ response = await async_w3.socket.make_request(
419
+ RPCEndpoint("eth_getBlockByNumber"), ["latest", True]
420
+ )
421
+ assert "id" in response, "Expected 'id' key in response."
422
+ assert "jsonrpc" in response, "Expected 'jsonrpc' key in response."
423
+ assert "result" in response, "Expected 'result' key in response."
424
+ assert all(k in response["result"].keys() for k in SOME_BLOCK_KEYS)
425
+ assert not isinstance(response["result"]["number"], int) # assert not processed
web3/exceptions.py CHANGED
@@ -327,6 +327,12 @@ class TaskNotRunning(Web3Exception):
327
327
  super().__init__(message)
328
328
 
329
329
 
330
+ class PersistentConnectionClosedOK(Web3Exception):
331
+ """
332
+ Raised when a persistent connection is closed gracefully by the server.
333
+ """
334
+
335
+
330
336
  class Web3RPCError(Web3Exception):
331
337
  """
332
338
  Raised when a JSON-RPC response contains an error field.
web3/manager.py CHANGED
@@ -20,9 +20,6 @@ from eth_utils.toolz import (
20
20
  from hexbytes import (
21
21
  HexBytes,
22
22
  )
23
- from websockets.exceptions import (
24
- ConnectionClosedOK,
25
- )
26
23
 
27
24
  from web3._utils.batching import (
28
25
  RequestBatcher,
@@ -472,22 +469,50 @@ class RequestManager:
472
469
 
473
470
  # -- persistent connection -- #
474
471
 
475
- async def send(self, method: RPCEndpoint, params: Any) -> RPCResponse:
472
+ async def socket_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
476
473
  provider = cast(PersistentConnectionProvider, self._provider)
477
474
  request_func = await provider.request_func(
478
475
  cast("AsyncWeb3", self.w3), cast("MiddlewareOnion", self.middleware_onion)
479
476
  )
480
477
  self.logger.debug(
481
- "Making request to open socket connection: "
478
+ "Making request to open socket connection and waiting for response: "
482
479
  f"{provider.get_endpoint_uri_or_ipc_path()}, method: {method}"
483
480
  )
484
481
  response = await request_func(method, params)
485
482
  return await self._process_response(response)
486
483
 
484
+ async def send(self, method: RPCEndpoint, params: Any) -> None:
485
+ provider = cast(PersistentConnectionProvider, self._provider)
486
+ # run through the request processors of the middleware
487
+ for mw_class in self.middleware_onion.as_tuple_of_middleware():
488
+ mw = mw_class(self.w3)
489
+ method, params = mw.request_processor(method, params)
490
+
491
+ self.logger.debug(
492
+ "Sending request to open socket connection: "
493
+ f"{provider.get_endpoint_uri_or_ipc_path()}, method: {method}"
494
+ )
495
+ await provider.socket_send(provider.encode_rpc_request(method, params))
496
+
497
+ async def recv(self) -> RPCResponse:
498
+ provider = cast(PersistentConnectionProvider, self._provider)
499
+ self.logger.debug(
500
+ "Getting next response from open socket connection: "
501
+ f"{provider.get_endpoint_uri_or_ipc_path()}"
502
+ )
503
+ # pop from the queue since the listener task is responsible for reading
504
+ # directly from the socket
505
+ request_response_cache = self._request_processor._request_response_cache
506
+ _key, response = await request_response_cache.async_await_and_popitem(
507
+ last=False,
508
+ timeout=provider.request_timeout,
509
+ )
510
+ return await self._process_response(response)
511
+
487
512
  def _persistent_message_stream(self) -> "_AsyncPersistentMessageStream":
488
513
  return _AsyncPersistentMessageStream(self)
489
514
 
490
- async def _get_next_message(self) -> Any:
515
+ async def _get_next_message(self) -> RPCResponse:
491
516
  return await self._message_stream().__anext__()
492
517
 
493
518
  async def _message_stream(self) -> AsyncGenerator[RPCResponse, None]:
@@ -515,12 +540,13 @@ class RequestManager:
515
540
  # if response is an active subscription response, process it
516
541
  yield await self._process_response(response)
517
542
  except TaskNotRunning:
543
+ await asyncio.sleep(0)
518
544
  self._provider._handle_listener_task_exceptions()
519
545
  self.logger.error(
520
546
  "Message listener background task has stopped unexpectedly. "
521
547
  "Stopping message stream."
522
548
  )
523
- raise StopAsyncIteration
549
+ return
524
550
 
525
551
  async def _process_response(self, response: RPCResponse) -> RPCResponse:
526
552
  provider = cast(PersistentConnectionProvider, self._provider)
@@ -586,7 +612,4 @@ class _AsyncPersistentMessageStream:
586
612
  return self
587
613
 
588
614
  async def __anext__(self) -> RPCResponse:
589
- try:
590
- return await self.manager._get_next_message()
591
- except ConnectionClosedOK:
592
- raise StopAsyncIteration
615
+ return await self.manager._get_next_message()
web3/module.py CHANGED
@@ -138,7 +138,7 @@ def retrieve_async_method_call_fn(
138
138
 
139
139
  try:
140
140
  method_str = cast(RPCEndpoint, method_str)
141
- return await async_w3.manager.send(method_str, params)
141
+ return await async_w3.manager.socket_request(method_str, params)
142
142
  except Exception as e:
143
143
  if (
144
144
  cache_key is not None
@@ -11,11 +11,9 @@ from pathlib import (
11
11
  import sys
12
12
  from typing import (
13
13
  Any,
14
- List,
15
14
  Optional,
16
15
  Tuple,
17
16
  Union,
18
- cast,
19
17
  )
20
18
 
21
19
  from eth_utils import (
@@ -23,20 +21,12 @@ from eth_utils import (
23
21
  )
24
22
 
25
23
  from web3.types import (
26
- RPCEndpoint,
27
24
  RPCResponse,
28
25
  )
29
26
 
30
27
  from . import (
31
28
  PersistentConnectionProvider,
32
29
  )
33
- from ..._utils.batching import (
34
- BATCH_REQUEST_ID,
35
- sort_batch_response_by_response_ids,
36
- )
37
- from ..._utils.caching import (
38
- async_handle_request_caching,
39
- )
40
30
  from ...exceptions import (
41
31
  ProviderConnectionError,
42
32
  Web3TypeError,
@@ -91,12 +81,7 @@ class AsyncIPCProvider(PersistentConnectionProvider):
91
81
  return False
92
82
 
93
83
  try:
94
- request_data = self.encode_rpc_request(
95
- RPCEndpoint("web3_clientVersions"), []
96
- )
97
- self._writer.write(request_data)
98
- current_request_id = json.loads(request_data)["id"]
99
- await self._get_response_for_request_id(current_request_id, timeout=2)
84
+ await self.make_request("web3_clientVersion", [])
100
85
  return True
101
86
  except (OSError, ProviderConnectionError) as e:
102
87
  if show_traceback:
@@ -105,55 +90,33 @@ class AsyncIPCProvider(PersistentConnectionProvider):
105
90
  )
106
91
  return False
107
92
 
108
- async def _provider_specific_connect(self) -> None:
109
- self._reader, self._writer = await async_get_ipc_socket(self.ipc_path)
110
-
111
- async def _provider_specific_disconnect(self) -> None:
112
- if self._writer and not self._writer.is_closing():
113
- self._writer.close()
114
- await self._writer.wait_closed()
115
- self._writer = None
116
- if self._reader:
117
- self._reader = None
118
-
119
- async def _reset_socket(self) -> None:
120
- self._writer.close()
121
- await self._writer.wait_closed()
122
- self._reader, self._writer = await async_get_ipc_socket(self.ipc_path)
123
-
124
- @async_handle_request_caching
125
- async def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
93
+ async def socket_send(self, request_data: bytes) -> None:
126
94
  if self._writer is None:
127
95
  raise ProviderConnectionError(
128
96
  "Connection to ipc socket has not been initiated for the provider."
129
97
  )
130
98
 
131
- request_data = self.encode_rpc_request(method, params)
132
- try:
133
- self._writer.write(request_data)
134
- await self._writer.drain()
135
- except OSError as e:
136
- # Broken pipe
137
- if e.errno == errno.EPIPE:
138
- # one extra attempt, then give up
139
- await self._reset_socket()
140
- self._writer.write(request_data)
141
- await self._writer.drain()
99
+ return await asyncio.wait_for(
100
+ self._socket_send(request_data), timeout=self.request_timeout
101
+ )
142
102
 
143
- current_request_id = json.loads(request_data)["id"]
144
- response = await self._get_response_for_request_id(current_request_id)
103
+ async def socket_recv(self) -> RPCResponse:
104
+ while True:
105
+ # yield to the event loop to allow other tasks to run
106
+ await asyncio.sleep(0)
145
107
 
146
- return response
108
+ try:
109
+ response, pos = self._decoder.raw_decode(self._raw_message)
110
+ self._raw_message = self._raw_message[pos:].lstrip()
111
+ return response
112
+ except JSONDecodeError:
113
+ # read more data from the socket if the current raw message is
114
+ # incomplete
115
+ self._raw_message += to_text(await self._reader.read(4096)).lstrip()
147
116
 
148
- async def make_batch_request(
149
- self, requests: List[Tuple[RPCEndpoint, Any]]
150
- ) -> List[RPCResponse]:
151
- if self._writer is None:
152
- raise ProviderConnectionError(
153
- "Connection to ipc socket has not been initiated for the provider."
154
- )
117
+ # -- private methods -- #
155
118
 
156
- request_data = self.encode_batch_rpc_request(requests)
119
+ async def _socket_send(self, request_data: bytes) -> None:
157
120
  try:
158
121
  self._writer.write(request_data)
159
122
  await self._writer.drain()
@@ -165,32 +128,24 @@ class AsyncIPCProvider(PersistentConnectionProvider):
165
128
  self._writer.write(request_data)
166
129
  await self._writer.drain()
167
130
 
168
- response = cast(
169
- List[RPCResponse], await self._get_response_for_request_id(BATCH_REQUEST_ID)
170
- )
171
- return response
172
-
173
- async def _provider_specific_message_listener(self) -> None:
174
- self._raw_message += to_text(await self._reader.read(4096)).lstrip()
131
+ async def _reset_socket(self) -> None:
132
+ self._writer.close()
133
+ await self._writer.wait_closed()
134
+ self._reader, self._writer = await async_get_ipc_socket(self.ipc_path)
175
135
 
176
- while self._raw_message:
177
- try:
178
- response, pos = self._decoder.raw_decode(self._raw_message)
179
- except JSONDecodeError:
180
- break
136
+ async def _provider_specific_connect(self) -> None:
137
+ self._reader, self._writer = await async_get_ipc_socket(self.ipc_path)
181
138
 
182
- if isinstance(response, list):
183
- response = sort_batch_response_by_response_ids(response)
139
+ async def _provider_specific_disconnect(self) -> None:
140
+ if self._writer and not self._writer.is_closing():
141
+ self._writer.close()
142
+ await self._writer.wait_closed()
143
+ self._writer = None
144
+ if self._reader:
145
+ self._reader = None
184
146
 
185
- is_subscription = (
186
- response.get("method") == "eth_subscription"
187
- if not isinstance(response, list)
188
- else False
189
- )
190
- await self._request_processor.cache_raw_response(
191
- response, subscription=is_subscription
192
- )
193
- self._raw_message = self._raw_message[pos:].lstrip()
147
+ async def _provider_specific_socket_reader(self) -> RPCResponse:
148
+ return await self.socket_recv()
194
149
 
195
150
  def _error_log_listener_task_exception(self, e: Exception) -> None:
196
151
  super()._error_log_listener_task_exception(e)
@@ -1,13 +1,17 @@
1
1
  from abc import (
2
2
  ABC,
3
+ abstractmethod,
3
4
  )
4
5
  import asyncio
6
+ import json
5
7
  import logging
6
8
  from typing import (
7
9
  Any,
8
10
  List,
9
11
  Optional,
12
+ Tuple,
10
13
  Union,
14
+ cast,
11
15
  )
12
16
 
13
17
  from websockets import (
@@ -15,13 +19,20 @@ from websockets import (
15
19
  WebSocketException,
16
20
  )
17
21
 
22
+ from web3._utils.batching import (
23
+ BATCH_REQUEST_ID,
24
+ sort_batch_response_by_response_ids,
25
+ )
18
26
  from web3._utils.caching import (
27
+ async_handle_request_caching,
19
28
  generate_cache_key,
20
29
  )
21
30
  from web3.exceptions import (
31
+ PersistentConnectionClosedOK,
22
32
  ProviderConnectionError,
23
33
  TaskNotRunning,
24
34
  TimeExhausted,
35
+ Web3AttributeError,
25
36
  )
26
37
  from web3.providers.async_base import (
27
38
  AsyncJSONBaseProvider,
@@ -30,6 +41,7 @@ from web3.providers.persistent.request_processor import (
30
41
  RequestProcessor,
31
42
  )
32
43
  from web3.types import (
44
+ RPCEndpoint,
33
45
  RPCId,
34
46
  RPCResponse,
35
47
  )
@@ -70,7 +82,7 @@ class PersistentConnectionProvider(AsyncJSONBaseProvider, ABC):
70
82
  elif hasattr(self, "ipc_path"):
71
83
  return str(self.ipc_path)
72
84
  else:
73
- raise AttributeError(
85
+ raise Web3AttributeError(
74
86
  "`PersistentConnectionProvider` must have either `endpoint_uri` or "
75
87
  "`ipc_path` attribute."
76
88
  )
@@ -128,6 +140,44 @@ class PersistentConnectionProvider(AsyncJSONBaseProvider, ABC):
128
140
  f"Successfully disconnected from: {self.get_endpoint_uri_or_ipc_path()}"
129
141
  )
130
142
 
143
+ @async_handle_request_caching
144
+ async def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
145
+ request_data = self.encode_rpc_request(method, params)
146
+ await self.socket_send(request_data)
147
+
148
+ current_request_id = json.loads(request_data)["id"]
149
+ response = await self._get_response_for_request_id(current_request_id)
150
+
151
+ return response
152
+
153
+ async def make_batch_request(
154
+ self, requests: List[Tuple[RPCEndpoint, Any]]
155
+ ) -> List[RPCResponse]:
156
+ request_data = self.encode_batch_rpc_request(requests)
157
+ await self.socket_send(request_data)
158
+
159
+ response = cast(
160
+ List[RPCResponse], await self._get_response_for_request_id(BATCH_REQUEST_ID)
161
+ )
162
+ return response
163
+
164
+ # -- abstract methods -- #
165
+
166
+ @abstractmethod
167
+ async def socket_send(self, request_data: bytes) -> None:
168
+ """
169
+ Send an encoded RPC request to the provider over the persistent connection.
170
+ """
171
+ raise NotImplementedError("Must be implemented by subclasses")
172
+
173
+ @abstractmethod
174
+ async def socket_recv(self) -> RPCResponse:
175
+ """
176
+ Receive, decode, and return an RPC response from the provider over the
177
+ persistent connection.
178
+ """
179
+ raise NotImplementedError("Must be implemented by subclasses")
180
+
131
181
  # -- private methods -- #
132
182
 
133
183
  async def _provider_specific_connect(self) -> None:
@@ -136,7 +186,7 @@ class PersistentConnectionProvider(AsyncJSONBaseProvider, ABC):
136
186
  async def _provider_specific_disconnect(self) -> None:
137
187
  raise NotImplementedError("Must be implemented by subclasses")
138
188
 
139
- async def _provider_specific_message_listener(self) -> None:
189
+ async def _provider_specific_socket_reader(self) -> RPCResponse:
140
190
  raise NotImplementedError("Must be implemented by subclasses")
141
191
 
142
192
  def _message_listener_callback(
@@ -158,8 +208,28 @@ class PersistentConnectionProvider(AsyncJSONBaseProvider, ABC):
158
208
  # the use of sleep(0) seems to be the most efficient way to yield control
159
209
  # back to the event loop to share the loop with other tasks.
160
210
  await asyncio.sleep(0)
211
+
161
212
  try:
162
- await self._provider_specific_message_listener()
213
+ response = await self._provider_specific_socket_reader()
214
+
215
+ if isinstance(response, list):
216
+ response = sort_batch_response_by_response_ids(response)
217
+
218
+ subscription = (
219
+ response.get("method") == "eth_subscription"
220
+ if not isinstance(response, list)
221
+ else False
222
+ )
223
+ await self._request_processor.cache_raw_response(
224
+ response, subscription=subscription
225
+ )
226
+ except PersistentConnectionClosedOK as e:
227
+ self.logger.info(
228
+ "Message listener background task has ended gracefully: "
229
+ f"{e.user_message}"
230
+ )
231
+ # trigger a return to end the listener task and initiate the callback fn
232
+ return
163
233
  except Exception as e:
164
234
  if not self.silence_listener_task_exceptions:
165
235
  raise e
@@ -202,10 +272,6 @@ class PersistentConnectionProvider(AsyncJSONBaseProvider, ABC):
202
272
  request_cache_key = generate_cache_key(request_id)
203
273
 
204
274
  while True:
205
- # check if an exception was recorded in the listener task and raise it
206
- # in the main loop if so
207
- self._handle_listener_task_exceptions()
208
-
209
275
  if request_cache_key in self._request_processor._request_response_cache:
210
276
  self.logger.debug(
211
277
  f"Popping response for id {request_id} from cache."
@@ -215,6 +281,9 @@ class PersistentConnectionProvider(AsyncJSONBaseProvider, ABC):
215
281
  )
216
282
  return popped_response
217
283
  else:
284
+ # check if an exception was recorded in the listener task and raise
285
+ # it in the main loop if so
286
+ self._handle_listener_task_exceptions()
218
287
  await asyncio.sleep(0)
219
288
 
220
289
  try:
@@ -27,16 +27,58 @@ class PersistentConnection:
27
27
  def __init__(self, w3: "AsyncWeb3"):
28
28
  self._manager = w3.manager
29
29
 
30
- # -- public methods -- #
31
30
  @property
32
31
  def subscriptions(self) -> Dict[str, Any]:
32
+ """
33
+ Return the active subscriptions on the persistent connection.
34
+
35
+ :return: The active subscriptions on the persistent connection.
36
+ :rtype: Dict[str, Any]
37
+ """
33
38
  return self._manager._request_processor.active_subscriptions
34
39
 
35
- async def send(self, method: RPCEndpoint, params: Any) -> RPCResponse:
36
- return await self._manager.send(method, params)
40
+ async def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
41
+ """
42
+ Make a request to the persistent connection and return the response. This method
43
+ does not process the response as it would when invoking a method via the
44
+ appropriate module on the `AsyncWeb3` instance,
45
+ e.g. `w3.eth.get_block("latest")`.
46
+
47
+ :param method: The RPC method, e.g. `eth_getBlockByNumber`.
48
+ :param params: The RPC method parameters, e.g. `["0x1337", False]`.
49
+
50
+ :return: The processed response from the persistent connection.
51
+ :rtype: RPCResponse
52
+ """
53
+ return await self._manager.socket_request(method, params)
54
+
55
+ async def send(self, method: RPCEndpoint, params: Any) -> None:
56
+ """
57
+ Send a raw, unprocessed message to the persistent connection.
37
58
 
38
- async def recv(self) -> Any:
39
- return await self._manager._get_next_message()
59
+ :param method: The RPC method, e.g. `eth_getBlockByNumber`.
60
+ :param params: The RPC method parameters, e.g. `["0x1337", False]`.
61
+
62
+ :return: None
63
+ """
64
+ await self._manager.send(method, params)
65
+
66
+ async def recv(self) -> RPCResponse:
67
+ """
68
+ Receive the next unprocessed response for a request from the persistent
69
+ connection.
70
+
71
+ :return: The next unprocessed response for a request from the persistent
72
+ connection.
73
+ :rtype: RPCResponse
74
+ """
75
+ return await self._manager.recv()
40
76
 
41
77
  def process_subscriptions(self) -> "_AsyncPersistentMessageStream":
78
+ """
79
+ Asynchronous iterator that yields messages from the subscription message stream.
80
+
81
+ :return: The subscription message stream.
82
+ :rtype: _AsyncPersistentMessageStream
83
+ """
42
84
  return self._manager._persistent_message_stream()
@@ -5,11 +5,8 @@ import os
5
5
  from typing import (
6
6
  Any,
7
7
  Dict,
8
- List,
9
8
  Optional,
10
- Tuple,
11
9
  Union,
12
- cast,
13
10
  )
14
11
 
15
12
  from eth_typing import (
@@ -25,17 +22,12 @@ from websockets.client import (
25
22
  connect,
26
23
  )
27
24
  from websockets.exceptions import (
25
+ ConnectionClosedOK,
28
26
  WebSocketException,
29
27
  )
30
28
 
31
- from web3._utils.batching import (
32
- BATCH_REQUEST_ID,
33
- sort_batch_response_by_response_ids,
34
- )
35
- from web3._utils.caching import (
36
- async_handle_request_caching,
37
- )
38
29
  from web3.exceptions import (
30
+ PersistentConnectionClosedOK,
39
31
  ProviderConnectionError,
40
32
  Web3ValidationError,
41
33
  )
@@ -43,7 +35,6 @@ from web3.providers.persistent import (
43
35
  PersistentConnectionProvider,
44
36
  )
45
37
  from web3.types import (
46
- RPCEndpoint,
47
38
  RPCResponse,
48
39
  )
49
40
 
@@ -122,18 +113,7 @@ class WebSocketProvider(PersistentConnectionProvider):
122
113
  ) from e
123
114
  return False
124
115
 
125
- async def _provider_specific_connect(self) -> None:
126
- self._ws = await connect(self.endpoint_uri, **self.websocket_kwargs)
127
-
128
- async def _provider_specific_disconnect(self) -> None:
129
- if self._ws is not None and not self._ws.closed:
130
- await self._ws.close()
131
- self._ws = None
132
-
133
- @async_handle_request_caching
134
- async def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
135
- request_data = self.encode_rpc_request(method, params)
136
-
116
+ async def socket_send(self, request_data: bytes) -> None:
137
117
  if self._ws is None:
138
118
  raise ProviderConnectionError(
139
119
  "Connection to websocket has not been initiated for the provider."
@@ -143,44 +123,24 @@ class WebSocketProvider(PersistentConnectionProvider):
143
123
  self._ws.send(request_data), timeout=self.request_timeout
144
124
  )
145
125
 
146
- current_request_id = json.loads(request_data)["id"]
147
- response = await self._get_response_for_request_id(current_request_id)
148
-
149
- return response
126
+ async def socket_recv(self) -> RPCResponse:
127
+ raw_response = await self._ws.recv()
128
+ return json.loads(raw_response)
150
129
 
151
- async def make_batch_request(
152
- self, requests: List[Tuple[RPCEndpoint, Any]]
153
- ) -> List[RPCResponse]:
154
- request_data = self.encode_batch_rpc_request(requests)
130
+ # -- private methods -- #
155
131
 
156
- if self._ws is None:
157
- raise ProviderConnectionError(
158
- "Connection to websocket has not been initiated for the provider."
159
- )
160
-
161
- await asyncio.wait_for(
162
- self._ws.send(request_data), timeout=self.request_timeout
163
- )
164
-
165
- response = cast(
166
- List[RPCResponse],
167
- await self._get_response_for_request_id(BATCH_REQUEST_ID),
168
- )
169
- return response
170
-
171
- async def _provider_specific_message_listener(self) -> None:
172
- async for raw_message in self._ws:
173
- await asyncio.sleep(0)
132
+ async def _provider_specific_connect(self) -> None:
133
+ self._ws = await connect(self.endpoint_uri, **self.websocket_kwargs)
174
134
 
175
- response = json.loads(raw_message)
176
- if isinstance(response, list):
177
- response = sort_batch_response_by_response_ids(response)
135
+ async def _provider_specific_disconnect(self) -> None:
136
+ if self._ws is not None and not self._ws.closed:
137
+ await self._ws.close()
138
+ self._ws = None
178
139
 
179
- subscription = (
180
- response.get("method") == "eth_subscription"
181
- if not isinstance(response, list)
182
- else False
183
- )
184
- await self._request_processor.cache_raw_response(
185
- response, subscription=subscription
140
+ async def _provider_specific_socket_reader(self) -> RPCResponse:
141
+ try:
142
+ return await self.socket_recv()
143
+ except ConnectionClosedOK:
144
+ raise PersistentConnectionClosedOK(
145
+ user_message="WebSocket connection received `ConnectionClosedOK`."
186
146
  )
web3/utils/caching.py CHANGED
@@ -1,6 +1,8 @@
1
+ import asyncio
1
2
  from collections import (
2
3
  OrderedDict,
3
4
  )
5
+ import time
4
6
  from typing import (
5
7
  Any,
6
8
  Dict,
@@ -46,8 +48,30 @@ class SimpleCache:
46
48
 
47
49
  return self._data.pop(key)
48
50
 
51
+ def popitem(self, last: bool = True) -> Tuple[str, Any]:
52
+ return self._data.popitem(last=last)
53
+
49
54
  def __contains__(self, key: str) -> bool:
50
55
  return key in self._data
51
56
 
52
57
  def __len__(self) -> int:
53
58
  return len(self._data)
59
+
60
+ # -- async utility methods -- #
61
+
62
+ async def async_await_and_popitem(
63
+ self, last: bool = True, timeout: float = 10.0
64
+ ) -> Tuple[str, Any]:
65
+ start = time.time()
66
+ end_time = start + timeout
67
+ while True:
68
+ await asyncio.sleep(0)
69
+ try:
70
+ return self.popitem(last=last)
71
+ except KeyError:
72
+ now = time.time()
73
+ if now >= end_time:
74
+ raise asyncio.TimeoutError(
75
+ "Timeout waiting for item to be available"
76
+ )
77
+ await asyncio.sleep(min(0.1, end_time - now))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: web3
3
- Version: 7.0.0b7
3
+ Version: 7.0.0b8
4
4
  Summary: web3: A Python library for interacting with Ethereum
5
5
  Home-page: https://github.com/ethereum/web3.py
6
6
  Author: The Ethereum Foundation
@@ -79,7 +79,9 @@ Requires-Dist: pytest >=7.0.0 ; extra == 'test'
79
79
  [![Python versions](https://img.shields.io/pypi/pyversions/web3.svg)](https://pypi.python.org/pypi/web3)
80
80
  [![Docs build](https://readthedocs.org/projects/web3py/badge/?version=latest)](https://web3py.readthedocs.io/en/latest/?badge=latest)
81
81
 
82
- A Python library for interacting with Ethereum.
82
+ ## A Python Library for Interacting with Ethereum
83
+
84
+ web3.py allows you to interact with the Ethereum blockchain using Python, enabling you to build decentralized applications, interact with smart contracts, and much more.
83
85
 
84
86
  - Python 3.8+ support
85
87
 
@@ -94,7 +96,7 @@ ______________________________________________________________________
94
96
 
95
97
  For additional guides, examples, and APIs, see the [documentation](https://web3py.readthedocs.io/en/latest/).
96
98
 
97
- ## Want to help?
99
+ ## Want to Help?
98
100
 
99
101
  Want to file a bug, contribute some code, or improve documentation? Excellent! Read up on our
100
102
  guidelines for [contributing](https://web3py.readthedocs.io/en/latest/contributing.html),
@@ -103,4 +105,6 @@ then check out issues that are labeled
103
105
 
104
106
  ______________________________________________________________________
105
107
 
106
- #### Questions on implementation or usage? Join the conversation on [discord](https://discord.gg/GHryRvPB84).
108
+ ## Questions on Implementation or Usage?
109
+
110
+ Join the conversation in the Ethereum Python Community [Discord](https://discord.gg/GHryRvPB84).
@@ -14,13 +14,13 @@ ens/specs/normalization_spec.json,sha256=xn3N9a-6KHMLu3MeCBsmOxSzIzUQykzE9EscKK1
14
14
  web3/__init__.py,sha256=P11QAEV_GYoZq9ij8gDzFx5tKzJY2aMXG-keg2Lg1xs,1277
15
15
  web3/constants.py,sha256=eQLRQVMFPbgpOjjkPTMHkY-syncJuO-sPX5UrCSRjzQ,564
16
16
  web3/datastructures.py,sha256=Yc45cXgoXvhV0HPvnmkFFOEVDtLr-Pftc_f5q-uQY1M,10939
17
- web3/exceptions.py,sha256=VoB2dwZagBVTJn9712xcdJJPyq_bwhFt4prf2H9ukMs,8742
17
+ web3/exceptions.py,sha256=gpFB_l-MqCrBxW5CaSh9WpvdC4vdVCxxM8FVNc744wk,8887
18
18
  web3/geth.py,sha256=IQYeqiVSqcskuXWgDR35UBuVsD-whhvTpDltO4vvCvE,5867
19
19
  web3/logs.py,sha256=ROs-mDMH_ZOecE7hfbWA5yp27G38FbLjX4lO_WtlZxQ,198
20
20
  web3/main.py,sha256=AFAV0Y_zIjo9ZuAlQTEMbUPN5iGyHRpS8mS-wFkw8-E,14350
21
- web3/manager.py,sha256=9ze-9eluei5_CCA3G-kMP_KWzllD2iv9oJQbeXyU3ao,20730
21
+ web3/manager.py,sha256=X2unu2J5EXCmLVx6MG0dMSxR3mqis-R_PF-KEaoOzfw,21950
22
22
  web3/method.py,sha256=Uv29Vng93VC5-HHeRok30PUbGCg42SNA2YsxiweTgWA,8593
23
- web3/module.py,sha256=Djz589nMXZzelVELGPzsw9CHgqmKTUwOZ7pTgkCPs-U,5996
23
+ web3/module.py,sha256=rz_cqGNlzLGCHLE8zDf-hW4SpDfLgzVEW8pgDxM45Y0,6006
24
24
  web3/net.py,sha256=Y3vPzHWVFkfHEZoJxjDOt4tp5ERmZrMuyi4ZFOLmIeA,1562
25
25
  web3/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
26
26
  web3/testing.py,sha256=Ury_-7XSstJ8bkCfdGEi4Cr76QzSfW7v_zfPlDlLJj0,923
@@ -49,7 +49,7 @@ web3/_utils/http.py,sha256=NNcbf_K814Ggpt0YiGTWjV3CCUrLkpZpQDru4f-3C9E,235
49
49
  web3/_utils/http_session_manager.py,sha256=zOIEXELSYsbIq49JUJFY1jN5iPjENBkX1CPucqE3LNE,10726
50
50
  web3/_utils/hypothesis.py,sha256=4Cm4iOWv-uP9irg_Pv63kYNDYUAGhnUH6fOPWRw3A0g,209
51
51
  web3/_utils/math.py,sha256=4oU5YdbQBXElxK00CxmUZ94ApXFu9QT_TrO0Kho1HTs,1083
52
- web3/_utils/method_formatters.py,sha256=khFByaPfqy6_BjxznaNiO04fgMYTRYwD-7nIhXmNT6I,33507
52
+ web3/_utils/method_formatters.py,sha256=nVKmwq9WWxtecACBNaLlZaVRQdv2HKyADsdYd3RIUNE,34213
53
53
  web3/_utils/module.py,sha256=GuVePloTlIBZwFDOjg0zasp53HSJ32umxN1nQhqW-8Y,3175
54
54
  web3/_utils/normalizers.py,sha256=Q5urvzI9nbtNiqzhrrIMtZ5NqOAxWE4Xp5srZFs0Baw,7392
55
55
  web3/_utils/rpc_abi.py,sha256=ey3rw3j2jC9ybs1FZpyCPReA0Mra7TwRX1a7GTtOPeE,8392
@@ -86,12 +86,12 @@ web3/_utils/contract_sources/contract_data/storage_contract.py,sha256=vP1Qaekjld
86
86
  web3/_utils/contract_sources/contract_data/string_contract.py,sha256=y6EFnum4zDrrRUmuXExCGr2bhZMrerbMiqJiamUU2Tw,11210
87
87
  web3/_utils/contract_sources/contract_data/tuple_contracts.py,sha256=QuqPfv3BxjqDjeLScnxJDEPtPZMAB4jwHxsAOeVd6kk,23176
88
88
  web3/_utils/module_testing/__init__.py,sha256=tPFAaX7xOR50CPTq24UeY-1CX1LQxxmEOPr0-tIRkoE,376
89
- web3/_utils/module_testing/eth_module.py,sha256=sMrza5Yg_Sw8xdCUnSZL_Kzj7MyZL_Qxv1ady43vCn8,185007
89
+ web3/_utils/module_testing/eth_module.py,sha256=5fr1PSUECdijf5Me7G6ZSs98tDRfpFjhAQQqzSnT23U,185007
90
90
  web3/_utils/module_testing/go_ethereum_admin_module.py,sha256=_c-6SyzZkfAJ-7ySXUpw9FEr4cg-ShRdAGSAHWanCtY,3406
91
91
  web3/_utils/module_testing/go_ethereum_txpool_module.py,sha256=5f8XL8-2x3keyGRaITxMQYl9oQzjgqGn8zobB-j9BPs,1176
92
- web3/_utils/module_testing/module_testing_utils.py,sha256=m5-mnIf29F2hGgFX-7kv0kdESTcvMu9UtzK0RGT3XTU,5908
92
+ web3/_utils/module_testing/module_testing_utils.py,sha256=UCP95K8YWX6zTXspzmVxlSUN8TMaK6-Hzvq2W_VNZNA,5956
93
93
  web3/_utils/module_testing/net_module.py,sha256=ifUTC-5fTcQbwpm0X09OdD5RSPnn00T8klFeYe8tTm4,1272
94
- web3/_utils/module_testing/persistent_connection_provider.py,sha256=py52pWgdP4E3JORGTFo-CEzKqb56FwKSlzdaH-i7oQs,16618
94
+ web3/_utils/module_testing/persistent_connection_provider.py,sha256=SciOaB4p56M0MGsPr5C_qO7_mJ_hHuJo4ONzTHTiCX4,17816
95
95
  web3/_utils/module_testing/utils.py,sha256=7jYtIKfOdrQnj1pDB0gLyoN_b8U3ZyEYbMU4dxaLljs,10023
96
96
  web3/_utils/module_testing/web3_module.py,sha256=lfLOshDOr-SpnaN0iVcff-q-HUOw8XSBIwQeBO7TaJo,25121
97
97
  web3/auto/__init__.py,sha256=ZbzAiCZMdt_tCTTPvH6t8NCVNroKKkt7TSVBBNR74Is,44
@@ -136,12 +136,12 @@ web3/providers/eth_tester/defaults.py,sha256=9aPe0x2C5wahdGI_rZjiGR3Fe2LbMjKWH9o
136
136
  web3/providers/eth_tester/main.py,sha256=U19sNDeHs36A4IYQ0HFGyXdZvuXiYvoSMNWVuki0WwI,7807
137
137
  web3/providers/eth_tester/middleware.py,sha256=3h8q1WBu5CLiBYwonWFeAR_5pUy_vqgiDmi7wOzuorc,12971
138
138
  web3/providers/persistent/__init__.py,sha256=X7tFKJL5BXSwciq5_bRwGRB6bfdWBkIPPWMqCjXIKrA,411
139
- web3/providers/persistent/async_ipc.py,sha256=YtSZWlKwnr78ofBW31jTPpDAOK_Cs305oT-Y7Uhdhnw,6213
140
- web3/providers/persistent/persistent.py,sha256=2dJtCfhK_Sm6wrEx807XbO0YhADM27agEI8sEgNwoWE,8918
141
- web3/providers/persistent/persistent_connection.py,sha256=56yjpwDMM2phkJ_M5_QB-A6_CjZc5VrkWhNDweA70d0,1109
139
+ web3/providers/persistent/async_ipc.py,sha256=7xgk9hxEXCA2yyHoRSFlDjPWTM_EJN7OVjRHTmPl9ts,4692
140
+ web3/providers/persistent/persistent.py,sha256=8dezjPWoabZ6Wk47t7BRAksj3eAGeQnmpiZ6XafcnqQ,11344
141
+ web3/providers/persistent/persistent_connection.py,sha256=RCoYNobRE9IMtxhByT0gTNby4eFqAOEDAqYzphtHNB8,2683
142
142
  web3/providers/persistent/request_processor.py,sha256=E1jJwsvrPBTbufCy2LeNvMdRXf1H-Iz5WTsqyFvPGc0,14504
143
143
  web3/providers/persistent/utils.py,sha256=gfY7w1HB8xuE7OujSrbwWYjQuQ8nzRBoxoL8ESinqWM,1140
144
- web3/providers/persistent/websocket.py,sha256=L0jIBaSq2XBAPbzpJgBjDsUB0sfIl8p4af5fqIl56a0,5446
144
+ web3/providers/persistent/websocket.py,sha256=2EuBOdkEBTp6ge53UlJbjV0eAUJ143TkmqrOLTQl9kk,4231
145
145
  web3/providers/rpc/__init__.py,sha256=mObsuwjr7xyHnnRlwzsmbp2JgZdn2NXSSctvpye4AuQ,149
146
146
  web3/providers/rpc/async_rpc.py,sha256=hibsCoyrAD199ExAEyHRsEnZ0_rDP_2EC2k5g-D2zhw,5609
147
147
  web3/providers/rpc/rpc.py,sha256=ePlGdlkvQTaf2Wi2khxiY-rRj-OFcPlpDMuTPLKaAyU,5655
@@ -158,10 +158,10 @@ web3/utils/__init__.py,sha256=gDQ032U1WUUmzOdrrIdcQS4s_x5gUeRHV7xQ7kTClJs,596
158
158
  web3/utils/abi.py,sha256=naNkD7_XQGV8hd4CkxytLKWCcgzUjkb7q3ERwRVNICI,498
159
159
  web3/utils/address.py,sha256=KC_IpEbixSCuMhaW6V2QCyyJTYKYGS9c8QtI9_aH7zQ,967
160
160
  web3/utils/async_exception_handling.py,sha256=GZWSBFC0-Wmwz1tpTie-1AKRbIQH7JkmBpf5bXrUTzY,3320
161
- web3/utils/caching.py,sha256=4U-vh61m1dlslwEn_Nl7ybIlMF4W2IAcvMTH-iFzrNM,1618
161
+ web3/utils/caching.py,sha256=IG_IxW-jyiRklrIyUgjOj3GQvcXrok0KLDX3ch_6wuA,2390
162
162
  web3/utils/exception_handling.py,sha256=k31JROfUyKIm9PoEWOtYSkIq9wL8SOBwQfnSLNQyfnM,3097
163
- web3-7.0.0b7.dist-info/LICENSE,sha256=ScEyLx1vWrB0ybKiZKKTXm5QhVksHCEUtTp4lwYV45I,1095
164
- web3-7.0.0b7.dist-info/METADATA,sha256=RNKH0xkVnw06eSlXOfiad-MEcEfrii1_9W7EeQRb9L4,4813
165
- web3-7.0.0b7.dist-info/WHEEL,sha256=mguMlWGMX-VHnMpKOjjQidIo1ssRlCFu4a4mBpz1s2M,91
166
- web3-7.0.0b7.dist-info/top_level.txt,sha256=iwupuJh7wgypXrpk_awszyri3TahRr5vxSphNyvt1bU,9
167
- web3-7.0.0b7.dist-info/RECORD,,
163
+ web3-7.0.0b8.dist-info/LICENSE,sha256=ScEyLx1vWrB0ybKiZKKTXm5QhVksHCEUtTp4lwYV45I,1095
164
+ web3-7.0.0b8.dist-info/METADATA,sha256=ZrFhndCk4vfboI5JrBJX90Pai4HHeQNZ8KRtYAz_dB4,5015
165
+ web3-7.0.0b8.dist-info/WHEEL,sha256=Wyh-_nZ0DJYolHNn1_hMa4lM7uDedD_RGVwbmTjyItk,91
166
+ web3-7.0.0b8.dist-info/top_level.txt,sha256=iwupuJh7wgypXrpk_awszyri3TahRr5vxSphNyvt1bU,9
167
+ web3-7.0.0b8.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (70.1.1)
2
+ Generator: setuptools (71.1.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5