Skip to content

Commit 48f2a31

Browse files
authored
Merge pull request #580 from opentensor/fix/thewhaleking/change-root-only-sudo
change root only sudo hyperparams
2 parents f7490ca + ac40408 commit 48f2a31

File tree

4 files changed

+145
-27
lines changed

4 files changed

+145
-27
lines changed

bittensor_cli/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4816,7 +4816,7 @@ def sudo_set(
48164816
wallet = self.wallet_ask(
48174817
wallet_name, wallet_path, wallet_hotkey, ask_for=[WO.NAME, WO.PATH]
48184818
)
4819-
result = self._run_command(
4819+
result, err_msg = self._run_command(
48204820
sudo.sudo_set_hyperparameter(
48214821
wallet,
48224822
self.initialize_chain(network),
@@ -4828,7 +4828,7 @@ def sudo_set(
48284828
)
48294829
)
48304830
if json_output:
4831-
json_console.print(json.dumps({"success": result}))
4831+
json_console.print(json.dumps({"success": result, "err_msg": err_msg}))
48324832
return result
48334833

48344834
def sudo_get(

bittensor_cli/src/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -631,10 +631,10 @@ class WalletValidationTypes(Enum):
631631
"min_allowed_weights": ("sudo_set_min_allowed_weights", False),
632632
"max_weights_limit": ("sudo_set_max_weight_limit", False),
633633
"tempo": ("sudo_set_tempo", True),
634-
"min_difficulty": ("sudo_set_min_difficulty", False),
634+
"min_difficulty": ("sudo_set_min_difficulty", True),
635635
"max_difficulty": ("sudo_set_max_difficulty", False),
636636
"weights_version": ("sudo_set_weights_version_key", False),
637-
"weights_rate_limit": ("sudo_set_weights_set_rate_limit", False),
637+
"weights_rate_limit": ("sudo_set_weights_set_rate_limit", True),
638638
"adjustment_interval": ("sudo_set_adjustment_interval", True),
639639
"activity_cutoff": ("sudo_set_activity_cutoff", False),
640640
"target_regs_per_interval": ("sudo_set_target_registrations_per_interval", True),
@@ -645,15 +645,15 @@ class WalletValidationTypes(Enum):
645645
"serving_rate_limit": ("sudo_set_serving_rate_limit", False),
646646
"max_validators": ("sudo_set_max_allowed_validators", True),
647647
"adjustment_alpha": ("sudo_set_adjustment_alpha", False),
648-
"difficulty": ("sudo_set_difficulty", False),
648+
"difficulty": ("sudo_set_difficulty", True),
649649
"commit_reveal_period": (
650650
"sudo_set_commit_reveal_weights_interval",
651651
False,
652652
),
653653
"commit_reveal_weights_enabled": ("sudo_set_commit_reveal_weights_enabled", False),
654654
"alpha_values": ("sudo_set_alpha_values", False),
655655
"liquid_alpha_enabled": ("sudo_set_liquid_alpha_enabled", False),
656-
"registration_allowed": ("sudo_set_network_registration_allowed", False),
656+
"registration_allowed": ("sudo_set_network_registration_allowed", True),
657657
"network_pow_registration_allowed": (
658658
"sudo_set_network_pow_registration_allowed",
659659
False,

bittensor_cli/src/commands/sudo.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ async def set_hyperparameter_extrinsic(
177177
wait_for_inclusion: bool = False,
178178
wait_for_finalization: bool = True,
179179
prompt: bool = True,
180-
) -> bool:
180+
) -> tuple[bool, str]:
181181
"""Sets a hyperparameter for a specific subnetwork.
182182
183183
:param subtensor: initialized SubtensorInterface object
@@ -200,13 +200,14 @@ async def set_hyperparameter_extrinsic(
200200
params=[netuid],
201201
)
202202
if subnet_owner != wallet.coldkeypub.ss58_address:
203-
err_console.print(
203+
err_msg = (
204204
":cross_mark: [red]This wallet doesn't own the specified subnet.[/red]"
205205
)
206-
return False
206+
err_console.print(err_msg)
207+
return False, err_msg
207208

208-
if not unlock_key(wallet).success:
209-
return False
209+
if not (ulw := unlock_key(wallet)).success:
210+
return False, ulw.message
210211

211212
arbitrary_extrinsic = False
212213

@@ -218,15 +219,14 @@ async def set_hyperparameter_extrinsic(
218219
)
219220
extrinsic = parameter
220221
if not arbitrary_extrinsic:
221-
err_console.print(
222-
":cross_mark: [red]Invalid hyperparameter specified.[/red]"
223-
)
224-
return False
222+
err_msg = ":cross_mark: [red]Invalid hyperparameter specified.[/red]"
223+
err_console.print(err_msg)
224+
return False, err_msg
225225
if sudo_ and prompt:
226226
if not Confirm.ask(
227227
"This hyperparam is only settable by root sudo users. If you are not, this will fail. Please confirm"
228228
):
229-
return False
229+
return False, "This hyperparam is only settable by root sudo users"
230230

231231
substrate = subtensor.substrate
232232
msg_value = value if not arbitrary_extrinsic else call_params
@@ -254,10 +254,11 @@ async def set_hyperparameter_extrinsic(
254254
]
255255

256256
if len(value) < len(non_netuid_fields):
257-
err_console.print(
257+
err_msg = (
258258
"Not enough values provided in the list for all parameters"
259259
)
260-
return False
260+
err_console.print(err_msg)
261+
return False, err_msg
261262

262263
call_params.update(
263264
{name: val for name, val in zip(non_netuid_fields, value)}
@@ -290,20 +291,20 @@ async def set_hyperparameter_extrinsic(
290291
)
291292
if not success:
292293
err_console.print(f":cross_mark: [red]Failed[/red]: {err_msg}")
293-
return False
294+
return False, err_msg
294295
elif arbitrary_extrinsic:
295296
console.print(
296297
f":white_heavy_check_mark: "
297298
f"[dark_sea_green3]Hyperparameter {parameter} values changed to {call_params}[/dark_sea_green3]"
298299
)
299-
return True
300+
return True, ""
300301
# Successful registration, final check for membership
301302
else:
302303
console.print(
303304
f":white_heavy_check_mark: "
304305
f"[dark_sea_green3]Hyperparameter {parameter} changed to {value}[/dark_sea_green3]"
305306
)
306-
return True
307+
return True, ""
307308

308309

309310
async def _get_senate_members(
@@ -619,25 +620,26 @@ async def sudo_set_hyperparameter(
619620
param_value: Optional[str],
620621
prompt: bool,
621622
json_output: bool,
622-
):
623+
) -> tuple[bool, str]:
623624
"""Set subnet hyperparameters."""
624625
is_allowed_value, value = allowed_value(param_name, param_value)
625626
if not is_allowed_value:
626-
err_console.print(
627+
err_msg = (
627628
f"Hyperparameter [dark_orange]{param_name}[/dark_orange] value is not within bounds. "
628629
f"Value is {param_value} but must be {value}"
629630
)
630-
return False
631-
success = await set_hyperparameter_extrinsic(
631+
err_console.print(err_msg)
632+
return False, err_msg
633+
success, err_msg = await set_hyperparameter_extrinsic(
632634
subtensor, wallet, netuid, param_name, value, prompt=prompt
633635
)
634636
if json_output:
635-
return success
637+
return success, err_msg
636638
if success:
637639
console.print("\n")
638640
print_verbose("Fetching hyperparameters")
639641
await get_hyperparameters(subtensor, netuid=netuid)
640-
return success
642+
return success, err_msg
641643

642644

643645
async def get_hyperparameters(
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import json
2+
3+
from bittensor_cli.src import HYPERPARAMS
4+
5+
"""
6+
Verify commands:
7+
8+
* btcli subnets create
9+
* btcli sudo set
10+
* btcli sudo get
11+
"""
12+
13+
14+
def test_hyperparams_setting(local_chain, wallet_setup):
15+
netuid = 2
16+
wallet_path_alice = "//Alice"
17+
# Create wallet for Alice
18+
keypair_alice, wallet_alice, wallet_path_alice, exec_command_alice = wallet_setup(
19+
wallet_path_alice
20+
)
21+
22+
# Register a subnet with sudo as Alice
23+
result = exec_command_alice(
24+
command="subnets",
25+
sub_command="create",
26+
extra_args=[
27+
"--wallet-path",
28+
wallet_path_alice,
29+
"--network",
30+
"ws://127.0.0.1:9945",
31+
"--wallet-name",
32+
wallet_alice.name,
33+
"--wallet-hotkey",
34+
wallet_alice.hotkey_str,
35+
"--subnet-name",
36+
"Test Subnet",
37+
"--repo",
38+
"https://github.com/username/repo",
39+
"--contact",
40+
41+
"--url",
42+
"https://testsubnet.com",
43+
"--discord",
44+
"alice#1234",
45+
"--description",
46+
"A test subnet for e2e testing",
47+
"--additional-info",
48+
"Created by Alice",
49+
"--logo-url",
50+
"https://testsubnet.com/logo.png",
51+
"--no-prompt",
52+
"--json-output",
53+
],
54+
)
55+
result_output = json.loads(result.stdout)
56+
assert result_output["success"] is True
57+
assert result_output["netuid"] == netuid
58+
print(result_output)
59+
60+
# Fetch the hyperparameters of the subnet
61+
hyperparams = exec_command_alice(
62+
command="sudo",
63+
sub_command="get",
64+
extra_args=[
65+
"--network",
66+
"ws://127.0.0.1:9945",
67+
"--netuid",
68+
netuid,
69+
"--json-out",
70+
],
71+
)
72+
73+
# Parse all hyperparameters and single out max_burn in TAO
74+
all_hyperparams = json.loads(hyperparams.stdout)
75+
hp = {}
76+
for hyperparam in all_hyperparams:
77+
hp[hyperparam["hyperparameter"]] = hyperparam["value"]
78+
for key, (_, sudo_only) in HYPERPARAMS.items():
79+
if key in hp.keys() and not sudo_only:
80+
if isinstance(hp[key], bool):
81+
new_val = not hp[key]
82+
elif isinstance(hp[key], int):
83+
if hp[key] < 100:
84+
new_val = hp[key] + 1
85+
else:
86+
new_val = hp[key] - 1
87+
else:
88+
raise ValueError(
89+
f"Unrecognized hyperparameter value type: {key}: {hp[key]}"
90+
)
91+
cmd = exec_command_alice(
92+
command="sudo",
93+
sub_command="set",
94+
extra_args=[
95+
"--wallet-path",
96+
wallet_path_alice,
97+
"--network",
98+
"ws://127.0.0.1:9945",
99+
"--wallet-name",
100+
wallet_alice.name,
101+
"--wallet-hotkey",
102+
wallet_alice.hotkey_str,
103+
"--netuid",
104+
netuid,
105+
"--json-out",
106+
"--no-prompt",
107+
"--param",
108+
key,
109+
"--value",
110+
new_val,
111+
],
112+
)
113+
cmd_json = json.loads(cmd.stdout)
114+
assert cmd_json["success"] is True, (key, new_val, cmd.stdout, cmd_json)
115+
print(f"Successfully set hyperparameter {key} to value {new_val}")
116+
print("Successfully set hyperparameters")

0 commit comments

Comments
 (0)