Skip to content

Commit

Permalink
Merge branch 'staging' into feat/use-bt-decode-in-runtime-call
Browse files Browse the repository at this point in the history
  • Loading branch information
thewhaleking authored Dec 5, 2024
2 parents 39ae665 + eef8e28 commit bc2fb45
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 101 deletions.
32 changes: 18 additions & 14 deletions bittensor_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ class Options:
use_password = typer.Option(
True,
help="Set this to `True` to protect the generated Bittensor key with a password.",
is_flag=True,
flag_value=False,
)
public_hex_key = typer.Option(None, help="The public key in hex format.")
ss58_address = typer.Option(
Expand Down Expand Up @@ -1843,8 +1841,6 @@ def wallet_regen_hotkey(
use_password: bool = typer.Option(
False, # Overriden to False
help="Set to 'True' to protect the generated Bittensor key with a password.",
is_flag=True,
flag_value=True,
),
quiet: bool = Options.quiet,
verbose: bool = Options.verbose,
Expand Down Expand Up @@ -1901,8 +1897,6 @@ def wallet_new_hotkey(
use_password: bool = typer.Option(
False, # Overriden to False
help="Set to 'True' to protect the generated Bittensor key with a password.",
is_flag=True,
flag_value=True,
),
quiet: bool = Options.quiet,
verbose: bool = Options.verbose,
Expand Down Expand Up @@ -3665,7 +3659,7 @@ def stake_set_children(
prompt: bool = Options.prompt,
):
"""
Set child hotkeys on specified subnets.
Set child hotkeys on a specified subnet (or all). Overrides currently set children.
Users can specify the 'proportion' to delegate to child hotkeys (ss58 address). The sum of proportions cannot be greater than 1.
Expand Down Expand Up @@ -3750,7 +3744,7 @@ def stake_revoke_children(
prompt: bool = Options.prompt,
):
"""
Remove all children hotkeys on a specified subnet.
Remove all children hotkeys on a specified subnet (or all).
This command is used to remove delegated authority from all child hotkeys, removing their position and influence on the subnet.
Expand Down Expand Up @@ -3890,12 +3884,12 @@ def sudo_set(
"""
self.verbosity_handler(quiet, verbose)

hyperparams = self._run_command(
sudo.get_hyperparameters(self.initialize_chain(network), netuid)
)

if not hyperparams:
raise typer.Exit()
if not param_name or not param_value:
hyperparams = self._run_command(
sudo.get_hyperparameters(self.initialize_chain(network), netuid)
)
if not hyperparams:
raise typer.Exit()

if not param_name:
hyperparam_list = [field.name for field in fields(SubnetHyperparameters)]
Expand All @@ -3910,6 +3904,16 @@ def sudo_set(
)
param_name = hyperparam_list[choice - 1]

if param_name in ["alpha_high", "alpha_low"]:
param_name = "alpha_values"
low_val = FloatPrompt.ask(
"Enter the new value for [dark_orange]alpha_low[/dark_orange]"
)
high_val = FloatPrompt.ask(
"Enter the new value for [dark_orange]alpha_high[/dark_orange]"
)
param_value = f"{low_val},{high_val}"

if not param_value:
param_value = Prompt.ask(
f"Enter the new value for [dark_orange]{param_name}[/dark_orange] in the VALUE column format"
Expand Down
52 changes: 30 additions & 22 deletions bittensor_cli/src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,28 +156,36 @@ class WalletValidationTypes(Enum):


HYPERPARAMS = {
"serving_rate_limit": "sudo_set_serving_rate_limit",
"min_difficulty": "sudo_set_min_difficulty",
"max_difficulty": "sudo_set_max_difficulty",
"weights_version": "sudo_set_weights_version_key",
"weights_rate_limit": "sudo_set_weights_set_rate_limit",
"max_weight_limit": "sudo_set_max_weight_limit",
"immunity_period": "sudo_set_immunity_period",
"min_allowed_weights": "sudo_set_min_allowed_weights",
"activity_cutoff": "sudo_set_activity_cutoff",
"network_registration_allowed": "sudo_set_network_registration_allowed",
"network_pow_registration_allowed": "sudo_set_network_pow_registration_allowed",
"min_burn": "sudo_set_min_burn",
"max_burn": "sudo_set_max_burn",
"adjustment_alpha": "sudo_set_adjustment_alpha",
"rho": "sudo_set_rho",
"kappa": "sudo_set_kappa",
"difficulty": "sudo_set_difficulty",
"bonds_moving_avg": "sudo_set_bonds_moving_average",
"commit_reveal_weights_interval": "sudo_set_commit_reveal_weights_interval",
"commit_reveal_weights_enabled": "sudo_set_commit_reveal_weights_enabled",
"alpha_values": "sudo_set_alpha_values",
"liquid_alpha_enabled": "sudo_set_liquid_alpha_enabled",
# btcli name: (subtensor method, sudo bool)
"rho": ("sudo_set_rho", False),
"kappa": ("sudo_set_kappa", False),
"immunity_period": ("sudo_set_immunity_period", False),
"min_allowed_weights": ("sudo_set_min_allowed_weights", False),
"max_weights_limit": ("sudo_set_max_weight_limit", False),
"tempo": ("sudo_set_tempo", True),
"min_difficulty": ("sudo_set_min_difficulty", False),
"max_difficulty": ("sudo_set_max_difficulty", False),
"weights_version": ("sudo_set_weights_version_key", False),
"weights_rate_limit": ("sudo_set_weights_set_rate_limit", False),
"adjustment_interval": ("sudo_set_adjustment_interval", True),
"activity_cutoff": ("sudo_set_activity_cutoff", False),
"target_regs_per_interval": ("sudo_set_target_registrations_per_interval", True),
"min_burn": ("sudo_set_min_burn", False),
"max_burn": ("sudo_set_max_burn", False),
"bonds_moving_avg": ("sudo_set_bonds_moving_average", False),
"max_regs_per_block": ("sudo_set_max_registrations_per_block", True),
"serving_rate_limit": ("sudo_set_serving_rate_limit", False),
"max_validators": ("sudo_set_max_allowed_validators", True),
"adjustment_alpha": ("sudo_set_adjustment_alpha", False),
"difficulty": ("sudo_set_difficulty", False),
"commit_reveal_weights_interval": (
"sudo_set_commit_reveal_weights_interval",
False,
),
"commit_reveal_weights_enabled": ("sudo_set_commit_reveal_weights_enabled", False),
"alpha_values": ("sudo_set_alpha_values", False),
"liquid_alpha_enabled": ("sudo_set_liquid_alpha_enabled", False),
"registration_allowed": ("sudo_set_network_registration_allowed", False),
}

# Help Panels for cli help
Expand Down
57 changes: 27 additions & 30 deletions bittensor_cli/src/bittensor/async_substrate_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from dataclasses import dataclass
from hashlib import blake2b
from itertools import chain
from typing import Optional, Any, Union, Callable, Awaitable, cast
from typing import Optional, Any, Union, Callable, Awaitable, cast, TYPE_CHECKING
from types import SimpleNamespace

from bt_decode import (
Expand All @@ -15,6 +15,7 @@
MetadataV15,
)
from async_property import async_property
from bittensor_wallet import Keypair
from scalecodec import GenericExtrinsic
from scalecodec.base import ScaleBytes, ScaleType, RuntimeConfigurationObject
from scalecodec.type_registry import load_type_registry_preset
Expand All @@ -26,12 +27,16 @@
BlockNotFound,
)
from substrateinterface.storage import StorageKey
import websockets
from websockets.asyncio.client import connect
from websockets.exceptions import ConnectionClosed

from .utils import bytes_from_hex_string_result, encode_account_id

from bittensor_cli.src.bittensor.utils import hex_to_bytes

if TYPE_CHECKING:
from websockets.asyncio.client import ClientConnection

ResultHandler = Callable[[dict, Any], Awaitable[tuple[dict, bool]]]


Expand Down Expand Up @@ -471,7 +476,7 @@ def add_item(
self.block_hashes[block_hash] = runtime

def retrieve(
self, block: Optional[int], block_hash: Optional[str]
self, block: Optional[int] = None, block_hash: Optional[str] = None
) -> Optional["Runtime"]:
if block is not None:
return self.blocks.get(block)
Expand Down Expand Up @@ -662,7 +667,7 @@ def __init__(
# TODO allow setting max concurrent connections and rpc subscriptions per connection
# TODO reconnection logic
self.ws_url = ws_url
self.ws: Optional[websockets.WebSocketClientProtocol] = None
self.ws: Optional["ClientConnection"] = None
self.id = 0
self.max_subscriptions = max_subscriptions
self.max_connections = max_connections
Expand All @@ -684,15 +689,12 @@ async def __aenter__(self):
self._exit_task.cancel()
if not self._initialized:
self._initialized = True
await self._connect()
self.ws = await asyncio.wait_for(
connect(self.ws_url, **self._options), timeout=10
)
self._receiving_task = asyncio.create_task(self._start_receiving())
return self

async def _connect(self):
self.ws = await asyncio.wait_for(
websockets.connect(self.ws_url, **self._options), timeout=10
)

async def __aexit__(self, exc_type, exc_val, exc_tb):
async with self._lock:
self._in_use -= 1
Expand Down Expand Up @@ -733,9 +735,7 @@ async def shutdown(self):

async def _recv(self) -> None:
try:
response = json.loads(
await cast(websockets.WebSocketClientProtocol, self.ws).recv()
)
response = json.loads(await self.ws.recv())
async with self._lock:
self._open_subscriptions -= 1
if "id" in response:
Expand All @@ -744,7 +744,7 @@ async def _recv(self) -> None:
self._received[response["params"]["subscription"]] = response
else:
raise KeyError(response)
except websockets.ConnectionClosed:
except ConnectionClosed:
raise
except KeyError as e:
raise e
Expand All @@ -755,7 +755,7 @@ async def _start_receiving(self):
await self._recv()
except asyncio.CancelledError:
pass
except websockets.ConnectionClosed:
except ConnectionClosed:
# TODO try reconnect, but only if it's needed
raise

Expand All @@ -772,7 +772,7 @@ async def send(self, payload: dict) -> int:
try:
await self.ws.send(json.dumps({**payload, **{"id": original_id}}))
return original_id
except websockets.ConnectionClosed:
except ConnectionClosed:
raise

async def retrieve(self, item_id: int) -> Optional[dict]:
Expand Down Expand Up @@ -810,13 +810,13 @@ def __init__(
"""
self.chain_endpoint = chain_endpoint
self.__chain = chain_name
options = {
"max_size": 2**32,
"write_limit": 2**16,
}
if version.parse(websockets.__version__) < version.parse("14.0"):
options.update({"read_limit": 2**16})
self.ws = Websocket(chain_endpoint, options=options)
self.ws = Websocket(
chain_endpoint,
options={
"max_size": 2**32,
"write_limit": 2**16,
},
)
self._lock = asyncio.Lock()
self.last_block_hash: Optional[str] = None
self.config = {
Expand Down Expand Up @@ -1259,7 +1259,7 @@ async def create_storage_key(
-------
StorageKey
"""
runtime = await self.init_runtime(block_hash=block_hash)
await self.init_runtime(block_hash=block_hash)

return StorageKey.create_from_storage_function(
pallet,
Expand Down Expand Up @@ -1679,7 +1679,7 @@ async def _process_response(
self,
response: dict,
subscription_id: Union[int, str],
value_scale_type: Optional[str],
value_scale_type: Optional[str] = None,
storage_item: Optional[ScaleType] = None,
runtime: Optional[Runtime] = None,
result_handler: Optional[ResultHandler] = None,
Expand Down Expand Up @@ -1893,7 +1893,6 @@ async def compose_call(
call_params = {}

await self.init_runtime(block_hash=block_hash)

call = self.runtime_config.create_scale_object(
type_string="Call", metadata=self.metadata
)
Expand Down Expand Up @@ -2211,7 +2210,8 @@ async def create_signed_extrinsic(
:return: The signed Extrinsic
"""
await self.init_runtime()
if not self.metadata:
await self.init_runtime()

# Check requirements
if not isinstance(call, GenericCall):
Expand Down Expand Up @@ -2264,7 +2264,6 @@ async def create_signed_extrinsic(
extrinsic = self.runtime_config.create_scale_object(
type_string="Extrinsic", metadata=self.metadata
)

value = {
"account_id": f"0x{keypair.public_key.hex()}",
"signature": f"0x{signature.hex()}",
Expand All @@ -2282,9 +2281,7 @@ async def create_signed_extrinsic(
signature_cls = self.runtime_config.get_decoder_class("ExtrinsicSignature")
if issubclass(signature_cls, self.runtime_config.get_decoder_class("Enum")):
value["signature_version"] = signature_version

extrinsic.encode(value)

return extrinsic

async def get_chain_finalised_head(self):
Expand Down
Loading

0 comments on commit bc2fb45

Please sign in to comment.