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

More E2E tests #2754

Merged
merged 5 commits into from
Mar 18, 2025
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
5 changes: 3 additions & 2 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2020,10 +2020,11 @@ async def get_vote_data(
block_hash=block_hash,
reuse_block_hash=reuse_block,
)

if vote_data is None:
return None
else:
return ProposalVoteData(vote_data)

return ProposalVoteData.from_dict(vote_data)

async def get_uid_for_hotkey_on_subnet(
self,
Expand Down
31 changes: 18 additions & 13 deletions bittensor/core/chain_data/proposal_vote_data.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
from dataclasses import dataclass

from bittensor.core.chain_data.info_base import InfoBase
from bittensor.core.chain_data.utils import decode_account_id


# Senate / Proposal data
class ProposalVoteData:
@dataclass
class ProposalVoteData(InfoBase):
"""
Senate / Proposal data
"""

index: int
threshold: int
ayes: list[str]
nays: list[str]
end: int

def __init__(self, proposal_dict: dict) -> None:
self.index = proposal_dict["index"]
self.threshold = proposal_dict["threshold"]
self.ayes = self.decode_ss58_tuples(proposal_dict["ayes"])
self.nays = self.decode_ss58_tuples(proposal_dict["nays"])
self.end = proposal_dict["end"]

@staticmethod
def decode_ss58_tuples(line: tuple):
"""Decodes a tuple of ss58 addresses formatted as bytes tuples."""
return [decode_account_id(line[x][0]) for x in range(len(line))]
@classmethod
def from_dict(cls, proposal_dict: dict) -> "ProposalVoteData":
return cls(
ayes=[decode_account_id(key) for key in proposal_dict["ayes"]],
end=proposal_dict["end"],
index=proposal_dict["index"],
nays=[decode_account_id(key) for key in proposal_dict["nays"]],
threshold=proposal_dict["threshold"],
)
5 changes: 3 additions & 2 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1579,10 +1579,11 @@ def get_vote_data(
params=[proposal_hash],
block_hash=self.determine_block_hash(block),
)

if vote_data is None:
return None
else:
return ProposalVoteData(vote_data)

return ProposalVoteData.from_dict(vote_data)

def get_uid_for_hotkey_on_subnet(
self, hotkey_ss58: str, netuid: int, block: Optional[int] = None
Expand Down
106 changes: 106 additions & 0 deletions tests/e2e_tests/test_delegate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
import bittensor
from bittensor.core.chain_data.chain_identity import ChainIdentity
from bittensor.core.chain_data.delegate_info import DelegatedInfo, DelegateInfo
from bittensor.core.chain_data.proposal_vote_data import ProposalVoteData
from bittensor.utils.balance import Balance
from tests.e2e_tests.utils.chain_interactions import (
propose,
set_identity,
sudo_set_admin_utils,
vote,
)
from tests.helpers.helpers import CLOSE_IN_VALUE

DEFAULT_DELEGATE_TAKE = 0.179995422293431

Expand Down Expand Up @@ -321,3 +325,105 @@ def test_nominator_min_required_stake(local_chain, subtensor, alice_wallet, bob_
)

assert stake == Balance(0)


def test_get_vote_data(subtensor, alice_wallet):
"""
Tests:
- Sends Propose
- Checks existing Proposals
- Votes
- Checks Proposal is updated
"""

subtensor.root_register(alice_wallet)

proposals = subtensor.query_map(
"Triumvirate",
"ProposalOf",
params=[],
)

assert proposals.records == []

success, error = propose(
subtensor,
alice_wallet,
proposal=subtensor.substrate.compose_call(
call_module="Triumvirate",
call_function="set_members",
call_params={
"new_members": [],
"prime": None,
"old_count": 0,
},
),
duration=1_000_000,
)

assert error == ""
assert success is True

proposals = subtensor.query_map(
"Triumvirate",
"ProposalOf",
params=[],
)
proposals = {
bytes(proposal_hash[0]): proposal.value for proposal_hash, proposal in proposals
}

assert list(proposals.values()) == [
{
"Triumvirate": (
{
"set_members": {
"new_members": (),
"prime": None,
"old_count": 0,
},
},
),
},
]

proposal_hash = list(proposals.keys())[0]
proposal_hash = f"0x{proposal_hash.hex()}"

proposal = subtensor.get_vote_data(
proposal_hash,
)

assert proposal == ProposalVoteData(
ayes=[],
end=CLOSE_IN_VALUE(1_000_000, subtensor.block),
index=0,
nays=[],
threshold=3,
)

success, error = vote(
subtensor,
alice_wallet,
alice_wallet.hotkey.ss58_address,
proposal_hash,
index=0,
approve=True,
)

assert error == ""
assert success is True

proposal = subtensor.get_vote_data(
proposal_hash,
)

assert proposal == ProposalVoteData(
ayes=[
alice_wallet.hotkey.ss58_address,
],
end=CLOSE_IN_VALUE(1_000_000, subtensor.block),
index=0,
nays=[],
threshold=3,
)
17 changes: 17 additions & 0 deletions tests/e2e_tests/test_incentive.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,26 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
assert alice_neuron.rank < 0.5

bob_neuron = metagraph.neurons[1]

assert bob_neuron.incentive > 0.5
assert bob_neuron.consensus > 0.5
assert bob_neuron.rank > 0.5
assert bob_neuron.trust == 1

bonds = subtensor.bonds(netuid)

assert bonds == [
(
0,
[
(0, 65535),
(1, 65535),
],
),
(
1,
[],
),
]

print("✅ Passed test_incentive")
148 changes: 148 additions & 0 deletions tests/e2e_tests/test_staking.py
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,151 @@ def test_safe_swap_stake_scenarios(subtensor, alice_wallet, bob_wallet):
assert dest_stake > Balance(
0
), "Destination stake should be non-zero after successful swap"


def test_move_stake(subtensor, alice_wallet, bob_wallet):
"""
Tests:
- Adding stake
- Moving stake from one hotkey-subnet pair to another
"""

subtensor.burned_register(
alice_wallet,
netuid=1,
wait_for_inclusion=True,
wait_for_finalization=True,
)

assert subtensor.add_stake(
alice_wallet,
alice_wallet.hotkey.ss58_address,
netuid=1,
amount=Balance.from_tao(1_000),
wait_for_inclusion=True,
wait_for_finalization=True,
)

stakes = subtensor.get_stake_for_coldkey(alice_wallet.coldkey.ss58_address)

assert stakes == [
StakeInfo(
hotkey_ss58=alice_wallet.hotkey.ss58_address,
coldkey_ss58=alice_wallet.coldkey.ss58_address,
netuid=1,
stake=ANY_BALANCE,
locked=Balance(0),
emission=ANY_BALANCE,
drain=0,
is_registered=True,
),
]

subtensor.register_subnet(bob_wallet)

assert subtensor.move_stake(
alice_wallet,
origin_hotkey=alice_wallet.hotkey.ss58_address,
origin_netuid=1,
destination_hotkey=bob_wallet.hotkey.ss58_address,
destination_netuid=2,
amount=stakes[0].stake,
wait_for_finalization=True,
wait_for_inclusion=True,
)

stakes = subtensor.get_stake_for_coldkey(alice_wallet.coldkey.ss58_address)

assert stakes == [
StakeInfo(
hotkey_ss58=bob_wallet.hotkey.ss58_address,
coldkey_ss58=alice_wallet.coldkey.ss58_address,
netuid=2,
stake=ANY_BALANCE,
locked=Balance(0),
emission=ANY_BALANCE,
drain=0,
is_registered=True,
),
]


def test_transfer_stake(subtensor, alice_wallet, bob_wallet, dave_wallet):
"""
Tests:
- Adding stake
- Transfering stake from one coldkey-subnet pair to another
"""

subtensor.burned_register(
alice_wallet,
netuid=1,
wait_for_inclusion=True,
wait_for_finalization=True,
)

assert subtensor.add_stake(
alice_wallet,
alice_wallet.hotkey.ss58_address,
netuid=1,
amount=Balance.from_tao(1_000),
wait_for_inclusion=True,
wait_for_finalization=True,
)

alice_stakes = subtensor.get_stake_for_coldkey(alice_wallet.coldkey.ss58_address)

assert alice_stakes == [
StakeInfo(
hotkey_ss58=alice_wallet.hotkey.ss58_address,
coldkey_ss58=alice_wallet.coldkey.ss58_address,
netuid=1,
stake=ANY_BALANCE,
locked=Balance(0),
emission=ANY_BALANCE,
drain=0,
is_registered=True,
),
]

bob_stakes = subtensor.get_stake_for_coldkey(bob_wallet.coldkey.ss58_address)

assert bob_stakes == []

subtensor.register_subnet(dave_wallet)
subtensor.burned_register(
bob_wallet,
netuid=2,
wait_for_inclusion=True,
wait_for_finalization=True,
)

assert subtensor.transfer_stake(
alice_wallet,
destination_coldkey_ss58=bob_wallet.coldkey.ss58_address,
hotkey_ss58=alice_wallet.hotkey.ss58_address,
origin_netuid=1,
destination_netuid=2,
amount=alice_stakes[0].stake,
wait_for_inclusion=True,
wait_for_finalization=True,
)

alice_stakes = subtensor.get_stake_for_coldkey(alice_wallet.coldkey.ss58_address)

assert alice_stakes == []

bob_stakes = subtensor.get_stake_for_coldkey(bob_wallet.coldkey.ss58_address)

assert bob_stakes == [
StakeInfo(
hotkey_ss58=alice_wallet.hotkey.ss58_address,
coldkey_ss58=bob_wallet.coldkey.ss58_address,
netuid=2,
stake=ANY_BALANCE,
locked=Balance(0),
emission=ANY_BALANCE,
drain=0,
is_registered=False,
),
]
Loading
Loading