Skip to content

Commit a71cf44

Browse files
authored
Merge pull request #2980 from opentensor/fix/thewhaleking/optimisations
optimisations mostly related to liquidity_list
2 parents 6e76b64 + 4974adf commit a71cf44

File tree

9 files changed

+210
-152
lines changed

9 files changed

+210
-152
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
@@ -768,7 +769,8 @@ async def state_call(
768769
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
769770
return await self.substrate.rpc_request(
770771
method="state_call",
771-
params=[method, data, block_hash] if block_hash else [method, data],
772+
params=[method, data],
773+
block_hash=block_hash,
772774
reuse_block_hash=reuse_block,
773775
)
774776

@@ -2288,29 +2290,35 @@ async def get_liquidity_list(
22882290
block=block, block_hash=block_hash, reuse_block=reuse_block
22892291
)
22902292

2291-
query = self.substrate.query
2293+
# Fetch global fees and current price
2294+
fee_global_tao_query_sk = await self.substrate.create_storage_key(
2295+
pallet="Swap",
2296+
storage_function="FeeGlobalTao",
2297+
params=[netuid],
2298+
block_hash=block_hash,
2299+
)
2300+
fee_global_alpha_query_sk = await self.substrate.create_storage_key(
2301+
pallet="Swap",
2302+
storage_function="FeeGlobalAlpha",
2303+
params=[netuid],
2304+
block_hash=block_hash,
2305+
)
2306+
sqrt_price_query_sk = await self.substrate.create_storage_key(
2307+
pallet="Swap",
2308+
storage_function="AlphaSqrtPrice",
2309+
params=[netuid],
2310+
block_hash=block_hash,
2311+
)
22922312
(
2293-
fee_global_tao,
2294-
fee_global_alpha,
2295-
sqrt_price,
2313+
(fee_global_tao_query, fee_global_alpha_query, sqrt_price_query),
22962314
positions_response,
22972315
) = await asyncio.gather(
2298-
query(
2299-
module="Swap",
2300-
storage_function="FeeGlobalTao",
2301-
params=[netuid],
2302-
block_hash=block_hash,
2303-
),
2304-
query(
2305-
module="Swap",
2306-
storage_function="FeeGlobalAlpha",
2307-
params=[netuid],
2308-
block_hash=block_hash,
2309-
),
2310-
query(
2311-
module="Swap",
2312-
storage_function="AlphaSqrtPrice",
2313-
params=[netuid],
2316+
self.substrate.query_multi(
2317+
[
2318+
fee_global_tao_query_sk,
2319+
fee_global_alpha_query_sk,
2320+
sqrt_price_query_sk,
2321+
],
23142322
block_hash=block_hash,
23152323
),
23162324
self.query_map(
@@ -2321,36 +2329,46 @@ async def get_liquidity_list(
23212329
),
23222330
)
23232331
# convert to floats
2324-
fee_global_tao = fixed_to_float(fee_global_tao)
2325-
fee_global_alpha = fixed_to_float(fee_global_alpha)
2326-
sqrt_price = fixed_to_float(sqrt_price)
2332+
fee_global_tao = fixed_to_float(fee_global_tao_query[1])
2333+
fee_global_alpha = fixed_to_float(fee_global_alpha_query[1])
2334+
sqrt_price = fixed_to_float(sqrt_price_query[1])
23272335

23282336
# Fetch global fees and current price
23292337
current_tick = price_to_tick(sqrt_price**2)
23302338

23312339
# Fetch positions
2332-
positions = []
2340+
positions_values: list[tuple[dict, int, int]] = []
2341+
positions_storage_keys: list[StorageKey] = []
23332342
async for _, p in positions_response:
23342343
position = p.value
23352344

23362345
tick_low_idx = position.get("tick_low")[0]
23372346
tick_high_idx = position.get("tick_high")[0]
2338-
2339-
tick_low, tick_high = await asyncio.gather(
2340-
query(
2341-
module="Swap",
2342-
storage_function="Ticks",
2343-
params=[netuid, tick_low_idx],
2344-
block_hash=block_hash,
2345-
),
2346-
query(
2347-
module="Swap",
2348-
storage_function="Ticks",
2349-
params=[netuid, tick_high_idx],
2350-
block_hash=block_hash,
2351-
),
2347+
positions_values.append((position, tick_low_idx, tick_high_idx))
2348+
tick_low_sk = await self.substrate.create_storage_key(
2349+
pallet="Swap",
2350+
storage_function="Ticks",
2351+
params=[netuid, tick_low_idx],
2352+
block_hash=block_hash,
23522353
)
2354+
tick_high_sk = await self.substrate.create_storage_key(
2355+
pallet="Swap",
2356+
storage_function="Ticks",
2357+
params=[netuid, tick_high_idx],
2358+
block_hash=block_hash,
2359+
)
2360+
positions_storage_keys.extend([tick_low_sk, tick_high_sk])
23532361

2362+
# query all our ticks at once
2363+
ticks_query = await self.substrate.query_multi(
2364+
positions_storage_keys, block_hash=block_hash
2365+
)
2366+
# iterator with just the values
2367+
ticks = iter([x[1] for x in ticks_query])
2368+
positions = []
2369+
for position, tick_low_idx, tick_high_idx in positions_values:
2370+
tick_low = next(ticks)
2371+
tick_high = next(ticks)
23542372
# Calculate fees above/below range for both tokens
23552373
tao_below = get_fees(
23562374
current_tick=current_tick,

bittensor/core/subtensor.py

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@
99
from async_substrate_interface.substrate_addons import RetrySyncSubstrate
1010
from async_substrate_interface.sync_substrate import SubstrateInterface
1111
from async_substrate_interface.types import ScaleObj
12+
from async_substrate_interface.utils.storage import StorageKey
1213
from bittensor_drand import get_encrypted_commitment
1314
from numpy.typing import NDArray
14-
from bittensor.utils import deprecated_message
15+
1516
from bittensor.core.async_subtensor import ProposalVoteData
1617
from bittensor.core.axon import Axon
1718
from bittensor.core.chain_data import (
@@ -105,6 +106,7 @@
105106
torch,
106107
u16_normalized_float,
107108
u64_normalized_float,
109+
deprecated_message,
108110
)
109111
from bittensor.utils.balance import (
110112
Balance,
@@ -428,8 +430,7 @@ def state_call(
428430
"""
429431
block_hash = self.determine_block_hash(block)
430432
return self.substrate.rpc_request(
431-
method="state_call",
432-
params=[method, data, block_hash] if block_hash else [method, data],
433+
method="state_call", params=[method, data], block_hash=block_hash
433434
)
434435

435436
# Common subtensor calls ===========================================================================================
@@ -1530,31 +1531,41 @@ def get_liquidity_list(
15301531
logging.debug(f"Subnet {netuid} is not active.")
15311532
return None
15321533

1533-
query = self.substrate.query
15341534
block_hash = self.determine_block_hash(block)
15351535

15361536
# Fetch global fees and current price
1537-
fee_global_tao_query = query(
1538-
module="Swap",
1537+
fee_global_tao_query_sk = self.substrate.create_storage_key(
1538+
pallet="Swap",
15391539
storage_function="FeeGlobalTao",
15401540
params=[netuid],
15411541
block_hash=block_hash,
15421542
)
1543-
fee_global_alpha_query = query(
1544-
module="Swap",
1543+
fee_global_alpha_query_sk = self.substrate.create_storage_key(
1544+
pallet="Swap",
15451545
storage_function="FeeGlobalAlpha",
15461546
params=[netuid],
15471547
block_hash=block_hash,
15481548
)
1549-
sqrt_price_query = query(
1550-
module="Swap",
1549+
sqrt_price_query_sk = self.substrate.create_storage_key(
1550+
pallet="Swap",
15511551
storage_function="AlphaSqrtPrice",
15521552
params=[netuid],
15531553
block_hash=block_hash,
15541554
)
1555-
fee_global_tao = fixed_to_float(fee_global_tao_query)
1556-
fee_global_alpha = fixed_to_float(fee_global_alpha_query)
1557-
sqrt_price = fixed_to_float(sqrt_price_query)
1555+
fee_global_tao_query, fee_global_alpha_query, sqrt_price_query = (
1556+
self.substrate.query_multi(
1557+
[
1558+
fee_global_tao_query_sk,
1559+
fee_global_alpha_query_sk,
1560+
sqrt_price_query_sk,
1561+
],
1562+
block_hash=block_hash,
1563+
)
1564+
)
1565+
1566+
fee_global_tao = fixed_to_float(fee_global_tao_query[1])
1567+
fee_global_alpha = fixed_to_float(fee_global_alpha_query[1])
1568+
sqrt_price = fixed_to_float(sqrt_price_query[1])
15581569
current_tick = price_to_tick(sqrt_price**2)
15591570

15601571
# Fetch positions
@@ -1564,26 +1575,38 @@ def get_liquidity_list(
15641575
block=block,
15651576
params=[netuid, wallet.coldkeypub.ss58_address],
15661577
)
1567-
1568-
positions = []
1578+
positions_values: list[tuple[dict, int, int]] = []
1579+
positions_storage_keys: list[StorageKey] = []
15691580
for _, p in positions_response:
15701581
position = p.value
15711582

15721583
tick_low_idx = position["tick_low"][0]
15731584
tick_high_idx = position["tick_high"][0]
15741585

1575-
tick_low = query(
1576-
module="Swap",
1586+
tick_low_sk = self.substrate.create_storage_key(
1587+
pallet="Swap",
15771588
storage_function="Ticks",
15781589
params=[netuid, tick_low_idx],
15791590
block_hash=block_hash,
15801591
)
1581-
tick_high = query(
1582-
module="Swap",
1592+
tick_high_sk = self.substrate.create_storage_key(
1593+
pallet="Swap",
15831594
storage_function="Ticks",
15841595
params=[netuid, tick_high_idx],
15851596
block_hash=block_hash,
15861597
)
1598+
positions_values.append((position, tick_low_idx, tick_high_idx))
1599+
positions_storage_keys.extend([tick_low_sk, tick_high_sk])
1600+
# query all our ticks at once
1601+
ticks_query = self.substrate.query_multi(
1602+
positions_storage_keys, block_hash=block_hash
1603+
)
1604+
# iterator with just the values
1605+
ticks = iter([x[1] for x in ticks_query])
1606+
positions = []
1607+
for position, tick_low_idx, tick_high_idx in positions_values:
1608+
tick_low = next(ticks)
1609+
tick_high = next(ticks)
15871610

15881611
# Calculate fees above/below range for both tokens
15891612
tao_below = get_fees(

bittensor/utils/balance.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,7 @@ def fixed_to_float(
350350
) -> float:
351351
# By default, this is a U64F64
352352
# which is 64 bits of integer and 64 bits of fractional
353-
354-
data: int = fixed["bits"]
353+
data: int = fb.value if isinstance((fb := fixed["bits"]), ScaleType) else fb
355354

356355
# Logical and to get the fractional part; remaining is the integer part
357356
fractional_part = data & (2**frac_bits - 1)

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.2"
4040
]
4141

4242
[project.optional-dependencies]

tests/unit_tests/extrinsics/asyncex/test_registration.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import pytest
22

3-
from bittensor.core import async_subtensor
43
from bittensor.core.extrinsics.asyncex import registration as async_registration
54

65

tests/unit_tests/extrinsics/test_set_weights.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
from unittest.mock import MagicMock, patch
22

33
import pytest
4-
import torch
4+
# import torch
55

6-
from bittensor.core import subtensor as subtensor_module
76
from bittensor.core.extrinsics.set_weights import (
87
_do_set_weights,
98
set_weights_extrinsic,

0 commit comments

Comments
 (0)