Skip to content

Commit

Permalink
fix: split trace_filter batches in half if too much
Browse files Browse the repository at this point in the history
  • Loading branch information
BobTheBuidler authored Dec 11, 2024
1 parent c9cf9cc commit 041847e
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions eth_portfolio/_ledgers/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,24 @@ class InternalTransfersList(PandableList[InternalTransfer]):
"""


trace_filter = a_sync.Semaphore(64, __name__ + ".trace_semaphore")(
eth_retry.auto_retry(dank_mids.eth.trace_filter)
)

@a_sync.Semaphore(64, __name__ + ".trace_semaphore")
@eth_retry.auto_retry
async def trace_filter(fromBlock: int, toBlock: int, **kwargs) -> List[FilterTrace]:
try:
return await dank_mids.eth.trace_filter({"fromBlock": fromBlock, "toBlock": toBlock, **kwargs})
except ClientResponseError as e:
if e.status != HTTPStatus.SERVICE_UNAVAILABLE or toBlock == fromBlock:
raise

range_size = toBlock - fromBlock + 1
chunk_size = range_size // 2
halfway = fromBlock + chunk_size
results = await asyncio.gather(
trace_filter(fromBlock=fromBlock, toBlock=halfway, **kwargs),
trace_filter(fromBlock=halfway+1, toBlock=toBlock, **kwargs),
)
return results[0] + results[1]


@alru_cache(maxsize=None)
async def get_transaction_status(txhash: str) -> Status:
Expand Down Expand Up @@ -484,7 +498,7 @@ async def get_traces(filter_params: TraceFilterParams) -> List[FilterTrace]:

check_status_tasks = a_sync.TaskMapping(get_transaction_status)

for trace in await trace_filter(filter_params):
for trace in await trace_filter(**filter_params):
if "error" in trace:
continue

Expand Down

0 comments on commit 041847e

Please sign in to comment.