Skip to content

Commit

Permalink
fix: trace_filter batch splitting logic (#139)
Browse files Browse the repository at this point in the history
* chore: add detail to exc

* chore: `black .`

* fix: trace filter range chunking

* chore: `black .`

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
BobTheBuidler and github-actions[bot] authored Dec 15, 2024
1 parent 3ff1f08 commit 5b2a70e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
3 changes: 2 additions & 1 deletion eth_portfolio/_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
BASE_PATH = f"./cache/{chain.id}/"
EXECUTOR = a_sync.PruningThreadPoolExecutor(32)


def cache_to_disk(fn: AnyFn[P, T]) -> AnyFn[P, T]:
module = fn.__module__.replace(".", "/")
cache_path_for_fn = BASE_PATH + module + "/" + fn.__name__
Expand All @@ -35,7 +36,7 @@ async def disk_cache_wrap(*args: P.args, **kwargs: P.kwargs) -> T:
async with aiofiles.open(cache_path, "rb", executor=EXECUTOR) as f:
with contextlib.suppress(EOFError):
return pickle.loads(await f.read())

async_result: T = await fn(*args, **kwargs)
await __cache_write(cache_path, async_result)
return async_result
Expand Down
8 changes: 5 additions & 3 deletions eth_portfolio/_ledgers/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,10 +473,12 @@ async def trace_filter(fromBlock: int, toBlock: int, **kwargs) -> List[FilterTra
except ClientResponseError as e:
if e.status != HTTPStatus.SERVICE_UNAVAILABLE or toBlock == fromBlock:
raise

range_size = toBlock - fromBlock + 1

from_block = int(fromBlock, 16)
range_size = int(toBlock, 16) - from_block + 1
chunk_size = range_size // 2
halfway = fromBlock + chunk_size
halfway = from_block + chunk_size

results = await asyncio.gather(
trace_filter(fromBlock=fromBlock, toBlock=halfway, **kwargs),
trace_filter(fromBlock=halfway + 1, toBlock=toBlock, **kwargs),
Expand Down
2 changes: 1 addition & 1 deletion eth_portfolio/_loaders/token_transfer.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ async def get_symbol(token: ERC20) -> Optional[str]:
return None


@alru_cache(ttl=60*60)
@alru_cache(ttl=60 * 60)
@stuck_coro_debugger
@cache_to_disk
async def _get_transaction_index(hash: str) -> int:
Expand Down

0 comments on commit 5b2a70e

Please sign in to comment.