Skip to content

Commit 97961e6

Browse files
authored
Merge pull request #560 from opentensor/fix/thewhaleking/improve-speed-of-fetch-coldkey-hotkey-identities
Improved speed of query_all_identities and fetch_coldkey_hotkey_identities
2 parents b0531b6 + 45b4053 commit 97961e6

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

bittensor_cli/src/bittensor/subtensor_interface.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from typing import Optional, Any, Union, TypedDict, Iterable
44

55
import aiohttp
6+
from async_substrate_interface.utils.storage import StorageKey
67
from bittensor_wallet import Wallet
78
from bittensor_wallet.utils import SS58_FORMAT
89
from scalecodec import GenericCall
@@ -881,9 +882,10 @@ async def query_all_identities(
881882
storage_function="IdentitiesV2",
882883
block_hash=block_hash,
883884
reuse_block_hash=reuse_block,
885+
fully_exhaust=True,
884886
)
885887
all_identities = {}
886-
async for ss58_address, identity in identities:
888+
for ss58_address, identity in identities.records:
887889
all_identities[decode_account_id(ss58_address[0])] = decode_hex_identity(
888890
identity.value
889891
)
@@ -939,22 +941,22 @@ async def fetch_coldkey_hotkey_identities(
939941
:param reuse_block: Whether to reuse the last-used blockchain block hash.
940942
:return: Dict with 'coldkeys' and 'hotkeys' as keys.
941943
"""
942-
943-
coldkey_identities = await self.query_all_identities()
944+
if block_hash is None:
945+
block_hash = await self.substrate.get_chain_head()
946+
coldkey_identities = await self.query_all_identities(block_hash=block_hash)
944947
identities = {"coldkeys": {}, "hotkeys": {}}
945-
if not coldkey_identities:
946-
return identities
947-
query = await self.substrate.query_multiple( # TODO probably more efficient to do this with query_multi
948-
params=list(coldkey_identities.keys()),
949-
module="SubtensorModule",
950-
storage_function="OwnedHotkeys",
951-
block_hash=block_hash,
952-
reuse_block_hash=reuse_block,
953-
)
948+
sks = [
949+
await self.substrate.create_storage_key(
950+
"SubtensorModule", "OwnedHotkeys", [ck], block_hash=block_hash
951+
)
952+
for ck in coldkey_identities.keys()
953+
]
954+
query = await self.substrate.query_multi(sks, block_hash=block_hash)
954955

955-
for coldkey_ss58, hotkeys in query.items():
956+
storage_key: StorageKey
957+
for storage_key, hotkeys in query:
958+
coldkey_ss58 = storage_key.params[0]
956959
coldkey_identity = coldkey_identities.get(coldkey_ss58)
957-
hotkeys = [decode_account_id(hotkey[0]) for hotkey in hotkeys or []]
958960

959961
identities["coldkeys"][coldkey_ss58] = {
960962
"identity": coldkey_identity,

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ scripts = { btcli = "bittensor_cli.cli:main" }
1515
requires-python = ">=3.9,<3.14"
1616
dependencies = [
1717
"wheel",
18-
"async-substrate-interface>=1.1.0",
18+
"async-substrate-interface>=1.4.2",
1919
"aiohttp~=3.10.2",
2020
"backoff~=2.2.1",
2121
"click<8.2.0", # typer.testing.CliRunner(mix_stderr=) is broken in click 8.2.0+

0 commit comments

Comments
 (0)