Skip to content

Commit 11d0842

Browse files
committed
Remove caching decorator from base providers; fix typing:
- Base providers don't implement ``make_request`` and so can't cache requests. Remove the request caching decorator from the base provider classes. - Fix typing in the request caching utils, which should now be tighter, expecting an ``RPCEndpoint`` instead of a generic ``str``.
1 parent 93b5fcd commit 11d0842

File tree

5 files changed

+39
-32
lines changed

5 files changed

+39
-32
lines changed

web3/_utils/caching/caching_utils.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
from web3.exceptions import (
5454
Web3TypeError,
5555
)
56+
from web3.types import (
57+
RPCEndpoint,
58+
)
5659
from web3.utils import (
5760
RequestCacheValidationThreshold,
5861
)
@@ -65,7 +68,6 @@
6568
from web3.types import ( # noqa: F401
6669
AsyncMakeRequestFn,
6770
MakeRequestFn,
68-
RPCEndpoint,
6971
RPCResponse,
7072
)
7173

@@ -93,7 +95,7 @@ def generate_cache_key(value: Any) -> str:
9395
class RequestInformation:
9496
def __init__(
9597
self,
96-
method: "RPCEndpoint",
98+
method: RPCEndpoint,
9799
params: Any,
98100
response_formatters: Tuple[
99101
Union[Dict[str, Callable[..., Any]], Callable[..., Any]],
@@ -133,7 +135,7 @@ def __init__(
133135

134136
def is_cacheable_request(
135137
provider: Union[ASYNC_PROVIDER_TYPE, SYNC_PROVIDER_TYPE],
136-
method: "RPCEndpoint",
138+
method: RPCEndpoint,
137139
params: Any,
138140
) -> bool:
139141
if not (provider.cache_allowed_requests and method in provider.cacheable_requests):
@@ -173,7 +175,7 @@ def is_cacheable_request(
173175
}
174176

175177
INTERNAL_VALIDATION_MAP: Dict[
176-
"RPCEndpoint",
178+
RPCEndpoint,
177179
Callable[
178180
[SYNC_PROVIDER_TYPE, Sequence[Any], Dict[str, Any]],
179181
bool,
@@ -197,7 +199,9 @@ def set_threshold_if_empty(provider: SYNC_PROVIDER_TYPE) -> None:
197199
try:
198200
# turn off momentarily to avoid recursion
199201
provider.cache_allowed_requests = False
200-
chain_id_result = provider.make_request("eth_chainId", [])["result"]
202+
chain_id_result = provider.make_request(RPCEndpoint("eth_chainId"), [])[
203+
"result"
204+
]
201205
chain_id = int(chain_id_result, 16)
202206

203207
if current_threshold is empty:
@@ -214,7 +218,7 @@ def set_threshold_if_empty(provider: SYNC_PROVIDER_TYPE) -> None:
214218

215219
def _should_cache_response(
216220
provider: SYNC_PROVIDER_TYPE,
217-
method: "RPCEndpoint",
221+
method: RPCEndpoint,
218222
params: Sequence[Any],
219223
response: "RPCResponse",
220224
) -> bool:
@@ -232,10 +236,10 @@ def _should_cache_response(
232236

233237

234238
def handle_request_caching(
235-
func: Callable[[SYNC_PROVIDER_TYPE, "RPCEndpoint", Any], "RPCResponse"]
239+
func: Callable[[SYNC_PROVIDER_TYPE, RPCEndpoint, Any], "RPCResponse"]
236240
) -> Callable[..., "RPCResponse"]:
237241
def wrapper(
238-
provider: SYNC_PROVIDER_TYPE, method: "RPCEndpoint", params: Any
242+
provider: SYNC_PROVIDER_TYPE, method: RPCEndpoint, params: Any
239243
) -> "RPCResponse":
240244
if is_cacheable_request(provider, method, params):
241245
request_cache = provider._request_cache
@@ -266,7 +270,7 @@ def wrapper(
266270
Union[bool, Coroutine[Any, Any, bool]],
267271
]
268272

269-
ASYNC_INTERNAL_VALIDATION_MAP: Dict["RPCEndpoint", ASYNC_VALIDATOR_TYPE] = {
273+
ASYNC_INTERNAL_VALIDATION_MAP: Dict[RPCEndpoint, ASYNC_VALIDATOR_TYPE] = {
270274
**{endpoint: always_cache_request for endpoint in ALWAYS_CACHE},
271275
**{
272276
endpoint: async_validate_from_block_id_in_params
@@ -292,7 +296,9 @@ async def async_set_threshold_if_empty(provider: ASYNC_PROVIDER_TYPE) -> None:
292296
try:
293297
# turn off momentarily to avoid recursion
294298
provider.cache_allowed_requests = False
295-
chain_id_result = await provider.make_request("eth_chainId", [])
299+
chain_id_result = await provider.make_request(
300+
RPCEndpoint("eth_chainId"), []
301+
)
296302
chain_id = int(chain_id_result["result"], 16)
297303

298304
if current_threshold is empty:
@@ -309,7 +315,7 @@ async def async_set_threshold_if_empty(provider: ASYNC_PROVIDER_TYPE) -> None:
309315

310316
async def _async_should_cache_response(
311317
provider: ASYNC_PROVIDER_TYPE,
312-
method: "RPCEndpoint",
318+
method: RPCEndpoint,
313319
params: Sequence[Any],
314320
response: "RPCResponse",
315321
) -> bool:
@@ -333,11 +339,11 @@ async def _async_should_cache_response(
333339

334340
def async_handle_request_caching(
335341
func: Callable[
336-
[ASYNC_PROVIDER_TYPE, "RPCEndpoint", Any], Coroutine[Any, Any, "RPCResponse"]
342+
[ASYNC_PROVIDER_TYPE, RPCEndpoint, Any], Coroutine[Any, Any, "RPCResponse"]
337343
],
338344
) -> Callable[..., Coroutine[Any, Any, "RPCResponse"]]:
339345
async def wrapper(
340-
provider: ASYNC_PROVIDER_TYPE, method: "RPCEndpoint", params: Any
346+
provider: ASYNC_PROVIDER_TYPE, method: RPCEndpoint, params: Any
341347
) -> "RPCResponse":
342348
if is_cacheable_request(provider, method, params):
343349
request_cache = provider._request_cache

web3/_utils/caching/request_caching_validation.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
Union,
99
)
1010

11+
from web3.types import (
12+
RPCEndpoint,
13+
)
1114
from web3.utils import (
1215
RequestCacheValidationThreshold,
1316
)
@@ -50,7 +53,7 @@ def is_beyond_validation_threshold(
5053
if isinstance(threshold, RequestCacheValidationThreshold):
5154
# if mainnet and threshold is "finalized" or "safe"
5255
threshold_block = provider.make_request(
53-
"eth_getBlockByNumber", [threshold.value, False]
56+
RPCEndpoint("eth_getBlockByNumber"), [threshold.value, False]
5457
)["result"]
5558
# we should have a `blocknum` to compare against
5659
return blocknum <= int(threshold_block["number"], 16)
@@ -59,7 +62,7 @@ def is_beyond_validation_threshold(
5962
# if validating via `blocknum` from params, we need to get the timestamp
6063
# for the block with `blocknum`.
6164
block = provider.make_request(
62-
"eth_getBlockByNumber", [hex(blocknum), False]
65+
RPCEndpoint("eth_getBlockByNumber"), [hex(blocknum), False]
6366
)["result"]
6467
block_timestamp = int(block["timestamp"], 16)
6568

@@ -107,9 +110,9 @@ def validate_from_blocknum_in_result(
107110
if "blockNumber" in result:
108111
blocknum = result.get("blockNumber")
109112
# make an extra call to get the block values
110-
block = provider.make_request("eth_getBlockByNumber", [blocknum, False])[
111-
"result"
112-
]
113+
block = provider.make_request(
114+
RPCEndpoint("eth_getBlockByNumber"), [blocknum, False]
115+
)["result"]
113116
return is_beyond_validation_threshold(
114117
provider,
115118
blocknum=int(blocknum, 16),
@@ -145,9 +148,9 @@ def validate_from_blockhash_in_params(
145148
provider.cache_allowed_requests = False
146149

147150
# make an extra call to get the block number from the hash
148-
block = provider.make_request("eth_getBlockByHash", [params[0], False])[
149-
"result"
150-
]
151+
block = provider.make_request(
152+
RPCEndpoint("eth_getBlockByHash"), [params[0], False]
153+
)["result"]
151154
return is_beyond_validation_threshold(
152155
provider,
153156
blocknum=int(block["number"], 16),
@@ -177,14 +180,14 @@ async def async_is_beyond_validation_threshold(
177180
if isinstance(threshold, RequestCacheValidationThreshold):
178181
# if mainnet and threshold is "finalized" or "safe"
179182
threshold_block = await provider.make_request(
180-
"eth_getBlockByNumber", [threshold.value, False]
183+
RPCEndpoint("eth_getBlockByNumber"), [threshold.value, False]
181184
)
182185
# we should have a `blocknum` to compare against
183186
return blocknum <= int(threshold_block["result"]["number"], 16)
184187
elif isinstance(threshold, int):
185188
if not block_timestamp:
186189
block = await provider.make_request(
187-
"eth_getBlockByNumber", [hex(blocknum), False]
190+
RPCEndpoint("eth_getBlockByNumber"), [hex(blocknum), False]
188191
)
189192
block_timestamp = int(block["result"]["timestamp"], 16)
190193

@@ -233,7 +236,7 @@ async def async_validate_from_blocknum_in_result(
233236
blocknum = result.get("blockNumber")
234237
# make an extra call to get the block values
235238
block = await provider.make_request(
236-
"eth_getBlockByNumber", [blocknum, False]
239+
RPCEndpoint("eth_getBlockByNumber"), [blocknum, False]
237240
)
238241
return await async_is_beyond_validation_threshold(
239242
provider,
@@ -268,7 +271,9 @@ async def async_validate_from_blockhash_in_params(
268271
provider.cache_allowed_requests = False
269272

270273
# make an extra call to get the block number from the hash
271-
response = await provider.make_request("eth_getBlockByHash", [params[0], False])
274+
response = await provider.make_request(
275+
RPCEndpoint("eth_getBlockByHash"), [params[0], False]
276+
)
272277
return await async_is_beyond_validation_threshold(
273278
provider,
274279
blocknum=int(response["result"]["number"], 16),

web3/_utils/module_testing/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,9 @@ def __enter__(self) -> "Self":
113113

114114
return self
115115

116-
# define __exit__ with typing information
117116
def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
118117
# mypy error: Cannot assign to a method
119-
self.w3.provider.make_request = self._make_request # type: ignore[method-assign] # noqa: E501
118+
self.w3.provider.make_request = self._make_request # type: ignore[assignment]
120119
# reset request func cache to re-build request_func with original make_request
121120
self.w3.provider._request_func_cache = (None, None)
122121

@@ -175,6 +174,7 @@ def _mock_request_handler(
175174
return mocked_response
176175

177176
# -- async -- #
177+
178178
async def __aenter__(self) -> "Self":
179179
# mypy error: Cannot assign to a method
180180
self.w3.provider.make_request = self._async_mock_request_handler # type: ignore[method-assign] # noqa: E501
@@ -184,7 +184,7 @@ async def __aenter__(self) -> "Self":
184184

185185
async def __aexit__(self, exc_type: Any, exc_value: Any, traceback: Any) -> None:
186186
# mypy error: Cannot assign to a method
187-
self.w3.provider.make_request = self._make_request # type: ignore[method-assign] # noqa: E501
187+
self.w3.provider.make_request = self._make_request # type: ignore[assignment]
188188
# reset request func cache to re-build request_func with original make_request
189189
self.w3.provider._request_func_cache = (None, None)
190190

web3/providers/async_base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

2323
from web3._utils.caching import (
2424
CACHEABLE_REQUESTS,
25-
async_handle_request_caching,
2625
)
2726
from web3._utils.empty import (
2827
Empty,
@@ -137,7 +136,6 @@ async def batch_request_func(
137136
self._batch_request_func_cache = (middleware, accumulator_fn)
138137
return self._batch_request_func_cache[-1]
139138

140-
@async_handle_request_caching
141139
async def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
142140
raise NotImplementedError("Providers must implement this method")
143141

web3/providers/base.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from web3._utils.caching import (
2222
CACHEABLE_REQUESTS,
23-
handle_request_caching,
2423
)
2524
from web3._utils.empty import (
2625
Empty,
@@ -109,7 +108,6 @@ def request_func(
109108

110109
return self._request_func_cache[-1]
111110

112-
@handle_request_caching
113111
def make_request(self, method: RPCEndpoint, params: Any) -> RPCResponse:
114112
raise NotImplementedError("Providers must implement this method")
115113

0 commit comments

Comments
 (0)