Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests/senate e2e #96

Merged
merged 6 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bittensor_cli/src/commands/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,7 @@ async def senate_vote(
subtensor=subtensor,
wallet=wallet,
proposal_hash=proposal_hash,
proposal_idx=vote_data["index"],
proposal_idx=vote_data.index,
vote=vote,
wait_for_inclusion=True,
wait_for_finalization=False,
Expand Down
112 changes: 112 additions & 0 deletions tests/e2e_tests/test_senate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
"""
Verify commands:

* btcli root senate
* btcli root proposals
* btcli root senate-vote
* btcli root nominate
* btcli root register
"""

import asyncio
from .utils import call_add_proposal


def test_senate(local_chain, wallet_setup):
"""
Test the senate functionality in Bittensor
Steps:
1. Create a wallet for Bob
2. Assert bob is not part of the senate
3. Execute root register for Bob
4. Assert Bob is now part of the senate
5. Manually add a proposal to the chain

Raises:
AssertionError: If any of the checks or verifications fail
"""
print("Testing Senate commands 🧪")

wallet_path_bob = "//Bob"

# Create wallet for Bob
keypair_bob, wallet_bob, wallet_path_bob, exec_command_bob = wallet_setup(
wallet_path_bob
)

# Fetch existing senate list
root_senate = exec_command_bob(
command="root",
sub_command="senate",
extra_args=[
"--chain",
"ws://127.0.0.1:9945",
]
)

# Assert Bob is not part of the senate yet
assert wallet_bob.hotkey.ss58_address not in root_senate.stdout

# Register Bob to the root network (0)
# Registering to root automatically makes you a senator if eligible
root_register = exec_command_bob(
command="root",
sub_command="register",
extra_args=[
"--wallet-path",
wallet_path_bob,
"--chain",
"ws://127.0.0.1:9945",
"--wallet-name",
wallet_bob.name,
"--hotkey",
wallet_bob.hotkey_str,
"--network",
"local",
"--no-prompt",
],
)
assert "✅ Registered" in root_register.stdout

# Fetch the senate members after registering to root
root_senate_after_reg = exec_command_bob(
command="root",
sub_command="senate",
extra_args=[
"--chain",
"ws://127.0.0.1:9945",
]
)

# Assert Bob is now part of the senate
assert wallet_bob.hotkey.ss58_address in root_senate_after_reg.stdout

success = asyncio.run(call_add_proposal(local_chain, wallet_bob))
assert success is True

# Fetch proposals
proposals = exec_command_bob(
command="root",
sub_command="proposals",
extra_args=[
"--chain",
"ws://127.0.0.1:9945",
]
)

proposals_output = proposals.stdout.splitlines()[8].split()

# Assert the hash is of correct length
assert len(proposals_output[0]) == 68

# 0 Ayes for the proposal
assert proposals_output[2] == '0'

# 0 Nayes for the proposal
assert proposals_output[3] == '0'

# Assert initial threshold is 3
assert proposals_output[1] == '3'



35 changes: 33 additions & 2 deletions tests/e2e_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import shutil
import subprocess
import sys
from typing import List, Tuple
from typing import List, Tuple, TYPE_CHECKING

from bittensor_cli.cli import CLIManager
from bittensor_wallet import Wallet
from substrateinterface import Keypair
from typer.testing import CliRunner
from bittensor_wallet import Wallet

if TYPE_CHECKING:
from bittensor_cli.src.bittensor.async_substrate_interface import AsyncSubstrateInterface

template_path = os.getcwd() + "/neurons/"
templates_repo = "templates repository"
Expand Down Expand Up @@ -282,3 +285,31 @@ def uninstall_templates(install_dir):
)
# Delete everything in directory
shutil.rmtree(install_dir)


async def call_add_proposal(substrate: "AsyncSubstrateInterface", wallet: Wallet) -> bool:
async with substrate:
proposal_call = await substrate.compose_call(
call_module="System",
call_function="remark",
call_params={"remark": [0]},
)
call = await substrate.compose_call(
call_module="Triumvirate",
call_function="propose",
call_params={
"proposal": proposal_call,
"length_bound": 100_000,
"duration": 100_000_000,
},
)

extrinsic = await substrate.create_signed_extrinsic(call=call, keypair=wallet.coldkey)
response = await substrate.submit_extrinsic(
extrinsic,
wait_for_inclusion=True,
wait_for_finalization=True,
)

await response.process_events()
return await response.is_success
Loading