Skip to content

Commit f6c86d4

Browse files
authored
Merge pull request #2738 from opentensor/fix/thewhaleking/not-querying-metagraph-at-block
All metagraph subtensor methods now use block
2 parents 1fffe26 + b7ff756 commit f6c86d4

File tree

1 file changed

+23
-21
lines changed

1 file changed

+23
-21
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

0 commit comments

Comments
 (0)