From efb95e3f33bc4a2131e9400b71d6c77610eaa50f Mon Sep 17 00:00:00 2001 From: Dowland Aiello Date: Tue, 15 Oct 2024 14:47:50 +0000 Subject: [PATCH] Fix dealocks, add caching in binary search. --- main.py | 4 ++-- src/scheduler.py | 3 --- src/strategies/util.py | 34 +++++++++++++++++++--------------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/main.py b/main.py index a42d382c..5dc563a1 100644 --- a/main.py +++ b/main.py @@ -17,7 +17,7 @@ from typing import Any, cast from cosmpy.aerial.client import LedgerClient from cosmpy.aerial.wallet import LocalWallet -from src.scheduler import Scheduler, Ctx +from src.scheduler import Scheduler, Ctx, MAX_SKIP_CONCURRENT_CALLS from src.util import ( custom_neutron_network_config, DISCOVERY_CONCURRENCY_FACTOR, @@ -215,7 +215,7 @@ async def main() -> None: chain_id: load_chain_info(info) for (chain_id, info) in denom_file["chain_info"].items() }, - Semaphore(), + Semaphore(MAX_SKIP_CONCURRENT_CALLS), ).recover_history() sched = Scheduler(ctx, strategy) diff --git a/src/scheduler.py b/src/scheduler.py index 09bea634..8e6259ee 100644 --- a/src/scheduler.py +++ b/src/scheduler.py @@ -268,13 +268,10 @@ async def query_denom_route( from_chain_info = await self.query_chain_info( transfer_info["from_chain_id"] ) - print("here", transfer_info["to_chain_id"]) to_chain_info = await self.query_chain_info( transfer_info["to_chain_id"] ) - print(to_chain_info) - if not from_chain_info or not to_chain_info: return None diff --git a/src/strategies/util.py b/src/strategies/util.py index 06404224..3da3ae6d 100644 --- a/src/strategies/util.py +++ b/src/strategies/util.py @@ -1209,31 +1209,33 @@ async def quantities_for_route_profit( mid = starting_amount // 2 plans: dict[int, list[int]] = {} - - # Plans sorted by profit, for purposes of returning the best plan plans_by_profit: list[int] = [] attempts: int = 0 - while mid > 0 and mid <= starting_amount and attempts < MAX_EVAL_PROBES: + while ( + left != right + and mid != right + and mid > 0 + and mid <= starting_amount + and attempts < MAX_EVAL_PROBES + ): attempts += 1 - quantities: list[int] = await quantities_for_starting_amount(mid, route) + quantities: list[int] = ( + plans[mid] + if mid in plans + else await quantities_for_starting_amount(mid, route) + ) plans[mid] = quantities ctx.log_route( r, "info", - "Got execution plan @ %d: [%s] (best candidates: [%s])", + "Got execution plan @ %d: [%s]", [ mid, ", ".join((str(qty) for qty in quantities)), - ", ".join( - ( - f"[{', '.join((str(qty) for qty in plans[plan_idx]))}]" - for plan_idx in plans_by_profit[:-5] - ) - ), ], ) @@ -1244,14 +1246,14 @@ async def quantities_for_route_profit( # Insert in sorted position if len(quantities) > len(route): - insort(plans_by_profit, mid, key=lambda idx: plans[idx][-1] - plans[idx][0]) + plans_by_profit.append(mid) # Continue checking plans, since this quantity was not profitable if len(quantities) <= len(route) or profit <= 0: right = mid mid = left + (right - left) // 2 - ctx.log_route(r, "info", "Probing lower execution plans", []) + ctx.log_route(r, "debug", "Probing lower execution plans", []) continue @@ -1269,9 +1271,11 @@ async def quantities_for_route_profit( # This plan is profitable, but a bigger plan might be even more profitable left = mid - mid += (right - left) // 2 + mid = (right - left) // 2 + + ctx.log_route(r, "debug", "Probing higher execution plans", []) - ctx.log_route(r, "info", "Probing higher execution plans", []) + plans_by_profit.sort(key=lambda idx: plans[idx][-1] - plans[idx][0]) if len(plans_by_profit) == 0: return (0, [])