From 6bda1387876e88562f9974620149b0555ecb5061 Mon Sep 17 00:00:00 2001 From: Reece Williams Date: Sat, 11 Feb 2023 14:48:27 -0600 Subject: [PATCH] work around for abci_query id bypass --- HELPERS.py | 7 ++++--- RequestsHandler.py | 8 ++++++-- rpc.py | 18 +++++++++++++++--- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/HELPERS.py b/HELPERS.py index a004edd..daf7e3d 100644 --- a/HELPERS.py +++ b/HELPERS.py @@ -1,8 +1,9 @@ import re from os import getenv -import CONFIG import httpx + +import CONFIG from CONFIG import REDIS_DB from HELPERS_TYPES import CallType, Mode @@ -29,14 +30,14 @@ def ttl_block_only(cache_seconds: int = 0): } -def increment_call_value(key, amount: int = 1): +def increment_call_value(key: str, amount: int = 1): global total_calls if CONFIG.ENABLE_COUNTER == False: return if key not in total_calls: - total_calls[key] = 0 + total_calls[str(key)] = 0 if total_calls[key] >= CONFIG.INC_EVERY: REDIS_DB.incr(f"{key}", amount=total_calls[key]) diff --git a/RequestsHandler.py b/RequestsHandler.py index 4ddceaa..9544982 100644 --- a/RequestsHandler.py +++ b/RequestsHandler.py @@ -19,11 +19,15 @@ def set_cache_for_time_if_valid( use_hset: bool = False, second_key: str = "", ): - increment_call_value(call_key) + if cache_seconds == Mode.NO_CACHE.value: + # useful for broadcasted txs + return + if status_code == 200: - if cache_seconds == Mode.FOR_BLOCK_TIME.value: # -2 + # -2 = clear when a new block is minted + if cache_seconds == Mode.FOR_BLOCK_TIME.value: if CONFIG.DEFAULT_CACHE_SECONDS > 0: cache_seconds = CONFIG.DEFAULT_CACHE_SECONDS else: diff --git a/rpc.py b/rpc.py index 7f61493..968a2b2 100644 --- a/rpc.py +++ b/rpc.py @@ -187,15 +187,27 @@ def post_rpc_endpoint(): use_hset = use_redis_hashset(method) key = f"{CONFIG.RPC_PREFIX};{ttl_block_only(cache_seconds)};{method}" # We save/get requests data since it also has the id of said requests from json RPC. + + modified_data = dict(REQ_DATA) + original_req_id = int(dict(REQ_DATA).get("id", 0)) + + # we set the save key as -1 id since that is not real. This way on requests we are forced to change it back to the original requests + # this ensures we cache things such as status independent of the requested id. + modified_data["id"] = -1 + if use_hset: - v = REDIS_DB.hget(key, str(REQ_DATA)) + v = REDIS_DB.hget(key, str(modified_data)) else: - key = f"{key};{REQ_DATA}" + key = f"{key};{modified_data}" v = REDIS_DB.get(key) if v: increment_call_value(CallType.RPC_POST_CACHE.value) - return jsonify(json.loads(v)) + # replace the id with the original id so the requests is valid and in the order requested. + # else we get: Error: wrong ID: response ID (0) does not match request ID (1) + v = json.loads(v) + v["id"] = original_req_id + return jsonify(v) res = RPC_HANDLER.handle_single_rpc_post_request( json.dumps(REQ_DATA), key, method, cache_seconds, use_hset