Skip to content
Open
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
57 changes: 53 additions & 4 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1395,8 +1395,25 @@ async def get_all_commitments(
A mapping of the ss58:commitment with the commitment as a string.

Example:
Retrieve and process commitment data for all neurons in a subnet::

# TODO add example of how to handle realistic commitment data
from bittensor import AsyncSubtensor

async with AsyncSubtensor(network="finney") as subtensor:
netuid = 1 # Target subnet

# Get all commitments for the subnet
commitments = await subtensor.get_all_commitments(netuid=netuid)

# Process the commitment data
for hotkey_ss58, commitment_data in commitments.items():
if commitment_data:
print(f"Hotkey: {hotkey_ss58[:16]}...")
print(f"Commitment: {commitment_data}")

# Check how many neurons have active commitments
active_commitments = {k: v for k, v in commitments.items() if v}
print(f"Active commitments: {len(active_commitments)}/{len(commitments)}")
"""
query = await self.query_map(
module="Commitments",
Expand Down Expand Up @@ -1955,10 +1972,24 @@ async def get_commitment(
reuse_block: Whether to reuse the last-used block hash. Do not set if using `block_hash` or `block`.

Returns:
The commitment data as a string.
The commitment data as a string, or empty string if no commitment exists or decoding fails.

Example:
Retrieve commitment data for a specific neuron::

from bittensor import AsyncSubtensor

async with AsyncSubtensor(network="finney") as subtensor:
netuid = 1 # Target subnet
uid = 0 # Neuron UID

# TODO: add a real example of how to handle realistic commitment data, or chop example
# Get the commitment for this neuron
commitment = await subtensor.get_commitment(netuid=netuid, uid=uid)

if commitment:
print(f"Neuron {uid} has commitment: {commitment}")
else:
print(f"Neuron {uid} has no active commitment")

Notes:
- <https://docs.learnbittensor.org/glossary#commit-reveal>
Expand Down Expand Up @@ -1992,7 +2023,6 @@ async def get_commitment_metadata(
block_hash: Optional[str] = None,
reuse_block: bool = False,
) -> Union[str, dict]:
# TODO: how to handle return data? need good example @roman
"""Fetches raw commitment metadata from specific subnet for given hotkey.

Parameters:
Expand All @@ -2006,6 +2036,25 @@ async def get_commitment_metadata(
The raw commitment metadata. Returns a dict when commitment data exists,
or an empty string when no commitment is found for the given hotkey on the subnet.

Example:
Fetch and handle raw commitment metadata::

from bittensor import AsyncSubtensor

async with AsyncSubtensor(network="finney") as subtensor:
netuid = 1
hotkey_ss58 = "5D..." # Replace with actual hotkey

metadata = await subtensor.get_commitment_metadata(netuid, hotkey_ss58)

# Handle the return type
if isinstance(metadata, dict) and metadata:
# Commitment exists - metadata contains raw blockchain data
print(f"Raw metadata: {metadata}")
else:
# No commitment found for this hotkey
print("No commitment found")

Notes:
- <https://docs.learnbittensor.org/glossary#commit-reveal>
"""
Expand Down
57 changes: 53 additions & 4 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1101,8 +1101,25 @@ def get_all_commitments(
A mapping of the ss58:commitment with the commitment as a string.

Example:
Retrieve and process commitment data for all neurons in a subnet::

# TODO add example of how to handle realistic commitment data
from bittensor import Subtensor

subtensor = Subtensor(network="finney")
netuid = 1 # Target subnet

# Get all commitments for the subnet
commitments = subtensor.get_all_commitments(netuid=netuid)

# Process the commitment data
for hotkey_ss58, commitment_data in commitments.items():
if commitment_data:
print(f"Hotkey: {hotkey_ss58[:16]}...")
print(f"Commitment: {commitment_data}")

# Check how many neurons have active commitments
active_commitments = {k: v for k, v in commitments.items() if v}
print(f"Active commitments: {len(active_commitments)}/{len(commitments)}")
"""
query = self.query_map(
module="Commitments",
Expand Down Expand Up @@ -1567,10 +1584,24 @@ def get_commitment(self, netuid: int, uid: int, block: Optional[int] = None) ->
block: The block number to query. If `None`, queries the current chain head.

Returns:
The commitment data as a string.
The commitment data as a string, or empty string if no commitment exists or decoding fails.

Example:
Retrieve commitment data for a specific neuron::

from bittensor import Subtensor

subtensor = Subtensor(network="finney")
netuid = 1 # Target subnet
uid = 0 # Neuron UID

# TODO: add a real example of how to handle realistic commitment data, or chop example
# Get the commitment for this neuron
commitment = subtensor.get_commitment(netuid=netuid, uid=uid)

if commitment:
print(f"Neuron {uid} has commitment: {commitment}")
else:
print(f"Neuron {uid} has no active commitment")

Notes:
- <https://docs.learnbittensor.org/glossary#commit-reveal>
Expand All @@ -1594,7 +1625,6 @@ def get_commitment(self, netuid: int, uid: int, block: Optional[int] = None) ->
def get_commitment_metadata(
self, netuid: int, hotkey_ss58: str, block: Optional[int] = None
) -> Union[str, dict]:
# TODO: how to handle return data? need good example @roman
"""Fetches raw commitment metadata from specific subnet for given hotkey.

Parameters:
Expand All @@ -1606,6 +1636,25 @@ def get_commitment_metadata(
The raw commitment metadata. Returns a dict when commitment data exists,
or an empty string when no commitment is found for the given hotkey on the subnet.

Example:
Fetch and handle raw commitment metadata::

from bittensor import Subtensor

subtensor = Subtensor(network="finney")
netuid = 1
hotkey_ss58 = "5D..." # Replace with actual hotkey

metadata = subtensor.get_commitment_metadata(netuid, hotkey_ss58)

# Handle the return type
if isinstance(metadata, dict) and metadata:
# Commitment exists - metadata contains raw blockchain data
print(f"Raw metadata: {metadata}")
else:
# No commitment found for this hotkey
print("No commitment found")

Notes:
- <https://docs.learnbittensor.org/glossary#commit-reveal>
"""
Expand Down