Skip to content

Commit f43b004

Browse files
Merge branch 'staging' into fix/zyzniewski/burned_register_supports_root_subnet
2 parents d7e9589 + f6c86d4 commit f43b004

File tree

3 files changed

+143
-115
lines changed

3 files changed

+143
-115
lines changed

bittensor/core/metagraph.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import asyncio
12
import copy
23
import os
34
import pickle
@@ -1397,13 +1398,13 @@ async def sync(
13971398

13981399
# If not a 'lite' version, compute and set weights and bonds for each neuron
13991400
if not lite:
1400-
await self._set_weights_and_bonds(subtensor=subtensor)
1401+
await self._set_weights_and_bonds(subtensor=subtensor, block=block)
14011402

14021403
# Fills in the stake associated attributes of a class instance from a chain response.
14031404
await self._get_all_stakes_from_chain(block=block)
14041405

14051406
# apply MetagraphInfo data to instance
1406-
await self._apply_metagraph_info()
1407+
await self._apply_metagraph_info(block=block)
14071408

14081409
async def _initialize_subtensor(
14091410
self, subtensor: "AsyncSubtensor"
@@ -1476,9 +1477,7 @@ async def _assign_neurons(
14761477
self.neurons = await subtensor.neurons(block=block, netuid=self.netuid)
14771478
self.lite = lite
14781479

1479-
async def _set_weights_and_bonds(
1480-
self, subtensor: Optional["AsyncSubtensor"] = None
1481-
):
1480+
async def _set_weights_and_bonds(self, subtensor: "AsyncSubtensor", block: int):
14821481
"""
14831482
Computes and sets the weights and bonds for each neuron in the metagraph. This method is responsible for
14841483
processing the raw weight and bond data obtained from the network and converting it into a structured format
@@ -1499,6 +1498,7 @@ async def _set_weights_and_bonds(
14991498
[neuron.weights for neuron in self.neurons],
15001499
"weights",
15011500
subtensor,
1501+
block=block,
15021502
)
15031503
else:
15041504
self.weights = self._process_weights_or_bonds(
@@ -1509,7 +1509,7 @@ async def _set_weights_and_bonds(
15091509
)
15101510

15111511
async def _process_root_weights(
1512-
self, data: list, attribute: str, subtensor: "AsyncSubtensor"
1512+
self, data: list, attribute: str, subtensor: "AsyncSubtensor", block: int
15131513
) -> Union[NDArray, "torch.nn.Parameter"]:
15141514
"""
15151515
Specifically processes the root weights data for the metagraph. This method is similar to :func:`_process_weights_or_bonds`
@@ -1530,8 +1530,10 @@ async def _process_root_weights(
15301530
self.root_weights = self._process_root_weights(raw_root_weights_data, "weights", subtensor)
15311531
"""
15321532
data_array = []
1533-
n_subnets = await subtensor.get_total_subnets() or 0
1534-
subnets = await subtensor.get_subnets()
1533+
n_subnets_, subnets = await asyncio.gather(
1534+
subtensor.get_total_subnets(block=block), subtensor.get_subnets(block=block)
1535+
)
1536+
n_subnets = n_subnets_ or 0
15351537
for item in data:
15361538
if len(item) == 0:
15371539
if use_torch():
@@ -1612,9 +1614,11 @@ async def _get_all_stakes_from_chain(self, block: int):
16121614
except (SubstrateRequestException, AttributeError) as e:
16131615
logging.debug(e)
16141616

1615-
async def _apply_metagraph_info(self):
1617+
async def _apply_metagraph_info(self, block: int):
16161618
"""Retrieves metagraph information for a specific subnet and applies it using a mixin."""
1617-
metagraph_info = await self.subtensor.get_metagraph_info(self.netuid)
1619+
metagraph_info = await self.subtensor.get_metagraph_info(
1620+
self.netuid, block=block
1621+
)
16181622
if metagraph_info:
16191623
self._apply_metagraph_info_mixin(metagraph_info=metagraph_info)
16201624

@@ -1711,13 +1715,13 @@ def sync(
17111715

17121716
# If not a 'lite' version, compute and set weights and bonds for each neuron
17131717
if not lite:
1714-
self._set_weights_and_bonds(subtensor=subtensor)
1718+
self._set_weights_and_bonds(subtensor=subtensor, block=block)
17151719

17161720
# Fills in the stake associated attributes of a class instance from a chain response.
17171721
self._get_all_stakes_from_chain(block=block)
17181722

17191723
# apply MetagraphInfo data to instance
1720-
self._apply_metagraph_info()
1724+
self._apply_metagraph_info(block=block)
17211725

17221726
def _initialize_subtensor(self, subtensor: "Subtensor") -> "Subtensor":
17231727
"""
@@ -1787,7 +1791,7 @@ def _assign_neurons(self, block: int, lite: bool, subtensor: "Subtensor"):
17871791
self.neurons = subtensor.neurons(block=block, netuid=self.netuid)
17881792
self.lite = lite
17891793

1790-
def _set_weights_and_bonds(self, subtensor: Optional["Subtensor"] = None):
1794+
def _set_weights_and_bonds(self, block: int, subtensor: "Subtensor"):
17911795
"""
17921796
Computes and sets the weights and bonds for each neuron in the metagraph. This method is responsible for
17931797
processing the raw weight and bond data obtained from the network and converting it into a structured format
@@ -1804,9 +1808,7 @@ def _set_weights_and_bonds(self, subtensor: Optional["Subtensor"] = None):
18041808
"""
18051809
if self.netuid == 0:
18061810
self.weights = self._process_root_weights(
1807-
[neuron.weights for neuron in self.neurons],
1808-
"weights",
1809-
subtensor,
1811+
[neuron.weights for neuron in self.neurons], "weights", subtensor, block
18101812
)
18111813
else:
18121814
self.weights = self._process_weights_or_bonds(
@@ -1817,7 +1819,7 @@ def _set_weights_and_bonds(self, subtensor: Optional["Subtensor"] = None):
18171819
)
18181820

18191821
def _process_root_weights(
1820-
self, data: list, attribute: str, subtensor: "Subtensor"
1822+
self, data: list, attribute: str, subtensor: "Subtensor", block: int
18211823
) -> Union[NDArray, "torch.nn.Parameter"]:
18221824
"""
18231825
Specifically processes the root weights data for the metagraph. This method is similar to :func:`_process_weights_or_bonds`
@@ -1838,8 +1840,8 @@ def _process_root_weights(
18381840
self.root_weights = self._process_root_weights(raw_root_weights_data, "weights", subtensor)
18391841
"""
18401842
data_array = []
1841-
n_subnets = subtensor.get_total_subnets() or 0
1842-
subnets = subtensor.get_subnets()
1843+
n_subnets = subtensor.get_total_subnets(block=block) or 0
1844+
subnets = subtensor.get_subnets(block=block)
18431845
for item in data:
18441846
if len(item) == 0:
18451847
if use_torch():
@@ -1920,9 +1922,9 @@ def _get_all_stakes_from_chain(self, block: int):
19201922
except (SubstrateRequestException, AttributeError) as e:
19211923
logging.debug(e)
19221924

1923-
def _apply_metagraph_info(self):
1925+
def _apply_metagraph_info(self, block: int):
19241926
"""Retrieves metagraph information for a specific subnet and applies it using a mixin."""
1925-
metagraph_info = self.subtensor.get_metagraph_info(self.netuid)
1927+
metagraph_info = self.subtensor.get_metagraph_info(self.netuid, block=block)
19261928
if metagraph_info:
19271929
self._apply_metagraph_info_mixin(metagraph_info=metagraph_info)
19281930

tests/e2e_tests/test_incentive.py

Lines changed: 15 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22

33
import pytest
44

5-
from bittensor import Balance
6-
75
from tests.e2e_tests.utils.chain_interactions import (
86
sudo_set_hyperparameter_values,
97
wait_epoch,
10-
sudo_set_admin_utils,
118
)
129

1310

1411
@pytest.mark.asyncio
15-
@pytest.mark.parametrize("local_chain", [False], indirect=True)
1612
async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wallet):
1713
"""
1814
Test the incentive mechanism and interaction of miners/validators
@@ -35,21 +31,6 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
3531
# Verify subnet <netuid> created successfully
3632
assert subtensor.subnet_exists(netuid), "Subnet wasn't created successfully"
3733

38-
# Change tempo to 10
39-
tempo_set = 10
40-
assert (
41-
sudo_set_admin_utils(
42-
local_chain,
43-
alice_wallet,
44-
call_function="sudo_set_tempo",
45-
call_params={"netuid": netuid, "tempo": tempo_set},
46-
return_error_message=True,
47-
)[0]
48-
is True
49-
)
50-
tempo = subtensor.get_subnet_hyperparameters(netuid=netuid).tempo
51-
assert tempo_set == tempo
52-
5334
# Register Bob as a neuron on the subnet
5435
assert subtensor.burned_register(
5536
bob_wallet, netuid
@@ -60,27 +41,9 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
6041
len(subtensor.neurons(netuid=netuid)) == 2
6142
), "Alice & Bob not registered in the subnet"
6243

63-
# Add stake for Alice
64-
assert subtensor.add_stake(
65-
alice_wallet,
66-
netuid=netuid,
67-
amount=Balance.from_tao(1_000),
68-
wait_for_inclusion=True,
69-
wait_for_finalization=True,
70-
), "Failed to add stake for Alice"
71-
7244
# Wait for the first epoch to pass
7345
await wait_epoch(subtensor, netuid)
7446

75-
# Add further stake so validator permit is activated
76-
assert subtensor.add_stake(
77-
alice_wallet,
78-
netuid=netuid,
79-
amount=Balance.from_tao(1_000),
80-
wait_for_inclusion=True,
81-
wait_for_finalization=True,
82-
), "Failed to add stake for Alice"
83-
8447
# Get latest metagraph
8548
metagraph = subtensor.metagraph(netuid)
8649

@@ -91,6 +54,9 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
9154
assert alice_neuron.dividends == 0
9255
assert alice_neuron.stake.tao > 0
9356
assert alice_neuron.validator_trust == 0
57+
assert alice_neuron.incentive == 0
58+
assert alice_neuron.consensus == 0
59+
assert alice_neuron.rank == 0
9460

9561
bob_neuron = metagraph.neurons[1]
9662

@@ -109,12 +75,13 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
10975
)
11076

11177
async with templates.miner(bob_wallet, netuid):
112-
async with templates.validator(alice_wallet, netuid):
113-
# Wait for the Validator to process and set_weights
114-
await asyncio.sleep(5)
78+
async with templates.validator(alice_wallet, netuid) as validator:
79+
# wait for the Validator to process and set_weights
80+
async with asyncio.timeout(15):
81+
await validator.set_weights.wait()
11582

11683
# Wait few epochs
117-
await wait_epoch(subtensor, netuid, times=2)
84+
await wait_epoch(subtensor, netuid, times=4)
11885

11986
# Refresh metagraph
12087
metagraph = subtensor.metagraph(netuid)
@@ -125,12 +92,15 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
12592
assert alice_neuron.validator_permit is True
12693
assert alice_neuron.dividends == 1.0
12794
assert alice_neuron.stake.tao > 0
128-
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
12999

130100
bob_neuron = metagraph.neurons[1]
131-
assert bob_neuron.incentive == 1
132-
assert bob_neuron.consensus == 1
133-
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
134104
assert bob_neuron.trust == 1
135105

136106
print("✅ Passed test_incentive")

0 commit comments

Comments
 (0)