From cb16e2ab1f95de395ab5d4654d221e7800df1d4f Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Mon, 16 May 2022 23:58:11 -0400 Subject: [PATCH] fix: reattempt_call_and_return_exception (#2) --- dank_mids/controller.py | 8 ++++---- tests/test_dank_mids.py | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/dank_mids/controller.py b/dank_mids/controller.py index 0b6c4ce4..69cb3698 100644 --- a/dank_mids/controller.py +++ b/dank_mids/controller.py @@ -20,12 +20,12 @@ from dank_mids.loggers import (demo_logger, main_logger, sort_lazy_logger, sort_logger) -instances = [] +instances: List["DankMiddlewareController"] = [] -def reattempt_call_and_return_exception(inputs: List, block: str, web3: Web3) -> Exception: +def reattempt_call_and_return_exception(target: str, calldata: bytes, block: str, web3: Web3) -> Exception: """ NOTE: This runs synchronously in a subprocess in order to bypass Dank Middleware without blocking the event loop. """ try: - return web3.eth.call({"to": inputs[0], "data": inputs[1]}, block) + return web3.eth.call({"to": target, "data": calldata}, block) except Exception as e: return e @@ -213,7 +213,7 @@ async def spoof_response(self, cid: int, block: str, params: List, data: Optiona or (isinstance(data, bytes) and HexBytes(data).hex() in BAD_HEXES) ): target, calldata = params - data = await run_in_subprocess(reattempt_call_and_return_exception, [target, calldata], self.sync_w3) + data = await run_in_subprocess(reattempt_call_and_return_exception, target, calldata, block, self.sync_w3) # We were able to get a usable response from single call. # Add contract to DO_NOT_BATCH list if not isinstance(data, Exception): diff --git a/tests/test_dank_mids.py b/tests/test_dank_mids.py index 04d344f6..1f52a54c 100644 --- a/tests/test_dank_mids.py +++ b/tests/test_dank_mids.py @@ -1,10 +1,13 @@ -from dank_mids.controller import instances, demo_logger +import logging + +from brownie import chain +from dank_mids.controller import demo_logger, instances from multicall import Call from multicall.utils import await_awaitable, gather -import logging from tests.fixtures import dank_w3 + demo_logger.addHandler(logging.StreamHandler()) demo_logger.setLevel(logging.DEBUG) @@ -31,3 +34,16 @@ def test_dank_middleware(): assert mid < cid / 5, f"Batched {cid} calls into {mid} multicalls. Performance underwhelming." assert bid < mid / 2, f"{bid} batches required {mid} multicalls. Performance underwhelming." +def test_bad_hex_handling(): + chainlinkfeed = "0xfe67209f6FE3BA6cE36d0941700085C194e958DF" + assert await_awaitable(Call(chainlinkfeed, 'latestAnswer()(uint)', block_id=14_000_000).coroutine()) == 15717100 + assert chainlinkfeed in instances[0].DO_NOT_BATCH + +def test_next_cid(): + assert instances[0].next_cid + 1 == instances[0].next_cid + +def test_next_mid(): + assert instances[0].next_mid + 1 == instances[0].next_mid + +def test_next_bid(): + assert instances[0].next_bid + 1 == instances[0].next_bid