Skip to content

Commit

Permalink
Fix get cached latest block (#2346)
Browse files Browse the repository at this point in the history
* Fix get cached latest block
  • Loading branch information
kamyab98 authored and pacrob committed May 23, 2022
1 parent 7f525d7 commit 0dcf2ba
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions newsfragments/2185.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix bug in _is_latest_block_number_request in cache middleware
25 changes: 25 additions & 0 deletions tests/core/middleware/test_latest_block_based_cache_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
5 changes: 4 additions & 1 deletion web3/middleware/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
cast,
)

from eth_utils import (
is_list_like,
)
import lru

from web3._utils.caching import (
Expand Down Expand Up @@ -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

Expand Down

0 comments on commit 0dcf2ba

Please sign in to comment.