Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ef56750
create the copy of `bittensor/core/subtensor.py` with async suffix.
Oct 17, 2024
8886cc8
add async_substrate_interface.py
Oct 28, 2024
f24481d
update `bittensor.utils.format_error_message` to be compatible with a…
Oct 29, 2024
873e239
update `bittensor.core.chain_data`
Oct 29, 2024
c4e7d20
update `bittensor.core.async_subtensor.py` from btcli
Oct 29, 2024
3be23e5
add DelegatesDetails for async_subtensor
Oct 29, 2024
fdc9a83
add validate_chain_endpoint for async_subtensor
Oct 29, 2024
e6fe7b9
update async_substrate_interface.py by Optional where acceptable and …
Oct 29, 2024
a4fe55e
improve settings for async_subtensor.py
Oct 29, 2024
b303526
fix format errors
Oct 29, 2024
809cbf1
fix annotations
Oct 29, 2024
c8a4559
add async_subtensor.py with adaptation to SDK (all methods checked a…
Oct 29, 2024
fa6d48b
update settings.py to be compatible with async_extrinsics
Oct 29, 2024
affb858
add async_transfer extrinsic
Oct 29, 2024
3a28617
add async_registration extrinsic
Oct 29, 2024
0d2dea7
add async_root extrinsics
Oct 29, 2024
0f34613
Merge branch 'staging' into feat/roman/async-subtensor-stage-one
Oct 29, 2024
a03209e
ruff
Oct 29, 2024
d1e182d
Update bittensor/core/extrinsics/async_transfer.py
basfroman Oct 29, 2024
2063b21
fix comments review
Nov 1, 2024
43fc02d
avoid non-direct import within inner code (fix circular import)
Nov 1, 2024
d4d1e9c
del unused code
Nov 1, 2024
fc4dcf1
update AsyncSubtensor (add methods, fix tests)
Nov 1, 2024
318436b
Update bittensor/core/async_subtensor.py
basfroman Nov 4, 2024
883ba20
Merge remote-tracking branch 'origin/staging' into feat/roman/async-s…
Nov 4, 2024
fc6f1e7
fix await properties and remove double format_error_message call
Nov 4, 2024
dbf91f2
fix review comments
Nov 4, 2024
4125995
improve docstrings
Nov 4, 2024
a4b2128
fix
Nov 4, 2024
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
782 changes: 568 additions & 214 deletions bittensor/core/async_subtensor.py

Large diffs are not rendered by default.

92 changes: 42 additions & 50 deletions bittensor/core/extrinsics/async_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
from bittensor_wallet import Wallet
from bittensor_wallet.errors import KeyFileError
from numpy.typing import NDArray
from rich.prompt import Confirm
from rich.table import Table, Column
from substrateinterface.exceptions import SubstrateRequestException

from bittensor.utils import u16_normalized_float, format_error_message
Expand All @@ -22,6 +20,19 @@


async def get_limits(subtensor: "AsyncSubtensor") -> tuple[int, float]:
"""
Retrieves the minimum allowed weights and maximum weight limit for the given subnet.

These values are fetched asynchronously using `asyncio.gather` to run both requests concurrently.

Args:
subtensor (AsyncSubtensor): The AsyncSubtensor object used to interface with the network's substrate node.

Returns:
tuple[int, float]: A tuple containing:
- `min_allowed_weights` (int): The minimum allowed weights.
- `max_weight_limit` (float): The maximum weight limit, normalized to a float value.
"""
# Get weight restrictions.
maw, mwl = await asyncio.gather(
subtensor.get_hyperparameter("MinAllowedWeights", netuid=0),
Expand All @@ -35,19 +46,21 @@ async def get_limits(subtensor: "AsyncSubtensor") -> tuple[int, float]:
async def root_register_extrinsic(
subtensor: "AsyncSubtensor",
wallet: Wallet,
netuid: int,
wait_for_inclusion: bool = True,
wait_for_finalization: bool = True,
prompt: bool = False,
) -> bool:
"""Registers the wallet to root network.

:param subtensor: The AsyncSubtensor object
:param wallet: Bittensor wallet object.
:param wait_for_inclusion: If set, waits for the extrinsic to enter a block before returning `True`, or returns `False` if the extrinsic fails to enter the block within the timeout.
:param wait_for_finalization: If set, waits for the extrinsic to be finalized on the chain before returning `True`, or returns `False` if the extrinsic fails to be finalized within the timeout.
:param prompt: If `True`, the call waits for confirmation from the user before proceeding.
Arguments:
subtensor (bittensor.core.async_subtensor.AsyncSubtensor): The AsyncSubtensor object
wallet (bittensor_wallet.Wallet): Bittensor wallet object.
netuid (int): Subnet uid.
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning `True`, or returns `False` if the extrinsic fails to enter the block within the timeout.
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning `True`, or returns `False` if the extrinsic fails to be finalized within the timeout.

:return: `True` if extrinsic was finalized or included in the block. If we did not wait for finalization/inclusion, the response is `True`.
Returns:
`True` if extrinsic was finalized or included in the block. If we did not wait for finalization/inclusion, the response is `True`.
"""

try:
Expand All @@ -60,7 +73,7 @@ async def root_register_extrinsic(
f"Checking if hotkey (<blue>{wallet.hotkey_str}</blue>) is registered on root."
)
is_registered = await subtensor.is_hotkey_registered(
netuid=0, hotkey_ss58=wallet.hotkey.ss58_address
netuid=netuid, hotkey_ss58=wallet.hotkey.ss58_address
)
if is_registered:
logging.error(
Expand All @@ -82,7 +95,7 @@ async def root_register_extrinsic(
)

if not success:
logging.error(f":cross_mark: <red>Failed</red>: {err_msg}")
logging.error(f":cross_mark: <red>Failed error:</red> {err_msg}")
time.sleep(0.5)
return False

Expand All @@ -91,11 +104,11 @@ async def root_register_extrinsic(
uid = await subtensor.substrate.query(
module="SubtensorModule",
storage_function="Uids",
params=[0, wallet.hotkey.ss58_address],
params=[netuid, wallet.hotkey.ss58_address],
)
if uid is not None:
logging.info(
f":white_heavy_check_mark: <green>Registered with UID</green> <blue>{uid}</blue>"
f":white_heavy_check_mark: <green>Registered with UID</green> <blue>{uid}</blue>."
)
return True
else:
Expand All @@ -106,28 +119,26 @@ async def root_register_extrinsic(

async def set_root_weights_extrinsic(
subtensor: "AsyncSubtensor",
wallet: Wallet,
wallet: "Wallet",
netuids: Union[NDArray[np.int64], list[int]],
weights: Union[NDArray[np.float32], list[float]],
version_key: int = 0,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
prompt: bool = False,
) -> bool:
"""Sets the given weights and values on chain for wallet hotkey account.

:param subtensor: The AsyncSubtensor object
:param wallet: Bittensor wallet object.
:param netuids: The `netuid` of the subnet to set weights for.
:param weights: Weights to set. These must be `float` s and must correspond to the passed `netuid` s.
:param version_key: The version key of the validator.
:param wait_for_inclusion: If set, waits for the extrinsic to enter a block before returning `True`, or returns
`False` if the extrinsic fails to enter the block within the timeout.
:param wait_for_finalization: If set, waits for the extrinsic to be finalized on the chain before returning `True`,
or returns `False` if the extrinsic fails to be finalized within the timeout.
:param prompt: If `True`, the call waits for confirmation from the user before proceeding.
:return: `True` if extrinsic was finalized or included in the block. If we did not wait for finalization/inclusion,
the response is `True`.
Arguments:
subtensor (bittensor.core.async_subtensor.AsyncSubtensor): The AsyncSubtensor object
wallet (bittensor_wallet.Wallet): Bittensor wallet object.
netuids (Union[NDArray[np.int64], list[int]]): The `netuid` of the subnet to set weights for.
weights (Union[NDArray[np.float32], list[float]]): Weights to set. These must be `float` s and must correspond to the passed `netuid` s.
version_key (int): The version key of the validator.
wait_for_inclusion (bool): If set, waits for the extrinsic to enter a block before returning `True`, or returns `False` if the extrinsic fails to enter the block within the timeout.
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning `True`, or returns `False` if the extrinsic fails to be finalized within the timeout.

Returns:
`True` if extrinsic was finalized or included in the block. If we did not wait for finalization/inclusion, the response is `True`.
"""

async def _do_set_weights():
Expand Down Expand Up @@ -168,13 +179,13 @@ async def _do_set_weights():
)

if my_uid is None:
logging.error("Your hotkey is not registered to the root network")
logging.error("Your hotkey is not registered to the root network.")
return False

try:
wallet.unlock_coldkey()
except KeyFileError:
logging.error("Error decrypting coldkey (possibly incorrect password)")
logging.error("Error decrypting coldkey (possibly incorrect password).")
return False

# First convert types.
Expand Down Expand Up @@ -203,25 +214,6 @@ async def _do_set_weights():
f"Raw weights -> Normalized weights: <blue>{weights}</blue> -> <green>{formatted_weights}</green>"
)

# Ask before moving on.
if prompt:
table = Table(
Column("[dark_orange]Netuid", justify="center", style="bold green"),
Column(
"[dark_orange]Weight", justify="center", style="bold light_goldenrod2"
),
expand=False,
show_edge=False,
)
print("Netuid | Weight")

for netuid, weight in zip(netuids, formatted_weights):
table.add_row(str(netuid), f"{weight:.8f}")
print(f"{netuid} | {weight}")

if not Confirm.ask("\nDo you want to set these root weights?"):
return False

try:
logging.info(":satellite: <magenta>Setting root weights...<magenta>")
weight_uids, weight_vals = convert_weights_and_uids_for_emit(netuids, weights)
Expand All @@ -236,10 +228,10 @@ async def _do_set_weights():
return True
else:
fmt_err = format_error_message(error_message, subtensor.substrate)
logging.error(f":cross_mark: <red>Failed</red>: {fmt_err}")
logging.error(f":cross_mark: <red>Failed error:</red> {fmt_err}")
return False

except SubstrateRequestException as e:
fmt_err = format_error_message(e, subtensor.substrate)
logging.error(f":cross_mark: <red>Failed</red>: error:{fmt_err}")
logging.error(f":cross_mark: <red>Failed error:</red> {fmt_err}")
return False
Loading