Skip to content

Commit 3c27b30

Browse files
committed
Changes from comments on PR #3483
1 parent cedb113 commit 3c27b30

File tree

3 files changed

+24
-17
lines changed

3 files changed

+24
-17
lines changed

tests/core/caching-utils/test_request_caching.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,8 @@ def test_blocknum_validation_against_validation_threshold_when_caching(
240240
):
241241
assert len(w3.provider._request_cache.items()) == 0
242242
w3.manager.request_blocking(endpoint, [blocknum, False])
243-
assert len(w3.provider._request_cache.items()) == int(should_cache)
243+
cached_items = len(w3.provider._request_cache.items())
244+
assert cached_items == 1 if should_cache else cached_items == 0
244245

245246

246247
@pytest.mark.parametrize(
@@ -281,7 +282,8 @@ def test_block_id_param_caching(
281282
):
282283
assert len(w3.provider._request_cache.items()) == 0
283284
w3.manager.request_blocking(RPCEndpoint(endpoint), [block_id, False])
284-
assert len(w3.provider._request_cache.items()) == int(should_cache)
285+
cached_items = len(w3.provider._request_cache.items())
286+
assert cached_items == 1 if should_cache else cached_items == 0
285287

286288

287289
@pytest.mark.parametrize(
@@ -530,7 +532,8 @@ async def test_async_blocknum_validation_against_validation_threshold(
530532
):
531533
assert len(async_w3.provider._request_cache.items()) == 0
532534
await async_w3.manager.coro_request(endpoint, [blocknum, False])
533-
assert len(async_w3.provider._request_cache.items()) == int(should_cache)
535+
cached_items = len(async_w3.provider._request_cache.items())
536+
assert cached_items == 1 if should_cache else cached_items == 0
534537

535538

536539
@pytest.mark.asyncio
@@ -572,7 +575,8 @@ async def test_async_block_id_param_caching(
572575
):
573576
assert len(async_w3.provider._request_cache.items()) == 0
574577
await async_w3.manager.coro_request(RPCEndpoint(endpoint), [block_id, False])
575-
assert len(async_w3.provider._request_cache.items()) == int(should_cache)
578+
cached_items = len(async_w3.provider._request_cache.items())
579+
assert cached_items == 1 if should_cache else cached_items == 0
576580

577581

578582
@pytest.mark.asyncio

web3/_utils/caching/caching_utils.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
from asyncio import (
2+
iscoroutinefunction,
3+
)
14
import collections
25
import hashlib
36
import threading
@@ -31,7 +34,6 @@
3134
from web3._utils.caching.request_caching_validation import (
3235
UNCACHEABLE_BLOCK_IDS,
3336
always_cache_request,
34-
async_always_cache_request,
3537
async_validate_blockhash_in_params,
3638
async_validate_blocknum_in_params,
3739
async_validate_blocknum_in_result,
@@ -197,13 +199,13 @@ def wrapper(
197199

198200
# -- async -- #
199201

200-
ASYNC_INTERNAL_VALIDATION_MAP: Dict[
201-
"RPCEndpoint",
202-
Callable[
203-
[ASYNC_PROVIDER_TYPE, Sequence[Any], Dict[str, Any]], Coroutine[Any, Any, bool]
204-
],
205-
] = {
206-
**{endpoint: async_always_cache_request for endpoint in ALWAYS_CACHE},
202+
ASYNC_VALIDATOR_TYPE = Callable[
203+
["AsyncBaseProvider", Sequence[Any], Dict[str, Any]],
204+
Union[bool, Coroutine[Any, Any, bool]],
205+
]
206+
207+
ASYNC_INTERNAL_VALIDATION_MAP: Dict["RPCEndpoint", ASYNC_VALIDATOR_TYPE] = {
208+
**{endpoint: always_cache_request for endpoint in ALWAYS_CACHE},
207209
**{endpoint: async_validate_blocknum_in_params for endpoint in BLOCKNUM_IN_PARAMS},
208210
**{endpoint: async_validate_blocknum_in_result for endpoint in BLOCKNUM_IN_RESULT},
209211
**{
@@ -225,7 +227,12 @@ async def _async_should_cache_response(
225227
method in ASYNC_INTERNAL_VALIDATION_MAP
226228
and provider.request_cache_validation_threshold is not None
227229
):
228-
return await ASYNC_INTERNAL_VALIDATION_MAP[method](provider, params, result)
230+
cache_validator = ASYNC_INTERNAL_VALIDATION_MAP[method]
231+
return (
232+
await cache_validator(provider, params, result)
233+
if iscoroutinefunction(cache_validator)
234+
else cache_validator(provider, params, result)
235+
)
229236
return True
230237

231238

web3/_utils/caching/request_caching_validation.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ def validate_blockhash_in_params(
8282
# -- async -- #
8383

8484

85-
async def async_always_cache_request(*_args: Any, **_kwargs: Any) -> bool:
86-
return True
87-
88-
8985
async def async_is_beyond_validation_threshold(
9086
provider: ASYNC_PROVIDER_TYPE, blocknum: int
9187
) -> bool:

0 commit comments

Comments
 (0)