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 #2678

Merged
merged 14 commits into from
Mar 3, 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
4 changes: 1 addition & 3 deletions tests/e2e_tests/test_commit_reveal_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
), "Unable to enable commit reveal on the subnet"

# Verify commit_reveal was enabled
assert subtensor.get_subnet_hyperparameters(
netuid=netuid,
).commit_reveal_weights_enabled, "Failed to enable commit/reveal"
assert subtensor.commit_reveal_enabled(netuid), "Failed to enable commit/reveal"
logging.console.info("Commit reveal enabled")

# Change the weights rate limit on the subnet
Expand Down
16 changes: 5 additions & 11 deletions tests/e2e_tests/test_commit_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ async def test_commit_and_reveal_weights_legacy(local_chain, subtensor, alice_wa
netuid,
), "Unable to enable commit reveal on the subnet"

assert subtensor.get_subnet_hyperparameters(
netuid=netuid,
).commit_reveal_weights_enabled, "Failed to enable commit/reveal"
assert subtensor.commit_reveal_enabled(netuid), "Failed to enable commit/reveal"

assert (
subtensor.get_subnet_hyperparameters(netuid=netuid).commit_reveal_period == 1
Expand Down Expand Up @@ -114,11 +112,9 @@ async def test_commit_and_reveal_weights_legacy(local_chain, subtensor, alice_wa
assert commit_block > 0, f"Invalid block number: {commit_block}"

# Query the WeightCommitRevealInterval storage map
reveal_periods = subtensor.query_module(
module="SubtensorModule", name="RevealPeriodEpochs", params=[netuid]
)
periods = reveal_periods
assert periods > 0, "Invalid RevealPeriodEpochs"
assert (
subtensor.get_subnet_reveal_period_epochs(netuid) > 0
), "Invalid RevealPeriodEpochs"

# Wait until the reveal block range
await wait_epoch(subtensor, netuid)
Expand Down Expand Up @@ -187,9 +183,7 @@ async def test_commit_weights_uses_next_nonce(local_chain, subtensor, alice_wall
netuid,
), "Unable to enable commit reveal on the subnet"

assert subtensor.get_subnet_hyperparameters(
netuid=netuid,
).commit_reveal_weights_enabled, "Failed to enable commit/reveal"
assert subtensor.commit_reveal_enabled(netuid), "Failed to enable commit/reveal"

assert (
subtensor.get_subnet_hyperparameters(netuid=netuid).commit_reveal_period == 1
Expand Down
330 changes: 330 additions & 0 deletions tests/e2e_tests/test_delegate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
import pytest

from bittensor.core.chain_data.delegate_info import DelegateInfo, DelegatedInfo
from bittensor.utils.balance import Balance
from bittensor.utils.delegates_details import DelegatesDetails
from tests.e2e_tests.utils.chain_interactions import (
decrease_take,
increase_take,
registry_set_identity,
sudo_set_admin_utils,
)


DEFAULT_DELEGATE_TAKE = 0.179995422293431


def test_identity(subtensor, alice_wallet, bob_wallet):
"""
Tests:
- Check Delegate's default identity
- Update Delegate's identity
"""

identities = subtensor.get_delegate_identities()

assert alice_wallet.hotkey.ss58_address not in identities

# Replace hotkey as it's the same as coldkey.
# It's required to edit identity later.
# Otherwise only subnet owner can do this.
alice_wallet.set_hotkey(
keypair=bob_wallet.hotkey,
encrypt=False,
overwrite=True,
)

subtensor.root_register(
alice_wallet,
wait_for_inclusion=True,
wait_for_finalization=True,
)

identities = subtensor.get_delegate_identities()

assert alice_wallet.hotkey.ss58_address not in identities

success, error = registry_set_identity(
subtensor,
alice_wallet,
alice_wallet.hotkey.ss58_address,
display=b"Alice Display",
web=b"https://bittensor.com/",
)

assert error == ""
assert success is True

identities = subtensor.get_delegate_identities()

assert alice_wallet.hotkey.ss58_address in identities

alice_identity = identities[alice_wallet.hotkey.ss58_address]

assert alice_identity == DelegatesDetails(
additional=[],
display="Alice Display",
email="",
image="",
legal="",
pgp_fingerprint=None,
riot="",
twitter="",
web="https://bittensor.com/",
)


def test_change_take(local_chain, subtensor, alice_wallet):
"""
Tests:
- Get default Delegate's take once registered in root subnet
- Increase and decreased Delegate's take
- Try corner cases (increase/decrease beyond allowed min/max)
"""

success, error = decrease_take(
subtensor,
alice_wallet,
0.1,
)

assert success is False
assert "`HotKeyAccountNotExists(Module)`" in error

subtensor.root_register(
alice_wallet,
wait_for_inclusion=True,
wait_for_finalization=True,
)

assert (
subtensor.get_delegate_take(alice_wallet.hotkey.ss58_address)
== DEFAULT_DELEGATE_TAKE
)

success, error = increase_take(
subtensor,
alice_wallet,
0.5,
)

assert success is False
assert "`DelegateTakeTooHigh(Module)`" in error

# increase_take but try to change from 0.18 to 0.1
success, error = increase_take(
subtensor,
alice_wallet,
0.1,
)

assert "`DelegateTakeTooLow(Module)`" in error
assert success is False

success, error = decrease_take(
subtensor,
alice_wallet,
0.1,
)

assert success is True
assert error == ""

take = subtensor.get_delegate_take(alice_wallet.hotkey.ss58_address)

assert take == 0.09999237048905166

success, error = increase_take(
subtensor,
alice_wallet,
0.15,
)

assert success is False
assert "`DelegateTxRateLimitExceeded(Module)`" in error

take = subtensor.get_delegate_take(alice_wallet.hotkey.ss58_address)

assert take == 0.09999237048905166

sudo_set_admin_utils(
local_chain,
alice_wallet,
call_function="sudo_set_tx_delegate_take_rate_limit",
call_params={
"tx_rate_limit": 0,
},
)

success, error = increase_take(
subtensor,
alice_wallet,
0.15,
)

assert success is True
assert error == ""

take = subtensor.get_delegate_take(alice_wallet.hotkey.ss58_address)

assert take == 0.14999618524452582


@pytest.mark.asyncio
async def test_delegates(subtensor, alice_wallet, bob_wallet):
"""
Tests:
- Check default Delegates
- Register Delegates
- Check if Hotkey is a Delegate
- Nominator Staking
"""

assert subtensor.get_delegates() == []
assert subtensor.get_delegated(alice_wallet.coldkey.ss58_address) == []
assert subtensor.get_delegate_by_hotkey(alice_wallet.hotkey.ss58_address) is None
assert subtensor.get_delegate_by_hotkey(bob_wallet.hotkey.ss58_address) is None

assert subtensor.is_hotkey_delegate(alice_wallet.hotkey.ss58_address) is False
assert subtensor.is_hotkey_delegate(bob_wallet.hotkey.ss58_address) is False

subtensor.root_register(
alice_wallet,
wait_for_inclusion=True,
wait_for_finalization=True,
)
subtensor.root_register(
bob_wallet,
wait_for_inclusion=True,
wait_for_finalization=True,
)

assert subtensor.is_hotkey_delegate(alice_wallet.hotkey.ss58_address) is True
assert subtensor.is_hotkey_delegate(bob_wallet.hotkey.ss58_address) is True

alice_delegate = subtensor.get_delegate_by_hotkey(alice_wallet.hotkey.ss58_address)

assert alice_delegate == DelegateInfo(
hotkey_ss58=alice_wallet.hotkey.ss58_address,
owner_ss58=alice_wallet.coldkey.ss58_address,
take=DEFAULT_DELEGATE_TAKE,
validator_permits=[],
registrations=[0],
return_per_1000=Balance(0),
total_daily_return=Balance(0),
total_stake={},
nominators={},
)

bob_delegate = subtensor.get_delegate_by_hotkey(bob_wallet.hotkey.ss58_address)

assert bob_delegate == DelegateInfo(
hotkey_ss58=bob_wallet.hotkey.ss58_address,
owner_ss58=bob_wallet.coldkey.ss58_address,
take=DEFAULT_DELEGATE_TAKE,
validator_permits=[],
registrations=[0],
return_per_1000=Balance(0),
total_daily_return=Balance(0),
total_stake={},
nominators={},
)

delegates = subtensor.get_delegates()

assert delegates == [
bob_delegate,
alice_delegate,
]

assert subtensor.get_delegated(bob_wallet.coldkey.ss58_address) == []

subtensor.add_stake(
bob_wallet,
alice_wallet.hotkey.ss58_address,
netuid=0,
amount=Balance.from_tao(10_000),
wait_for_inclusion=True,
wait_for_finalization=True,
)

assert subtensor.get_delegated(bob_wallet.coldkey.ss58_address) == [
DelegatedInfo(
hotkey_ss58=alice_wallet.hotkey.ss58_address,
owner_ss58=alice_wallet.coldkey.ss58_address,
take=DEFAULT_DELEGATE_TAKE,
validator_permits=[],
registrations=[0],
return_per_1000=Balance(0),
total_daily_return=Balance(0),
netuid=0,
stake=Balance.from_tao(9_999.99995),
),
]


def test_nominator_min_required_stake(local_chain, subtensor, alice_wallet, bob_wallet):
"""
Tests:
- Check default NominatorMinRequiredStake
- Add Stake to Nominate
- Update NominatorMinRequiredStake
- Check Nominator is removed
"""

minimum_required_stake = subtensor.get_minimum_required_stake()

assert minimum_required_stake == Balance(0)

subtensor.root_register(
alice_wallet,
wait_for_inclusion=True,
wait_for_finalization=True,
)
subtensor.root_register(
bob_wallet,
wait_for_inclusion=True,
wait_for_finalization=True,
)

success = subtensor.add_stake(
alice_wallet,
bob_wallet.hotkey.ss58_address,
netuid=0,
amount=Balance.from_tao(10_000),
wait_for_inclusion=True,
wait_for_finalization=True,
)

assert success is True

stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=0,
)

assert stake == Balance.from_tao(9_999.99995)

# this will trigger clear_small_nominations
sudo_set_admin_utils(
local_chain,
alice_wallet,
call_function="sudo_set_nominator_min_required_stake",
call_params={
"min_stake": "100000000000000",
},
return_error_message=True,
)

minimum_required_stake = subtensor.get_minimum_required_stake()

assert minimum_required_stake == Balance.from_tao(100_000)

stake = subtensor.get_stake(
alice_wallet.coldkey.ss58_address,
bob_wallet.hotkey.ss58_address,
netuid=0,
)

assert stake == Balance(0)
Loading
Loading