Skip to content

Commit a22f45b

Browse files
tests: wait for Miner/Validator to fully start
1 parent 2ee6f7a commit a22f45b

File tree

2 files changed

+121
-55
lines changed

2 files changed

+121
-55
lines changed

tests/e2e_tests/test_incentive.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
4141
len(subtensor.neurons(netuid=netuid)) == 2
4242
), "Alice & Bob not registered in the subnet"
4343

44+
# Wait for the first epoch to pass
45+
await wait_epoch(subtensor, netuid)
46+
4447
# Get latest metagraph
4548
metagraph = subtensor.metagraph(netuid)
4649

@@ -51,6 +54,9 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
5154
assert alice_neuron.dividends == 0
5255
assert alice_neuron.stake.tao > 0
5356
assert alice_neuron.validator_trust == 0
57+
assert alice_neuron.incentive == 0
58+
assert alice_neuron.consensus == 0
59+
assert alice_neuron.rank == 0
5460

5561
bob_neuron = metagraph.neurons[1]
5662

@@ -69,9 +75,10 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
6975
)
7076

7177
async with templates.miner(bob_wallet, netuid):
72-
async with templates.validator(alice_wallet, netuid):
78+
async with templates.validator(alice_wallet, netuid) as validator:
7379
# wait for the Validator to process and set_weights
74-
await asyncio.sleep(5)
80+
async with asyncio.timeout(15):
81+
await validator.set_weights.wait()
7582

7683
# Wait few epochs
7784
await wait_epoch(subtensor, netuid, times=4)
@@ -85,12 +92,15 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
8592
assert alice_neuron.validator_permit is True
8693
assert alice_neuron.dividends == 1.0
8794
assert alice_neuron.stake.tao > 0
88-
assert alice_neuron.validator_trust == 1
95+
assert alice_neuron.validator_trust > 0.99
96+
assert alice_neuron.incentive < 0.5
97+
assert alice_neuron.consensus < 0.5
98+
assert alice_neuron.rank < 0.5
8999

90100
bob_neuron = metagraph.neurons[1]
91-
assert bob_neuron.incentive == 1
92-
assert bob_neuron.consensus == 1
93-
assert bob_neuron.rank == 1
101+
assert bob_neuron.incentive > 0.5
102+
assert bob_neuron.consensus > 0.5
103+
assert bob_neuron.rank > 0.5
94104
assert bob_neuron.trust == 1
95105

96106
print("✅ Passed test_incentive")

tests/e2e_tests/utils/e2e_test_utils.py

Lines changed: 105 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import contextlib
32
import os
43
import shutil
54
import subprocess
@@ -84,6 +83,107 @@ def uninstall_templates(install_dir):
8483

8584

8685
class Templates:
86+
class Miner:
87+
def __init__(self, dir, wallet, netuid):
88+
self.dir = dir
89+
self.wallet = wallet
90+
self.netuid = netuid
91+
self.process = None
92+
93+
self.started = asyncio.Event()
94+
95+
async def __aenter__(self):
96+
self.process = await asyncio.create_subprocess_exec(
97+
sys.executable,
98+
f"{self.dir}/miner.py",
99+
"--netuid",
100+
str(self.netuid),
101+
"--subtensor.network",
102+
"local",
103+
"--subtensor.chain_endpoint",
104+
"ws://localhost:9944",
105+
"--wallet.path",
106+
self.wallet.path,
107+
"--wallet.name",
108+
self.wallet.name,
109+
"--wallet.hotkey",
110+
"default",
111+
env={
112+
"BT_LOGGING_INFO": "1",
113+
},
114+
stdout=asyncio.subprocess.PIPE,
115+
)
116+
117+
self.__reader_task = asyncio.create_task(self._reader())
118+
119+
async with asyncio.timeout(30):
120+
await self.started.wait()
121+
122+
return self
123+
124+
async def __aexit__(self, exc_type, exc_value, traceback):
125+
self.process.terminate()
126+
self.__reader_task.cancel()
127+
128+
await self.process.wait()
129+
130+
async def _reader(self):
131+
async for line in self.process.stdout:
132+
if b"Starting main loop" in line:
133+
self.started.set()
134+
135+
class Validator:
136+
def __init__(self, dir, wallet, netuid):
137+
self.dir = dir
138+
self.wallet = wallet
139+
self.netuid = netuid
140+
self.process = None
141+
142+
self.started = asyncio.Event()
143+
self.set_weights = asyncio.Event()
144+
145+
async def __aenter__(self):
146+
self.process = await asyncio.create_subprocess_exec(
147+
sys.executable,
148+
f"{self.dir}/validator.py",
149+
"--netuid",
150+
str(self.netuid),
151+
"--subtensor.network",
152+
"local",
153+
"--subtensor.chain_endpoint",
154+
"ws://localhost:9944",
155+
"--wallet.path",
156+
self.wallet.path,
157+
"--wallet.name",
158+
self.wallet.name,
159+
"--wallet.hotkey",
160+
"default",
161+
env={
162+
"BT_LOGGING_INFO": "1",
163+
},
164+
stdout=asyncio.subprocess.PIPE,
165+
)
166+
167+
self.__reader_task = asyncio.create_task(self._reader())
168+
169+
async with asyncio.timeout(30):
170+
await self.started.wait()
171+
172+
return self
173+
174+
async def __aexit__(self, exc_type, exc_value, traceback):
175+
self.process.terminate()
176+
self.__reader_task.cancel()
177+
178+
await self.process.wait()
179+
180+
async def _reader(self):
181+
async for line in self.process.stdout:
182+
if b"Starting validator loop." in line:
183+
self.started.set()
184+
elif b"Successfully set weights and Finalized." in line:
185+
self.set_weights.set()
186+
87187
def __init__(self):
88188
self.dir = clone_or_update_templates()
89189

@@ -93,52 +193,8 @@ def __enter__(self):
93193
def __exit__(self, exc_type, exc_value, traceback):
94194
uninstall_templates(self.dir)
95195

96-
@contextlib.asynccontextmanager
97-
async def miner(self, wallet, netuid):
98-
process = await asyncio.create_subprocess_exec(
99-
sys.executable,
100-
f"{self.dir}/miner.py",
101-
"--netuid",
102-
str(netuid),
103-
"--subtensor.network",
104-
"local",
105-
"--subtensor.chain_endpoint",
106-
"ws://localhost:9944",
107-
"--wallet.path",
108-
wallet.path,
109-
"--wallet.name",
110-
wallet.name,
111-
"--wallet.hotkey",
112-
"default",
113-
)
114-
115-
yield
116-
117-
process.terminate()
118-
119-
await process.wait()
120-
121-
@contextlib.asynccontextmanager
122-
async def validator(self, wallet, netuid):
123-
process = await asyncio.create_subprocess_exec(
124-
sys.executable,
125-
f"{self.dir}/validator.py",
126-
"--netuid",
127-
str(netuid),
128-
"--subtensor.network",
129-
"local",
130-
"--subtensor.chain_endpoint",
131-
"ws://localhost:9944",
132-
"--wallet.path",
133-
wallet.path,
134-
"--wallet.name",
135-
wallet.name,
136-
"--wallet.hotkey",
137-
"default",
138-
)
139-
140-
yield
141-
142-
process.terminate()
196+
def miner(self, wallet, netuid):
197+
return self.Miner(self.dir, wallet, netuid)
143198

144-
await process.wait()
199+
def validator(self, wallet, netuid):
200+
return self.Validator(self.dir, wallet, netuid)

0 commit comments

Comments
 (0)