diff --git a/newsfragments/2185.bugfix.rst b/newsfragments/2185.bugfix.rst new file mode 100644 index 0000000000..cb4f8c097f --- /dev/null +++ b/newsfragments/2185.bugfix.rst @@ -0,0 +1 @@ +Fix bug in _is_latest_block_number_request in cache middleware diff --git a/tests/core/middleware/test_latest_block_based_cache_middleware.py b/tests/core/middleware/test_latest_block_based_cache_middleware.py index 2c5b529174..104a986062 100644 --- a/tests/core/middleware/test_latest_block_based_cache_middleware.py +++ b/tests/core/middleware/test_latest_block_based_cache_middleware.py @@ -250,3 +250,28 @@ def error_cb(method, params): w3.manager.request_blocking('fake_endpoint', []) assert next(counter) == 2 + + +def test_latest_block_cache_middleware_does_not_cache_get_latest_block( + w3_base, + block_data_middleware, + result_generator_middleware): + w3 = w3_base + w3.middleware_onion.add(block_data_middleware) + w3.middleware_onion.add(result_generator_middleware) + + current_block_hash = w3.eth.get_block('latest')['hash'] + + def cache_class(): + return { + generate_cache_key( + (current_block_hash, 'eth_getBlockByNumber', ['latest']) + ): {'result': 'value-a'}, + } + + w3.middleware_onion.add(construct_latest_block_based_cache_middleware( + cache_class=cache_class, + rpc_whitelist={'eth_getBlockByNumber'}, + )) + + assert w3.manager.request_blocking('eth_getBlockByNumber', ['latest']) != 'value-a' diff --git a/web3/middleware/cache.py b/web3/middleware/cache.py index 91331327bc..953aa4cdfe 100644 --- a/web3/middleware/cache.py +++ b/web3/middleware/cache.py @@ -12,6 +12,9 @@ cast, ) +from eth_utils import ( + is_list_like, +) import lru from web3._utils.caching import ( @@ -318,7 +321,7 @@ def middleware(method: RPCEndpoint, params: Any) -> RPCResponse: def _is_latest_block_number_request(method: RPCEndpoint, params: Any) -> bool: if method != 'eth_getBlockByNumber': return False - elif params[:1] == ['latest']: + elif is_list_like(params) and tuple(params[:1]) == ('latest',): return True return False