diff --git a/bittensor/subtensor.py b/bittensor/subtensor.py index d309a79504..a8c4f2bd30 100644 --- a/bittensor/subtensor.py +++ b/bittensor/subtensor.py @@ -4270,9 +4270,9 @@ def make_substrate_call_with_retry(encoded_hotkey_: List[int]): return self.substrate.rpc_request( method="delegateInfo_getDelegate", # custom rpc method - params=[encoded_hotkey_, block_hash] - if block_hash - else [encoded_hotkey_], + params=( + [encoded_hotkey_, block_hash] if block_hash else [encoded_hotkey_] + ), ) encoded_hotkey = ss58_to_vec_u8(hotkey_ss58) @@ -4376,9 +4376,9 @@ def make_substrate_call_with_retry(encoded_coldkey_: List[int]): return self.substrate.rpc_request( method="delegateInfo_getDelegated", - params=[block_hash, encoded_coldkey_] - if block_hash - else [encoded_coldkey_], + params=( + [block_hash, encoded_coldkey_] if block_hash else [encoded_coldkey_] + ), ) encoded_coldkey = ss58_to_vec_u8(coldkey_ss58) diff --git a/tests/e2e_tests/multistep/test_axon.py b/tests/e2e_tests/multistep/test_axon.py index f23fb4da7b..e4e087ffae 100644 --- a/tests/e2e_tests/multistep/test_axon.py +++ b/tests/e2e_tests/multistep/test_axon.py @@ -32,7 +32,7 @@ @pytest.mark.asyncio async def test_axon(local_chain): # Register root as Alice - alice_keypair, exec_command, wallet_path = setup_wallet("//Alice") + alice_keypair, exec_command, wallet_path = setup_wallet("//Alice", True) exec_command(RegisterSubnetworkCommand, ["s", "create"]) # Verify subnet 1 created successfully diff --git a/tests/e2e_tests/multistep/test_dendrite.py b/tests/e2e_tests/multistep/test_dendrite.py index 6abde7464d..9d1cf3d32c 100644 --- a/tests/e2e_tests/multistep/test_dendrite.py +++ b/tests/e2e_tests/multistep/test_dendrite.py @@ -35,13 +35,13 @@ @pytest.mark.asyncio async def test_dendrite(local_chain): # Register root as Alice - the subnet owner - alice_keypair, exec_command, wallet_path = setup_wallet("//Alice") + alice_keypair, exec_command, wallet_path = setup_wallet("//Alice", True) exec_command(RegisterSubnetworkCommand, ["s", "create"]) # Verify subnet 1 created successfully assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize() - bob_keypair, exec_command, wallet_path = setup_wallet("//Bob") + bob_keypair, exec_command, wallet_path = setup_wallet("//Bob", True) # Register a neuron to the subnet exec_command( diff --git a/tests/e2e_tests/multistep/test_incentive.py b/tests/e2e_tests/multistep/test_incentive.py index ea5809dd7f..1f9a24000c 100644 --- a/tests/e2e_tests/multistep/test_incentive.py +++ b/tests/e2e_tests/multistep/test_incentive.py @@ -44,13 +44,13 @@ @pytest.mark.asyncio async def test_incentive(local_chain): # Register root as Alice - the subnet owner and validator - alice_keypair, alice_exec_command, alice_wallet_path = setup_wallet("//Alice") + alice_keypair, alice_exec_command, alice_wallet_path = setup_wallet("//Alice", True) alice_exec_command(RegisterSubnetworkCommand, ["s", "create"]) # Verify subnet 1 created successfully assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize() # Register Bob as miner - bob_keypair, bob_exec_command, bob_wallet_path = setup_wallet("//Bob") + bob_keypair, bob_exec_command, bob_wallet_path = setup_wallet("//Bob", True) # Register Alice as neuron to the subnet alice_exec_command( diff --git a/tests/e2e_tests/multistep/test_last_tx_block.py b/tests/e2e_tests/multistep/test_last_tx_block.py index b97d54f8fa..06f0e9bae3 100644 --- a/tests/e2e_tests/multistep/test_last_tx_block.py +++ b/tests/e2e_tests/multistep/test_last_tx_block.py @@ -9,7 +9,7 @@ # https://discord.com/channels/799672011265015819/1176889736636407808/1236057424134144152 def test_takes(local_chain): # Register root as Alice - keypair, exec_command, wallet_path = setup_wallet("//Alice") + keypair, exec_command, wallet_path = setup_wallet("//Alice", True) exec_command(RootRegisterCommand, ["root", "register"]) # Create subnet 1 and verify created successfully @@ -21,7 +21,7 @@ def test_takes(local_chain): assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize() # Register and nominate Bob - keypair, exec_command, wallet_path = setup_wallet("//Bob") + keypair, exec_command, wallet_path = setup_wallet("//Bob", True) assert ( local_chain.query( "SubtensorModule", "LastTxBlock", [keypair.ss58_address] diff --git a/tests/e2e_tests/subcommands/delegation/test_set_delegate_take.py b/tests/e2e_tests/subcommands/delegation/test_set_delegate_take.py index cc7b1b5744..1100aa23a6 100644 --- a/tests/e2e_tests/subcommands/delegation/test_set_delegate_take.py +++ b/tests/e2e_tests/subcommands/delegation/test_set_delegate_take.py @@ -8,7 +8,7 @@ def test_set_delegate_increase_take(local_chain): # Register root as Alice - keypair, exec_command, wallet_path = setup_wallet("//Alice") + keypair, exec_command, wallet_path = setup_wallet("//Alice", True) exec_command(RootRegisterCommand, ["root", "register"]) # Create subnet 1 and verify created successfully @@ -20,7 +20,7 @@ def test_set_delegate_increase_take(local_chain): assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize() # Register and nominate Bob - keypair, exec_command, wallet_path = setup_wallet("//Bob") + keypair, exec_command, wallet_path = setup_wallet("//Bob", True) assert ( local_chain.query( "SubtensorModule", "LastTxBlock", [keypair.ss58_address] diff --git a/tests/e2e_tests/subcommands/root/__init__.py b/tests/e2e_tests/subcommands/root/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/e2e_tests/subcommands/root/test_root_delegate_list.py b/tests/e2e_tests/subcommands/root/test_root_delegate_list.py new file mode 100644 index 0000000000..980f14ca7d --- /dev/null +++ b/tests/e2e_tests/subcommands/root/test_root_delegate_list.py @@ -0,0 +1,21 @@ +from bittensor.commands.delegates import ListDelegatesCommand +from bittensor.commands.root import RootRegisterCommand +from bittensor.commands.delegates import SetTakeCommand +from ...utils import new_wallet + + +# delegate seems hard code the network config +def test_root_delegate_list(local_chain, capsys): + (wallet, exec_command) = new_wallet("//Alice", "//Bob") + + # 1200 hardcoded block gap + exec_command( + ListDelegatesCommand, + ["root", "list_delegates"], + ) + + captured = capsys.readouterr() + lines = captured.out.splitlines() + + # the command print too many lines + assert len(lines) > 200 diff --git a/tests/e2e_tests/subcommands/root/test_root_get_set_weights.py b/tests/e2e_tests/subcommands/root/test_root_get_set_weights.py new file mode 100644 index 0000000000..9cbc4e0d79 --- /dev/null +++ b/tests/e2e_tests/subcommands/root/test_root_get_set_weights.py @@ -0,0 +1,38 @@ +from bittensor.commands.root import RootSetWeightsCommand, RootGetWeightsCommand +from bittensor.commands.network import RegisterSubnetworkCommand +from ...utils import new_wallet, sudo_call_set_network_limit + + +# we can't test it now since the root network weights can't be set +# Test case to set weights for root network and get the weights +def test_root_get_set_weights(local_chain, capsys): + (wallet, exec_command) = new_wallet("//Alice", "//Bob") + assert sudo_call_set_network_limit(local_chain, wallet) + assert not local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize() + + exec_command(RegisterSubnetworkCommand, ["s", "create"]) + exec_command(RegisterSubnetworkCommand, ["s", "create"]) + exec_command(RegisterSubnetworkCommand, ["s", "create"]) + + assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize() + assert local_chain.query("SubtensorModule", "NetworksAdded", [2]).serialize() + assert local_chain.query("SubtensorModule", "NetworksAdded", [4]).serialize() + + netuids = "1,2,4" + weights = "0.1,0.3,0.6" + + # this command need update, should set the netuid. subtensor not accept the weight set for root network + exec_command( + RootSetWeightsCommand, + ["root", "weights", "--netuids", netuids, "--weights", weights], + ) + + weights = local_chain.query("SubtensorModule", "Weights", [1, 0]) + + exec_command( + RootGetWeightsCommand, + ["root", "get_weights"], + ) + + captured = capsys.readouterr() + lines = captured.out.splitlines() diff --git a/tests/e2e_tests/subcommands/root/test_root_list.py b/tests/e2e_tests/subcommands/root/test_root_list.py new file mode 100644 index 0000000000..a186944e24 --- /dev/null +++ b/tests/e2e_tests/subcommands/root/test_root_list.py @@ -0,0 +1,30 @@ +from bittensor.commands.root import RootList +from ...utils import new_wallet +from bittensor.commands.network import RegisterSubnetworkCommand +import bittensor + + +# test case to list the root network +def test_root_list(local_chain, capsys): + (wallet, exec_command) = new_wallet("//Alice", "//Bob") + + exec_command(RootList, ["root", "list"]) + captured = capsys.readouterr() + lines = captured.out.split("\n") + + assert len(lines) >= 4 + bittensor.logging.info(lines) + + # assert "Root Network" in lines[0] + # assert "UID NAME ADDRESS STAKE" in lines[1] + + exec_command(RegisterSubnetworkCommand, ["s", "create"]) + + exec_command(RootList, ["root", "list"]) + captured = capsys.readouterr() + lines = captured.out.splitlines() + + assert len(lines) >= 4 + # assert "Root Network" in lines[0] + # assert "UID NAME ADDRESS STAKE" in lines[1] + # assert "1" in lines[2] diff --git a/tests/e2e_tests/subcommands/root/test_root_nominate.py b/tests/e2e_tests/subcommands/root/test_root_nominate.py new file mode 100644 index 0000000000..4fd2d07441 --- /dev/null +++ b/tests/e2e_tests/subcommands/root/test_root_nominate.py @@ -0,0 +1,47 @@ +from bittensor.commands.delegates import NominateCommand +from bittensor.commands.stake import StakeCommand +from bittensor.commands.delegates import SetTakeCommand +from bittensor.commands.network import RegisterSubnetworkCommand +from bittensor.commands.register import RegisterCommand + +from ...utils import ( + new_wallet, + call_add_proposal, + sudo_call_set_network_limit, + sudo_call_set_target_stakes_per_interval, +) + + +# delegate seems hard code the network config +def test_root_nominate(local_chain, capsys): + (wallet, exec_command) = new_wallet("//Alice", "//Bob") + + delegates = local_chain.query( + "SubtensorModule", + "Delegates", + [wallet.hotkey.ss58_address], + ) + + assert delegates == 11796 + + assert sudo_call_set_network_limit(local_chain, wallet) + assert sudo_call_set_target_stakes_per_interval(local_chain, wallet) + + exec_command(RegisterSubnetworkCommand, ["s", "create"]) + exec_command(RegisterCommand, ["s", "register", "--neduid", "1"]) + + exec_command( + NominateCommand, + [ + "root", + "nominate", + ], + ) + + delegates = local_chain.query( + "SubtensorModule", + "Delegates", + [wallet.hotkey.ss58_address], + ) + + assert delegates == 11796 diff --git a/tests/e2e_tests/subcommands/root/test_root_register_root_network.py b/tests/e2e_tests/subcommands/root/test_root_register_root_network.py new file mode 100644 index 0000000000..e6153b0665 --- /dev/null +++ b/tests/e2e_tests/subcommands/root/test_root_register_root_network.py @@ -0,0 +1,18 @@ +from bittensor.commands.root import RootRegisterCommand +from ...utils import new_wallet + + +# Example test using the local_chain fixture +def test_root_register_root_network(local_chain, capsys): + (wallet, exec_command) = new_wallet("//Alice", "//Bob") + + uid = local_chain.query("SubtensorModule", "Uids", [0, wallet.hotkey.ss58_address]) + assert uid == None + + exec_command( + RootRegisterCommand, + ["root", "register"], + ) + + uid = local_chain.query("SubtensorModule", "Uids", [0, wallet.hotkey.ss58_address]) + assert uid != None diff --git a/tests/e2e_tests/subcommands/root/test_root_senate_view.py b/tests/e2e_tests/subcommands/root/test_root_senate_view.py new file mode 100644 index 0000000000..e0b311d845 --- /dev/null +++ b/tests/e2e_tests/subcommands/root/test_root_senate_view.py @@ -0,0 +1,37 @@ +from bittensor.commands.senate import SenateCommand +from ...utils import new_wallet, sudo_call_add_senate_member +import bittensor + + +# Example test using the local_chain fixture +def test_root_senate_view(local_chain, capsys): + (wallet, exec_command) = new_wallet("//Alice", "//Bob") + + members = local_chain.query("SenateMembers", "Members").serialize() + assert len(members) >= 3 + + exec_command( + SenateCommand, + ["root", "senate"], + ) + + captured = capsys.readouterr() + lines = captured.out.splitlines() + + assert len(lines) >= 7 + + sudo_call_add_senate_member(local_chain, wallet) + + members = local_chain.query("SenateMembers", "Members").serialize() + bittensor.logging.info(members) + assert len(members) == 4 + + exec_command( + SenateCommand, + ["root", "senate"], + ) + + captured = capsys.readouterr() + lines = captured.out.splitlines() + + assert len(lines) >= 8 diff --git a/tests/e2e_tests/subcommands/root/test_root_senate_vote.py b/tests/e2e_tests/subcommands/root/test_root_senate_vote.py new file mode 100644 index 0000000000..9af609c93a --- /dev/null +++ b/tests/e2e_tests/subcommands/root/test_root_senate_vote.py @@ -0,0 +1,41 @@ +from bittensor.commands.senate import VoteCommand +from bittensor.commands.root import RootRegisterCommand + +from ...utils import ( + new_wallet, + call_add_proposal, +) + + +# test case to vote the proposal +def test_root_senate_vote(local_chain, capsys, monkeypatch): + (wallet, exec_command) = new_wallet("//Alice", "//Bob") + monkeypatch.setattr("rich.prompt.Confirm.ask", lambda self: True) + + exec_command( + RootRegisterCommand, + ["root", "register"], + ) + + members = local_chain.query("Triumvirate", "Members") + proposals = local_chain.query("Triumvirate", "Proposals").serialize() + + assert len(members) == 3 + assert len(proposals) == 0 + + call_add_proposal(local_chain, wallet) + + proposals = local_chain.query("Triumvirate", "Proposals").serialize() + + assert len(proposals) == 1 + proposal_hash = proposals[0] + + exec_command( + VoteCommand, + ["root", "senate_vote", "--proposal", proposal_hash], + ) + + voting = local_chain.query("Triumvirate", "Voting", [proposal_hash]).serialize() + + assert len(voting["ayes"]) == 1 + assert voting["ayes"][0] == wallet.hotkey.ss58_address diff --git a/tests/e2e_tests/subcommands/root/test_root_view_proposal.py b/tests/e2e_tests/subcommands/root/test_root_view_proposal.py new file mode 100644 index 0000000000..3c7a56b784 --- /dev/null +++ b/tests/e2e_tests/subcommands/root/test_root_view_proposal.py @@ -0,0 +1,34 @@ +from bittensor.commands.senate import ProposalsCommand + +from ...utils import ( + new_wallet, + call_add_proposal, +) +import bittensor + + +# test case to add and view the proposals +def test_root_view_proposal(local_chain, capsys): + (wallet, exec_command) = new_wallet("//Alice", "//Bob") + + proposals = local_chain.query("Triumvirate", "Proposals").serialize() + + assert len(proposals) == 0 + + call_add_proposal(local_chain, wallet) + + proposals = local_chain.query("Triumvirate", "Proposals").serialize() + + assert len(proposals) == 1 + + exec_command( + ProposalsCommand, + ["root", "proposals"], + ) + + captured = capsys.readouterr() + lines = captured.out.splitlines() + for line in lines: + bittensor.logging.info(line) + + assert len(lines) >= 6 diff --git a/tests/e2e_tests/subcommands/stake/__init__.py b/tests/e2e_tests/subcommands/stake/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/e2e_tests/subcommands/stake/test_show.py b/tests/e2e_tests/subcommands/stake/test_show.py new file mode 100644 index 0000000000..8ac76980da --- /dev/null +++ b/tests/e2e_tests/subcommands/stake/test_show.py @@ -0,0 +1,16 @@ +from bittensor.commands.stake import StakeShow +from ...utils import setup_wallet + + +# Example test using the local_chain fixture +def test_stake_show(local_chain, capsys): + (keypair, exec_command) = setup_wallet("//Alice") + + exec_command(StakeShow, ["stake", "show"]) + captured = capsys.readouterr() + lines = captured.out.split("\n") + + assert len(lines) >= 5 + # assert "Coldkey" in lines[0] + # assert "default" in lines[1] + # assert "default" in lines[2] diff --git a/tests/e2e_tests/subcommands/stake/test_stake_add_remove.py b/tests/e2e_tests/subcommands/stake/test_stake_add_remove.py new file mode 100644 index 0000000000..79ae740819 --- /dev/null +++ b/tests/e2e_tests/subcommands/stake/test_stake_add_remove.py @@ -0,0 +1,69 @@ +from bittensor.commands.stake import StakeCommand +from bittensor.commands.unstake import UnStakeCommand +from bittensor.commands.network import RegisterSubnetworkCommand +from bittensor.commands.register import RegisterCommand +from ...utils import ( + new_wallet, + sudo_call_set_network_limit, + sudo_call_set_target_stakes_per_interval, +) + + +# Example test using the local_chain fixture +def test_stake_add(local_chain): + (wallet, exec_command) = new_wallet("//Alice", "//Bob") + assert sudo_call_set_network_limit(local_chain, wallet) + assert sudo_call_set_target_stakes_per_interval(local_chain, wallet) + + assert not (local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize()) + + exec_command(RegisterSubnetworkCommand, ["s", "create"]) + assert local_chain.query("SubtensorModule", "NetworksAdded", [1]) + + assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize() + + assert ( + local_chain.query( + "SubtensorModule", "LastTxBlock", [wallet.hotkey.ss58_address] + ).serialize() + == 0 + ) + + assert ( + local_chain.query( + "SubtensorModule", "LastTxBlockDelegateTake", [wallet.hotkey.ss58_address] + ).serialize() + == 0 + ) + + exec_command(RegisterCommand, ["s", "register", "--neduid", "1"]) + + assert ( + local_chain.query( + "SubtensorModule", "TotalHotkeyStake", [wallet.hotkey.ss58_address] + ).serialize() + == 0 + ) + + stake_amount = 2 + exec_command(StakeCommand, ["stake", "add", "--amount", str(stake_amount)]) + exact_stake = local_chain.query( + "SubtensorModule", "TotalHotkeyStake", [wallet.hotkey.ss58_address] + ).serialize() + withdraw_loss = 1_000_000 + stake_amount_in_rao = stake_amount * 1_000_000_000 + + assert ( + exact_stake > stake_amount_in_rao - withdraw_loss + and exact_stake <= stake_amount_in_rao + ) + + # we can test remove after set the stake rate limit larger than 1 + remove_amount = 1 + exec_command(UnStakeCommand, ["stake", "remove", "--amount", str(remove_amount)]) + assert ( + local_chain.query( + "SubtensorModule", "TotalHotkeyStake", [wallet.hotkey.ss58_address] + ).serialize() + == exact_stake - remove_amount * 1_000_000_000 + ) diff --git a/tests/e2e_tests/subcommands/stake/test_stake_show.py b/tests/e2e_tests/subcommands/stake/test_stake_show.py new file mode 100644 index 0000000000..4f0ba904ad --- /dev/null +++ b/tests/e2e_tests/subcommands/stake/test_stake_show.py @@ -0,0 +1,16 @@ +from bittensor.commands.stake import StakeShow +from ...utils import setup_wallet + + +# Example test using the local_chain fixture +def test_stake_show(local_chain, capsys): + (keypair, exec_command) = setup_wallet("//Alice") + + exec_command(StakeShow, ["stake", "show"]) + captured = capsys.readouterr() + lines = captured.out.splitlines() + + assert len(lines) >= 5 + # assert "Coldkey" in lines[0] + # assert "default" in lines[1] + # assert "default" in lines[2] diff --git a/tests/e2e_tests/subcommands/wallet/test_faucet.py b/tests/e2e_tests/subcommands/wallet/test_faucet.py index 0e647387b6..3aebebd72c 100644 --- a/tests/e2e_tests/subcommands/wallet/test_faucet.py +++ b/tests/e2e_tests/subcommands/wallet/test_faucet.py @@ -15,7 +15,7 @@ @pytest.mark.parametrize("local_chain", [False], indirect=True) def test_faucet(local_chain): # Register root as Alice - keypair, exec_command, wallet_path = setup_wallet("//Alice") + keypair, exec_command, wallet_path = setup_wallet("//Alice", True) exec_command(RegisterSubnetworkCommand, ["s", "create"]) # Verify subnet 1 created successfully diff --git a/tests/e2e_tests/subcommands/wallet/test_transfer.py b/tests/e2e_tests/subcommands/wallet/test_transfer.py index 5b491b3f0d..e19c4ad418 100644 --- a/tests/e2e_tests/subcommands/wallet/test_transfer.py +++ b/tests/e2e_tests/subcommands/wallet/test_transfer.py @@ -5,7 +5,7 @@ # Example test using the local_chain fixture def test_transfer(local_chain): - keypair, exec_command, wallet_path = setup_wallet("//Alice") + keypair, exec_command, wallet_path = setup_wallet("//Alice", True) acc_before = local_chain.query("System", "Account", [keypair.ss58_address]) exec_command( diff --git a/tests/e2e_tests/subcommands/wallet/test_wallet.py b/tests/e2e_tests/subcommands/wallet/test_wallet.py new file mode 100644 index 0000000000..3fff665d86 --- /dev/null +++ b/tests/e2e_tests/subcommands/wallet/test_wallet.py @@ -0,0 +1,24 @@ +from bittensor.commands.list import ListCommand +from bittensor.subtensor import subtensor + +from ...utils import setup_wallet + + +def test_wallet_list(local_chain: subtensor, capsys): + (keypair, exec_command) = setup_wallet("//Alice") + + exec_command( + ListCommand, + [ + "wallet", + "list", + ], + ) + + captured = capsys.readouterr() + lines = captured.out.splitlines() + # can't check the output now since there is a info about bittensor version + assert len(lines) >= 4 + # assert "└──" in lines[1] + # assert "default (5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY)" in lines[2] + # assert "└── default (5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY)" in lines[3] diff --git a/tests/e2e_tests/subcommands/weights/test_commit_weights.py b/tests/e2e_tests/subcommands/weights/test_commit_weights.py index 90738d041f..d11f3454f9 100644 --- a/tests/e2e_tests/subcommands/weights/test_commit_weights.py +++ b/tests/e2e_tests/subcommands/weights/test_commit_weights.py @@ -29,7 +29,7 @@ def test_commit_and_reveal_weights(local_chain): # Register root as Alice - keypair, exec_command, wallet_path = setup_wallet("//Alice") + keypair, exec_command, wallet_path = setup_wallet("//Alice", True) exec_command(RegisterSubnetworkCommand, ["s", "create"]) # define values diff --git a/tests/e2e_tests/utils.py b/tests/e2e_tests/utils.py index 4b485e3bd9..13e2928d2d 100644 --- a/tests/e2e_tests/utils.py +++ b/tests/e2e_tests/utils.py @@ -1,19 +1,18 @@ +from substrateinterface import Keypair, SubstrateInterface +from typing import List import os import shutil import subprocess import sys -from typing import List - from bittensor import Keypair - import bittensor template_path = os.getcwd() + "/neurons/" repo_name = "templates repository" -def setup_wallet(uri: str): +def setup_wallet(uri: str, with_path: bool = False): keypair = Keypair.create_from_uri(uri) wallet_path = "/tmp/btcli-e2e-wallet-{}".format(uri.strip("/")) wallet = bittensor.wallet(path=wallet_path) @@ -39,6 +38,171 @@ def exec_command(command, extra_args: List[str]): cli_instance = bittensor.cli(config) command.run(cli_instance) + if with_path: + return (keypair, exec_command, wallet_path) + else: + return (keypair, exec_command) + + +def new_wallet(uri: str, uri2: str): + keypair_1 = Keypair.create_from_uri(uri) + keypair_2 = Keypair.create_from_uri(uri2) + wallet_path = "/tmp/btcli-e2e-wallet-{}-{}".format(uri.strip("/"), uri2.strip("/")) + wallet = bittensor.wallet(path=wallet_path) + wallet.set_coldkey(keypair=keypair_1, encrypt=False, overwrite=True) + wallet.set_coldkeypub(keypair=keypair_1, encrypt=False, overwrite=True) + wallet.set_hotkey(keypair=keypair_2, encrypt=False, overwrite=True) + + def exec_command(command, extra_args: List[str]): + parser = bittensor.cli.__create_parser__() + args = extra_args + [ + "--no_prompt", + "--subtensor.network", + "local", + "--subtensor.chain_endpoint", + "ws://localhost:9945", + "--wallet.path", + wallet_path, + ] + config = bittensor.config( + parser=parser, + args=args, + ) + cli_instance = bittensor.cli(config) + command.run(cli_instance) + + return (wallet, exec_command) + + +def sudo_call_set_network_limit( + substrate: SubstrateInterface, wallet: bittensor.wallet +) -> bool: + inner_call = substrate.compose_call( + call_module="AdminUtils", + call_function="sudo_set_network_rate_limit", + call_params={"rate_limit": 1}, + ) + call = substrate.compose_call( + call_module="Sudo", + call_function="sudo", + call_params={"call": inner_call}, + ) + + extrinsic = substrate.create_signed_extrinsic(call=call, keypair=wallet.coldkey) + response = substrate.submit_extrinsic( + extrinsic, + wait_for_inclusion=True, + wait_for_finalization=True, + ) + + response.process_events() + return response.is_success + + +def sudo_call_set_target_stakes_per_interval( + substrate: SubstrateInterface, wallet: bittensor.wallet +) -> bool: + inner_call = substrate.compose_call( + call_module="AdminUtils", + call_function="sudo_set_target_stakes_per_interval", + call_params={"target_stakes_per_interval": 100}, + ) + call = substrate.compose_call( + call_module="Sudo", + call_function="sudo", + call_params={"call": inner_call}, + ) + + extrinsic = substrate.create_signed_extrinsic(call=call, keypair=wallet.coldkey) + response = substrate.submit_extrinsic( + extrinsic, + wait_for_inclusion=True, + wait_for_finalization=True, + ) + + response.process_events() + return response.is_success + + +def sudo_call_add_senate_member( + substrate: SubstrateInterface, wallet: bittensor.wallet +) -> bool: + inner_call = substrate.compose_call( + call_module="SenateMembers", + call_function="add_member", + call_params={"who": wallet.hotkey.ss58_address}, + ) + call = substrate.compose_call( + call_module="Sudo", + call_function="sudo", + call_params={"call": inner_call}, + ) + + extrinsic = substrate.create_signed_extrinsic(call=call, keypair=wallet.coldkey) + response = substrate.submit_extrinsic( + extrinsic, + wait_for_inclusion=True, + wait_for_finalization=True, + ) + + response.process_events() + return response.is_success + + +def call_add_proposal(substrate: SubstrateInterface, wallet: bittensor.wallet) -> bool: + proposal_call = substrate.compose_call( + call_module="System", + call_function="remark", + call_params={"remark": [0]}, + ) + call = substrate.compose_call( + call_module="Triumvirate", + call_function="propose", + call_params={ + "proposal": proposal_call, + "length_bound": 100_000, + "duration": 100_000_000, + }, + ) + + extrinsic = substrate.create_signed_extrinsic(call=call, keypair=wallet.coldkey) + response = substrate.submit_extrinsic( + extrinsic, + wait_for_inclusion=True, + wait_for_finalization=True, + ) + + response.process_events() + return response.is_success + + +def sudo_call_set_triumvirate_members( + substrate: SubstrateInterface, wallet: bittensor.wallet +) -> bool: + inner_call = substrate.compose_call( + call_module="Triumvirate", + call_function="set_members", + call_params={ + "new_members": [wallet.hotkey.ss58_address], + "prime": wallet.coldkey.ss58_address, + "old_count": 0, + }, + ) + call = substrate.compose_call( + call_module="Sudo", + call_function="sudo", + call_params={"call": inner_call}, + ) + + extrinsic = substrate.create_signed_extrinsic(call=call, keypair=wallet.coldkey) + response = substrate.submit_extrinsic( + extrinsic, + wait_for_inclusion=True, + wait_for_finalization=True, + ) + + response.process_events() + return response.is_success return keypair, exec_command, wallet_path