Skip to content

Commit 7efb09b

Browse files
committed
Cleaned up logic, added tests
1 parent 006e952 commit 7efb09b

File tree

2 files changed

+96
-18
lines changed

2 files changed

+96
-18
lines changed

bittensor_cli/cli.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -388,18 +388,16 @@ def verbosity_console_handler(verbosity_level: int = 1) -> None:
388388
json_console.quiet = False
389389

390390

391-
def get_optional_netuid(
392-
netuid: Optional[Union[int, list[int]]], all_netuids: bool
393-
) -> Optional[int]:
391+
def get_optional_netuid(netuid: Optional[int], all_netuids: bool) -> Optional[int]:
394392
"""
395393
Parses options to determine if the user wants to use a specific netuid or all netuids (None)
396394
397395
Returns:
398396
None if using all netuids, otherwise int for the netuid to use
399397
"""
400-
if (blank_netuid := (netuid is None or netuid == [])) and all_netuids is True:
398+
if netuid is None and all_netuids is True:
401399
return None
402-
elif blank_netuid and all_netuids is False:
400+
elif netuid is None and all_netuids is False:
403401
answer = Prompt.ask(
404402
f"Enter the [{COLORS.G.SUBHEAD_MAIN}]netuid"
405403
f"[/{COLORS.G.SUBHEAD_MAIN}] to use. Leave blank for all netuids",
@@ -3276,29 +3274,42 @@ def stake_add(
32763274
32773275
"""
32783276
netuids = netuids or []
3279-
if netuids:
3280-
netuids = parse_to_list(
3281-
netuids, int, "Netuids must be ints separated by commas", False
3282-
)
32833277
self.verbosity_handler(quiet, verbose, json_output)
32843278
safe_staking = self.ask_safe_staking(safe_staking)
32853279
if safe_staking:
32863280
rate_tolerance = self.ask_rate_tolerance(rate_tolerance)
32873281
allow_partial_stake = self.ask_partial_stake(allow_partial_stake)
32883282
console.print("\n")
3289-
if not netuids:
3290-
netuid_ = get_optional_netuid(netuids, all_netuids)
3283+
3284+
if netuids:
3285+
netuids = parse_to_list(
3286+
netuids, int, "Netuids must be ints separated by commas", False
3287+
)
3288+
else:
3289+
netuid_ = get_optional_netuid(None, all_netuids)
32913290
netuids = [netuid_] if netuid_ else None
3291+
if netuids:
3292+
for netuid_ in netuids:
3293+
# ensure no negative netuids make it into our list
3294+
validate_netuid(netuid_)
32923295

32933296
if stake_all and amount:
32943297
print_error(
32953298
"Cannot specify an amount and 'stake-all'. Choose one or the other."
32963299
)
3297-
raise typer.Exit()
3300+
return
32983301

32993302
if stake_all and not amount:
33003303
if not Confirm.ask("Stake all the available TAO tokens?", default=False):
3301-
raise typer.Exit()
3304+
return
3305+
3306+
if (
3307+
stake_all
3308+
and (isinstance(netuids, list) and len(netuids) > 1)
3309+
or (netuids is None)
3310+
):
3311+
print_error("Cannot stake all to multiple subnets.")
3312+
return
33023313

33033314
if all_hotkeys and include_hotkeys:
33043315
print_error(

tests/e2e_tests/test_staking_sudo.py

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ def test_staking(local_chain, wallet_setup):
3636
"""
3737
print("Testing staking and sudo commands🧪")
3838
netuid = 2
39+
multiple_netuids = [2, 3]
3940
wallet_path_alice = "//Alice"
4041

4142
# Create wallet for Alice
@@ -91,7 +92,42 @@ def test_staking(local_chain, wallet_setup):
9192
)
9293
result_output = json.loads(result.stdout)
9394
assert result_output["success"] is True
94-
assert result_output["netuid"] == 2
95+
assert result_output["netuid"] == netuid
96+
97+
# Register another subnet with sudo as Alice
98+
result_for_second_repo = exec_command_alice(
99+
command="subnets",
100+
sub_command="create",
101+
extra_args=[
102+
"--wallet-path",
103+
wallet_path_alice,
104+
"--chain",
105+
"ws://127.0.0.1:9945",
106+
"--wallet-name",
107+
wallet_alice.name,
108+
"--wallet-hotkey",
109+
wallet_alice.hotkey_str,
110+
"--subnet-name",
111+
"Test Subnet",
112+
"--repo",
113+
"https://github.com/username/repo",
114+
"--contact",
115+
116+
"--url",
117+
"https://testsubnet.com",
118+
"--discord",
119+
"alice#1234",
120+
"--description",
121+
"A test subnet for e2e testing",
122+
"--additional-info",
123+
"Created by Alice",
124+
"--no-prompt",
125+
"--json-output",
126+
],
127+
)
128+
result_output_second = json.loads(result_for_second_repo.stdout)
129+
assert result_output_second["success"] is True
130+
assert result_output_second["netuid"] == multiple_netuids[1]
95131

96132
# Register Alice in netuid = 1 using her hotkey
97133
register_subnet = exec_command_alice(
@@ -192,7 +228,7 @@ def test_staking(local_chain, wallet_setup):
192228
assert get_identity_output["additional"] == sn_add_info
193229

194230
# Add stake to Alice's hotkey
195-
add_stake = exec_command_alice(
231+
add_stake_single = exec_command_alice(
196232
command="stake",
197233
sub_command="add",
198234
extra_args=[
@@ -216,10 +252,10 @@ def test_staking(local_chain, wallet_setup):
216252
"144",
217253
],
218254
)
219-
assert "✅ Finalized" in add_stake.stdout, add_stake.stderr
255+
assert "✅ Finalized" in add_stake_single.stdout, add_stake_single.stderr
220256

221257
# Execute stake show for Alice's wallet
222-
show_stake = exec_command_alice(
258+
show_stake_adding_single = exec_command_alice(
223259
command="stake",
224260
sub_command="list",
225261
extra_args=[
@@ -235,7 +271,8 @@ def test_staking(local_chain, wallet_setup):
235271

236272
# Assert correct stake is added
237273
cleaned_stake = [
238-
re.sub(r"\s+", " ", line) for line in show_stake.stdout.splitlines()
274+
re.sub(r"\s+", " ", line)
275+
for line in show_stake_adding_single.stdout.splitlines()
239276
]
240277
stake_added = cleaned_stake[8].split("│")[3].strip().split()[0]
241278
assert Balance.from_tao(float(stake_added)) >= Balance.from_tao(90)
@@ -284,6 +321,36 @@ def test_staking(local_chain, wallet_setup):
284321
)
285322
assert "✅ Finalized" in remove_stake.stdout
286323

324+
add_stake_multiple = exec_command_alice(
325+
command="stake",
326+
sub_command="add",
327+
extra_args=[
328+
"--netuids",
329+
",".join(str(x) for x in multiple_netuids),
330+
"--wallet-path",
331+
wallet_path_alice,
332+
"--wallet-name",
333+
wallet_alice.name,
334+
"--hotkey",
335+
wallet_alice.hotkey_str,
336+
"--chain",
337+
"ws://127.0.0.1:9945",
338+
"--amount",
339+
"100",
340+
"--tolerance",
341+
"0.1",
342+
"--partial",
343+
"--no-prompt",
344+
"--era",
345+
"144",
346+
],
347+
)
348+
assert "✅ Finalized" in add_stake_multiple.stdout, add_stake_multiple.stderr
349+
for netuid_ in multiple_netuids:
350+
assert f"Stake added to netuid: {netuid_}" in add_stake_multiple.stdout, (
351+
add_stake_multiple.stderr
352+
)
353+
287354
# Fetch the hyperparameters of the subnet
288355
hyperparams = exec_command_alice(
289356
command="sudo",

0 commit comments

Comments
 (0)