Skip to content
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
143 changes: 41 additions & 102 deletions bittensor/core/async_subtensor.py

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion bittensor/core/axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from fastapi.responses import JSONResponse
from fastapi.routing import serialize_response
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.types import ASGIApp
from starlette.requests import Request
from starlette.responses import Response

Expand Down Expand Up @@ -1097,7 +1098,7 @@ class AxonMiddleware(BaseHTTPMiddleware):
such as response header updating and logging.
"""

def __init__(self, app: "AxonMiddleware", axon: "Axon"):
def __init__(self, app: ASGIApp, axon: "Axon"):
"""
Initialize the AxonMiddleware class.

Expand Down
3 changes: 1 addition & 2 deletions bittensor/core/chain_data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from .subnet_identity import SubnetIdentity
from .subnet_info import SubnetInfo
from .subnet_state import SubnetState
from .utils import decode_account_id, process_stake_data
from .utils import process_stake_data
from .weight_commit_info import WeightCommitInfo

ProposalCallData = GenericCall
Expand Down Expand Up @@ -78,6 +78,5 @@
"SubnetInfo",
"SubnetState",
"WeightCommitInfo",
"decode_account_id",
"process_stake_data",
]
20 changes: 6 additions & 14 deletions bittensor/core/chain_data/coldkey_swap.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from dataclasses import asdict, dataclass, fields
from typing import Optional

from async_substrate_interface.types import ScaleObj

from bittensor.core.chain_data.utils import decode_account_id
from scalecodec.base import ScaleType


@dataclass
Expand Down Expand Up @@ -34,7 +31,7 @@ class ColdkeySwapAnnouncementInfo:

@classmethod
def from_query(
cls, coldkey_ss58: str, query: "ScaleObj"
cls, coldkey_ss58: str, query: Optional[ScaleType]
) -> Optional["ColdkeySwapAnnouncementInfo"]:
"""
Creates a ColdkeySwapAnnouncementInfo object from a Substrate query result.
Expand Down Expand Up @@ -69,7 +66,7 @@ def from_record(cls, record: tuple) -> "ColdkeySwapAnnouncementInfo":
Returns:
ColdkeySwapAnnouncementInfo object with announcement details for the coldkey from the record.
"""
coldkey_ss58 = decode_account_id(record[0])
coldkey_ss58 = record[0]
announcement_data = record[1]
return cls.from_query(coldkey_ss58, announcement_data)

Expand Down Expand Up @@ -98,7 +95,7 @@ class ColdkeySwapDisputeInfo:

@classmethod
def from_query(
cls, coldkey_ss58: str, query: "ScaleObj"
cls, coldkey_ss58: str, query: ScaleType
) -> Optional["ColdkeySwapDisputeInfo"]:
"""
Creates a ColdkeySwapDisputeInfo object from a Substrate query result.
Expand All @@ -115,7 +112,7 @@ def from_query(
return cls(coldkey=coldkey_ss58, disputed_block=int(query.value))

@classmethod
def from_record(cls, record: tuple) -> "ColdkeySwapDisputeInfo":
def from_record(cls, record: tuple[str, int]) -> "ColdkeySwapDisputeInfo":
"""
Creates a ColdkeySwapDisputeInfo object from a query_map record.

Expand All @@ -126,12 +123,7 @@ def from_record(cls, record: tuple) -> "ColdkeySwapDisputeInfo":
Returns:
ColdkeySwapDisputeInfo object with dispute details for the coldkey from the record.
"""
coldkey_ss58 = decode_account_id(record[0])
val = record[1]
disputed_block = (
int(val.value) if getattr(val, "value", None) is not None else int(val)
)
return cls(coldkey=coldkey_ss58, disputed_block=disputed_block)
return cls(coldkey=record[0], disputed_block=int(record[1]))


@dataclass
Expand Down
11 changes: 3 additions & 8 deletions bittensor/core/chain_data/crowdloan_info.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from dataclasses import dataclass
from typing import Optional

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


Expand Down Expand Up @@ -47,18 +46,14 @@ def from_dict(cls, idx: int, data: dict) -> "CrowdloanInfo":
"""Returns a CrowdloanInfo object from decoded chain data."""
return cls(
id=idx,
creator=decode_account_id(data["creator"]),
creator=data["creator"],
deposit=Balance.from_rao(data["deposit"]),
min_contribution=Balance.from_rao(data["min_contribution"]),
end=data["end"],
cap=Balance.from_rao(data["cap"]),
funds_account=decode_account_id(data["funds_account"])
if data.get("funds_account")
else None,
funds_account=data["funds_account"],
raised=Balance.from_rao(data["raised"]),
target_address=decode_account_id(data.get("target_address"))
if data.get("target_address")
else None,
target_address=data.get("target_address"),
call=data.get("call") if data.get("call") else None,
finalized=data["finalized"],
contributors_count=data["contributors_count"],
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 @@ -47,14 +46,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 @@ -96,8 +95,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 @@ -34,10 +33,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
42 changes: 9 additions & 33 deletions bittensor/core/chain_data/metagraph_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,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 (
get_netuid_and_mechid_by_storage_index,
u64_normalized_float as u64tf,
Expand All @@ -25,8 +24,7 @@ def get_selective_metagraph_commitments(
if commitments := decoded.get("commitments"):
result = []
for commitment in commitments:
account_id_bytes, commitment_bytes = commitment
hotkey = decode_account_id(account_id_bytes)
hotkey, commitment_bytes = commitment
commitment = bytes(
commitment_bytes[SELECTIVE_METAGRAPH_COMMITMENTS_OFFSET:]
).decode("utf-8", errors="ignore")
Expand Down Expand Up @@ -209,16 +207,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"),
owner_coldkey=decoded.get("owner_coldkey"),
# Tempo terms.
block=decoded["block"],
tempo=decoded["tempo"],
Expand Down Expand Up @@ -312,16 +302,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"),
coldkeys=decoded.get("coldkeys"),
identities=decoded["identities"],
axons=decoded.get("axons", []),
active=decoded["active"],
Expand Down Expand Up @@ -383,19 +365,13 @@ def _from_dict(cls, decoded: dict) -> "MetagraphInfo":
),
# Dividend break down
tao_dividends_per_hotkey=(
[
(decode_account_id(alpha[0]), _tbwu(alpha[1]))
for alpha in decoded["tao_dividends_per_hotkey"]
]
if decoded.get("tao_dividends_per_hotkey") is not None
[(ss58, _tbwu(alpha)) for (ss58, alpha) in tdph]
if (tdph := decoded.get("tao_dividends_per_hotkey")) is not None
else None
),
alpha_dividends_per_hotkey=(
[
(decode_account_id(adphk[0]), _tbwu(adphk[1], _netuid))
for adphk in decoded["alpha_dividends_per_hotkey"]
]
if decoded.get("alpha_dividends_per_hotkey") is not None
[(ss58, _tbwu(adphk, _netuid)) for (ss58, adphk) in adph]
if (adph := decoded.get("alpha_dividends_per_hotkey")) is not None
else None
),
validators=[v for v in decoded["validators"]]
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 @@ -120,8 +120,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 @@ -87,8 +87,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"],
)
Loading