Skip to content

Commit

Permalink
Merge pull request #1987 from opentensor/feature/opendansor/e2e-fauce…
Browse files Browse the repository at this point in the history
…t-test

Add E2E faucet test
  • Loading branch information
opendansor committed Jun 11, 2024
2 parents 80c2e19 + 173d184 commit c4a4d51
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 4 deletions.
9 changes: 7 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,12 @@ 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
args = "" if param is None else f"fast_blocks={param}"

# compile commands to send to process
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
86 changes: 86 additions & 0 deletions tests/e2e_tests/subcommands/wallet/test_faucet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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
assert (
new_wallet_balance.tao == 999899.0
) # after 3 runs we should see an increase of 900 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

0 comments on commit c4a4d51

Please sign in to comment.