Skip to content

Era param for all extrinsics #2784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 12 commits into from
Closed
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
20 changes: 17 additions & 3 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@
set_weights_extrinsic,
reveal_weights_extrinsic,
)
from bittensor.core.extrinsics.options import (
DEFAULT_SET_WEIGHTS_EXTRINSIC_ERA,
ExtrinsicEra,
ExtrinsicEraTypes,
)
from bittensor.core.metagraph import AsyncMetagraph
from bittensor.core.settings import version_as_int, TYPE_REGISTRY
from bittensor.core.types import ParamWithTypes, SubtensorMixin
Expand Down Expand Up @@ -3423,6 +3428,7 @@ async def commit_weights(
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
max_retries: int = 5,
era: Optional[ExtrinsicEraTypes] = None,
) -> tuple[bool, str]:
"""
Commits a hash of the neuron's weights to the Bittensor blockchain using the provided wallet.
Expand All @@ -3440,6 +3446,7 @@ async def commit_weights(
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain. Default is
``False``.
max_retries (int): The number of maximum attempts to commit weights. Default is ``5``.
era: Blocks for which the transaction should be valid.

Returns:
tuple[bool, str]: ``True`` if the weight commitment is successful, False otherwise. And `msg`, a string
Expand All @@ -3452,6 +3459,9 @@ async def commit_weights(
success = False
message = "No attempt made. Perhaps it is too soon to commit weights!"

if isinstance(era, dict):
era = ExtrinsicEra(**era)

logging.info(
f"Committing weights with params: netuid={netuid}, uids={uids}, weights={weights}, "
f"version_key={version_key}"
Expand All @@ -3476,6 +3486,7 @@ async def commit_weights(
commit_hash=commit_hash,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
era=era,
)
if success:
break
Expand Down Expand Up @@ -3934,8 +3945,8 @@ async def set_weights(
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
max_retries: int = 5,
era: Optional[ExtrinsicEraTypes] = DEFAULT_SET_WEIGHTS_EXTRINSIC_ERA,
block_time: float = 12.0,
period: int = 5,
):
"""
Sets the inter-neuronal weights for the specified neuron. This process involves specifying the influence or
Expand All @@ -3956,7 +3967,6 @@ async def set_weights(
``False``.
max_retries (int): The number of maximum attempts to set weights. Default is ``5``.
block_time (float): The amount of seconds for block duration. Default is 12.0 seconds.
period (int, optional): The period in seconds to wait for extrinsic inclusion or finalization. Defaults to 5.

Returns:
tuple[bool, str]: ``True`` if the setting of weights is successful, False otherwise. And `msg`, a string
Expand All @@ -3966,6 +3976,9 @@ async def set_weights(
contribution are influenced by the weights it sets towards others【81†source】.
"""

if isinstance(era, dict):
era = ExtrinsicEra(**era)

async def _blocks_weight_limit() -> bool:
bslu, wrl = await asyncio.gather(
self.blocks_since_last_update(netuid, uid),
Expand Down Expand Up @@ -4005,6 +4018,7 @@ async def _blocks_weight_limit() -> bool:
version_key=version_key,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
era=era if era is not DEFAULT_SET_WEIGHTS_EXTRINSIC_ERA else None,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for backward compatibility: commit_reveal_v3_extrinsic has default era = None

block_time=block_time,
)
retries += 1
Expand All @@ -4031,7 +4045,7 @@ async def _blocks_weight_limit() -> bool:
version_key=version_key,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
period=period,
era=era,
)
except Exception as e:
logging.error(f"Error setting weights: {e}")
Expand Down
23 changes: 17 additions & 6 deletions bittensor/core/extrinsics/asyncex/weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import numpy as np
from numpy.typing import NDArray

from bittensor.core.extrinsics.options import ExtrinsicEra
import bittensor.utils.weight_utils as weight_utils
from bittensor.core.settings import version_as_int
from bittensor.utils import format_error_message
Expand All @@ -23,6 +24,7 @@ async def _do_commit_weights(
commit_hash: str,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
era: Optional[ExtrinsicEra] = None,
) -> tuple[bool, Optional[str]]:
"""
Internal method to send a transaction to the Bittensor blockchain, committing the hash of a neuron's weights.
Expand All @@ -36,6 +38,7 @@ async def _do_commit_weights(
commit_hash (str): The hash of the neuron's weights to be committed.
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
era (ExtrinsicEra, optional): Blocks for which the transaction should be valid.

Returns:
tuple[bool, Optional[str]]: A tuple containing a success flag and an optional error message.
Expand All @@ -59,6 +62,7 @@ async def _do_commit_weights(
use_nonce=True,
nonce_key="hotkey",
sign_with="hotkey",
period=era.period if era else None,
)


Expand All @@ -69,6 +73,7 @@ async def commit_weights_extrinsic(
commit_hash: str,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
era: Optional[ExtrinsicEra] = None,
) -> tuple[bool, str]:
"""
Commits a hash of the neuron's weights to the Bittensor blockchain using the provided wallet.
Expand All @@ -82,6 +87,7 @@ async def commit_weights_extrinsic(
commit_hash (str): The hash of the neuron's weights to be committed.
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
era (ExtrinsicEra, optional): Blocks for which the transaction should be valid.

Returns:
tuple[bool, str]: ``True`` if the weight commitment is successful, False otherwise. And `msg`, a string
Expand All @@ -98,6 +104,7 @@ async def commit_weights_extrinsic(
commit_hash=commit_hash,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
era=era,
)

if success:
Expand Down Expand Up @@ -231,7 +238,9 @@ async def _do_set_weights(
version_key: int = version_as_int,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
period: int = 5,
era: ExtrinsicEra = ExtrinsicEra(
period=5,
),
) -> tuple[bool, Optional[str]]: # (success, error_message)
"""
Internal method to send a transaction to the Bittensor blockchain, setting weights
Expand All @@ -247,7 +256,7 @@ async def _do_set_weights(
version_key (int, optional): Version key for compatibility with the network.
wait_for_inclusion (bool, optional): Waits for the transaction to be included in a block.
wait_for_finalization (bool, optional): Waits for the transaction to be finalized on the blockchain.
period (int, optional): The period in seconds to wait for extrinsic inclusion or finalization. Defaults to 5.
era (ExtrinsicEra, optional): The period in blocks to wait for extrinsic inclusion or finalization. Defaults to 5.

Returns:
Tuple[bool, Optional[str]]: A tuple containing a success flag and an optional error message.
Expand All @@ -271,7 +280,7 @@ async def _do_set_weights(
wallet,
wait_for_inclusion,
wait_for_finalization,
period=period,
period=era.period if era else None,
use_nonce=True,
nonce_key="hotkey",
sign_with="hotkey",
Expand All @@ -287,7 +296,9 @@ async def set_weights_extrinsic(
version_key: int = 0,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
period: int = 5,
era: ExtrinsicEra = ExtrinsicEra(
period=5,
),
) -> tuple[bool, str]:
"""Sets the given weights and values on chain for wallet hotkey account.

Expand All @@ -303,7 +314,7 @@ async def set_weights_extrinsic(
returns ``False`` if the extrinsic fails to enter the block within the timeout.
wait_for_finalization (bool): If set, waits for the extrinsic to be finalized on the chain before returning
``True``, or returns ``False`` if the extrinsic fails to be finalized within the timeout.
period (int, optional): The period in seconds to wait for extrinsic inclusion or finalization. Defaults to 5.
era (ExtrinsicEra, optional): Blocks for which the transaction should be valid.

Returns:
success (bool): Flag is ``True`` if extrinsic was finalized or included in the block. If we did not wait for
Expand Down Expand Up @@ -333,7 +344,7 @@ async def set_weights_extrinsic(
version_key=version_key,
wait_for_finalization=wait_for_finalization,
wait_for_inclusion=wait_for_inclusion,
period=period,
era=era,
)

if not wait_for_finalization and not wait_for_inclusion:
Expand Down
11 changes: 10 additions & 1 deletion bittensor/core/extrinsics/commit_reveal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
from numpy.typing import NDArray

from bittensor.core.extrinsics.options import ExtrinsicEra
from bittensor.core.settings import version_as_int
from bittensor.utils.btlogging import logging
from bittensor.utils.weight_utils import convert_weights_and_uids_for_emit
Expand All @@ -24,6 +25,7 @@ def _do_commit_reveal_v3(
reveal_round: int,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
era: Optional[ExtrinsicEra] = None,
) -> tuple[bool, Optional[str]]:
"""
Executes the commit-reveal phase 3 for a given netuid and commit, and optionally waits for extrinsic inclusion or
Expand Down Expand Up @@ -57,7 +59,12 @@ def _do_commit_reveal_v3(
},
)
return subtensor.sign_and_send_extrinsic(
call, wallet, wait_for_inclusion, wait_for_finalization, sign_with="hotkey"
call,
wallet,
wait_for_inclusion,
wait_for_finalization,
period=era.period if era else None,
sign_with="hotkey",
)


Expand All @@ -70,6 +77,7 @@ def commit_reveal_v3_extrinsic(
version_key: int = version_as_int,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
era: Optional[ExtrinsicEra] = None,
block_time: float = 12.0,
) -> tuple[bool, str]:
"""
Expand Down Expand Up @@ -127,6 +135,7 @@ def commit_reveal_v3_extrinsic(
reveal_round=reveal_round,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
era=era,
)

if success is not True:
Expand Down
7 changes: 7 additions & 0 deletions bittensor/core/extrinsics/commit_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from typing import TYPE_CHECKING, Optional

from bittensor.core.extrinsics.options import ExtrinsicEra
from bittensor.utils import format_error_message
from bittensor.utils.btlogging import logging

Expand All @@ -17,6 +18,7 @@ def _do_commit_weights(
commit_hash: str,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
era: Optional[ExtrinsicEra] = None,
) -> tuple[bool, Optional[str]]:
"""
Internal method to send a transaction to the Bittensor blockchain, committing the hash of a neuron's weights.
Expand All @@ -29,6 +31,7 @@ def _do_commit_weights(
commit_hash (str): The hash of the neuron's weights to be committed.
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
era (ExtrinsicEra, optional): Blocks for which the transaction should be valid.

Returns:
tuple[bool, Optional[str]]: A tuple containing a success flag and an optional error message.
Expand All @@ -52,6 +55,7 @@ def _do_commit_weights(
use_nonce=True,
sign_with="hotkey",
nonce_key="hotkey",
period=era.period if era else None,
)


Expand All @@ -62,6 +66,7 @@ def commit_weights_extrinsic(
commit_hash: str,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
era: Optional[ExtrinsicEra] = None,
) -> tuple[bool, str]:
"""
Commits a hash of the neuron's weights to the Bittensor blockchain using the provided wallet.
Expand All @@ -74,6 +79,7 @@ def commit_weights_extrinsic(
commit_hash (str): The hash of the neuron's weights to be committed.
wait_for_inclusion (bool): Waits for the transaction to be included in a block.
wait_for_finalization (bool): Waits for the transaction to be finalized on the blockchain.
era (ExtrinsicEra, optional): Blocks for which the transaction should be valid.

Returns:
tuple[bool, str]: ``True`` if the weight commitment is successful, False otherwise. And `msg`, a string
Expand All @@ -90,6 +96,7 @@ def commit_weights_extrinsic(
commit_hash=commit_hash,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
era=era,
)

if success:
Expand Down
25 changes: 25 additions & 0 deletions bittensor/core/extrinsics/options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import dataclasses
from typing import Union


@dataclasses.dataclass(frozen=True)
class ExtrinsicEra:
"""
Defines blocks for which the transaction should be valid.

Attributes:
period (int): Length (in blocks) for which the transaction should be valid.
"""

period: int


ExtrinsicEraTypes = Union[
dict[str, int],
ExtrinsicEra,
]


DEFAULT_SET_WEIGHTS_EXTRINSIC_ERA = ExtrinsicEra(
period=5,
)
Loading