Skip to content

Commit

Permalink
work around for abci_query id bypass
Browse files Browse the repository at this point in the history
  • Loading branch information
Reecepbcups committed Feb 11, 2023
1 parent 4b2c94b commit 6bda138
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
7 changes: 4 additions & 3 deletions HELPERS.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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])
Expand Down
8 changes: 6 additions & 2 deletions RequestsHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 15 additions & 3 deletions rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6bda138

Please sign in to comment.