Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
ibraheem-opentensor committed Jul 30, 2024
1 parent 20bb968 commit e218e4c
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 60 deletions.
36 changes: 20 additions & 16 deletions tests/e2e_tests/subcommands/register/test_swap_hotkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@

@pytest.mark.asyncio
async def test_swap_hotkey_validator_owner(local_chain):
logging.info("Testing swap hotkey of validator_owner")
# Register root as Alice - the subnet owner and validator
alice_keypair, alice_exec_command, alice_wallet = setup_wallet("//Alice")
alice_exec_command(RegisterSubnetworkCommand, ["s", "create"])
# Verify subnet 1 created successfully
assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize()
logging.info("Subnet is registered successfully")

# Register Bob as miner
bob_keypair, bob_exec_command, bob_wallet = setup_wallet("//Bob")
Expand Down Expand Up @@ -71,9 +73,11 @@ async def test_swap_hotkey_validator_owner(local_chain):
],
)

logging.info("Alice and bob registered to the subnet")

subtensor = bittensor.subtensor(network="ws://localhost:9945")
# assert two neurons are in network
assert len(subtensor.neurons(netuid=1)) == 2
assert len(subtensor.neurons(netuid=1)) == 2, "Alice and Bob neurons not found in the network"

# register Bob as miner
cmd = " ".join(
Expand Down Expand Up @@ -102,7 +106,7 @@ async def test_swap_hotkey_validator_owner(local_chain):
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)

logging.info("Bob neuron is now mining")
await asyncio.sleep(
5
) # wait for 5 seconds for the metagraph to refresh with latest data
Expand Down Expand Up @@ -135,7 +139,7 @@ async def test_swap_hotkey_validator_owner(local_chain):
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE,
)

logging.info("Alice neuron is now validating")
await asyncio.sleep(
5
) # wait for 5 seconds for the metagraph and subtensor to refresh with latest data
Expand Down Expand Up @@ -179,28 +183,28 @@ async def test_swap_hotkey_validator_owner(local_chain):
wallet_tree = alice_exec_command(ListCommand, ["w", "list"], "get_tree")
num_hotkeys = len(wallet_tree.children[0].children)

assert alice_neuron.coldkey == "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"
assert alice_neuron.hotkey == alice_old_hotkey_address
assert alice_neuron.coldkey == "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY", "Alice coldkey not as expected"
assert alice_neuron.hotkey == alice_old_hotkey_address, "Alice hotkey not as expected"
assert (
alice_neuron.stake_dict["5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"].tao
== 10000.0
)
assert alice_neuron.hotkey == alice_neuron.coldkey
assert alice_neuron.hotkey == subtensor.get_all_subnets_info()[1].owner_ss58
assert alice_neuron.coldkey == subtensor.get_hotkey_owner(alice_old_hotkey_address)
assert subtensor.is_hotkey_delegate(alice_neuron.hotkey) is True
), "Alice tao not as expected"
assert alice_neuron.hotkey == alice_neuron.coldkey, "Coldkey and hotkey don't match"
assert alice_neuron.hotkey == subtensor.get_all_subnets_info()[1].owner_ss58, "Hotkey doesn't match owner address"
assert alice_neuron.coldkey == subtensor.get_hotkey_owner(alice_old_hotkey_address), "Coldkey doesn't match hotkey owner"
assert subtensor.is_hotkey_delegate(alice_neuron.hotkey) is True, "Alice is not a delegate"
assert (
subtensor.is_hotkey_registered_on_subnet(
hotkey_ss58=alice_neuron.hotkey, netuid=1
)
is True
)
), "Alice hotkey not registered on subnet"
assert (
subtensor.get_uid_for_hotkey_on_subnet(
hotkey_ss58=alice_neuron.hotkey, netuid=1
)
== alice_neuron.uid
)
), "Alice hotkey not regisred on netuid"
if num_hotkeys > 1:
logging.info(f"You have {num_hotkeys} hotkeys for Alice.")

Expand All @@ -223,7 +227,7 @@ async def test_swap_hotkey_validator_owner(local_chain):
"True",
],
)

logging.info("New hotkey is created")
# wait rate limit, until we are allowed to change hotkeys
rate_limit = subtensor.tx_rate_limit()
curr_block = subtensor.get_current_block()
Expand Down Expand Up @@ -259,9 +263,9 @@ async def test_swap_hotkey_validator_owner(local_chain):

assert (
alice_neuron.coldkey == "5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY"
) # cold key didnt change
assert alice_neuron.hotkey != alice_old_hotkey_address
assert alice_neuron.hotkey != alice_neuron.coldkey
), "Coldkey was changed" # cold key didnt change
assert alice_neuron.hotkey != alice_old_hotkey_address, "Hotkey is not updated w.r.t old_hotkey_address"
assert alice_neuron.hotkey != alice_neuron.coldkey, "Hotkey is not updated w.r.t coldkey"
assert (
alice_neuron.coldkey == subtensor.get_all_subnets_info()[1].owner_ss58
) # new hotkey address is subnet owner
Expand Down
22 changes: 15 additions & 7 deletions tests/e2e_tests/subcommands/root/test_root_senate_vote.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from bittensor.commands.senate import VoteCommand
from bittensor import logging
from bittensor.commands.root import RootRegisterCommand
from bittensor.commands.senate import VoteCommand

from ...utils import (
setup_wallet,
call_add_proposal,
setup_wallet,
)


def test_root_senate_vote(local_chain, capsys, monkeypatch):
logging.info("Testing test_root_senate_vote")
keypair, exec_command, wallet = setup_wallet("//Alice")
monkeypatch.setattr("rich.prompt.Confirm.ask", lambda self: True)

Expand All @@ -19,14 +21,18 @@ def test_root_senate_vote(local_chain, capsys, monkeypatch):
members = local_chain.query("Triumvirate", "Members")
proposals = local_chain.query("Triumvirate", "Proposals").serialize()

assert len(members) == 3
assert len(proposals) == 0
assert len(members) == 3, f"Expected 3 Triumvirate members, found {len(members)}"
assert (
len(proposals) == 0
), f"Expected 0 initial Triumvirate proposals, found {len(proposals)}"

call_add_proposal(local_chain, wallet)

proposals = local_chain.query("Triumvirate", "Proposals").serialize()

assert len(proposals) == 1
assert (
len(proposals) == 1
), f"Expected 1 proposal in the Triumvirate after addition, found {len(proposals)}"
proposal_hash = proposals[0]

exec_command(
Expand All @@ -36,5 +42,7 @@ def test_root_senate_vote(local_chain, capsys, monkeypatch):

voting = local_chain.query("Triumvirate", "Voting", [proposal_hash]).serialize()

assert len(voting["ayes"]) == 1
assert voting["ayes"][0] == wallet.hotkey.ss58_address
assert len(voting["ayes"]) == 2, f"Expected 1 ayes, found {len(voting['ayes'])}"
assert (
voting["ayes"][0] == wallet.hotkey.ss58_address
), "wallet hotkey address doesn't match 'ayes' address"
11 changes: 7 additions & 4 deletions tests/e2e_tests/subcommands/root/test_root_view_proposal.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import bittensor
from bittensor import logging
from bittensor.commands.senate import ProposalsCommand

from ...utils import (
setup_wallet,
call_add_proposal,
setup_wallet,
)
import bittensor


def test_root_view_proposal(local_chain, capsys):
logging.info("Testing test_root_view_proposal")
keypair, exec_command, wallet = setup_wallet("//Alice")

proposals = local_chain.query("Triumvirate", "Proposals").serialize()

assert len(proposals) == 0
assert len(proposals) == 0, "Proposals are not 0"

call_add_proposal(local_chain, wallet)

proposals = local_chain.query("Triumvirate", "Proposals").serialize()

assert len(proposals) == 1
assert len(proposals) == 1, "Added proposal not found"

exec_command(
ProposalsCommand,
Expand Down Expand Up @@ -62,3 +64,4 @@ def test_root_view_proposal(local_chain, capsys):
), f"Expected line starting with '0x78b8a348690f565efe3730cd8189f7388c0a896b6fd090276639c9130c0eba47', got {lines[3].strip()}"
assert lines[4].strip() == "\x00)", f"Expected '\x00)', got {lines[4].strip()}"
assert lines[5].strip() == "", f"Expected empty line, got {lines[5].strip()}"
logging.info("Finished test_root_view_proposal")
48 changes: 32 additions & 16 deletions tests/e2e_tests/subcommands/stake/test_stake_add_remove.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from bittensor.commands.stake import StakeCommand
from bittensor.commands.unstake import UnStakeCommand
from bittensor import logging
from bittensor.commands.network import RegisterSubnetworkCommand
from bittensor.commands.register import RegisterCommand
from bittensor.commands.stake import StakeCommand
from bittensor.commands.unstake import UnStakeCommand

from ...utils import (
setup_wallet,
sudo_call_set_network_limit,
Expand All @@ -10,30 +12,38 @@


def test_stake_add(local_chain):
logging.info("Testing test_stake_add")
alice_keypair, exec_command, wallet = setup_wallet("//Alice")
assert sudo_call_set_network_limit(local_chain, wallet)
assert sudo_call_set_target_stakes_per_interval(local_chain, wallet)
assert sudo_call_set_network_limit(
local_chain, wallet
), "Unable to set network limit"
assert sudo_call_set_target_stakes_per_interval(
local_chain, wallet
), "Unable to set target stakes per interval"

assert not (local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize())
assert not (
local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize()
), "Subnet was found in netuid 1"

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", "NetworksAdded", [1]
).serialize(), "Subnet 1 was successfully added"

assert (
local_chain.query(
"SubtensorModule", "LastTxBlock", [wallet.hotkey.ss58_address]
).serialize()
== 0
)
), "LastTxBlock is not 0"

assert (
local_chain.query(
"SubtensorModule", "LastTxBlockDelegateTake", [wallet.hotkey.ss58_address]
).serialize()
== 0
)
), "LastTxBlockDelegateTake is not 0"

exec_command(RegisterCommand, ["s", "register", "--neduid", "1"])

Expand All @@ -42,7 +52,7 @@ def test_stake_add(local_chain):
"SubtensorModule", "TotalHotkeyStake", [wallet.hotkey.ss58_address]
).serialize()
== 0
)
), "TotalHotkeyStake is not 0"

stake_amount = 2
exec_command(StakeCommand, ["stake", "add", "--amount", str(stake_amount)])
Expand All @@ -52,14 +62,20 @@ def test_stake_add(local_chain):
withdraw_loss = 1_000_000
stake_amount_in_rao = stake_amount * 1_000_000_000

assert stake_amount_in_rao - withdraw_loss < exact_stake <= stake_amount_in_rao
assert (
stake_amount_in_rao - withdraw_loss < exact_stake <= stake_amount_in_rao
), f"Stake amount mismatch: expected {exact_stake} to be between {stake_amount_in_rao - withdraw_loss} and {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)])
total_hotkey_stake = local_chain.query(
"SubtensorModule", "TotalHotkeyStake", [wallet.hotkey.ss58_address]
).serialize()
expected_stake = exact_stake - remove_amount * 1_000_000_000
assert (
local_chain.query(
"SubtensorModule", "TotalHotkeyStake", [wallet.hotkey.ss58_address]
).serialize()
== exact_stake - remove_amount * 1_000_000_000
)
total_hotkey_stake == expected_stake
), f"Unstake amount mismatch: expected {expected_stake}, but got {total_hotkey_stake}"

logging.info("Passed test_stake_add")
4 changes: 4 additions & 0 deletions tests/e2e_tests/subcommands/stake/test_stake_show.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from bittensor import logging
from bittensor.commands.stake import StakeShow

from ...utils import setup_wallet


def test_stake_show(local_chain, capsys):
logging.info("Testing test_stake_show")
keypair, exec_command, wallet = setup_wallet("//Alice")

# Execute the command
Expand Down Expand Up @@ -47,3 +50,4 @@ def test_stake_show(local_chain, capsys):
assert (
values3[2].replace("τ", "") == "0.00000/d"
), f"Expected '0.00000/d', got {values3[2]}."
logging.info("Passed test_stake_show")
32 changes: 23 additions & 9 deletions tests/e2e_tests/subcommands/subnet/test_metagraph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import bittensor
from bittensor import logging
from bittensor.commands import (
MetagraphCommand,
RegisterCommand,
Expand All @@ -19,19 +20,22 @@


def test_metagraph_command(local_chain, capsys):
logging.info("Testing test_metagraph_command")
# Register root as Alice
keypair, exec_command, wallet = setup_wallet("//Alice")
exec_command(RegisterSubnetworkCommand, ["s", "create"])

# Verify subnet 1 created successfully
assert local_chain.query("SubtensorModule", "NetworksAdded", [1]).serialize()
assert local_chain.query(
"SubtensorModule", "NetworksAdded", [1]
).serialize(), "Subnet wasn't created successfully"

subtensor = bittensor.subtensor(network="ws://localhost:9945")

metagraph = subtensor.metagraph(netuid=1)

# Assert metagraph is empty
assert len(metagraph.uids) == 0
assert len(metagraph.uids) == 0, "Metagraph is not empty"

# Execute btcli metagraph command
exec_command(MetagraphCommand, ["subnet", "metagraph", "--netuid", "1"])
Expand All @@ -40,7 +44,9 @@ def test_metagraph_command(local_chain, capsys):
lines = captured.out.splitlines()

# Assert metagraph is printed for netuid 1
assert "Metagraph: net: local:1" in lines[2]
assert (
"Metagraph: net: local:1" in lines[2]
), "Netuid 1 was not displayed in metagraph"

# Register Bob as neuron to the subnet
bob_keypair, bob_exec_command, bob_wallet = setup_wallet("//Bob")
Expand All @@ -58,21 +64,25 @@ def test_metagraph_command(local_chain, capsys):
lines = captured.out.splitlines()

# Assert neuron was registered
assert "✅ Registered" in lines[3]
assert "✅ Registered" in lines[3], "Neuron was not registered"

# Refresh the metagraph
metagraph = subtensor.metagraph(netuid=1)

# Assert metagraph has registered neuron
assert len(metagraph.uids) == 1
assert metagraph.hotkeys[0] == "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
assert len(metagraph.uids) == 1, "Metagraph doesn't have exactly 1 neuron"
assert (
metagraph.hotkeys[0] == "5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty"
), "Neuron's hotkey in metagraph doesn't match"
# Execute btcli metagraph command
exec_command(MetagraphCommand, ["subnet", "metagraph", "--netuid", "1"])

captured = capsys.readouterr()

# Assert the neuron is registered and displayed
assert "Metagraph: net: local:1" and "N: 1/1" in captured.out
assert (
"Metagraph: net: local:1" and "N: 1/1" in captured.out
), "Neuron isn't displayed in metagraph"

# Register Dave as neuron to the subnet
dave_keypair, dave_exec_command, dave_wallet = setup_wallet("//Dave")
Expand All @@ -90,14 +100,16 @@ def test_metagraph_command(local_chain, capsys):
lines = captured.out.splitlines()

# Assert neuron was registered
assert "✅ Registered" in lines[3]
assert "✅ Registered" in lines[3], "Neuron was not registered"

# Refresh the metagraph
metagraph = subtensor.metagraph(netuid=1)

# Assert metagraph has registered neuron
assert len(metagraph.uids) == 2
assert metagraph.hotkeys[1] == "5DAAnrj7VHTznn2AWBemMuyBwZWs6FNFjdyVXUeYum3PTXFy"
assert (
metagraph.hotkeys[1] == "5DAAnrj7VHTznn2AWBemMuyBwZWs6FNFjdyVXUeYum3PTXFy"
), "Neuron's hotkey in metagraph doesn't match"

# Execute btcli metagraph command
exec_command(MetagraphCommand, ["subnet", "metagraph", "--netuid", "1"])
Expand All @@ -106,3 +118,5 @@ def test_metagraph_command(local_chain, capsys):

# Assert the neuron is registered and displayed
assert "Metagraph: net: local:1" and "N: 2/2" in captured.out

logging.info("Passed test_metagraph_command")
Loading

0 comments on commit e218e4c

Please sign in to comment.