Skip to content

Commit

Permalink
add Global Weight to metagraph + test (#2371)
Browse files Browse the repository at this point in the history
* add Global Weight to metagraph + test

* ruff
  • Loading branch information
roman-opentensor authored Oct 25, 2024
1 parent d7778a0 commit 99d206a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
17 changes: 17 additions & 0 deletions bittensor/core/metagraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class MetagraphMixin(ABC):
local_stake: Union["torch.nn.Parameter", NDArray]
global_stake: Union["torch.nn.Parameter", NDArray]
stake_weights: Union["torch.nn.Parameter", NDArray]
global_weight: Union["torch.nn.Parameter", NDArray]
axons: list[AxonInfo]

@property
Expand Down Expand Up @@ -251,6 +252,7 @@ def SW(self) -> list[float]:
def S(self) -> float:
"""
Represents the value between 0.0 and 1.0. This gives the users do blacklists in terms of stake values.
Returns:
float: The value between 0.0 and 1.0 or None if stake_weights doesn't have zero index value.
"""
Expand All @@ -261,6 +263,16 @@ def S(self) -> float:
value = None
return value

@property
def GW(self) -> float:
"""
Represents Global Weights of subnet across all subnets.
Returns:
float: The value of Global Weights.
"""
return self.global_weight

@property
def R(self) -> Union[NDArray, "torch.nn.Parameter"]:
"""
Expand Down Expand Up @@ -614,6 +626,9 @@ def sync(
if not lite:
self._set_weights_and_bonds(subtensor=subtensor)

# Get global weight for netuid
self.global_weight = subtensor.get_global_weight(netuid=self.netuid)

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

Expand Down Expand Up @@ -1055,6 +1070,7 @@ def __init__(
self.local_stake: list[Balance] = []
self.global_stake: list[Balance] = []
self.stake_weights: list[float] = []
self.global_weight: Optional[float] = None
self.axons: list[AxonInfo] = []
if sync:
self.sync(block=None, lite=lite)
Expand Down Expand Up @@ -1237,6 +1253,7 @@ def __init__(
self.local_stake: list[Balance] = []
self.global_stake: list[Balance] = []
self.stake_weights: list[float] = []
self.global_weight: Optional[float] = None
self.axons: list[AxonInfo] = []
if sync:
self.sync(block=None, lite=lite)
Expand Down
4 changes: 2 additions & 2 deletions bittensor/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def turn_console_on():
MINERS_DIR = USER_BITTENSOR_DIR / "miners"

# Bittensor networks name
NETWORKS = ["local", "finney", "test", "archive"]
NETWORKS = ["local", "finney", "test", "archive", "rao"]

# Bittensor endpoints (Needs to use wss://)
FINNEY_ENTRYPOINT = "wss://entrypoint-finney.opentensor.ai:443"
Expand All @@ -71,7 +71,7 @@ def turn_console_on():
LOCAL_ENTRYPOINT = os.getenv("BT_SUBTENSOR_CHAIN_ENDPOINT") or "ws://127.0.0.1:9946"

DEFAULT_ENDPOINT = RAO_ENDPOINT
DEFAULT_NETWORK = NETWORKS[1]
DEFAULT_NETWORK = NETWORKS[4]

# Create dirs if they don't exist
WALLETS_DIR.mkdir(parents=True, exist_ok=True)
Expand Down
23 changes: 22 additions & 1 deletion bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@
transfer_extrinsic,
)
from bittensor.core.metagraph import Metagraph
from bittensor.utils import networking, torch, ss58_to_vec_u8, u16_normalized_float
from bittensor.utils import (
networking,
torch,
ss58_to_vec_u8,
u16_normalized_float,
u64_normalized_float,
)
from bittensor.utils.balance import Balance
from bittensor.utils.btlogging import logging
from bittensor.utils.registration import legacy_torch_api_compat
Expand Down Expand Up @@ -2053,6 +2059,21 @@ def make_substrate_call_with_retry(encoded_hotkey_: list[int]):

return DelegateInfo.from_vec_u8(result)

def get_global_weight(
self, netuid: int, block: Optional[str] = None
) -> Optional[float]:
"""Returns the subnet Global Weight across all subnets."""
result = self.substrate.query(
module="SubtensorModule",
storage_function="GlobalWeight",
params=[netuid],
block_hash=None if block is None else self.substrate.get_block_hash(block),
)
if hasattr(result, "value"):
return u64_normalized_float(result.value)

return None

# Subnet 27 uses this method
_do_serve_prometheus = do_serve_prometheus
# Subnet 27 uses this method name
Expand Down
10 changes: 6 additions & 4 deletions tests/integration_tests/test_subtensor_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,10 @@ def test_network_overrides(self):
"""Tests that the network overrides the chain_endpoint."""
# Argument importance: chain_endpoint (arg) > network (arg) > config.subtensor.chain_endpoint > config.subtensor.network
config0 = bittensor.Subtensor.config()
config0.subtensor.network = "finney"
config0.subtensor.chain_endpoint = "wss://finney.subtensor.io" # Should not match bittensor.core.settings.FINNEY_ENTRYPOINT
config0.subtensor.network = settings.DEFAULT_NETWORK
config0.subtensor.chain_endpoint = (
settings.RAO_ENDPOINT
) # Should not match bittensor.core.settings.FINNEY_ENTRYPOINT
assert config0.subtensor.chain_endpoint != settings.FINNEY_ENTRYPOINT

config1 = bittensor.Subtensor.config()
Expand Down Expand Up @@ -246,8 +248,8 @@ def test_get_balance(self):

def test_defaults_to_finney(self):
sub = bittensor.Subtensor()
assert sub.network == "finney"
assert sub.chain_endpoint == settings.FINNEY_ENTRYPOINT
assert sub.network == settings.DEFAULT_NETWORK
assert sub.chain_endpoint == settings.DEFAULT_ENDPOINT

def test_registration_multiprocessed_already_registered(self):
work_blocks_before_is_registered = random.randint(5, 10)
Expand Down

0 comments on commit 99d206a

Please sign in to comment.