Skip to content

AsyncSubtensor E2E tests #2775

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

Open
wants to merge 7 commits into
base: staging
Choose a base branch
from
Open
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
22 changes: 13 additions & 9 deletions tests/e2e_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
import time

import pytest
import pytest_asyncio
from async_substrate_interface import SubstrateInterface

from bittensor.core.async_subtensor import AsyncSubtensor
from bittensor.core.subtensor import Subtensor
from bittensor.utils.btlogging import logging
from tests.e2e_tests.utils.e2e_test_utils import (
SyncSubtensor,
Templates,
setup_wallet,
)
Expand Down Expand Up @@ -249,14 +250,17 @@ def templates():
yield templates


@pytest.fixture
def subtensor(local_chain):
return Subtensor(network="ws://localhost:9944")


@pytest.fixture
def async_subtensor(local_chain):
return AsyncSubtensor(network="ws://localhost:9944")
@pytest_asyncio.fixture(
params=[
SyncSubtensor,
AsyncSubtensor,
],
)
async def subtensor(local_chain, request):
async with request.param(
network=local_chain.url,
) as subtensor:
yield subtensor


@pytest.fixture
Expand Down
8 changes: 4 additions & 4 deletions tests/e2e_tests/test_axon.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ async def test_axon(subtensor, templates, alice_wallet):
netuid = 2

# Register a subnet, netuid 2
assert subtensor.register_subnet(alice_wallet), "Subnet wasn't created"
assert await subtensor.register_subnet(alice_wallet), "Subnet wasn't created"

# Verify subnet <netuid> created successfully
assert subtensor.subnet_exists(netuid), "Subnet wasn't created successfully"
assert await subtensor.subnet_exists(netuid), "Subnet wasn't created successfully"

metagraph = subtensor.metagraph(netuid)
metagraph = await subtensor.metagraph(netuid)

# Validate current metagraph stats
old_axon = metagraph.axons[0]
Expand All @@ -49,7 +49,7 @@ async def test_axon(subtensor, templates, alice_wallet):
await asyncio.sleep(5)

# Refresh the metagraph
metagraph = subtensor.metagraph(netuid)
metagraph = await subtensor.metagraph(netuid)
updated_axon = metagraph.axons[0]
external_ip = networking.get_external_ip()

Expand Down
46 changes: 25 additions & 21 deletions tests/e2e_tests/test_commit_reveal_v3.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
logging.console.info("Testing test_commit_and_reveal_weights")

# Register root as Alice
assert subtensor.register_subnet(alice_wallet), "Unable to register the subnet"
assert await subtensor.register_subnet(
alice_wallet,
), "Unable to register the subnet"

# Verify subnet 2 created successfully
assert subtensor.subnet_exists(netuid), "Subnet wasn't created successfully"
assert await subtensor.subnet_exists(netuid), "Subnet wasn't created successfully"

logging.console.info("Subnet 2 is registered")

Expand All @@ -52,7 +54,9 @@ 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.commit_reveal_enabled(netuid), "Failed to enable commit/reveal"
assert await 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 All @@ -68,9 +72,9 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle

# Verify weights rate limit was changed
assert (
subtensor.get_subnet_hyperparameters(netuid=netuid).weights_rate_limit == 0
), "Failed to set weights_rate_limit"
assert subtensor.weights_rate_limit(netuid=netuid) == 0
await subtensor.get_subnet_hyperparameters(netuid=netuid)
).weights_rate_limit == 0, "Failed to set weights_rate_limit"
assert await subtensor.weights_rate_limit(netuid=netuid) == 0
logging.console.info("sudo_set_weights_set_rate_limit executed: set to 0")

# Change the tempo of the subnet
Expand All @@ -84,7 +88,7 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
)[0]
is True
)
tempo = subtensor.get_subnet_hyperparameters(netuid=netuid).tempo
tempo = await subtensor.tempo(netuid)
assert tempo_set == tempo
logging.console.info(f"sudo_set_tempo executed: set to {tempo_set}")

Expand All @@ -96,14 +100,14 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
)

# Fetch current block and calculate next tempo for the subnet
current_block = subtensor.get_current_block()
current_block = await subtensor.get_current_block()
upcoming_tempo = next_tempo(current_block, tempo)
logging.console.info(
f"Checking if window is too low with Current block: {current_block}, next tempo: {upcoming_tempo}"
)

# Wait for 2 tempos to pass as CR3 only reveals weights after 2 tempos + 1
subtensor.wait_for_block((tempo_set * 2) + 1)
await subtensor.wait_for_block((tempo_set * 2) + 1)

# Lower than this might mean weights will get revealed before we can check them
if upcoming_tempo - current_block < 3:
Expand All @@ -113,15 +117,15 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
netuid=netuid,
reporting_interval=1,
)
current_block = subtensor.get_current_block()
latest_drand_round = subtensor.last_drand_round()
current_block = await subtensor.get_current_block()
latest_drand_round = await subtensor.last_drand_round()
upcoming_tempo = next_tempo(current_block, tempo)
logging.console.info(
f"Post first wait_interval (to ensure window isnt too low): {current_block}, next tempo: {upcoming_tempo}, drand: {latest_drand_round}"
)

# Commit weights
success, message = subtensor.set_weights(
success, message = await subtensor.set_weights(
alice_wallet,
netuid,
uids=weight_uids,
Expand All @@ -141,8 +145,8 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
f"Successfully set weights: uids {weight_uids}, weights {weight_vals}, reveal_round: {expected_reveal_round}"
)

current_block = subtensor.get_current_block()
latest_drand_round = subtensor.last_drand_round()
current_block = await subtensor.get_current_block()
latest_drand_round = await subtensor.last_drand_round()
upcoming_tempo = next_tempo(current_block, tempo)
logging.console.info(
f"After setting weights: Current block: {current_block}, next tempo: {upcoming_tempo}, drand: {latest_drand_round}"
Expand All @@ -154,36 +158,36 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
), "Revealed drand pulse is older than the drand pulse right after setting weights"

# Fetch current commits pending on the chain
commits_on_chain = subtensor.get_current_weight_commit_info(netuid=netuid)
commits_on_chain = await subtensor.get_current_weight_commit_info(netuid=netuid)
address, commit, reveal_round = commits_on_chain[0]

# Assert correct values are committed on the chain
assert expected_reveal_round == reveal_round
assert address == alice_wallet.hotkey.ss58_address

# Ensure no weights are available as of now
assert subtensor.weights(netuid=netuid) == []
assert await subtensor.weights(netuid=netuid) == []

# Wait for the next tempo so weights can be revealed
await wait_interval(
subtensor.get_subnet_hyperparameters(netuid=netuid).tempo,
tempo,
subtensor,
netuid=netuid,
reporting_interval=1,
)

# Fetch the latest drand pulse
latest_drand_round = subtensor.last_drand_round()
latest_drand_round = await subtensor.last_drand_round()
logging.console.info(
f"Latest drand round after waiting for tempo: {latest_drand_round}"
)

# wait until last_drand_round is the same or greeter than expected_reveal_round with sleep 3 second (as Drand round period)
while expected_reveal_round >= subtensor.last_drand_round():
while expected_reveal_round >= await subtensor.last_drand_round():
time.sleep(3)

# Fetch weights on the chain as they should be revealed now
revealed_weights_ = subtensor.weights(netuid=netuid)
revealed_weights_ = await subtensor.weights(netuid=netuid)

print("revealed weights", revealed_weights_)
revealed_weights = revealed_weights_[0][1]
Expand All @@ -192,7 +196,7 @@ async def test_commit_and_reveal_weights_cr3(local_chain, subtensor, alice_walle
assert weight_vals[0] == revealed_weights[0][1]

# Now that the commit has been revealed, there shouldn't be any pending commits
assert subtensor.get_current_weight_commit_info(netuid=netuid) == []
assert await subtensor.get_current_weight_commit_info(netuid=netuid) == []

# Ensure the drand_round is always in the positive w.r.t expected when revealed
assert (
Expand Down
Loading
Loading