Skip to content

Commit f77c182

Browse files
authored
Merge branch 'staging' into fix/thewhaleking/add-nonce-to-extrinsics-calls
2 parents 0046e25 + 9a04b32 commit f77c182

File tree

7 files changed

+131
-253
lines changed

7 files changed

+131
-253
lines changed

tests/e2e_tests/conftest.py

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@
1212

1313
from bittensor.utils.btlogging import logging
1414
from tests.e2e_tests.utils.e2e_test_utils import (
15-
clone_or_update_templates,
16-
install_templates,
15+
Templates,
1716
setup_wallet,
18-
template_path,
19-
uninstall_templates,
2017
)
2118

2219

@@ -38,15 +35,6 @@ def local_chain(request):
3835
# Compile commands to send to process
3936
cmds = shlex.split(f"{script_path} {args}")
4037

41-
# Start new node process
42-
process = subprocess.Popen(
43-
cmds,
44-
stdout=subprocess.PIPE,
45-
stderr=subprocess.STDOUT,
46-
text=True,
47-
preexec_fn=os.setsid,
48-
)
49-
5038
# Pattern match indicates node is compiled and ready
5139
pattern = re.compile(r"Imported #1")
5240
timestamp = int(time.time())
@@ -61,7 +49,7 @@ def wait_for_node_start(process, pattern):
6149
# 10 min as timeout
6250
if int(time.time()) - timestamp > 10 * 60:
6351
print("Subtensor not started in time")
64-
return
52+
raise TimeoutError
6553
if pattern.search(line):
6654
print("Node started!")
6755
break
@@ -77,38 +65,35 @@ def read_output():
7765
reader_thread = threading.Thread(target=read_output, daemon=True)
7866
reader_thread.start()
7967

80-
wait_for_node_start(process, pattern)
81-
82-
# Run the test, passing in substrate interface
83-
yield SubstrateInterface(url="ws://127.0.0.1:9944")
84-
85-
# Terminate the process group (includes all child processes)
86-
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
87-
88-
# Give some time for the process to terminate
89-
time.sleep(1)
90-
91-
# If the process is not terminated, send SIGKILL
92-
if process.poll() is None:
93-
os.killpg(os.getpgid(process.pid), signal.SIGKILL)
94-
95-
# Ensure the process has terminated
96-
process.wait()
97-
98-
99-
@pytest.fixture
68+
with subprocess.Popen(
69+
cmds,
70+
start_new_session=True,
71+
stderr=subprocess.STDOUT,
72+
stdout=subprocess.PIPE,
73+
text=True,
74+
) as process:
75+
try:
76+
wait_for_node_start(process, pattern)
77+
except TimeoutError:
78+
raise
79+
else:
80+
yield SubstrateInterface(url="ws://127.0.0.1:9944")
81+
finally:
82+
# Terminate the process group (includes all child processes)
83+
os.killpg(os.getpgid(process.pid), signal.SIGTERM)
84+
85+
try:
86+
process.wait(1)
87+
except subprocess.TimeoutExpired:
88+
# If the process is not terminated, send SIGKILL
89+
os.killpg(os.getpgid(process.pid), signal.SIGKILL)
90+
process.wait()
91+
92+
93+
@pytest.fixture(scope="session")
10094
def templates():
101-
logging.info("downloading and installing neuron templates from github")
102-
103-
templates_dir = clone_or_update_templates()
104-
105-
install_templates(templates_dir)
106-
107-
yield templates_dir
108-
109-
logging.info("uninstalling neuron templates")
110-
111-
uninstall_templates(template_path)
95+
with Templates() as templates:
96+
yield templates
11297

11398

11499
@pytest.fixture

tests/e2e_tests/test_axon.py

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import sys
32

43
import pytest
54

@@ -45,42 +44,14 @@ async def test_axon(subtensor, templates, alice_wallet):
4544
assert old_axon.port == 0, f"Expected port 0, but got {old_axon.port}"
4645
assert old_axon.ip_type == 0, f"Expected IP type 0, but got {old_axon.ip_type}"
4746

48-
# Prepare to run the miner
49-
cmd = " ".join(
50-
[
51-
f"{sys.executable}",
52-
f'"{templates}/miner.py"',
53-
"--netuid",
54-
str(netuid),
55-
"--subtensor.network",
56-
"local",
57-
"--subtensor.chain_endpoint",
58-
"ws://localhost:9944",
59-
"--wallet.path",
60-
alice_wallet.path,
61-
"--wallet.name",
62-
alice_wallet.name,
63-
"--wallet.hotkey",
64-
"default",
65-
]
66-
)
67-
68-
# Run the miner in the background
69-
await asyncio.create_subprocess_shell(
70-
cmd,
71-
stdout=asyncio.subprocess.PIPE,
72-
stderr=asyncio.subprocess.PIPE,
73-
)
74-
75-
print("Neuron Alice is now mining")
76-
77-
# Waiting for 5 seconds for metagraph to be updated
78-
await asyncio.sleep(5)
79-
80-
# Refresh the metagraph
81-
metagraph = subtensor.metagraph(netuid)
82-
updated_axon = metagraph.axons[0]
83-
external_ip = networking.get_external_ip()
47+
async with templates.miner(alice_wallet, netuid):
48+
# Waiting for 5 seconds for metagraph to be updated
49+
await asyncio.sleep(5)
50+
51+
# Refresh the metagraph
52+
metagraph = subtensor.metagraph(netuid)
53+
updated_axon = metagraph.axons[0]
54+
external_ip = networking.get_external_ip()
8455

8556
# Assert updated attributes
8657
assert (

tests/e2e_tests/test_dendrite.py

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import sys
32

43
import pytest
54

@@ -104,39 +103,13 @@ async def test_dendrite(local_chain, subtensor, templates, alice_wallet, bob_wal
104103
assert bob_neuron.validator_trust == 0.0
105104
assert bob_neuron.pruning_score == 0
106105

107-
# Prepare to run the validator
108-
cmd = " ".join(
109-
[
110-
f"{sys.executable}",
111-
f'"{templates}/validator.py"',
112-
"--netuid",
113-
str(netuid),
114-
"--subtensor.network",
115-
"local",
116-
"--subtensor.chain_endpoint",
117-
"ws://localhost:9944",
118-
"--wallet.path",
119-
bob_wallet.path,
120-
"--wallet.name",
121-
bob_wallet.name,
122-
"--wallet.hotkey",
123-
"default",
124-
]
125-
)
126-
127-
# Run the validator in the background
128-
await asyncio.create_subprocess_shell(
129-
cmd,
130-
stdout=asyncio.subprocess.PIPE,
131-
stderr=asyncio.subprocess.PIPE,
132-
)
133-
logging.console.info("Neuron Alice is now validating")
134-
await asyncio.sleep(5) # wait for 5 seconds for the Validator to process
106+
async with templates.validator(bob_wallet, netuid):
107+
await asyncio.sleep(5) # wait for 5 seconds for the Validator to process
135108

136-
await wait_epoch(subtensor, netuid=netuid)
109+
await wait_epoch(subtensor, netuid=netuid)
137110

138-
# Refresh metagraph
139-
metagraph = subtensor.metagraph(netuid)
111+
# Refresh metagraph
112+
metagraph = subtensor.metagraph(netuid)
140113

141114
# Refresh validator neuron
142115
updated_neuron = metagraph.neurons[1]

tests/e2e_tests/test_incentive.py

Lines changed: 8 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import sys
32
import time
43

54
import pytest
@@ -72,72 +71,16 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
7271
return_error_message=True,
7372
)
7473

75-
# Prepare to run Bob as miner
76-
cmd = " ".join(
77-
[
78-
f"{sys.executable}",
79-
f'"{templates}/miner.py"',
80-
"--netuid",
81-
str(netuid),
82-
"--subtensor.network",
83-
"local",
84-
"--subtensor.chain_endpoint",
85-
local_chain.chain_endpoint,
86-
"--wallet.path",
87-
bob_wallet.path,
88-
"--wallet.name",
89-
bob_wallet.name,
90-
"--wallet.hotkey",
91-
"default",
92-
"--logging.trace",
93-
]
94-
)
95-
96-
# Run Bob as miner in the background
97-
await asyncio.create_subprocess_shell(
98-
cmd,
99-
stdout=asyncio.subprocess.PIPE,
100-
stderr=asyncio.subprocess.PIPE,
101-
)
102-
print("Neuron Bob is now mining")
103-
104-
# Prepare to run Alice as validator
105-
cmd = " ".join(
106-
[
107-
f"{sys.executable}",
108-
f'"{templates}/validator.py"',
109-
"--netuid",
110-
str(netuid),
111-
"--subtensor.network",
112-
"local",
113-
"--subtensor.chain_endpoint",
114-
"ws://localhost:9944",
115-
"--wallet.path",
116-
alice_wallet.path,
117-
"--wallet.name",
118-
alice_wallet.name,
119-
"--wallet.hotkey",
120-
"default",
121-
"--logging.trace",
122-
]
123-
)
74+
async with templates.miner(bob_wallet, netuid):
75+
async with templates.validator(alice_wallet, netuid):
76+
# wait for the Validator to process and set_weights
77+
await asyncio.sleep(5)
12478

125-
# Run Alice as validator in the background
126-
await asyncio.create_subprocess_shell(
127-
cmd,
128-
stdout=asyncio.subprocess.PIPE,
129-
stderr=asyncio.subprocess.PIPE,
130-
)
131-
print("Neuron Alice is now validating")
132-
133-
# wait for the Validator to process and set_weights
134-
await asyncio.sleep(5)
79+
# Wait until next epoch
80+
await wait_epoch(subtensor, netuid)
13581

136-
# Wait until next epoch
137-
await wait_epoch(subtensor, netuid)
138-
139-
# Refresh metagraph
140-
metagraph = subtensor.metagraph(netuid)
82+
# Refresh metagraph
83+
metagraph = subtensor.metagraph(netuid)
14184

14285
# Get current emissions and validate that Alice has gotten tao
14386
alice_neuron = metagraph.neurons[0]

tests/e2e_tests/test_root_set_weights.py

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import sys
32
import pytest
43

54
from tests.e2e_tests.utils.chain_interactions import (
@@ -84,46 +83,19 @@ async def test_root_reg_hyperparams(
8483
assert subtensor.immunity_period(netuid=netuid) == default_immunity_period
8584
assert subtensor.tempo(netuid=netuid) == default_tempo
8685

87-
# Prepare to run Alice as validator
88-
cmd = " ".join(
89-
[
90-
f"{sys.executable}",
91-
f'"{templates}/validator.py"',
92-
"--netuid",
93-
str(netuid),
94-
"--subtensor.network",
95-
"local",
96-
"--subtensor.chain_endpoint",
97-
"ws://localhost:9944",
98-
"--wallet.path",
99-
alice_wallet.path,
100-
"--wallet.name",
101-
alice_wallet.name,
102-
"--wallet.hotkey",
103-
"default",
104-
"--logging.trace",
105-
]
106-
)
86+
async with templates.validator(alice_wallet, netuid):
87+
await asyncio.sleep(5) # Wait a bit for chain to process data
10788

108-
# Run Alice as validator in the background
109-
await asyncio.create_subprocess_shell(
110-
cmd,
111-
stdout=asyncio.subprocess.PIPE,
112-
stderr=asyncio.subprocess.PIPE,
113-
)
114-
print("Neuron Alice is now validating")
115-
await asyncio.sleep(5) # Wait a bit for chain to process data
89+
# Fetch uid against Alice's hotkey on sn 2 (it will be 0 as she is the only registered neuron)
90+
alice_uid_sn_2 = subtensor.get_uid_for_hotkey_on_subnet(
91+
alice_wallet.hotkey.ss58_address, netuid
92+
)
11693

117-
# Fetch uid against Alice's hotkey on sn 2 (it will be 0 as she is the only registered neuron)
118-
alice_uid_sn_2 = subtensor.get_uid_for_hotkey_on_subnet(
119-
alice_wallet.hotkey.ss58_address, netuid
120-
)
121-
122-
# Fetch the block since last update for the neuron
123-
block_since_update = subtensor.blocks_since_last_update(
124-
netuid=netuid, uid=alice_uid_sn_2
125-
)
126-
assert block_since_update is not None
94+
# Fetch the block since last update for the neuron
95+
block_since_update = subtensor.blocks_since_last_update(
96+
netuid=netuid, uid=alice_uid_sn_2
97+
)
98+
assert block_since_update is not None
12799

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

0 commit comments

Comments
 (0)