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

Add E2E faucet test #1987

Merged
merged 10 commits into from
Jun 11, 2024
10 changes: 8 additions & 2 deletions tests/e2e_tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@

# Fixture for setting up and tearing down a localnet.sh chain between tests
@pytest.fixture(scope="function")
def local_chain():
def local_chain(request):
param = request.param if hasattr(request, "param") else None
# Get the environment variable for the script path
script_path = os.getenv("LOCALNET_SH_PATH")

Expand All @@ -30,8 +31,13 @@ def local_chain():
logging.warning("LOCALNET_SH_PATH env variable is not set, e2e test skipped.")
pytest.skip("LOCALNET_SH_PATH environment variable is not set.")

# Check if param is None, and handle it accordingly
if param is None:
args = ""
else:
args = f"fast_blocks={param}"
cmds = shlex.split(f"{script_path} {args}")
# Start new node process
cmds = shlex.split(script_path)
process = subprocess.Popen(
cmds, stdout=subprocess.PIPE, text=True, preexec_fn=os.setsid
)
Expand Down
83 changes: 83 additions & 0 deletions tests/e2e_tests/subcommands/wallet/test_faucet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import pytest

import bittensor
from bittensor import logging
from bittensor.commands import (
RegisterCommand,
RegisterSubnetworkCommand,
RunFaucetCommand,
)
from tests.e2e_tests.utils import (
setup_wallet,
)


@pytest.mark.parametrize("local_chain", [False], indirect=True)
def test_faucet(local_chain):
# Register root as Alice
keypair, exec_command, wallet_path = setup_wallet("//Alice")
exec_command(RegisterSubnetworkCommand, ["s", "create"])

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

# Register a neuron to the subnet
exec_command(
RegisterCommand,
[
"s",
"register",
"--netuid",
"1",
"--wallet.name",
"default",
"--wallet.hotkey",
"default",
"--subtensor.network",
"local",
"--subtensor.chain_endpoint",
"ws://localhost:9945",
"--no_prompt",
],
)

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

# verify current balance
wallet_balance = subtensor.get_balance(keypair.ss58_address)
assert wallet_balance.tao == 998999.0

# run faucet 3 times
for i in range(3):
logging.info(f"faucet run #:{i+1}")
try:
exec_command(
RunFaucetCommand,
[
"wallet",
"faucet",
"--wallet.name",
"default",
"--wallet.hotkey",
"default",
"--subtensor.chain_endpoint",
"ws://localhost:9945",
],
)
logging.info(
f"wallet balance is {subtensor.get_balance(keypair.ss58_address).tao} tao"
)
except SystemExit as e:
logging.warning(
"Block not generated fast enough to be within 3 block seconds window."
)
# Handle the SystemExit exception
assert e.code == 1 # Assert that the exit code is 1
except Exception as e:
logging.warning(f"Unexpected exception occurred on faucet: {e}")

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

new_wallet_balance = subtensor.get_balance(keypair.ss58_address)
# verify balance increase
assert wallet_balance.tao < new_wallet_balance.tao
5 changes: 3 additions & 2 deletions tests/e2e_tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
import subprocess
import sys

import requests
from substrateinterface import Keypair
from typing import List

from bittensor import Keypair

import bittensor

template_path = os.getcwd() + "/neurons/"
Expand Down
Loading