Skip to content

[WIP] Use new bt-decode accountid functionality #2873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: staging
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
SubnetIdentity,
SubnetInfo,
WeightCommitInfo,
decode_account_id,
)
from bittensor.core.chain_data.chain_identity import ChainIdentity
from bittensor.core.chain_data.delegate_info import DelegatedInfo
Expand Down Expand Up @@ -308,6 +307,7 @@ def _get_substrate(
use_remote_preset=True,
chain_name="Bittensor",
_mock=_mock,
legacy_account_id_decode=False,
)
return AsyncSubstrateInterface(
url=self.chain_endpoint,
Expand All @@ -316,6 +316,7 @@ def _get_substrate(
use_remote_preset=True,
chain_name="Bittensor",
_mock=_mock,
legacy_account_id_decode=False,
)

# Subtensor queries ===========================================================================================
Expand Down Expand Up @@ -1007,7 +1008,7 @@ async def get_children(
formatted_children = []
for proportion, child in children.value:
# Convert U64 to int
formatted_child = decode_account_id(child[0])
formatted_child = child
normalized_proportion = u64_normalized_float(proportion)
formatted_children.append((normalized_proportion, formatted_child))
return True, formatted_children, ""
Expand Down Expand Up @@ -1060,7 +1061,7 @@ async def get_children_pending(
[
(
u64_normalized_float(proportion),
decode_account_id(child[0]),
child,
)
for proportion, child in children
],
Expand Down Expand Up @@ -1136,7 +1137,7 @@ async def get_all_commitments(
)
result = {}
async for id_, value in query:
result[decode_account_id(id_[0])] = decode_metadata(value)
result[id_] = decode_metadata(value)
return result

async def get_revealed_commitment_by_hotkey(
Expand Down Expand Up @@ -1345,7 +1346,7 @@ async def get_delegate_identities(
)

return {
decode_account_id(ss58_address[0]): ChainIdentity.from_dict(
ss58_address: ChainIdentity.from_dict(
decode_hex_identity_dict(identity.value),
)
async for ss58_address, identity in identities
Expand Down Expand Up @@ -1712,7 +1713,7 @@ async def get_all_neuron_certificates(
)
output = {}
async for key, item in query_certificates:
output[decode_account_id(key)] = Certificate(item.value)
output[key] = Certificate(item.value)
return output

async def get_neuron_for_pubkey_and_subnet(
Expand Down Expand Up @@ -1825,8 +1826,7 @@ async def get_owned_hotkeys(
block_hash=block_hash,
reuse_block_hash=reuse_block,
)

return [decode_account_id(hotkey[0]) for hotkey in owned_hotkeys or []]
return owned_hotkeys

async def get_stake(
self,
Expand Down
11 changes: 5 additions & 6 deletions bittensor/core/chain_data/delegate_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Optional

from bittensor.core.chain_data.info_base import InfoBase
from bittensor.core.chain_data.utils import decode_account_id
from bittensor.utils import u16_normalized_float
from bittensor.utils.balance import Balance

Expand Down Expand Up @@ -49,14 +48,14 @@ class DelegateInfo(DelegateInfoBase):

@classmethod
def _from_dict(cls, decoded: dict) -> Optional["DelegateInfo"]:
hotkey = decode_account_id(decoded.get("delegate_ss58"))
owner = decode_account_id(decoded.get("owner_ss58"))
hotkey = decoded.get("delegate_ss58")
owner = decoded.get("owner_ss58")

nominators = {}
total_stake_by_netuid = {}

for raw_nominator, raw_stakes in decoded.get("nominators", []):
nominator_ss58 = decode_account_id(raw_nominator)
nominator_ss58 = raw_nominator
stakes = {
int(netuid): Balance.from_rao(stake_amt).set_unit(int(netuid))
for (netuid, stake_amt) in raw_stakes
Expand Down Expand Up @@ -100,8 +99,8 @@ def _from_dict(
cls, decoded: tuple[dict, tuple[int, int]]
) -> Optional["DelegatedInfo"]:
delegate_info, (netuid, stake) = decoded
hotkey = decode_account_id(delegate_info.get("delegate_ss58"))
owner = decode_account_id(delegate_info.get("owner_ss58"))
hotkey = delegate_info.get("delegate_ss58")
owner = delegate_info.get("owner_ss58")
return cls(
hotkey_ss58=hotkey,
owner_ss58=owner,
Expand Down
5 changes: 2 additions & 3 deletions bittensor/core/chain_data/delegate_info_lite.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from dataclasses import dataclass

from bittensor.core.chain_data.info_base import InfoBase
from bittensor.core.chain_data.utils import decode_account_id
from bittensor.utils import u16_normalized_float
from bittensor.utils.balance import Balance

Expand Down Expand Up @@ -36,10 +35,10 @@ class DelegateInfoLite(InfoBase):
@classmethod
def _from_dict(cls, decoded: dict) -> "DelegateInfoLite":
return DelegateInfoLite(
delegate_ss58=decode_account_id(decoded["delegate_ss58"]),
delegate_ss58=decoded["delegate_ss58"],
take=u16_normalized_float(decoded["take"]),
nominators=decoded["nominators"],
owner_ss58=decode_account_id(decoded["owner_ss58"]),
owner_ss58=decoded["owner_ss58"],
registrations=decoded["registrations"],
validator_permits=decoded["validator_permits"],
return_per_1000=Balance.from_rao(decoded["return_per_1000"]),
Expand Down
5 changes: 2 additions & 3 deletions bittensor/core/chain_data/dynamic_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from typing import Optional, Union

from bittensor.core.chain_data.info_base import InfoBase
from bittensor.core.chain_data.utils import decode_account_id

from bittensor.core.chain_data.subnet_identity import SubnetIdentity
from bittensor.utils.balance import Balance, fixed_to_float
Expand Down Expand Up @@ -52,8 +51,8 @@ def _from_dict(cls, decoded: dict) -> "DynamicInfo":
True if int(decoded["netuid"]) > 0 else False
) # Root is not dynamic

owner_hotkey = decode_account_id(decoded["owner_hotkey"])
owner_coldkey = decode_account_id(decoded["owner_coldkey"])
owner_hotkey = decoded["owner_hotkey"]
owner_coldkey = decoded["owner_coldkey"]

emission = Balance.from_rao(decoded["emission"]).set_unit(0)
alpha_in = Balance.from_rao(decoded["alpha_in"]).set_unit(netuid)
Expand Down
29 changes: 6 additions & 23 deletions bittensor/core/chain_data/metagraph_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from bittensor.core.chain_data.chain_identity import ChainIdentity
from bittensor.core.chain_data.info_base import InfoBase
from bittensor.core.chain_data.subnet_identity import SubnetIdentity
from bittensor.core.chain_data.utils import decode_account_id
from bittensor.utils import u64_normalized_float as u64tf, u16_normalized_float as u16tf
from bittensor.utils.balance import Balance, fixed_to_float

Expand Down Expand Up @@ -177,16 +176,8 @@ def _from_dict(cls, decoded: dict) -> "MetagraphInfo":
identity=decoded["identity"],
network_registered_at=decoded["network_registered_at"],
# Keys for owner.
owner_hotkey=(
decode_account_id(decoded["owner_hotkey"][0])
if decoded.get("owner_hotkey") is not None
else None
),
owner_coldkey=(
decode_account_id(decoded["owner_coldkey"][0])
if decoded.get("owner_coldkey") is not None
else None
),
owner_hotkey=decoded.get("owner_hotkey") or None,
owner_coldkey=decoded.get("owner_coldkey") or None,
# Tempo terms.
block=decoded["block"],
tempo=decoded["tempo"],
Expand Down Expand Up @@ -280,16 +271,8 @@ def _from_dict(cls, decoded: dict) -> "MetagraphInfo":
else None
),
# Metagraph info.
hotkeys=(
[decode_account_id(ck) for ck in decoded.get("hotkeys", [])]
if decoded.get("hotkeys") is not None
else None
),
coldkeys=(
[decode_account_id(hk) for hk in decoded.get("coldkeys", [])]
if decoded.get("coldkeys") is not None
else None
),
hotkeys=decoded.get("hotkeys") or None,
coldkeys=decoded.get("coldkeys") or None,
identities=decoded["identities"],
axons=decoded.get("axons", []),
active=decoded["active"],
Expand Down Expand Up @@ -352,15 +335,15 @@ def _from_dict(cls, decoded: dict) -> "MetagraphInfo":
# Dividend break down
tao_dividends_per_hotkey=(
[
(decode_account_id(alpha[0]), _tbwu(alpha[1]))
(alpha[0], _tbwu(alpha[1]))
for alpha in decoded["tao_dividends_per_hotkey"]
]
if decoded.get("tao_dividends_per_hotkey") is not None
else None
),
alpha_dividends_per_hotkey=(
[
(decode_account_id(adphk[0]), _tbwu(adphk[1], _netuid))
(adphk[0], _tbwu(adphk[1], _netuid))
for adphk in decoded["alpha_dividends_per_hotkey"]
]
if decoded.get("alpha_dividends_per_hotkey") is not None
Expand Down
6 changes: 3 additions & 3 deletions bittensor/core/chain_data/neuron_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from bittensor.core.chain_data.axon_info import AxonInfo
from bittensor.core.chain_data.info_base import InfoBase
from bittensor.core.chain_data.prometheus_info import PrometheusInfo
from bittensor.core.chain_data.utils import decode_account_id, process_stake_data
from bittensor.core.chain_data.utils import process_stake_data
from bittensor.utils import u16_normalized_float
from bittensor.utils.balance import Balance

Expand Down Expand Up @@ -129,8 +129,8 @@ def _from_dict(cls, decoded: Any) -> "NeuronInfo":
"""Returns a NeuronInfo object from decoded chain data."""
stake_dict = process_stake_data(decoded["stake"])
total_stake = sum(stake_dict.values()) if stake_dict else Balance(0)
coldkey = decode_account_id(decoded["coldkey"])
hotkey = decode_account_id(decoded["hotkey"])
coldkey = decoded["coldkey"]
hotkey = decoded["hotkey"]
return NeuronInfo(
active=decoded["active"],
axon_info=AxonInfo.from_dict(
Expand Down
6 changes: 3 additions & 3 deletions bittensor/core/chain_data/neuron_info_lite.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from bittensor.core.chain_data.axon_info import AxonInfo
from bittensor.core.chain_data.info_base import InfoBase
from bittensor.core.chain_data.prometheus_info import PrometheusInfo
from bittensor.core.chain_data.utils import decode_account_id, process_stake_data
from bittensor.core.chain_data.utils import process_stake_data
from bittensor.utils import u16_normalized_float
from bittensor.utils.balance import Balance

Expand Down Expand Up @@ -96,8 +96,8 @@ def get_null_neuron() -> "NeuronInfoLite":
@classmethod
def _from_dict(cls, decoded: Any) -> "NeuronInfoLite":
"""Returns a NeuronInfoLite object from decoded chain data."""
coldkey = decode_account_id(decoded["coldkey"])
hotkey = decode_account_id(decoded["hotkey"])
coldkey = decoded["coldkey"]
hotkey = decoded["hotkey"]
stake_dict = process_stake_data(decoded["stake"])
stake = sum(stake_dict.values()) if stake_dict else Balance(0)

Expand Down
5 changes: 2 additions & 3 deletions bittensor/core/chain_data/proposal_vote_data.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from dataclasses import dataclass

from bittensor.core.chain_data.info_base import InfoBase
from bittensor.core.chain_data.utils import decode_account_id


@dataclass
Expand All @@ -19,9 +18,9 @@ class ProposalVoteData(InfoBase):
@classmethod
def from_dict(cls, proposal_dict: dict) -> "ProposalVoteData":
return cls(
ayes=[decode_account_id(key) for key in proposal_dict["ayes"]],
ayes=proposal_dict["ayes"],
end=proposal_dict["end"],
index=proposal_dict["index"],
nays=[decode_account_id(key) for key in proposal_dict["nays"]],
nays=proposal_dict["nays"],
threshold=proposal_dict["threshold"],
)
5 changes: 2 additions & 3 deletions bittensor/core/chain_data/stake_info.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from dataclasses import dataclass

from bittensor.core.chain_data.info_base import InfoBase
from bittensor.core.chain_data.utils import decode_account_id
from bittensor.utils.balance import Balance


Expand Down Expand Up @@ -30,8 +29,8 @@ def from_dict(cls, decoded: dict) -> "StakeInfo":
"""Returns a StakeInfo object from decoded chain data."""
netuid = decoded["netuid"]
return cls(
hotkey_ss58=decode_account_id(decoded["hotkey"]),
coldkey_ss58=decode_account_id(decoded["coldkey"]),
hotkey_ss58=decoded["hotkey"],
coldkey_ss58=decoded["coldkey"],
netuid=int(netuid),
stake=Balance.from_rao(decoded["stake"]).set_unit(netuid),
locked=Balance.from_rao(decoded["locked"]).set_unit(netuid),
Expand Down
3 changes: 1 addition & 2 deletions bittensor/core/chain_data/subnet_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from typing import Any

from bittensor.core.chain_data.info_base import InfoBase
from bittensor.core.chain_data.utils import decode_account_id
from bittensor.utils import u16_normalized_float
from bittensor.utils.balance import Balance

Expand Down Expand Up @@ -50,7 +49,7 @@ def _from_dict(cls, decoded: Any) -> "SubnetInfo":
min_allowed_weights=decoded["min_allowed_weights"],
modality=decoded["network_modality"],
netuid=decoded["netuid"],
owner_ss58=decode_account_id(decoded["owner"]),
owner_ss58=decoded["owner"],
rho=decoded["rho"],
scaling_law_power=decoded["scaling_law_power"],
subnetwork_n=decoded["subnetwork_n"],
Expand Down
5 changes: 2 additions & 3 deletions bittensor/core/chain_data/subnet_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from dataclasses import dataclass

from bittensor.core.chain_data.info_base import InfoBase
from bittensor.core.chain_data.utils import decode_account_id
from bittensor.utils import u16_normalized_float
from bittensor.utils.balance import Balance

Expand Down Expand Up @@ -38,8 +37,8 @@ def _from_dict(cls, decoded: dict) -> "SubnetState":
netuid = decoded["netuid"]
return SubnetState(
netuid=netuid,
hotkeys=[decode_account_id(hk) for hk in decoded.get("hotkeys", [])],
coldkeys=[decode_account_id(ck) for ck in decoded.get("coldkeys", [])],
hotkeys=decoded.get("hotkeys", []),
coldkeys=decoded.get("coldkeys", []),
active=decoded["active"],
validator_permit=decoded["validator_permit"],
pruning_score=[
Expand Down
10 changes: 3 additions & 7 deletions bittensor/core/chain_data/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def decode_account_id(account_id_bytes: Union[bytes, str]) -> str:
return ss58_encode(bytes(account_id_bytes).hex(), SS58_FORMAT)


def process_stake_data(stake_data: list) -> dict:
def process_stake_data(stake_data: list) -> dict[str, Balance]:
"""
Processes stake data to decode account IDs and convert stakes from rao to Balance objects.

Expand All @@ -127,11 +127,7 @@ def process_stake_data(stake_data: list) -> dict:
Returns:
dict: A dictionary with account IDs as keys and their corresponding Balance objects as values.
"""
decoded_stake_data = {}
for account_id_bytes, stake_ in stake_data:
account_id = decode_account_id(account_id_bytes)
decoded_stake_data.update({account_id: Balance.from_rao(stake_)})
return decoded_stake_data
return {account_id: Balance.from_rao(stake_) for account_id, stake_ in stake_data}


def decode_metadata(metadata: dict) -> str:
Expand Down Expand Up @@ -181,6 +177,6 @@ def decode_revealed_commitment_with_hotkey(
"""
key, data = encoded_data

ss58_address = decode_account_id(next(iter(key)))
ss58_address = next(iter(key))
block_data = tuple(decode_revealed_commitment(p) for p in data.value)
return ss58_address, block_data
3 changes: 1 addition & 2 deletions bittensor/core/chain_data/weight_commit_info.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dataclasses import dataclass
from bittensor.core.chain_data.utils import decode_account_id


@dataclass
Expand Down Expand Up @@ -34,4 +33,4 @@ def from_vec_u8(cls, data: tuple) -> tuple[str, str, int]:
commit_data = commit_data[0] if isinstance(commit_data, tuple) else commit_data
commit_hex = "0x" + "".join(format(x, "02x") for x in commit_data)

return decode_account_id(account_id_), commit_hex, round_number
return account_id_, commit_hex, round_number
Loading