Skip to content

Commit 9801676

Browse files
committed
[WIP] Optimisations
1 parent 9f47cf6 commit 9801676

File tree

2 files changed

+46
-31
lines changed

2 files changed

+46
-31
lines changed

bittensor/core/subtensor.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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
1415

@@ -429,7 +430,8 @@ def state_call(
429430
block_hash = self.determine_block_hash(block)
430431
return self.substrate.rpc_request(
431432
method="state_call",
432-
params=[method, data, block_hash] if block_hash else [method, data],
433+
params=[method, data],
434+
block_hash=block_hash
433435
)
434436

435437
# Common subtensor calls ===========================================================================================
@@ -484,20 +486,26 @@ def blocks_since_last_step(
484486
)
485487
return query.value if query is not None and hasattr(query, "value") else query
486488

487-
def blocks_since_last_update(self, netuid: int, uid: int) -> Optional[int]:
489+
def blocks_since_last_update(self, netuid: int, uid: int, block: Optional[int] = None) -> Optional[int]:
488490
"""
489491
Returns the number of blocks since the last update for a specific UID in the subnetwork.
490492
491493
Arguments:
492494
netuid (int): The unique identifier of the subnetwork.
493495
uid (int): The unique identifier of the neuron.
496+
block (int): The block number for this query.
494497
495498
Returns:
496499
Optional[int]: The number of blocks since the last update, or ``None`` if the subnetwork or UID does not
497500
exist.
498501
"""
499-
call = self.get_hyperparameter(param_name="LastUpdate", netuid=netuid)
500-
return None if not call else (self.get_current_block() - int(call[uid]))
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])
501509

502510
def bonds(
503511
self, netuid: int, block: Optional[int] = None
@@ -1492,31 +1500,25 @@ def get_liquidity_list(
14921500
logging.debug(f"Subnet {netuid} is not active.")
14931501
return None
14941502

1495-
query = self.substrate.query
14961503
block_hash = self.determine_block_hash(block)
14971504

14981505
# Fetch global fees and current price
1499-
fee_global_tao_query = query(
1500-
module="Swap",
1501-
storage_function="FeeGlobalTao",
1502-
params=[netuid],
1503-
block_hash=block_hash,
1506+
fee_global_tao_query_sk = self.substrate.create_storage_key(
1507+
pallet="Swap", storage_function="FeeGlobalTao", params=[netuid], block_hash=block_hash
15041508
)
1505-
fee_global_alpha_query = query(
1506-
module="Swap",
1507-
storage_function="FeeGlobalAlpha",
1508-
params=[netuid],
1509-
block_hash=block_hash,
1509+
fee_global_alpha_query_sk = self.substrate.create_storage_key(
1510+
pallet="Swap", storage_function="FeeGlobalAlpha", params=[netuid], block_hash=block_hash
15101511
)
1511-
sqrt_price_query = query(
1512-
module="Swap",
1513-
storage_function="AlphaSqrtPrice",
1514-
params=[netuid],
1515-
block_hash=block_hash,
1512+
sqrt_price_query_sk = self.substrate.create_storage_key(
1513+
pallet="Swap", storage_function="AlphaSqrtPrice", params=[netuid], block_hash=block_hash
15161514
)
1517-
fee_global_tao = fixed_to_float(fee_global_tao_query)
1518-
fee_global_alpha = fixed_to_float(fee_global_alpha_query)
1519-
sqrt_price = fixed_to_float(sqrt_price_query)
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)
1518+
1519+
fee_global_tao = fixed_to_float(fee_global_tao_query[1])
1520+
fee_global_alpha = fixed_to_float(fee_global_alpha_query[1])
1521+
sqrt_price = fixed_to_float(sqrt_price_query[1])
15201522
current_tick = price_to_tick(sqrt_price**2)
15211523

15221524
# Fetch positions
@@ -1526,26 +1528,36 @@ def get_liquidity_list(
15261528
block=block,
15271529
params=[netuid, wallet.coldkeypub.ss58_address],
15281530
)
1529-
1530-
positions = []
1531+
positions_values: list[tuple[dict, int, int]] = []
1532+
positions_storage_keys: list[StorageKey] = []
15311533
for _, p in positions_response:
15321534
position = p.value
15331535

15341536
tick_low_idx = position["tick_low"][0]
15351537
tick_high_idx = position["tick_high"][0]
15361538

1537-
tick_low = query(
1538-
module="Swap",
1539+
tick_low_sk = self.substrate.create_storage_key(
1540+
pallet="Swap",
15391541
storage_function="Ticks",
15401542
params=[netuid, tick_low_idx],
15411543
block_hash=block_hash,
15421544
)
1543-
tick_high = query(
1544-
module="Swap",
1545+
tick_high_sk = self.substrate.create_storage_key(
1546+
pallet="Swap",
15451547
storage_function="Ticks",
15461548
params=[netuid, tick_high_idx],
1547-
block_hash=block_hash,
1549+
block_hash=block_hash
15481550
)
1551+
positions_values.append((position, tick_low_idx, tick_high_idx))
1552+
positions_storage_keys.extend([tick_low_sk, tick_high_sk])
1553+
# query all our ticks at once
1554+
ticks_query = self.substrate.query_multi(positions_storage_keys, block_hash=block_hash)
1555+
# iterator with just the values
1556+
ticks = iter([x[1] for x in ticks_query])
1557+
positions = []
1558+
for position, tick_low_idx, tick_high_idx in positions_values:
1559+
tick_low = next(ticks)
1560+
tick_high = next(ticks)
15491561

15501562
# Calculate fees above/below range for both tokens
15511563
tao_below = get_fees(

tests/e2e_tests/conftest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@
1818
setup_wallet,
1919
)
2020

21-
LOCALNET_IMAGE_NAME = os.getenv("LOCALNET_IMAGE_NAME") or "ghcr.io/opentensor/subtensor-localnet:devnet-ready"
21+
LOCALNET_IMAGE_NAME = (
22+
os.getenv("LOCALNET_IMAGE_NAME")
23+
or "ghcr.io/opentensor/subtensor-localnet:devnet-ready"
24+
)
2225
CONTAINER_NAME_PREFIX = "test_local_chain_"
2326

2427

0 commit comments

Comments
 (0)