web3 7.0.0b5__py3-none-any.whl → 7.0.0b7__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.
- ens/__init__.py +13 -2
- web3/__init__.py +21 -5
- web3/_utils/batching.py +217 -0
- web3/_utils/caching.py +26 -2
- web3/_utils/compat/__init__.py +1 -0
- web3/_utils/contract_sources/contract_data/arrays_contract.py +3 -3
- web3/_utils/contract_sources/contract_data/bytes_contracts.py +5 -5
- web3/_utils/contract_sources/contract_data/constructor_contracts.py +7 -7
- web3/_utils/contract_sources/contract_data/contract_caller_tester.py +3 -3
- web3/_utils/contract_sources/contract_data/emitter_contract.py +3 -3
- web3/_utils/contract_sources/contract_data/event_contracts.py +5 -5
- web3/_utils/contract_sources/contract_data/extended_resolver.py +3 -3
- web3/_utils/contract_sources/contract_data/fallback_function_contract.py +3 -3
- web3/_utils/contract_sources/contract_data/function_name_tester_contract.py +3 -3
- web3/_utils/contract_sources/contract_data/math_contract.py +3 -3
- web3/_utils/contract_sources/contract_data/offchain_lookup.py +3 -3
- web3/_utils/contract_sources/contract_data/offchain_resolver.py +3 -3
- web3/_utils/contract_sources/contract_data/panic_errors_contract.py +3 -3
- web3/_utils/contract_sources/contract_data/payable_tester.py +3 -3
- web3/_utils/contract_sources/contract_data/receive_function_contracts.py +5 -5
- web3/_utils/contract_sources/contract_data/reflector_contracts.py +3 -3
- web3/_utils/contract_sources/contract_data/revert_contract.py +3 -3
- web3/_utils/contract_sources/contract_data/simple_resolver.py +3 -3
- web3/_utils/contract_sources/contract_data/storage_contract.py +3 -3
- web3/_utils/contract_sources/contract_data/string_contract.py +3 -3
- web3/_utils/contract_sources/contract_data/tuple_contracts.py +5 -5
- web3/_utils/events.py +2 -2
- web3/_utils/http.py +3 -0
- web3/_utils/http_session_manager.py +280 -0
- web3/_utils/method_formatters.py +0 -2
- web3/_utils/module_testing/eth_module.py +92 -119
- web3/_utils/module_testing/module_testing_utils.py +27 -9
- web3/_utils/module_testing/persistent_connection_provider.py +1 -0
- web3/_utils/module_testing/web3_module.py +438 -17
- web3/_utils/rpc_abi.py +0 -3
- web3/beacon/__init__.py +5 -0
- web3/beacon/async_beacon.py +9 -5
- web3/beacon/beacon.py +7 -5
- web3/contract/__init__.py +7 -0
- web3/contract/base_contract.py +10 -1
- web3/contract/utils.py +112 -4
- web3/eth/__init__.py +7 -0
- web3/eth/async_eth.py +5 -37
- web3/eth/eth.py +7 -57
- web3/exceptions.py +20 -0
- web3/gas_strategies/time_based.py +2 -2
- web3/main.py +21 -9
- web3/manager.py +113 -8
- web3/method.py +29 -9
- web3/middleware/__init__.py +17 -0
- web3/middleware/base.py +43 -0
- web3/module.py +47 -7
- web3/providers/__init__.py +21 -0
- web3/providers/async_base.py +55 -23
- web3/providers/base.py +59 -26
- web3/providers/eth_tester/__init__.py +5 -0
- web3/providers/eth_tester/defaults.py +0 -6
- web3/providers/eth_tester/middleware.py +3 -8
- web3/providers/ipc.py +23 -8
- web3/providers/legacy_websocket.py +26 -1
- web3/providers/persistent/__init__.py +7 -0
- web3/providers/persistent/async_ipc.py +60 -76
- web3/providers/persistent/persistent.py +134 -10
- web3/providers/persistent/request_processor.py +98 -14
- web3/providers/persistent/websocket.py +43 -66
- web3/providers/rpc/__init__.py +5 -0
- web3/providers/rpc/async_rpc.py +34 -12
- web3/providers/rpc/rpc.py +34 -12
- web3/providers/rpc/utils.py +0 -3
- web3/tools/benchmark/main.py +7 -6
- web3/tools/benchmark/node.py +1 -1
- web3/types.py +7 -1
- web3/utils/__init__.py +14 -5
- web3/utils/async_exception_handling.py +19 -7
- web3/utils/exception_handling.py +7 -5
- {web3-7.0.0b5.dist-info → web3-7.0.0b7.dist-info}/LICENSE +1 -1
- {web3-7.0.0b5.dist-info → web3-7.0.0b7.dist-info}/METADATA +33 -20
- {web3-7.0.0b5.dist-info → web3-7.0.0b7.dist-info}/RECORD +80 -80
- {web3-7.0.0b5.dist-info → web3-7.0.0b7.dist-info}/WHEEL +1 -1
- web3/_utils/contract_sources/contract_data/address_reflector.py +0 -29
- web3/_utils/request.py +0 -265
- {web3-7.0.0b5.dist-info → web3-7.0.0b7.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
import asyncio
|
|
2
|
+
from concurrent.futures import (
|
|
3
|
+
ThreadPoolExecutor,
|
|
4
|
+
)
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
import threading
|
|
8
|
+
from typing import (
|
|
9
|
+
Any,
|
|
10
|
+
Dict,
|
|
11
|
+
List,
|
|
12
|
+
Optional,
|
|
13
|
+
Union,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
from aiohttp import (
|
|
17
|
+
ClientResponse,
|
|
18
|
+
ClientSession,
|
|
19
|
+
ClientTimeout,
|
|
20
|
+
)
|
|
21
|
+
from eth_typing import (
|
|
22
|
+
URI,
|
|
23
|
+
)
|
|
24
|
+
import requests
|
|
25
|
+
|
|
26
|
+
from web3._utils.async_caching import (
|
|
27
|
+
async_lock,
|
|
28
|
+
)
|
|
29
|
+
from web3._utils.caching import (
|
|
30
|
+
generate_cache_key,
|
|
31
|
+
)
|
|
32
|
+
from web3._utils.http import (
|
|
33
|
+
DEFAULT_HTTP_TIMEOUT,
|
|
34
|
+
)
|
|
35
|
+
from web3.utils.caching import (
|
|
36
|
+
SimpleCache,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class HTTPSessionManager:
|
|
41
|
+
logger = logging.getLogger("web3._utils.http_session_manager.HTTPSessionManager")
|
|
42
|
+
_lock: threading.Lock = threading.Lock()
|
|
43
|
+
|
|
44
|
+
def __init__(
|
|
45
|
+
self,
|
|
46
|
+
cache_size: int = 100,
|
|
47
|
+
session_pool_max_workers: int = 5,
|
|
48
|
+
) -> None:
|
|
49
|
+
self.session_cache = SimpleCache(cache_size)
|
|
50
|
+
self.session_pool = ThreadPoolExecutor(max_workers=session_pool_max_workers)
|
|
51
|
+
|
|
52
|
+
@staticmethod
|
|
53
|
+
def get_default_http_endpoint() -> URI:
|
|
54
|
+
return URI(os.environ.get("WEB3_HTTP_PROVIDER_URI", "http://localhost:8545"))
|
|
55
|
+
|
|
56
|
+
def cache_and_return_session(
|
|
57
|
+
self,
|
|
58
|
+
endpoint_uri: URI,
|
|
59
|
+
session: requests.Session = None,
|
|
60
|
+
request_timeout: Optional[float] = None,
|
|
61
|
+
) -> requests.Session:
|
|
62
|
+
# cache key should have a unique thread identifier
|
|
63
|
+
cache_key = generate_cache_key(f"{threading.get_ident()}:{endpoint_uri}")
|
|
64
|
+
|
|
65
|
+
cached_session = self.session_cache.get_cache_entry(cache_key)
|
|
66
|
+
if cached_session is not None:
|
|
67
|
+
# If read from cache yields a session, no need to lock; return the session.
|
|
68
|
+
# Sync is a bit simpler in this way since a `requests.Session` doesn't
|
|
69
|
+
# really "close" in the same way that an async `ClientSession` does.
|
|
70
|
+
# When "closed", it still uses http / https adapters successfully if a
|
|
71
|
+
# request is made.
|
|
72
|
+
return cached_session
|
|
73
|
+
|
|
74
|
+
if session is None:
|
|
75
|
+
session = requests.Session()
|
|
76
|
+
|
|
77
|
+
with self._lock:
|
|
78
|
+
cached_session, evicted_items = self.session_cache.cache(cache_key, session)
|
|
79
|
+
self.logger.debug(f"Session cached: {endpoint_uri}, {cached_session}")
|
|
80
|
+
|
|
81
|
+
if evicted_items is not None:
|
|
82
|
+
evicted_sessions = evicted_items.values()
|
|
83
|
+
for evicted_session in evicted_sessions:
|
|
84
|
+
self.logger.debug(
|
|
85
|
+
"Session cache full. Session evicted from cache: "
|
|
86
|
+
f"{evicted_session}",
|
|
87
|
+
)
|
|
88
|
+
threading.Timer(
|
|
89
|
+
# If `request_timeout` is `None`, don't wait forever for the closing
|
|
90
|
+
# session to finish the request. Instead, wait over the default timeout.
|
|
91
|
+
request_timeout or DEFAULT_HTTP_TIMEOUT + 0.1,
|
|
92
|
+
self._close_evicted_sessions,
|
|
93
|
+
args=[evicted_sessions],
|
|
94
|
+
).start()
|
|
95
|
+
|
|
96
|
+
return cached_session
|
|
97
|
+
|
|
98
|
+
def get_response_from_get_request(
|
|
99
|
+
self, endpoint_uri: URI, *args: Any, **kwargs: Any
|
|
100
|
+
) -> requests.Response:
|
|
101
|
+
kwargs.setdefault("timeout", DEFAULT_HTTP_TIMEOUT)
|
|
102
|
+
session = self.cache_and_return_session(
|
|
103
|
+
endpoint_uri, request_timeout=kwargs["timeout"]
|
|
104
|
+
)
|
|
105
|
+
response = session.get(endpoint_uri, *args, **kwargs)
|
|
106
|
+
return response
|
|
107
|
+
|
|
108
|
+
def json_make_get_request(
|
|
109
|
+
self, endpoint_uri: URI, *args: Any, **kwargs: Any
|
|
110
|
+
) -> Dict[str, Any]:
|
|
111
|
+
response = self.get_response_from_get_request(endpoint_uri, *args, **kwargs)
|
|
112
|
+
response.raise_for_status()
|
|
113
|
+
return response.json()
|
|
114
|
+
|
|
115
|
+
def get_response_from_post_request(
|
|
116
|
+
self, endpoint_uri: URI, *args: Any, **kwargs: Any
|
|
117
|
+
) -> requests.Response:
|
|
118
|
+
kwargs.setdefault("timeout", DEFAULT_HTTP_TIMEOUT)
|
|
119
|
+
session = self.cache_and_return_session(
|
|
120
|
+
endpoint_uri, request_timeout=kwargs["timeout"]
|
|
121
|
+
)
|
|
122
|
+
response = session.post(endpoint_uri, *args, **kwargs)
|
|
123
|
+
return response
|
|
124
|
+
|
|
125
|
+
def make_post_request(
|
|
126
|
+
self, endpoint_uri: URI, data: Union[bytes, Dict[str, Any]], **kwargs: Any
|
|
127
|
+
) -> bytes:
|
|
128
|
+
response = self.get_response_from_post_request(
|
|
129
|
+
endpoint_uri, data=data, **kwargs
|
|
130
|
+
)
|
|
131
|
+
response.raise_for_status()
|
|
132
|
+
return response.content
|
|
133
|
+
|
|
134
|
+
def _close_evicted_sessions(self, evicted_sessions: List[requests.Session]) -> None:
|
|
135
|
+
for evicted_session in evicted_sessions:
|
|
136
|
+
evicted_session.close()
|
|
137
|
+
self.logger.debug(f"Closed evicted session: {evicted_session}")
|
|
138
|
+
|
|
139
|
+
# -- async -- #
|
|
140
|
+
|
|
141
|
+
async def async_cache_and_return_session(
|
|
142
|
+
self,
|
|
143
|
+
endpoint_uri: URI,
|
|
144
|
+
session: Optional[ClientSession] = None,
|
|
145
|
+
request_timeout: Optional[ClientTimeout] = None,
|
|
146
|
+
) -> ClientSession:
|
|
147
|
+
# cache key should have a unique thread identifier
|
|
148
|
+
cache_key = generate_cache_key(f"{threading.get_ident()}:{endpoint_uri}")
|
|
149
|
+
|
|
150
|
+
evicted_items = None
|
|
151
|
+
async with async_lock(self.session_pool, self._lock):
|
|
152
|
+
if cache_key not in self.session_cache:
|
|
153
|
+
if session is None:
|
|
154
|
+
session = ClientSession(raise_for_status=True)
|
|
155
|
+
|
|
156
|
+
cached_session, evicted_items = self.session_cache.cache(
|
|
157
|
+
cache_key, session
|
|
158
|
+
)
|
|
159
|
+
self.logger.debug(
|
|
160
|
+
f"Async session cached: {endpoint_uri}, {cached_session}"
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
else:
|
|
164
|
+
# get the cached session
|
|
165
|
+
cached_session = self.session_cache.get_cache_entry(cache_key)
|
|
166
|
+
session_is_closed = cached_session.closed
|
|
167
|
+
session_loop_is_closed = cached_session._loop.is_closed()
|
|
168
|
+
|
|
169
|
+
warning = (
|
|
170
|
+
"Async session was closed"
|
|
171
|
+
if session_is_closed
|
|
172
|
+
else (
|
|
173
|
+
"Loop was closed for async session"
|
|
174
|
+
if session_loop_is_closed
|
|
175
|
+
else None
|
|
176
|
+
)
|
|
177
|
+
)
|
|
178
|
+
if warning:
|
|
179
|
+
self.logger.debug(
|
|
180
|
+
f"{warning}: {endpoint_uri}, {cached_session}. "
|
|
181
|
+
f"Creating and caching a new async session for uri."
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
self.session_cache._data.pop(cache_key)
|
|
185
|
+
if not session_is_closed:
|
|
186
|
+
# if loop was closed but not the session, close the session
|
|
187
|
+
await cached_session.close()
|
|
188
|
+
self.logger.debug(
|
|
189
|
+
f"Async session closed and evicted from cache: {cached_session}"
|
|
190
|
+
)
|
|
191
|
+
|
|
192
|
+
# replace stale session with a new session at the cache key
|
|
193
|
+
_session = ClientSession(raise_for_status=True)
|
|
194
|
+
cached_session, evicted_items = self.session_cache.cache(
|
|
195
|
+
cache_key, _session
|
|
196
|
+
)
|
|
197
|
+
self.logger.debug(
|
|
198
|
+
f"Async session cached: {endpoint_uri}, {cached_session}"
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
if evicted_items is not None:
|
|
202
|
+
# At this point the evicted sessions are already popped out of the cache and
|
|
203
|
+
# just stored in the `evicted_sessions` dict. So we can kick off a future
|
|
204
|
+
# task to close them and it should be safe to pop out of the lock here.
|
|
205
|
+
evicted_sessions = list(evicted_items.values())
|
|
206
|
+
for evicted_session in evicted_sessions:
|
|
207
|
+
self.logger.debug(
|
|
208
|
+
"Async session cache full. Session evicted from cache: "
|
|
209
|
+
f"{evicted_session}",
|
|
210
|
+
)
|
|
211
|
+
# Kick off an asyncio `Task` to close the evicted sessions. In the case
|
|
212
|
+
# that the cache filled very quickly and some sessions have been evicted
|
|
213
|
+
# before their original request has been made, we set the timer to a bit
|
|
214
|
+
# more than the `request_timeout` for a call. This should make it so that
|
|
215
|
+
# any call from an evicted session can still be made before the session
|
|
216
|
+
# is closed.
|
|
217
|
+
asyncio.create_task(
|
|
218
|
+
self._async_close_evicted_sessions(
|
|
219
|
+
# if `ClientTimeout.total` is `None`, don't wait forever for the
|
|
220
|
+
# closing session to finish the request. Instead, use the default
|
|
221
|
+
# timeout.
|
|
222
|
+
request_timeout.total or DEFAULT_HTTP_TIMEOUT + 0.1,
|
|
223
|
+
evicted_sessions,
|
|
224
|
+
)
|
|
225
|
+
)
|
|
226
|
+
|
|
227
|
+
return cached_session
|
|
228
|
+
|
|
229
|
+
async def async_get_response_from_get_request(
|
|
230
|
+
self, endpoint_uri: URI, *args: Any, **kwargs: Any
|
|
231
|
+
) -> ClientResponse:
|
|
232
|
+
kwargs.setdefault("timeout", ClientTimeout(DEFAULT_HTTP_TIMEOUT))
|
|
233
|
+
session = await self.async_cache_and_return_session(
|
|
234
|
+
endpoint_uri, request_timeout=kwargs["timeout"]
|
|
235
|
+
)
|
|
236
|
+
response = await session.get(endpoint_uri, *args, **kwargs)
|
|
237
|
+
return response
|
|
238
|
+
|
|
239
|
+
async def async_json_make_get_request(
|
|
240
|
+
self, endpoint_uri: URI, *args: Any, **kwargs: Any
|
|
241
|
+
) -> Dict[str, Any]:
|
|
242
|
+
response = await self.async_get_response_from_get_request(
|
|
243
|
+
endpoint_uri, *args, **kwargs
|
|
244
|
+
)
|
|
245
|
+
response.raise_for_status()
|
|
246
|
+
return await response.json()
|
|
247
|
+
|
|
248
|
+
async def async_get_response_from_post_request(
|
|
249
|
+
self, endpoint_uri: URI, *args: Any, **kwargs: Any
|
|
250
|
+
) -> ClientResponse:
|
|
251
|
+
kwargs.setdefault("timeout", ClientTimeout(DEFAULT_HTTP_TIMEOUT))
|
|
252
|
+
session = await self.async_cache_and_return_session(
|
|
253
|
+
endpoint_uri, request_timeout=kwargs["timeout"]
|
|
254
|
+
)
|
|
255
|
+
response = await session.post(endpoint_uri, *args, **kwargs)
|
|
256
|
+
return response
|
|
257
|
+
|
|
258
|
+
async def async_make_post_request(
|
|
259
|
+
self, endpoint_uri: URI, data: Union[bytes, Dict[str, Any]], **kwargs: Any
|
|
260
|
+
) -> bytes:
|
|
261
|
+
response = await self.async_get_response_from_post_request(
|
|
262
|
+
endpoint_uri, data=data, **kwargs
|
|
263
|
+
)
|
|
264
|
+
response.raise_for_status()
|
|
265
|
+
return await response.read()
|
|
266
|
+
|
|
267
|
+
async def _async_close_evicted_sessions(
|
|
268
|
+
self, timeout: float, evicted_sessions: List[ClientSession]
|
|
269
|
+
) -> None:
|
|
270
|
+
await asyncio.sleep(timeout)
|
|
271
|
+
|
|
272
|
+
for evicted_session in evicted_sessions:
|
|
273
|
+
await evicted_session.close()
|
|
274
|
+
self.logger.debug(f"Closed evicted async session: {evicted_session}")
|
|
275
|
+
|
|
276
|
+
if any(not evicted_session.closed for evicted_session in evicted_sessions):
|
|
277
|
+
self.logger.warning(
|
|
278
|
+
"Some evicted async sessions were not properly closed: "
|
|
279
|
+
f"{evicted_sessions}"
|
|
280
|
+
)
|
web3/_utils/method_formatters.py
CHANGED
|
@@ -721,7 +721,6 @@ PYTHONIC_RESULT_FORMATTERS: Dict[RPCEndpoint, Callable[..., Any]] = {
|
|
|
721
721
|
RPC.eth_accounts: apply_list_to_array_formatter(to_checksum_address),
|
|
722
722
|
RPC.eth_blockNumber: to_integer_if_hex,
|
|
723
723
|
RPC.eth_chainId: to_integer_if_hex,
|
|
724
|
-
RPC.eth_coinbase: to_checksum_address,
|
|
725
724
|
RPC.eth_call: HexBytes,
|
|
726
725
|
RPC.eth_createAccessList: ACCESS_LIST_RESPONSE_FORMATTER,
|
|
727
726
|
RPC.eth_estimateGas: to_integer_if_hex,
|
|
@@ -761,7 +760,6 @@ PYTHONIC_RESULT_FORMATTERS: Dict[RPCEndpoint, Callable[..., Any]] = {
|
|
|
761
760
|
),
|
|
762
761
|
RPC.eth_getUncleCountByBlockHash: to_integer_if_hex,
|
|
763
762
|
RPC.eth_getUncleCountByBlockNumber: to_integer_if_hex,
|
|
764
|
-
RPC.eth_hashrate: to_integer_if_hex,
|
|
765
763
|
RPC.eth_protocolVersion: compose(
|
|
766
764
|
apply_formatter_if(is_0x_prefixed, to_integer_if_hex),
|
|
767
765
|
apply_formatter_if(is_integer, str),
|