Skip to content

Commit 0761f0d

Browse files
committed
Fixes liquidity_list abysmal performance
1 parent 9801676 commit 0761f0d

File tree

3 files changed

+90
-60
lines changed

3 files changed

+90
-60
lines changed

bittensor/core/async_subtensor.py

Lines changed: 57 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import scalecodec
1111
from async_substrate_interface import AsyncSubstrateInterface
1212
from async_substrate_interface.substrate_addons import RetryAsyncSubstrate
13+
from async_substrate_interface.utils.storage import StorageKey
1314
from bittensor_drand import get_encrypted_commitment
1415
from bittensor_wallet.utils import SS58_FORMAT
1516
from numpy.typing import NDArray
@@ -767,7 +768,8 @@ async def state_call(
767768
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
768769
return await self.substrate.rpc_request(
769770
method="state_call",
770-
params=[method, data, block_hash] if block_hash else [method, data],
771+
params=[method, data],
772+
block_hash=block_hash,
771773
reuse_block_hash=reuse_block,
772774
)
773775

@@ -2242,29 +2244,35 @@ async def get_liquidity_list(
22422244
block=block, block_hash=block_hash, reuse_block=reuse_block
22432245
)
22442246

2245-
query = self.substrate.query
2247+
# Fetch global fees and current price
2248+
fee_global_tao_query_sk = await self.substrate.create_storage_key(
2249+
pallet="Swap",
2250+
storage_function="FeeGlobalTao",
2251+
params=[netuid],
2252+
block_hash=block_hash,
2253+
)
2254+
fee_global_alpha_query_sk = await self.substrate.create_storage_key(
2255+
pallet="Swap",
2256+
storage_function="FeeGlobalAlpha",
2257+
params=[netuid],
2258+
block_hash=block_hash,
2259+
)
2260+
sqrt_price_query_sk = await self.substrate.create_storage_key(
2261+
pallet="Swap",
2262+
storage_function="AlphaSqrtPrice",
2263+
params=[netuid],
2264+
block_hash=block_hash,
2265+
)
22462266
(
2247-
fee_global_tao,
2248-
fee_global_alpha,
2249-
sqrt_price,
2267+
(fee_global_tao_query, fee_global_alpha_query, sqrt_price_query),
22502268
positions_response,
22512269
) = await asyncio.gather(
2252-
query(
2253-
module="Swap",
2254-
storage_function="FeeGlobalTao",
2255-
params=[netuid],
2256-
block_hash=block_hash,
2257-
),
2258-
query(
2259-
module="Swap",
2260-
storage_function="FeeGlobalAlpha",
2261-
params=[netuid],
2262-
block_hash=block_hash,
2263-
),
2264-
query(
2265-
module="Swap",
2266-
storage_function="AlphaSqrtPrice",
2267-
params=[netuid],
2270+
self.substrate.query_multi(
2271+
[
2272+
fee_global_tao_query_sk,
2273+
fee_global_alpha_query_sk,
2274+
sqrt_price_query_sk,
2275+
],
22682276
block_hash=block_hash,
22692277
),
22702278
self.query_map(
@@ -2275,36 +2283,46 @@ async def get_liquidity_list(
22752283
),
22762284
)
22772285
# convert to floats
2278-
fee_global_tao = fixed_to_float(fee_global_tao)
2279-
fee_global_alpha = fixed_to_float(fee_global_alpha)
2280-
sqrt_price = fixed_to_float(sqrt_price)
2286+
fee_global_tao = fixed_to_float(fee_global_tao_query[1])
2287+
fee_global_alpha = fixed_to_float(fee_global_alpha_query[1])
2288+
sqrt_price = fixed_to_float(sqrt_price_query[1])
22812289

22822290
# Fetch global fees and current price
22832291
current_tick = price_to_tick(sqrt_price**2)
22842292

22852293
# Fetch positions
2286-
positions = []
2294+
positions_values: list[tuple[dict, int, int]] = []
2295+
positions_storage_keys: list[StorageKey] = []
22872296
async for _, p in positions_response:
22882297
position = p.value
22892298

22902299
tick_low_idx = position.get("tick_low")[0]
22912300
tick_high_idx = position.get("tick_high")[0]
2292-
2293-
tick_low, tick_high = await asyncio.gather(
2294-
query(
2295-
module="Swap",
2296-
storage_function="Ticks",
2297-
params=[netuid, tick_low_idx],
2298-
block_hash=block_hash,
2299-
),
2300-
query(
2301-
module="Swap",
2302-
storage_function="Ticks",
2303-
params=[netuid, tick_high_idx],
2304-
block_hash=block_hash,
2305-
),
2301+
positions_values.append((position, tick_low_idx, tick_high_idx))
2302+
tick_low_sk = await self.substrate.create_storage_key(
2303+
pallet="Swap",
2304+
storage_function="Ticks",
2305+
params=[netuid, tick_low_idx],
2306+
block_hash=block_hash,
23062307
)
2308+
tick_high_sk = await self.substrate.create_storage_key(
2309+
pallet="Swap",
2310+
storage_function="Ticks",
2311+
params=[netuid, tick_high_idx],
2312+
block_hash=block_hash,
2313+
)
2314+
positions_storage_keys.extend([tick_low_sk, tick_high_sk])
23072315

2316+
# query all our ticks at once
2317+
ticks_query = await self.substrate.query_multi(
2318+
positions_storage_keys, block_hash=block_hash
2319+
)
2320+
# iterator with just the values
2321+
ticks = iter([x[1] for x in ticks_query])
2322+
positions = []
2323+
for position, tick_low_idx, tick_high_idx in positions_values:
2324+
tick_low = next(ticks)
2325+
tick_high = next(ticks)
23082326
# Calculate fees above/below range for both tokens
23092327
tao_below = get_fees(
23102328
current_tick=current_tick,

bittensor/core/subtensor.py

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -429,9 +429,7 @@ def state_call(
429429
"""
430430
block_hash = self.determine_block_hash(block)
431431
return self.substrate.rpc_request(
432-
method="state_call",
433-
params=[method, data],
434-
block_hash=block_hash
432+
method="state_call", params=[method, data], block_hash=block_hash
435433
)
436434

437435
# Common subtensor calls ===========================================================================================
@@ -486,26 +484,22 @@ def blocks_since_last_step(
486484
)
487485
return query.value if query is not None and hasattr(query, "value") else query
488486

489-
def blocks_since_last_update(self, netuid: int, uid: int, block: Optional[int] = None) -> Optional[int]:
487+
def blocks_since_last_update(self, netuid: int, uid: int) -> Optional[int]:
490488
"""
491489
Returns the number of blocks since the last update for a specific UID in the subnetwork.
492490
493491
Arguments:
494492
netuid (int): The unique identifier of the subnetwork.
495493
uid (int): The unique identifier of the neuron.
496-
block (int): The block number for this query.
497494
498495
Returns:
499496
Optional[int]: The number of blocks since the last update, or ``None`` if the subnetwork or UID does not
500497
exist.
501498
"""
502-
call = self.get_hyperparameter(param_name="LastUpdate", netuid=netuid, block=block)
503-
if not call:
504-
return None
505-
elif block is not None:
506-
return block - int(call[uid])
507-
else:
508-
return self.get_current_block() - int(call[uid])
499+
call = self.get_hyperparameter(
500+
param_name="LastUpdate", netuid=netuid, block=block
501+
)
502+
return None if call is None else self.get_current_block() - int(call[uid])
509503

510504
def bonds(
511505
self, netuid: int, block: Optional[int] = None
@@ -1504,17 +1498,33 @@ def get_liquidity_list(
15041498

15051499
# Fetch global fees and current price
15061500
fee_global_tao_query_sk = self.substrate.create_storage_key(
1507-
pallet="Swap", storage_function="FeeGlobalTao", params=[netuid], block_hash=block_hash
1501+
pallet="Swap",
1502+
storage_function="FeeGlobalTao",
1503+
params=[netuid],
1504+
block_hash=block_hash,
15081505
)
15091506
fee_global_alpha_query_sk = self.substrate.create_storage_key(
1510-
pallet="Swap", storage_function="FeeGlobalAlpha", params=[netuid], block_hash=block_hash
1507+
pallet="Swap",
1508+
storage_function="FeeGlobalAlpha",
1509+
params=[netuid],
1510+
block_hash=block_hash,
15111511
)
15121512
sqrt_price_query_sk = self.substrate.create_storage_key(
1513-
pallet="Swap", storage_function="AlphaSqrtPrice", params=[netuid], block_hash=block_hash
1513+
pallet="Swap",
1514+
storage_function="AlphaSqrtPrice",
1515+
params=[netuid],
1516+
block_hash=block_hash,
1517+
)
1518+
fee_global_tao_query, fee_global_alpha_query, sqrt_price_query = (
1519+
self.substrate.query_multi(
1520+
[
1521+
fee_global_tao_query_sk,
1522+
fee_global_alpha_query_sk,
1523+
sqrt_price_query_sk,
1524+
],
1525+
block_hash=block_hash,
1526+
)
15141527
)
1515-
fee_global_tao_query, fee_global_alpha_query, sqrt_price_query = self.substrate.query_multi(
1516-
[fee_global_tao_query_sk, fee_global_alpha_query_sk, sqrt_price_query_sk],
1517-
block_hash=block_hash)
15181528

15191529
fee_global_tao = fixed_to_float(fee_global_tao_query[1])
15201530
fee_global_alpha = fixed_to_float(fee_global_alpha_query[1])
@@ -1546,12 +1556,14 @@ def get_liquidity_list(
15461556
pallet="Swap",
15471557
storage_function="Ticks",
15481558
params=[netuid, tick_high_idx],
1549-
block_hash=block_hash
1559+
block_hash=block_hash,
15501560
)
15511561
positions_values.append((position, tick_low_idx, tick_high_idx))
15521562
positions_storage_keys.extend([tick_low_sk, tick_high_sk])
15531563
# query all our ticks at once
1554-
ticks_query = self.substrate.query_multi(positions_storage_keys, block_hash=block_hash)
1564+
ticks_query = self.substrate.query_multi(
1565+
positions_storage_keys, block_hash=block_hash
1566+
)
15551567
# iterator with just the values
15561568
ticks = iter([x[1] for x in ticks_query])
15571569
positions = []

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ dependencies = [
3636
"uvicorn",
3737
"bittensor-drand>=0.5.0",
3838
"bittensor-wallet>=3.1.0",
39-
"async-substrate-interface>=1.3.1"
39+
"async-substrate-interface>=1.4.1"
4040
]
4141

4242
[project.optional-dependencies]

0 commit comments

Comments
 (0)