Skip to content

Commit

Permalink
Ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
thewhaleking committed Jan 6, 2025
1 parent 3eaae9f commit d7bc745
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 26 deletions.
8 changes: 4 additions & 4 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
from bittensor.utils.btlogging import logging
from bittensor.utils.delegates_details import DelegatesDetails
from bittensor.utils.weight_utils import generate_weight_hash
from bittensor.core.metagraph import AsyncMetagraph
from bittensor.core.metagraph import Metagraph

if TYPE_CHECKING:
from scalecodec import ScaleType
Expand Down Expand Up @@ -133,7 +133,7 @@ def __init__(self, network: str = DEFAULT_NETWORK) -> None:
self.network = DEFAULTS.subtensor.network

self.substrate = AsyncSubstrateInterface(
chain_endpoint=self.chain_endpoint,
url=self.chain_endpoint,
ss58_format=SS58_FORMAT,
type_registry=TYPE_REGISTRY,
use_remote_preset=True,
Expand Down Expand Up @@ -2471,7 +2471,7 @@ async def block(self) -> int:

async def metagraph(
self, netuid: int, lite: bool = True, block: Optional[int] = None
) -> AsyncMetagraph:
) -> Metagraph:
"""
Returns a synced metagraph for a specified subnet within the Bittensor network. The metagraph represents the
network's structure, including neuron connections and interactions.
Expand All @@ -2487,7 +2487,7 @@ async def metagraph(
The metagraph is an essential tool for understanding the topology and dynamics of the Bittensor network's
decentralized architecture, particularly in relation to neuron interconnectivity and consensus processes.
"""
metagraph = AsyncMetagraph(
metagraph = Metagraph(
network=self.chain_endpoint,
netuid=netuid,
lite=lite,
Expand Down
18 changes: 1 addition & 17 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,6 @@ def add_args(cls, parser: "argparse.ArgumentParser", prefix: Optional[str] = Non
pass

# Inner private functions
@networking.ensure_connected
def _encode_params(
self,
call_definition: dict[str, list["ParamWithTypes"]],
Expand Down Expand Up @@ -458,7 +457,7 @@ def _get_hyperparameter(
return result.value

# Chain calls methods ==============================================================================================
@networking.ensure_connected

def query_subtensor(
self, name: str, block: Optional[int] = None, params: Optional[list] = None
) -> "ScaleType":
Expand All @@ -485,7 +484,6 @@ def query_subtensor(
),
)

@networking.ensure_connected
def query_map_subtensor(
self, name: str, block: Optional[int] = None, params: Optional[list] = None
) -> "QueryMapResult":
Expand Down Expand Up @@ -561,7 +559,6 @@ def query_runtime_api(

return obj.decode()

@networking.ensure_connected
def state_call(
self, method: str, data: str, block: Optional[int] = None
) -> dict[Any, Any]:
Expand All @@ -584,7 +581,6 @@ def state_call(
params=[method, data, block_hash] if block_hash else [method, data],
)

@networking.ensure_connected
def query_map(
self,
module: str,
Expand Down Expand Up @@ -615,7 +611,6 @@ def query_map(
),
)

@networking.ensure_connected
def query_constant(
self, module_name: str, constant_name: str, block: Optional[int] = None
) -> Optional["ScaleType"]:
Expand All @@ -640,7 +635,6 @@ def query_constant(
),
)

@networking.ensure_connected
def query_module(
self,
module: str,
Expand Down Expand Up @@ -671,7 +665,6 @@ def query_module(
),
)

@networking.ensure_connected
def get_account_next_index(self, address: str) -> int:
"""
Returns the next nonce for an account, taking into account the transaction pool.
Expand Down Expand Up @@ -767,7 +760,6 @@ def get_netuids_for_hotkey(
else []
)

@networking.ensure_connected
def get_current_block(self) -> int:
"""
Returns the current block number on the Bittensor blockchain. This function provides the latest block number, indicating the most recent state of the blockchain.
Expand Down Expand Up @@ -862,7 +854,6 @@ def blocks_since_last_update(self, netuid: int, uid: int) -> Optional[int]:
call = self._get_hyperparameter(param_name="LastUpdate", netuid=netuid)
return None if call is None else self.get_current_block() - int(call[uid])

@networking.ensure_connected
def get_block_hash(self, block_id: int) -> str:
"""
Retrieves the hash of a specific block on the Bittensor blockchain. The block hash is a unique identifier representing the cryptographic hash of the block's content, ensuring its integrity and immutability.
Expand Down Expand Up @@ -974,7 +965,6 @@ def get_neuron_certificate(
return None
return None

@networking.ensure_connected
def neuron_for_uid(
self, uid: int, netuid: int, block: Optional[int] = None
) -> "NeuronInfo":
Expand Down Expand Up @@ -1216,7 +1206,6 @@ def subnet_exists(self, netuid: int, block: Optional[int] = None) -> bool:
_result = self.query_subtensor("NetworksAdded", block, [netuid])
return getattr(_result, "value", False)

@networking.ensure_connected
def get_all_subnets_info(self, block: Optional[int] = None) -> list[SubnetInfo]:
"""
Retrieves detailed information about all subnets within the Bittensor network. This function provides comprehensive data on each subnet, including its characteristics and operational parameters.
Expand Down Expand Up @@ -1471,7 +1460,6 @@ def weights(

return w_map

@networking.ensure_connected
def get_balance(self, address: str, block: Optional[int] = None) -> "Balance":
"""
Retrieves the token balance of a specific address within the Bittensor network. This function queries the blockchain to determine the amount of Tao held by a given account.
Expand Down Expand Up @@ -1503,7 +1491,6 @@ def get_balance(self, address: str, block: Optional[int] = None) -> "Balance":

return Balance(result.value["data"]["free"])

@networking.ensure_connected
def get_transfer_fee(
self, wallet: "Wallet", dest: str, value: Union["Balance", float, int]
) -> "Balance":
Expand Down Expand Up @@ -1633,7 +1620,6 @@ def get_delegate_take(
else u16_normalized_float(_result.value)
)

@networking.ensure_connected
def get_delegate_by_hotkey(
self, hotkey_ss58: str, block: Optional[int] = None
) -> Optional[DelegateInfo]:
Expand Down Expand Up @@ -1723,7 +1709,6 @@ def get_hotkey_owner(
else result.value
)

@networking.ensure_connected
def get_minimum_required_stake(
self,
) -> Balance:
Expand Down Expand Up @@ -1760,7 +1745,6 @@ def tx_rate_limit(self, block: Optional[int] = None) -> Optional[int]:
result = self.query_subtensor("TxRateLimit", block)
return getattr(result, "value", None)

@networking.ensure_connected
def get_delegates(self, block: Optional[int] = None) -> list[DelegateInfo]:
"""
Retrieves a list of all delegate neurons within the Bittensor network. This function provides an overview of the neurons that are actively involved in the network's delegation system.
Expand Down
46 changes: 41 additions & 5 deletions bittensor/utils/substrate_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ class AsyncSubstrateInterface:

def __init__(
self,
chain_endpoint: str,
url: str,
use_remote_preset: bool = False,
auto_discover: bool = True,
ss58_format: Optional[int] = None,
Expand All @@ -910,7 +910,7 @@ def __init__(
Otherwise, some (most) methods will not work properly, and may raise exceptions.
Args:
chain_endpoint: the URI of the chain to connect to
url: the URI of the chain to connect to
use_remote_preset: whether to pull the preset from GitHub
auto_discover: whether to automatically pull the presets based on the chain name and type registry
ss58_format: the specific SS58 format to use
Expand All @@ -923,10 +923,11 @@ def __init__(
"""
self.max_retries = max_retries
self.retry_timeout = retry_timeout
self.chain_endpoint = chain_endpoint
self.chain_endpoint = url
self.url = url
self.__chain = chain_name
self.ws = Websocket(
chain_endpoint,
url,
options={
"max_size": 2**32,
"write_limit": 2**16,
Expand Down Expand Up @@ -3865,7 +3866,7 @@ def __init__(
mock: bool = False,
):
self._async_instance = AsyncSubstrateInterface(
chain_endpoint=chain_endpoint,
url=chain_endpoint,
use_remote_preset=use_remote_preset,
auto_discover=auto_discover,
ss58_format=ss58_format,
Expand Down Expand Up @@ -3896,3 +3897,38 @@ def sync_method(*args, **kwargs):
return self.event_loop.run_until_complete(attr)
else:
return attr

def query(
self,
module: str,
storage_function: str,
params: Optional[list] = None,
block_hash: Optional[str] = None,
raw_storage_key: Optional[bytes] = None,
subscription_handler=None,
reuse_block_hash: bool = False,
) -> "ScaleType":
return self.event_loop.run_until_complete(
self._async_instance.query(
module,
storage_function,
params,
block_hash,
raw_storage_key,
subscription_handler,
reuse_block_hash,
)
)

def get_constant(
self,
module_name: str,
constant_name: str,
block_hash: Optional[str] = None,
reuse_block_hash: bool = False,
) -> Optional["ScaleType"]:
return self.event_loop.run_until_complete(
self._async_instance.get_constant(
module_name, constant_name, block_hash, reuse_block_hash
)
)

0 comments on commit d7bc745

Please sign in to comment.