bittensor.utils#

Submodules#

Attributes#

Classes#

Certificate

str(object='') -> str

UnlockStatus

Functions#

decode_hex_identity_dict(info_dictionary)

Decodes a dictionary of hexadecimal identities.

deprecated_message(message)

Shows a deprecation warning message with the given message.

determine_chain_endpoint_and_network(network)

Determines the chain endpoint and network from the passed network or chain_endpoint.

float_to_u64(value)

Converts a float to a u64 int

format_error_message(error_message)

Formats an error message from the Subtensor error information for use in extrinsics.

get_caller_name([depth])

Return the name of the caller function.

get_explorer_url_for_network(network, block_hash, ...)

Returns the explorer url for the given block hash and network.

get_function_name()

Return the current function's name.

get_hash(content[, encoding])

get_mechid_storage_index(netuid, mechid)

Computes the storage index for a given netuid and mechid pair.

get_netuid_and_mechid_by_storage_index(storage_index)

Returns the netuid and mechid from the storage index.

is_valid_bittensor_address_or_public_key(address)

Checks if the given address is a valid destination address.

is_valid_ss58_address(address)

Checks if the given address is a valid ss58 address.

ss58_address_to_bytes(ss58_address)

Converts a ss58 address to a bytes object.

ss58_to_vec_u8(ss58_address)

strtobool(val)

Converts a string to a boolean value.

u16_normalized_float(x)

u64_normalized_float(x)

unlock_key(wallet[, unlock_type, raise_error])

Attempts to decrypt a wallet's coldkey or hotkey

validate_chain_endpoint(endpoint_url)

Validates if the provided endpoint URL is a valid WebSocket URL.

Package Contents#

class bittensor.utils.Certificate[source]#

Bases: str

str(object=’’) -> str str(bytes_or_buffer[, encoding[, errors]]) -> str

Create a new string object from the given object. If encoding or errors is specified, then the object must expose a data buffer that will be decoded using the given encoding and error handler. Otherwise, returns the result of object.__str__() (if defined) or repr(object). encoding defaults to sys.getdefaultencoding(). errors defaults to ‘strict’.

Initialize self. See help(type(self)) for accurate signature.

bittensor.utils.GLOBAL_MAX_SUBNET_COUNT = 4096#
bittensor.utils.RAOPERTAO = 1000000000.0#
bittensor.utils.U16_MAX = 65535#
bittensor.utils.U64_MAX = 18446744073709551615#
class bittensor.utils.UnlockStatus#

Bases: tuple

message#
success#
bittensor.utils.VersionCheckError#
bittensor.utils.check_version#
bittensor.utils.decode_hex_identity_dict(info_dictionary)[source]#

Decodes a dictionary of hexadecimal identities.

Parameters:

info_dictionary (dict[str, Any])

Return type:

dict[str, Any]

bittensor.utils.deprecated_message(message)#

Shows a deprecation warning message with the given message.

Parameters:

message (str)

Return type:

None

bittensor.utils.determine_chain_endpoint_and_network(network)[source]#

Determines the chain endpoint and network from the passed network or chain_endpoint.

Parameters:

network (str) – The network flag. The choices are: finney (main network), archive (archive network +300 blocks), local (local running network), test (test network).

Returns:

The network and chain endpoint flag. If passed, overrides the network argument.

Return type:

tuple[Optional[str], Optional[str]]

bittensor.utils.float_to_u64(value)[source]#

Converts a float to a u64 int

Parameters:

value (float)

Return type:

int

bittensor.utils.format_error_message(error_message)[source]#

Formats an error message from the Subtensor error information for use in extrinsics.

Parameters:

error_message (Union[dict, Exception]) – A dictionary containing the error information from Subtensor, or a SubstrateRequestException containing dictionary literal args.

Returns:

A formatted error message string.

Return type:

str

bittensor.utils.get_caller_name(depth=2)#

Return the name of the caller function.

Parameters:

depth (int)

Return type:

str

bittensor.utils.get_explorer_url_for_network(network, block_hash, network_map)[source]#

Returns the explorer url for the given block hash and network.

Parameters:
  • network (str) – The network to get the explorer url for.

  • block_hash (str) – The block hash to get the explorer url for.

  • network_map (dict[str, dict[str, str]]) – The network maps to get the explorer urls from.

Returns:

The explorer url for the given block hash and network. Or None if the network is not known.

Return type:

Optional[dict[str, str]]

bittensor.utils.get_function_name()#

Return the current function’s name.

Return type:

str

bittensor.utils.get_hash(content, encoding='utf-8')[source]#
bittensor.utils.get_mechid_storage_index(netuid, mechid)#

Computes the storage index for a given netuid and mechid pair.

Parameters:
  • netuid (int) – The netuid of the subnet.

  • mechid (int) – The mechid of the subnet.

Returns:

Storage index number for the subnet and mechanism id.

Return type:

int

bittensor.utils.get_netuid_and_mechid_by_storage_index(storage_index)#

Returns the netuid and mechid from the storage index.

Chain APIs (e.g., SubMetagraph response) returns netuid which is storage index that encodes both the netuid and mechid. This function reverses the encoding to extract these components.

Parameters:

storage_index (int) – The storage index of the subnet.

Returns:

  • netuid - subnet identifier.

  • mechid - mechanism identifier.

Return type:

tuple[int, int]

bittensor.utils.hex_to_bytes[source]#
bittensor.utils.hex_to_ss58#
bittensor.utils.is_valid_bittensor_address_or_public_key(address)[source]#

Checks if the given address is a valid destination address.

Parameters:

address (Union[str, bytes]) – The address to check.

Returns:

True if the address is a valid destination address, False otherwise.

Return type:

bool

bittensor.utils.is_valid_ss58_address(address)[source]#

Checks if the given address is a valid ss58 address.

Parameters:

address (str) – The address to check.

Returns:

True if the address is a valid ss58 address for Bittensor, False otherwise.

Return type:

bool

bittensor.utils.logging#
bittensor.utils.ss58_address_to_bytes(ss58_address)[source]#

Converts a ss58 address to a bytes object.

Parameters:

ss58_address (str)

Return type:

bytes

bittensor.utils.ss58_decode[source]#
bittensor.utils.ss58_to_hex#
bittensor.utils.ss58_to_vec_u8(ss58_address)[source]#
Parameters:

ss58_address (str)

Return type:

list[int]

bittensor.utils.strtobool(val)[source]#

Converts a string to a boolean value.

truth-y values are ‘y’, ‘yes’, ‘t’, ‘true’, ‘on’, and ‘1’; false-y values are ‘n’, ‘no’, ‘f’, ‘false’, ‘off’, and ‘0’.

Raises ValueError if ‘val’ is anything else.

Parameters:

val (str)

Return type:

Union[bool, Literal[‘==SUPRESS==’]]

bittensor.utils.torch#
bittensor.utils.u16_normalized_float(x)[source]#
Parameters:

x (int)

Return type:

float

bittensor.utils.u64_normalized_float(x)[source]#
Parameters:

x (int)

Return type:

float

bittensor.utils.unlock_key(wallet, unlock_type='coldkey', raise_error=False)[source]#

Attempts to decrypt a wallet’s coldkey or hotkey

Parameters:
  • wallet (bittensor_wallet.Wallet) – Bittensor Wallet instance.

  • unlock_type – the key type, ‘coldkey’ or ‘hotkey’.

  • raise_error – if False, will return (False, error msg), if True will raise the otherwise-caught exception.

Returns:

UnlockStatus for success status of unlock, with error message if unsuccessful

Raises:
  • bittensor_wallet.errors.PasswordError – incorrect password

  • bittensor_wallet.errors.KeyFileError – keyfile is corrupt, non-writable, or non-readable, or non-existent

Return type:

UnlockStatus

bittensor.utils.use_torch[source]#
bittensor.utils.validate_chain_endpoint(endpoint_url)[source]#

Validates if the provided endpoint URL is a valid WebSocket URL.

Parameters:

endpoint_url (str)

Return type:

tuple[bool, str]