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