Skip to content

Commit

Permalink
Merge pull request #2627 from opentensor/feat/roman/rpc-upgrades
Browse files Browse the repository at this point in the history
SKD implementation for Subtensor `Feat/RPC Upgrades`. PR #1205
  • Loading branch information
ibraheem-opentensor authored Feb 5, 2025
2 parents efdecbf + 5cb49e0 commit 20f00d5
Show file tree
Hide file tree
Showing 18 changed files with 94 additions and 96 deletions.
15 changes: 9 additions & 6 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2306,7 +2306,7 @@ async def neurons_lite(

async def query_identity(
self,
key: str,
coldkey_ss58: str,
block: Optional[int] = None,
block_hash: Optional[str] = None,
reuse_block: bool = False,
Expand All @@ -2317,7 +2317,8 @@ async def query_identity(
decentralized identity and governance system.
Arguments:
key (str): The key used to query the neuron's identity, typically the neuron's SS58 address.
coldkey_ss58 (str): The coldkey used to query the neuron's identity (technically the neuron's coldkey SS58
address).
block (Optional[int]): The blockchain block number for the query.
block_hash (str): The hash of the blockchain block number at which to perform the query.
reuse_block (bool): Whether to reuse the last-used blockchain block hash.
Expand All @@ -2334,14 +2335,16 @@ async def query_identity(
"""
block_hash = await self.determine_block_hash(block, block_hash, reuse_block)
identity_info = await self.substrate.query(
module="Registry",
storage_function="IdentityOf",
params=[key],
module="SubtensorModule",
storage_function="IdentitiesV2",
params=[coldkey_ss58],
block_hash=block_hash,
reuse_block_hash=reuse_block,
)
if not identity_info:
return {}
try:
return _decode_hex_identity_dict(identity_info["info"])
return _decode_hex_identity_dict(identity_info)
except TypeError:
return {}

Expand Down
18 changes: 15 additions & 3 deletions bittensor/core/chain_data/chain_identity.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
from dataclasses import dataclass
from bittensor.core.chain_data.info_base import InfoBase


@dataclass
class ChainIdentity:
class ChainIdentity(InfoBase):
"""Dataclass for chain identity information."""

# In `bittensor.core.chain_data.utils.custom_rpc_type_registry` represents as `ChainIdentityOf` structure.

name: str
url: str
github: str
image: str
discord: str
description: str
additional: str

@classmethod
def _from_dict(cls, decoded: dict) -> "ChainIdentity":
return cls(
name=decoded["name"],
url=decoded["url"],
github=decoded["github_repo"],
image=decoded["image"],
discord=decoded["discord"],
description=decoded["description"],
additional=decoded["additional"],
)
4 changes: 4 additions & 0 deletions bittensor/core/chain_data/dynamic_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ def _from_dict(cls, decoded: dict) -> "DynamicInfo":
subnet_contact=bytes(
decoded["subnet_identity"]["subnet_contact"]
).decode(),
subnet_url=bytes(decoded["subnet_identity"]["subnet_url"]).decode(),
discord=bytes(decoded["subnet_identity"]["discord"]).decode(),
description=bytes(decoded["subnet_identity"]["description"]).decode(),
additional=bytes(decoded["subnet_identity"]["additional"]).decode(),
)
else:
subnet_identity = None
Expand Down
6 changes: 3 additions & 3 deletions bittensor/core/chain_data/subnet_hyperparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SubnetHyperparameters(InfoBase):
max_validators (int): Maximum number of validators.
adjustment_alpha (int): Alpha value for adjustments.
difficulty (int): Difficulty level.
commit_reveal_weights_interval (int): Interval for commit-reveal weights.
commit_reveal_period (int): Interval for commit-reveal weights.
commit_reveal_weights_enabled (bool): Flag indicating if commit-reveal weights are enabled.
alpha_high (int): High value of alpha.
alpha_low (int): Low value of alpha.
Expand Down Expand Up @@ -60,7 +60,7 @@ class SubnetHyperparameters(InfoBase):
max_validators: int
adjustment_alpha: int
difficulty: int
commit_reveal_weights_interval: int
commit_reveal_period: int
commit_reveal_weights_enabled: bool
alpha_high: int
alpha_low: int
Expand Down Expand Up @@ -89,7 +89,7 @@ def _from_dict(cls, decoded: dict) -> "SubnetHyperparameters":
alpha_low=decoded["alpha_low"],
bonds_moving_avg=decoded["bonds_moving_avg"],
commit_reveal_weights_enabled=decoded["commit_reveal_weights_enabled"],
commit_reveal_weights_interval=decoded["commit_reveal_weights_interval"],
commit_reveal_period=decoded["commit_reveal_period"],
difficulty=decoded["difficulty"],
immunity_period=decoded["immunity_period"],
kappa=decoded["kappa"],
Expand Down
16 changes: 15 additions & 1 deletion bittensor/core/chain_data/subnet_identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,19 @@ class SubnetIdentity:
subnet_name: str
github_repo: str
subnet_contact: str
subnet_url: str
discord: str
description: str
additional: str

# TODO: Add other methods when fetching from chain
@classmethod
def _from_dict(cls, decoded: dict) -> "SubnetIdentity":
return cls(
subnet_name=decoded["subnet_name"],
github_repo=decoded["github_repo"],
subnet_contact=decoded["subnet_contact"],
subnet_url=decoded["subnet_url"],
discord=decoded["discord"],
description=decoded["description"],
additional=decoded["additional"],
)
2 changes: 1 addition & 1 deletion bittensor/core/chain_data/subnet_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _from_dict(cls, decoded: Any) -> "SubnetInfo":
for (netuid, req) in decoded["network_connect"]
},
difficulty=decoded["difficulty"],
emission_value=decoded["emission_values"],
emission_value=decoded["emission_value"],
immunity_period=decoded["immunity_period"],
kappa=decoded["kappa"],
max_allowed_validators=decoded["max_allowed_validators"],
Expand Down
4 changes: 1 addition & 3 deletions bittensor/core/extrinsics/asyncex/commit_reveal.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ async def commit_reveal_v3_extrinsic(
netuid, block_hash=current_block["header"]["hash"]
)
tempo = subnet_hyperparameters.tempo
subnet_reveal_period_epochs = (
subnet_hyperparameters.commit_reveal_weights_interval
)
subnet_reveal_period_epochs = subnet_hyperparameters.commit_reveal_period

# Encrypt `commit_hash` with t-lock and `get reveal_round`
commit_for_reveal, reveal_round = get_encrypted_commit(
Expand Down
4 changes: 1 addition & 3 deletions bittensor/core/extrinsics/commit_reveal.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ def commit_reveal_v3_extrinsic(
netuid, block=current_block
)
tempo = subnet_hyperparameters.tempo
subnet_reveal_period_epochs = (
subnet_hyperparameters.commit_reveal_weights_interval
)
subnet_reveal_period_epochs = subnet_hyperparameters.commit_reveal_period

# Encrypt `commit_hash` with t-lock and `get reveal_round`
commit_for_reveal, reveal_round = get_encrypted_commit(
Expand Down
15 changes: 8 additions & 7 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ def get_all_subnets_info(self, block: Optional[int] = None) -> list["SubnetInfo"
"""
result = self.query_runtime_api(
runtime_api="SubnetInfoRuntimeApi",
method="get_subnets_info",
method="get_subnets_info_v2",
params=[],
block=block,
)
Expand Down Expand Up @@ -1771,14 +1771,15 @@ def neurons_lite(

return NeuronInfoLite.list_from_dicts(result)

def query_identity(self, key: str, block: Optional[int] = None) -> dict:
def query_identity(self, coldkey_ss58: str, block: Optional[int] = None) -> dict:
"""
Queries the identity of a neuron on the Bittensor blockchain using the given key. This function retrieves
detailed identity information about a specific neuron, which is a crucial aspect of the network's
decentralized identity and governance system.
Arguments:
key (str): The key used to query the neuron's identity, typically the neuron's SS58 address.
coldkey_ss58 (str): The coldkey used to query the neuron's identity (technically the neuron's coldkey SS58
address).
block (Optional[int]): The blockchain block number for the query.
Returns:
Expand All @@ -1792,13 +1793,13 @@ def query_identity(self, key: str, block: Optional[int] = None) -> dict:
parameters.
"""
identity_info = self.substrate.query(
module="Registry",
storage_function="IdentityOf",
params=[key],
module="SubtensorModule",
storage_function="IdentitiesV2",
params=[coldkey_ss58],
block_hash=self.determine_block_hash(block),
)
try:
return _decode_hex_identity_dict(identity_info["info"])
return _decode_hex_identity_dict(identity_info)
except TypeError:
return {}

Expand Down
25 changes: 9 additions & 16 deletions bittensor/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,21 @@ def __new__(cls, data: Union[str, dict]):
def _decode_hex_identity_dict(info_dictionary: dict[str, Any]) -> dict[str, Any]:
# TODO why does this exist alongside `decode_hex_identity_dict`?
"""Decodes a dictionary of hexadecimal identities."""
decoded_info = {}
for k, v in info_dictionary.items():
if isinstance(v, dict):
item = next(iter(v.values()))
else:
item = v
if isinstance(item, tuple) and item:
if len(item) > 1:
try:
info_dictionary[k] = (
bytes(item).hex(sep=" ", bytes_per_sep=2).upper()
)
except UnicodeDecodeError:
logging.error(f"Could not decode: {k}: {item}.")
else:
try:
info_dictionary[k] = bytes(item[0]).decode("utf-8")
except UnicodeDecodeError:
logging.error(f"Could not decode: {k}: {item}.")
else:
info_dictionary[k] = item

return info_dictionary
if isinstance(item, tuple):
try:
decoded_info[k] = bytes(item).decode()
except UnicodeDecodeError:
print(f"Could not decode: {k}: {item}")
else:
decoded_info[k] = item
return decoded_info


def ss58_to_vec_u8(ss58_address: str) -> list[int]:
Expand Down
10 changes: 2 additions & 8 deletions tests/e2e_tests/test_commit_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,7 @@ async def test_commit_and_reveal_weights_legacy(local_chain):
)

assert (
subtensor.get_subnet_hyperparameters(
netuid=netuid
).commit_reveal_weights_interval
== 1
subtensor.get_subnet_hyperparameters(netuid=netuid).commit_reveal_period == 1
), "Failed to set commit/reveal periods"

assert (
Expand Down Expand Up @@ -233,10 +230,7 @@ async def test_commit_weights_uses_next_nonce(local_chain):
)

assert (
subtensor.get_subnet_hyperparameters(
netuid=netuid
).commit_reveal_weights_interval
== 1
subtensor.get_subnet_hyperparameters(netuid=netuid).commit_reveal_period == 1
), "Failed to set commit/reveal periods"

assert (
Expand Down
4 changes: 2 additions & 2 deletions tests/helpers/integration_websocket_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,9 +923,9 @@
}
},
"state_call": {
'["SubnetInfoRuntimeApi_get_subnets_info", "", null]': {
'["SubnetInfoRuntimeApi_get_subnets_info_v2", "", null]': {
"jsonrpc": "2.0",
"result": "0x08010028feff0100025a62020140010100feff0300c80001017501910100000002286bee0000000000000000000000000000000000000000000000000000000000000000010428feff0100025a62020140010100feff0300c804010479019101000002286bee02286bee0000000000000000000000000000000000000000000000000000000000000000",
"result": "0x08010028feff0100025a62020140010100feff0300c80001015d01910100000002286bee000000000000000000000000000000000000000000000000000000000000000000010428feff0100025a62020140010100feff0300c804010461019101000002286bee02286bee000000000000000000000000000000000000000000000000000000000000000000",
}
},
"state_getRuntimeVersion": {
Expand Down
Binary file modified tests/helpers/registry
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/integration_tests/test_subtensor_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async def test_get_all_subnets_info(mocker):
assert result[0].owner_ss58 == "5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM"
assert result[1].kappa == 32767
assert result[1].max_weight_limit == 65535
assert result[1].blocks_since_epoch == 94
assert result[1].blocks_since_epoch == 88


@pytest.mark.asyncio
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/extrinsics/asyncex/test_commit_reveal.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def hyperparams():
max_validators=0,
adjustment_alpha=0,
difficulty=0,
commit_reveal_weights_interval=0,
commit_reveal_period=0,
commit_reveal_weights_enabled=True,
alpha_high=0,
alpha_low=0,
Expand Down Expand Up @@ -223,7 +223,7 @@ async def test_commit_reveal_v3_extrinsic_success_with_torch(
mocked_get_encrypted_commit.assert_called_once_with(
uids=mocked_uids,
weights=mocked_weights,
subnet_reveal_period_epochs=mock_hyperparams.return_value.commit_reveal_weights_interval,
subnet_reveal_period_epochs=mock_hyperparams.return_value.commit_reveal_period,
version_key=async_commit_reveal.version_as_int,
tempo=mock_hyperparams.return_value.tempo,
netuid=fake_netuid,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/extrinsics/test_commit_reveal.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def hyperparams():
max_validators=0,
adjustment_alpha=0,
difficulty=0,
commit_reveal_weights_interval=0,
commit_reveal_period=0,
commit_reveal_weights_enabled=True,
alpha_high=0,
alpha_low=0,
Expand Down Expand Up @@ -209,7 +209,7 @@ def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor, hyperp
mocked_get_encrypted_commit.assert_called_once_with(
uids=mocked_uids,
weights=mocked_weights,
subnet_reveal_period_epochs=mock_hyperparams.return_value.commit_reveal_weights_interval,
subnet_reveal_period_epochs=mock_hyperparams.return_value.commit_reveal_period,
version_key=commit_reveal.version_as_int,
tempo=mock_hyperparams.return_value.tempo,
netuid=fake_netuid,
Expand Down
Loading

0 comments on commit 20f00d5

Please sign in to comment.