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