Skip to content

Commit

Permalink
fix: reattempt_call_and_return_exception (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored May 17, 2022
1 parent b4e0f39 commit cb16e2a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
8 changes: 4 additions & 4 deletions dank_mids/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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):
Expand Down
20 changes: 18 additions & 2 deletions tests/test_dank_mids.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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

0 comments on commit cb16e2a

Please sign in to comment.