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
51 changes: 30 additions & 21 deletions bittensor/core/async_subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
is_valid_ss58_address,
u16_normalized_float,
u64_normalized_float,
validate_max_attempts,
)
from bittensor.utils.balance import (
Balance,
Expand Down Expand Up @@ -5098,7 +5099,7 @@ async def commit_weights(
weights: Weights,
mechid: int = 0,
version_key: int = version_as_int,
max_retries: int = 5,
max_attempts: int = 5,
period: Optional[int] = 16,
raise_error: bool = True,
wait_for_inclusion: bool = False,
Expand All @@ -5116,7 +5117,7 @@ async def commit_weights(
weights: NumPy array of weight values corresponding to each UID.
mechid: The subnet mechanism unique identifier.
version_key: Version key for compatibility with the network.
max_retries: The number of maximum attempts to commit weights.
max_attempts: The number of maximum attempts to commit weights.
period: The number of blocks during which the transaction will remain valid after it's submitted. If
the transaction is not included in a block within that number of blocks, it will expire and be rejected.
You can think of it as an expiration date for the transaction.
Expand All @@ -5133,16 +5134,19 @@ async def commit_weights(
Notes:
See also: <https://docs.learnbittensor.org/glossary#commit-reveal>,
"""
retries = 0
attempt = 0
response = ExtrinsicResponse(False)

if attempt_check := validate_max_attempts(max_attempts, response):
return attempt_check

logging.debug(
f"Committing weights with params: "
f"netuid=[blue]{netuid}[/blue], uids=[blue]{uids}[/blue], weights=[blue]{weights}[/blue], "
f"version_key=[blue]{version_key}[/blue]"
)

while retries < max_retries and response.success is False:
while attempt < max_attempts and response.success is False:
try:
response = await commit_weights_extrinsic(
subtensor=self,
Expand All @@ -5161,7 +5165,7 @@ async def commit_weights(
return ExtrinsicResponse.from_exception(
raise_error=raise_error, error=error
)
retries += 1
attempt += 1

if not response.success:
logging.debug(
Expand Down Expand Up @@ -5655,7 +5659,7 @@ async def reveal_weights(
weights: Weights,
salt: Salt,
mechid: int = 0,
max_retries: int = 5,
max_attempts: int = 5,
version_key: int = version_as_int,
period: Optional[int] = 16,
raise_error: bool = False,
Expand All @@ -5673,7 +5677,7 @@ async def reveal_weights(
weights: NumPy array of weight values corresponding to each UID.
salt: NumPy array of salt values corresponding to the hash function.
mechid: The subnet mechanism unique identifier.
max_retries: The number of maximum attempts to reveal weights.
max_attempts: The number of maximum attempts to reveal weights.
version_key: Version key for compatibility with the network.
period: The number of blocks during which the transaction will remain valid after it's submitted. If the
transaction is not included in a block within that number of blocks, it will expire and be rejected. You
Expand All @@ -5690,10 +5694,13 @@ async def reveal_weights(

See also: <https://docs.learnbittensor.org/glossary#commit-reveal>,
"""
retries = 0
attempt = 0
response = ExtrinsicResponse(False)

while retries < max_retries and response.success is False:
if attempt_check := validate_max_attempts(max_attempts, response):
return attempt_check

while attempt < max_attempts and response.success is False:
try:
response = await reveal_weights_extrinsic(
subtensor=self,
Expand All @@ -5713,7 +5720,7 @@ async def reveal_weights(
return ExtrinsicResponse.from_exception(
raise_error=raise_error, error=error
)
retries += 1
attempt += 1

if not response.success:
logging.debug("No attempt made. Perhaps it is too soon to reveal weights!")
Expand Down Expand Up @@ -6010,7 +6017,7 @@ async def set_weights(
mechid: int = 0,
block_time: float = 12.0,
commit_reveal_version: int = 4,
max_retries: int = 5,
max_attempts: int = 5,
version_key: int = version_as_int,
period: Optional[int] = 8,
raise_error: bool = False,
Expand All @@ -6034,7 +6041,7 @@ async def set_weights(
mechid: The subnet mechanism unique identifier.
block_time: The number of seconds for block duration.
commit_reveal_version: The version of the chain commit-reveal protocol to use.
max_retries: The number of maximum attempts to set weights.
max_attempts: The number of maximum attempts to set weights.
version_key: Version key for compatibility with the network.
period: The number of blocks during which the transaction will remain valid after it's
submitted. If the transaction is not included in a block within that number of blocks, it will expire
Expand All @@ -6052,6 +6059,11 @@ async def set_weights(
Notes:
See <https://docs.learnbittensor.org/glossary#yuma-consensus>
"""
attempt = 0
response = ExtrinsicResponse(False)

if attempt_check := validate_max_attempts(max_attempts, response):
return attempt_check

async def _blocks_weight_limit() -> bool:
bslu, wrl = await asyncio.gather(
Expand All @@ -6060,9 +6072,6 @@ async def _blocks_weight_limit() -> bool:
)
return bslu > wrl

retries = 0
response = ExtrinsicResponse(False)

if (
uid := await self.get_uid_for_hotkey_on_subnet(
wallet.hotkey.ss58_address, netuid
Expand All @@ -6077,13 +6086,13 @@ async def _blocks_weight_limit() -> bool:
# go with `commit_timelocked_mechanism_weights_extrinsic` extrinsic

while (
retries < max_retries
attempt < max_attempts
and response.success is False
and await _blocks_weight_limit()
):
logging.debug(
f"Committing weights {weights} for subnet [blue]{netuid}[/blue]. "
f"Attempt [blue]{retries + 1}[blue] of [green]{max_retries}[/green]."
f"Attempt [blue]{attempt + 1}[blue] of [green]{max_attempts}[/green]."
)
try:
response = await commit_timelocked_weights_extrinsic(
Expand All @@ -6105,19 +6114,19 @@ async def _blocks_weight_limit() -> bool:
return ExtrinsicResponse.from_exception(
raise_error=raise_error, error=error
)
retries += 1
attempt += 1
else:
# go with `set_mechanism_weights_extrinsic`

while (
retries < max_retries
attempt < max_attempts
and response.success is False
and await _blocks_weight_limit()
):
try:
logging.debug(
f"Setting weights for subnet #[blue]{netuid}[/blue]. "
f"Attempt [blue]{retries + 1}[/blue] of [green]{max_retries}[/green]."
f"Attempt [blue]{attempt + 1}[/blue] of [green]{max_attempts}[/green]."
)
response = await set_weights_extrinsic(
subtensor=self,
Expand All @@ -6136,7 +6145,7 @@ async def _blocks_weight_limit() -> bool:
return ExtrinsicResponse.from_exception(
raise_error=raise_error, error=error
)
retries += 1
attempt += 1

if not response.success:
logging.debug(
Expand Down
51 changes: 30 additions & 21 deletions bittensor/core/subtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
is_valid_ss58_address,
u16_normalized_float,
u64_normalized_float,
validate_max_attempts,
)
from bittensor.utils.balance import (
Balance,
Expand Down Expand Up @@ -3883,7 +3884,7 @@ def commit_weights(
weights: Weights,
mechid: int = 0,
version_key: int = version_as_int,
max_retries: int = 5,
max_attempts: int = 5,
period: Optional[int] = 16,
raise_error: bool = True,
wait_for_inclusion: bool = False,
Expand All @@ -3901,7 +3902,7 @@ def commit_weights(
weights: NumPy array of weight values corresponding to each UID.
mechid: Subnet mechanism unique identifier.
version_key: Version key for compatibility with the network.
max_retries: The number of maximum attempts to commit weights.
max_attempts: The number of maximum attempts to commit weights.
period: The number of blocks during which the transaction will remain valid after it's submitted. If
the transaction is not included in a block within that number of blocks, it will expire and be rejected.
You can think of it as an expiration date for the transaction.
Expand All @@ -3915,16 +3916,19 @@ def commit_weights(
This function allows neurons to create a tamper-proof record of their weight distribution at a specific point in
time, enhancing transparency and accountability within the Bittensor network.
"""
retries = 0
attempt = 0
response = ExtrinsicResponse(False)

if attempt_check := validate_max_attempts(max_attempts, response):
return attempt_check

logging.debug(
f"Committing weights with params: "
f"netuid=[blue]{netuid}[/blue], uids=[blue]{uids}[/blue], weights=[blue]{weights}[/blue], "
f"version_key=[blue]{version_key}[/blue]"
)

while retries < max_retries and response.success is False:
while attempt < max_attempts and response.success is False:
try:
response = commit_weights_extrinsic(
subtensor=self,
Expand All @@ -3943,7 +3947,7 @@ def commit_weights(
return ExtrinsicResponse.from_exception(
raise_error=raise_error, error=error
)
retries += 1
attempt += 1

if not response.success:
logging.debug(
Expand Down Expand Up @@ -4437,7 +4441,7 @@ def reveal_weights(
weights: Weights,
salt: Salt,
mechid: int = 0,
max_retries: int = 5,
max_attempts: int = 5,
version_key: int = version_as_int,
period: Optional[int] = 16,
raise_error: bool = False,
Expand All @@ -4455,7 +4459,7 @@ def reveal_weights(
weights: NumPy array of weight values corresponding to each UID.
salt: NumPy array of salt values corresponding to the hash function.
mechid: The subnet mechanism unique identifier.
max_retries: The number of maximum attempts to reveal weights.
max_attempts: The number of maximum attempts to reveal weights.
version_key: Version key for compatibility with the network.
period: The number of blocks during which the transaction will remain valid after it's submitted. If the
transaction is not included in a block within that number of blocks, it will expire and be rejected. You
Expand All @@ -4472,10 +4476,13 @@ def reveal_weights(

See also: <https://docs.learnbittensor.org/glossary#commit-reveal>,
"""
retries = 0
attempt = 0
response = ExtrinsicResponse(False)

while retries < max_retries and response.success is False:
if attempt_check := validate_max_attempts(max_attempts, response):
return attempt_check

while attempt < max_attempts and response.success is False:
try:
response = reveal_weights_extrinsic(
subtensor=self,
Expand All @@ -4495,7 +4502,7 @@ def reveal_weights(
return ExtrinsicResponse.from_exception(
raise_error=raise_error, error=error
)
retries += 1
attempt += 1

if not response.success:
logging.debug("No attempt made. Perhaps it is too soon to reveal weights!")
Expand Down Expand Up @@ -4779,7 +4786,7 @@ def set_weights(
mechid: int = 0,
block_time: float = 12.0,
commit_reveal_version: int = 4,
max_retries: int = 5,
max_attempts: int = 5,
version_key: int = version_as_int,
period: Optional[int] = 8,
raise_error: bool = False,
Expand All @@ -4800,7 +4807,7 @@ def set_weights(
mechid: The subnet mechanism unique identifier.
block_time: The number of seconds for block duration.
commit_reveal_version: The version of the chain commit-reveal protocol to use.
max_retries: The number of maximum attempts to set weights.
max_attempts: The number of maximum attempts to set weights.
version_key: Version key for compatibility with the network.
period: The number of blocks during which the transaction will remain valid after it's
submitted. If the transaction is not included in a block within that number of blocks, it will expire
Expand All @@ -4818,15 +4825,17 @@ def set_weights(
Notes:
See <https://docs.learnbittensor.org/glossary#yuma-consensus>
"""
attempt = 0
response = ExtrinsicResponse(False)

if attempt_check := validate_max_attempts(max_attempts, response):
return attempt_check

def _blocks_weight_limit() -> bool:
bslu = cast(int, self.blocks_since_last_update(netuid, cast(int, uid)))
wrl = cast(int, self.weights_rate_limit(netuid))
return bslu > wrl

retries = 0
response = ExtrinsicResponse(False)

if (
uid := self.get_uid_for_hotkey_on_subnet(wallet.hotkey.ss58_address, netuid)
) is None:
Expand All @@ -4839,13 +4848,13 @@ def _blocks_weight_limit() -> bool:
# go with `commit_reveal_weights_extrinsic` extrinsic

while (
retries < max_retries
attempt < max_attempts
and response.success is False
and _blocks_weight_limit()
):
logging.debug(
f"Committing weights {weights} for subnet [blue]{netuid}[/blue]. "
f"Attempt [blue]{retries + 1}[blue] of [green]{max_retries}[/green]."
f"Attempt [blue]{attempt + 1}[blue] of [green]{max_attempts}[/green]."
)
try:
response = commit_timelocked_weights_extrinsic(
Expand All @@ -4867,19 +4876,19 @@ def _blocks_weight_limit() -> bool:
return ExtrinsicResponse.from_exception(
raise_error=raise_error, error=error
)
retries += 1
attempt += 1
else:
# go with `set_mechanism_weights_extrinsic`

while (
retries < max_retries
attempt < max_attempts
and response.success is False
and _blocks_weight_limit()
):
try:
logging.debug(
f"Setting weights for subnet [blue]{netuid}[/blue]. "
f"Attempt [blue]{retries + 1}[/blue] of [green]{max_retries}[/green]."
f"Attempt [blue]{attempt + 1}[/blue] of [green]{max_attempts}[/green]."
)
response = set_weights_extrinsic(
subtensor=self,
Expand All @@ -4898,7 +4907,7 @@ def _blocks_weight_limit() -> bool:
return ExtrinsicResponse.from_exception(
raise_error=raise_error, error=error
)
retries += 1
attempt += 1

if not response.success:
logging.debug(
Expand Down
Loading
Loading