Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,8 @@ async def get_commitment(
)
try:
return decode_metadata(metadata)
except TypeError:
except Exception as error:
logging.error(error)
return ""

async def get_last_commitment_bonds_reset_block(
Expand Down Expand Up @@ -1609,7 +1610,12 @@ async def get_all_commitments(
)
result = {}
async for id_, value in query:
result[decode_account_id(id_[0])] = decode_metadata(value.value)
try:
result[decode_account_id(id_[0])] = decode_metadata(value)
except Exception as error:
logging.error(
f"Error decoding [red]{id_}[/red] and [red]{value}[/red]: {error}"
)
return result

async def get_revealed_commitment_by_hotkey(
Expand Down
27 changes: 25 additions & 2 deletions bittensor/core/chain_data/metagraph_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from dataclasses import dataclass
from typing import Optional, Union

from bittensor.core import settings
from bittensor.core.chain_data.axon_info import AxonInfo
from bittensor.core.chain_data.chain_identity import ChainIdentity
Expand All @@ -13,6 +12,26 @@
from bittensor.utils.balance import Balance, fixed_to_float


SELECTIVE_METAGRAPH_COMMITMENTS_OFFSET = 14


def get_selective_metagraph_commitments(
decoded: dict,
) -> Optional[tuple[tuple[str, str]]]:
"""Returns a tuple of hotkeys and commitments from decoded chain data if provided, else None."""
if commitments := decoded.get("commitments"):
result = []
for commitment in commitments:
account_id_bytes, commitment_bytes = commitment
hotkey = decode_account_id(account_id_bytes)
commitment = bytes(
commitment_bytes[SELECTIVE_METAGRAPH_COMMITMENTS_OFFSET:]
).decode("utf-8", errors="ignore")
result.append((hotkey, commitment))
return tuple(result)
return None


# to balance with unit (shortcut)
def _tbwu(val: Optional[int], netuid: Optional[int] = 0) -> Optional[Balance]:
"""Returns a Balance object from a value and unit."""
Expand Down Expand Up @@ -147,7 +166,9 @@ class MetagraphInfo(InfoBase):
] # List of dividend payout in alpha via subnet.

# List of validators
validators: list[str]
validators: Optional[list[str]]

commitments: Optional[tuple[tuple[str, str]]]

@classmethod
def _from_dict(cls, decoded: dict) -> "MetagraphInfo":
Expand Down Expand Up @@ -375,6 +396,7 @@ def _from_dict(cls, decoded: dict) -> "MetagraphInfo":
validators=[v for v in decoded["validators"]]
if decoded.get("validators") is not None
else None,
commitments=get_selective_metagraph_commitments(decoded),
)


Expand Down Expand Up @@ -508,6 +530,7 @@ class SelectiveMetagraphIndex(Enum):
TaoDividendsPerHotkey = 70
AlphaDividendsPerHotkey = 71
Validators = 72
Commitments = 73

@staticmethod
def all_indices() -> list[int]:
Expand Down
6 changes: 3 additions & 3 deletions bittensor/core/chain_data/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ def process_stake_data(stake_data: list) -> dict:

def decode_metadata(metadata: dict) -> str:
commitment = metadata["info"]["fields"][0][0]
bytes_tuple_ = commitment[next(iter(commitment.keys()))]
bytes_tuple = bytes_tuple_[0] if len(bytes_tuple_) > 0 else bytes_tuple_
return bytes(bytes_tuple).decode()
raw_bytes = next(iter(commitment.values()))
byte_tuple = raw_bytes[0] if raw_bytes else raw_bytes
return bytes(byte_tuple).decode("utf-8", errors="ignore")


def decode_block(data: bytes) -> int:
Expand Down
11 changes: 8 additions & 3 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -956,8 +956,8 @@ def get_commitment(self, netuid: int, uid: int, block: Optional[int] = None) ->
metadata = cast(dict, get_metadata(self, netuid, hotkey, block))
try:
return decode_metadata(metadata)

except TypeError:
except Exception as error:
logging.error(error)
return ""

def get_last_commitment_bonds_reset_block(
Expand Down Expand Up @@ -998,7 +998,12 @@ def get_all_commitments(
)
result = {}
for id_, value in query:
result[decode_account_id(id_[0])] = decode_metadata(value)
try:
result[decode_account_id(id_[0])] = decode_metadata(value)
except Exception as error:
logging.error(
f"Error decoding [red]{id_}[/red] and [red]{value}[/red]: {error}"
)
return result

def get_revealed_commitment_by_hotkey(
Expand Down
28 changes: 17 additions & 11 deletions tests/e2e_tests/test_incentive.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


@pytest.mark.asyncio
async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wallet):
async def test_incentive(subtensor, templates, alice_wallet, bob_wallet):
"""
Test the incentive mechanism and interaction of miners/validators

Expand All @@ -23,21 +23,20 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
Raises:
AssertionError: If any of the checks or verifications fail
"""
logging.console.info("Testing [blue]test_incentive[/blue]")
alice_subnet_netuid = subtensor.subnets.get_total_subnets() # 2

# turn off admin freeze window limit for testing
assert (
sudo_set_admin_utils(
local_chain,
alice_wallet,
substrate=subtensor.substrate,
wallet=alice_wallet,
call_function="sudo_set_admin_freeze_window",
call_params={"window": 0},
)[0]
is True
), "Failed to set admin freeze window to 0"

print("Testing test_incentive")
alice_subnet_netuid = subtensor.get_total_subnets() # 2

# Register root as Alice - the subnet owner and validator
assert subtensor.register_subnet(alice_wallet, True, True), "Subnet wasn't created"

Expand All @@ -48,8 +47,8 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa

# Disable commit_reveal on the subnet to check proper behavior
status, error = sudo_set_admin_utils(
local_chain,
alice_wallet,
substrate=subtensor.substrate,
wallet=alice_wallet,
call_function="sudo_set_commit_reveal_weights_enabled",
call_params={
"netuid": alice_subnet_netuid,
Expand All @@ -71,7 +70,14 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
)

# Wait for the first epoch to pass
await wait_epoch(subtensor, alice_subnet_netuid)
next_epoch_start_block = subtensor.subnets.get_next_epoch_start_block(
netuid=alice_subnet_netuid
)
subtensor.wait_for_block(next_epoch_start_block + 1)

while subtensor.neurons(netuid=alice_subnet_netuid)[0].validator_permit is False:
await asyncio.sleep(0.25)
print(f">>> block {subtensor.block}")

# Get current miner/validator stats
alice_neuron = subtensor.neurons(netuid=alice_subnet_netuid)[0]
Expand All @@ -93,8 +99,8 @@ async def test_incentive(local_chain, subtensor, templates, alice_wallet, bob_wa
# update weights_set_rate_limit for fast-blocks
tempo = subtensor.tempo(alice_subnet_netuid)
status, error = sudo_set_admin_utils(
local_chain,
alice_wallet,
substrate=subtensor.substrate,
wallet=alice_wallet,
call_function="sudo_set_weights_set_rate_limit",
call_params={
"netuid": alice_subnet_netuid,
Expand Down
4 changes: 4 additions & 0 deletions tests/e2e_tests/test_metagraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ def test_metagraph_info(subtensor, alice_wallet, bob_wallet):
("5C4hrfjw9DjXZTzV3MwzrrAr9P1MJhSrvWGWqi1eSuyUpnhM", Balance(0).set_unit(1))
],
validators=None,
commitments=None,
)

assert metagraph_info == expected_metagraph_info
Expand Down Expand Up @@ -371,6 +372,7 @@ def test_metagraph_info(subtensor, alice_wallet, bob_wallet):
tao_dividends_per_hotkey=[],
alpha_dividends_per_hotkey=[],
validators=None,
commitments=None,
),
metagraph_info,
]
Expand Down Expand Up @@ -552,6 +554,7 @@ def test_metagraph_info_with_indexes(subtensor, alice_wallet, bob_wallet):
tao_dividends_per_hotkey=None,
alpha_dividends_per_hotkey=None,
validators=None,
commitments=None,
)

assert wait_to_start_call(subtensor, alice_wallet, alice_subnet_netuid)
Expand Down Expand Up @@ -670,6 +673,7 @@ def test_metagraph_info_with_indexes(subtensor, alice_wallet, bob_wallet):
tao_dividends_per_hotkey=None,
alpha_dividends_per_hotkey=None,
validators=None,
commitments=None,
)


Expand Down
21 changes: 17 additions & 4 deletions tests/e2e_tests/test_subtensor_functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import asyncio
import time

import pytest

from bittensor.core.extrinsics.utils import get_extrinsic_fee
from bittensor.utils.balance import Balance
from tests.e2e_tests.utils.chain_interactions import (
wait_epoch,
Expand Down Expand Up @@ -67,6 +68,17 @@ async def test_subtensor_extrinsics(subtensor, templates, alice_wallet, bob_wall
"Unable to register the subnet"
)

# TODO: in SDKv10 replace this logic with using `ExtrinsicResponse.extrinsic_fee`
call = subtensor.substrate.compose_call(
call_module="SubtensorModule",
call_function="register_network",
call_params={
"hotkey": alice_wallet.hotkey.ss58_address,
"mechid": 1,
},
)
register_fee = get_extrinsic_fee(call, alice_wallet.hotkey, subtensor)

# Subnet burn cost is increased immediately after a subnet is registered
post_subnet_creation_cost = subtensor.get_subnet_burn_cost()

Expand All @@ -77,9 +89,10 @@ async def test_subtensor_extrinsics(subtensor, templates, alice_wallet, bob_wall

# Assert amount is deducted once a subnetwork is registered by Alice
alice_balance_post_sn = subtensor.get_balance(alice_wallet.coldkeypub.ss58_address)
assert alice_balance_post_sn + pre_subnet_creation_cost == initial_alice_balance, (
"Balance is the same even after registering a subnet"
)
assert (
alice_balance_post_sn + pre_subnet_creation_cost + register_fee
== initial_alice_balance
), "Balance is the same even after registering a subnet"

# Subnet 2 is added after registration
assert subtensor.get_subnets() == [0, 1, 2]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3174,7 +3174,7 @@ async def test_get_metagraph_info_all_fields(subtensor, mocker):

# Call
result = await subtensor.get_metagraph_info(
netuid=netuid, field_indices=[f for f in range(73)]
netuid=netuid, field_indices=[f for f in range(len(SelectiveMetagraphIndex))]
)

# Asserts
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/test_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3363,7 +3363,7 @@ def test_get_metagraph_info_all_fields(subtensor, mocker):

# Call
result = subtensor.get_metagraph_info(
netuid=netuid, field_indices=[f for f in range(73)]
netuid=netuid, field_indices=[f for f in range(len(SelectiveMetagraphIndex))]
)

# Asserts
Expand Down
Loading