Skip to content

Commit

Permalink
Fix dealocks, add caching in binary search.
Browse files Browse the repository at this point in the history
  • Loading branch information
dowlandaiello committed Oct 15, 2024
1 parent 260408a commit efb95e3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)

Expand Down
3 changes: 0 additions & 3 deletions src/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 19 additions & 15 deletions src/strategies/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)
),
],
)

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

Expand All @@ -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, [])
Expand Down

0 comments on commit efb95e3

Please sign in to comment.