Skip to content

Commit ca5d2c2

Browse files
Merge branch 'staging' into tests/zyzniewski/more_subtensor_unittests
2 parents d0495cb + 1a4ba77 commit ca5d2c2

File tree

9 files changed

+147
-382
lines changed

9 files changed

+147
-382
lines changed

bittensor/core/async_subtensor.py

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from functools import partial
66
from typing import Optional, Any, Union, Iterable, TYPE_CHECKING
77

8-
import aiohttp
98
import asyncstdlib as a
109
import numpy as np
1110
import scalecodec
@@ -28,6 +27,7 @@
2827
decode_account_id,
2928
DynamicInfo,
3029
)
30+
from bittensor.core.chain_data.chain_identity import ChainIdentity
3131
from bittensor.core.chain_data.delegate_info import DelegatedInfo
3232
from bittensor.core.chain_data.utils import decode_metadata
3333
from bittensor.core.config import Config
@@ -68,15 +68,14 @@
6868
reveal_weights_extrinsic,
6969
)
7070
from bittensor.core.metagraph import AsyncMetagraph
71-
from bittensor.core.settings import version_as_int, TYPE_REGISTRY, DELEGATES_DETAILS_URL
71+
from bittensor.core.settings import version_as_int, TYPE_REGISTRY
7272
from bittensor.core.types import ParamWithTypes, SubtensorMixin
7373
from bittensor.utils import (
74+
Certificate,
7475
decode_hex_identity_dict,
7576
format_error_message,
7677
torch,
7778
u16_normalized_float,
78-
_decode_hex_identity_dict,
79-
Certificate,
8079
u64_normalized_float,
8180
)
8281
from bittensor.utils.balance import (
@@ -85,7 +84,6 @@
8584
check_and_convert_to_balance,
8685
)
8786
from bittensor.utils.btlogging import logging
88-
from bittensor.utils.delegates_details import DelegatesDetails
8987
from bittensor.utils.weight_utils import generate_weight_hash
9088

9189
if TYPE_CHECKING:
@@ -1079,75 +1077,33 @@ async def get_delegate_identities(
10791077
block: Optional[int] = None,
10801078
block_hash: Optional[str] = None,
10811079
reuse_block: bool = False,
1082-
) -> dict[str, "DelegatesDetails"]:
1080+
) -> dict[str, ChainIdentity]:
10831081
"""
1084-
Fetches delegates identities from the chain and GitHub. Preference is given to chain data, and missing info is
1085-
filled-in by the info from GitHub. At some point, we want to totally move away from fetching this info from
1086-
GitHub, but chain data is still limited in that regard.
1082+
Fetches delegates identities from the chain.
10871083
10881084
Arguments:
10891085
block (Optional[int]): The blockchain block number for the query.
10901086
block_hash (str): the hash of the blockchain block for the query
10911087
reuse_block (bool): Whether to reuse the last-used blockchain block hash.
10921088
10931089
Returns:
1094-
Dict {ss58: DelegatesDetails, ...}
1090+
Dict {ss58: ChainIdentity, ...}
10951091
10961092
"""
10971093
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
1098-
timeout = aiohttp.ClientTimeout(10.0)
1099-
async with aiohttp.ClientSession(timeout=timeout) as session:
1100-
identities_info, response = await asyncio.gather(
1101-
self.substrate.query_map(
1102-
module="Registry",
1103-
storage_function="IdentityOf",
1104-
block_hash=block_hash,
1105-
reuse_block_hash=reuse_block,
1106-
),
1107-
session.get(DELEGATES_DETAILS_URL),
1108-
)
1109-
1110-
all_delegates_details = {}
1111-
async for ss58_address, identity in identities_info:
1112-
all_delegates_details.update(
1113-
{
1114-
decode_account_id(
1115-
ss58_address[0]
1116-
): DelegatesDetails.from_chain_data(
1117-
decode_hex_identity_dict(identity.value["info"])
1118-
)
1119-
}
1120-
)
1121-
1122-
if response.ok:
1123-
all_delegates: dict[str, Any] = await response.json(content_type=None)
1124-
1125-
for delegate_hotkey, delegate_details in all_delegates.items():
1126-
delegate_info = all_delegates_details.setdefault(
1127-
delegate_hotkey,
1128-
DelegatesDetails(
1129-
display=delegate_details.get("name", ""),
1130-
web=delegate_details.get("url", ""),
1131-
additional=delegate_details.get("description", ""),
1132-
pgp_fingerprint=delegate_details.get("fingerprint", ""),
1133-
),
1134-
)
1135-
delegate_info.display = (
1136-
delegate_info.display or delegate_details.get("name", "")
1137-
)
1138-
delegate_info.web = delegate_info.web or delegate_details.get(
1139-
"url", ""
1140-
)
1141-
delegate_info.additional = (
1142-
delegate_info.additional
1143-
or delegate_details.get("description", "")
1144-
)
1145-
delegate_info.pgp_fingerprint = (
1146-
delegate_info.pgp_fingerprint
1147-
or delegate_details.get("fingerprint", "")
1148-
)
1094+
identities = await self.substrate.query_map(
1095+
module="SubtensorModule",
1096+
storage_function="IdentitiesV2",
1097+
block_hash=block_hash,
1098+
reuse_block_hash=reuse_block,
1099+
)
11491100

1150-
return all_delegates_details
1101+
return {
1102+
decode_account_id(ss58_address[0]): ChainIdentity.from_dict(
1103+
decode_hex_identity_dict(identity.value),
1104+
)
1105+
async for ss58_address, identity in identities
1106+
}
11511107

11521108
async def get_delegate_take(
11531109
self,
@@ -2424,7 +2380,7 @@ async def query_identity(
24242380
block: Optional[int] = None,
24252381
block_hash: Optional[str] = None,
24262382
reuse_block: bool = False,
2427-
) -> dict:
2383+
) -> Optional[ChainIdentity]:
24282384
"""
24292385
Queries the identity of a neuron on the Bittensor blockchain using the given key. This function retrieves
24302386
detailed identity information about a specific neuron, which is a crucial aspect of the network's
@@ -2455,12 +2411,16 @@ async def query_identity(
24552411
block_hash=block_hash,
24562412
reuse_block_hash=reuse_block,
24572413
)
2414+
24582415
if not identity_info:
2459-
return {}
2416+
return None
2417+
24602418
try:
2461-
return _decode_hex_identity_dict(identity_info)
2419+
return ChainIdentity.from_dict(
2420+
decode_hex_identity_dict(identity_info),
2421+
)
24622422
except TypeError:
2463-
return {}
2423+
return None
24642424

24652425
async def recycle(
24662426
self,

bittensor/core/settings.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,6 @@
7272
# Wallet ss58 address length
7373
SS58_ADDRESS_LENGTH = 48
7474

75-
# Raw GitHub url for delegates registry file
76-
DELEGATES_DETAILS_URL = "https://raw.githubusercontent.com/opentensor/bittensor-delegates/main/public/delegates.json"
77-
7875
# Block Explorers map network to explorer url
7976
# Must all be polkadotjs explorer urls
8077
NETWORK_EXPLORER_MAP = {

bittensor/core/subtensor.py

Lines changed: 28 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@
55
from typing import TYPE_CHECKING, Any, Iterable, Optional, Union, cast
66

77
import numpy as np
8-
import requests
98
import scalecodec
109
from async_substrate_interface.errors import SubstrateRequestException
1110
from async_substrate_interface.types import ScaleObj
1211
from async_substrate_interface.sync_substrate import SubstrateInterface
13-
from async_substrate_interface.utils import json
1412
from numpy.typing import NDArray
1513

1614
from bittensor.core.async_subtensor import ProposalVoteData
@@ -29,6 +27,7 @@
2927
DelegatedInfo,
3028
decode_account_id,
3129
)
30+
from bittensor.core.chain_data.chain_identity import ChainIdentity
3231
from bittensor.core.chain_data.utils import decode_metadata
3332
from bittensor.core.config import Config
3433
from bittensor.core.extrinsics.commit_reveal import commit_reveal_v3_extrinsic
@@ -71,16 +70,14 @@
7170
version_as_int,
7271
SS58_FORMAT,
7372
TYPE_REGISTRY,
74-
DELEGATES_DETAILS_URL,
7573
)
7674
from bittensor.core.types import ParamWithTypes, SubtensorMixin
7775
from bittensor.utils import (
78-
torch,
79-
format_error_message,
76+
Certificate,
8077
decode_hex_identity_dict,
78+
format_error_message,
79+
torch,
8180
u16_normalized_float,
82-
_decode_hex_identity_dict,
83-
Certificate,
8481
u64_normalized_float,
8582
)
8683
from bittensor.utils.balance import (
@@ -90,7 +87,6 @@
9087
check_and_convert_to_balance,
9188
)
9289
from bittensor.utils.btlogging import logging
93-
from bittensor.utils.delegates_details import DelegatesDetails
9490
from bittensor.utils.weight_utils import generate_weight_hash
9591

9692
if TYPE_CHECKING:
@@ -815,62 +811,29 @@ def get_delegate_by_hotkey(
815811

816812
def get_delegate_identities(
817813
self, block: Optional[int] = None
818-
) -> dict[str, "DelegatesDetails"]:
814+
) -> dict[str, ChainIdentity]:
819815
"""
820-
Fetches delegates identities from the chain and GitHub. Preference is given to chain data, and missing info is
821-
filled-in by the info from GitHub. At some point, we want to totally move away from fetching this info from
822-
GitHub, but chain data is still limited in that regard.
816+
Fetches delegates identities from the chain.
823817
824818
Arguments:
825819
block (Optional[int]): The blockchain block number for the query.
826820
827821
Returns:
828-
Dict {ss58: DelegatesDetails, ...}
822+
Dict {ss58: ChainIdentity, ...}
829823
830824
"""
831-
block_hash = self.determine_block_hash(block)
832-
response = requests.get(DELEGATES_DETAILS_URL)
833-
identities_info = self.substrate.query_map(
834-
module="Registry", storage_function="IdentityOf", block_hash=block_hash
835-
)
836-
837-
all_delegates_details = {}
838-
for ss58_address, identity in identities_info:
839-
all_delegates_details.update(
840-
{
841-
decode_account_id(
842-
ss58_address[0]
843-
): DelegatesDetails.from_chain_data(
844-
decode_hex_identity_dict(identity.value["info"])
845-
)
846-
}
847-
)
848-
if response.ok:
849-
all_delegates: dict[str, Any] = json.loads(response.content)
850-
851-
for delegate_hotkey, delegate_details in all_delegates.items():
852-
delegate_info = all_delegates_details.setdefault(
853-
delegate_hotkey,
854-
DelegatesDetails(
855-
display=delegate_details.get("name", ""),
856-
web=delegate_details.get("url", ""),
857-
additional=delegate_details.get("description", ""),
858-
pgp_fingerprint=delegate_details.get("fingerprint", ""),
859-
),
860-
)
861-
delegate_info.display = delegate_info.display or delegate_details.get(
862-
"name", ""
863-
)
864-
delegate_info.web = delegate_info.web or delegate_details.get("url", "")
865-
delegate_info.additional = (
866-
delegate_info.additional or delegate_details.get("description", "")
867-
)
868-
delegate_info.pgp_fingerprint = (
869-
delegate_info.pgp_fingerprint
870-
or delegate_details.get("fingerprint", "")
871-
)
825+
identities = self.substrate.query_map(
826+
module="SubtensorModule",
827+
storage_function="IdentitiesV2",
828+
block_hash=self.determine_block_hash(block),
829+
)
872830

873-
return all_delegates_details
831+
return {
832+
decode_account_id(ss58_address[0]): ChainIdentity.from_dict(
833+
decode_hex_identity_dict(identity.value),
834+
)
835+
for ss58_address, identity in identities
836+
}
874837

875838
def get_delegate_take(
876839
self, hotkey_ss58: str, block: Optional[int] = None
@@ -1843,7 +1806,9 @@ def neurons_lite(
18431806

18441807
return NeuronInfoLite.list_from_dicts(result)
18451808

1846-
def query_identity(self, coldkey_ss58: str, block: Optional[int] = None) -> dict:
1809+
def query_identity(
1810+
self, coldkey_ss58: str, block: Optional[int] = None
1811+
) -> Optional[ChainIdentity]:
18471812
"""
18481813
Queries the identity of a neuron on the Bittensor blockchain using the given key. This function retrieves
18491814
detailed identity information about a specific neuron, which is a crucial aspect of the network's
@@ -1870,12 +1835,16 @@ def query_identity(self, coldkey_ss58: str, block: Optional[int] = None) -> dict
18701835
params=[coldkey_ss58],
18711836
block_hash=self.determine_block_hash(block),
18721837
)
1838+
18731839
if not identity_info:
1874-
return {}
1840+
return None
1841+
18751842
try:
1876-
return _decode_hex_identity_dict(identity_info)
1843+
return ChainIdentity.from_dict(
1844+
decode_hex_identity_dict(identity_info),
1845+
)
18771846
except TypeError:
1878-
return {}
1847+
return None
18791848

18801849
def recycle(self, netuid: int, block: Optional[int] = None) -> Optional[Balance]:
18811850
"""

0 commit comments

Comments
 (0)