Skip to content

Commit

Permalink
remove retry and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-opentensor committed Nov 5, 2024
1 parent e1a6a36 commit 2276f7f
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 464 deletions.
116 changes: 53 additions & 63 deletions bittensor/core/extrinsics/commit_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

from typing import Optional, TYPE_CHECKING

from retry import retry

from bittensor.core.extrinsics.utils import submit_extrinsic
from bittensor.utils import format_error_message
from bittensor.utils.btlogging import logging
Expand Down Expand Up @@ -60,37 +58,33 @@ def do_commit_weights(
This method ensures that the weight commitment is securely recorded on the Bittensor blockchain, providing a verifiable record of the neuron's weight distribution at a specific point in time.
"""

@retry(delay=1, tries=3, backoff=2, max_delay=4)
def make_substrate_call_with_retry():
call = self.substrate.compose_call(
call_module="SubtensorModule",
call_function="commit_weights",
call_params={
"netuid": netuid,
"commit_hash": commit_hash,
},
)
extrinsic = self.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
)
response = submit_extrinsic(
substrate=self.substrate,
extrinsic=extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

if not wait_for_finalization and not wait_for_inclusion:
return True, None
call = self.substrate.compose_call(
call_module="SubtensorModule",
call_function="commit_weights",
call_params={
"netuid": netuid,
"commit_hash": commit_hash,
},
)
extrinsic = self.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
)
response = submit_extrinsic(
substrate=self.substrate,
extrinsic=extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

response.process_events()
if response.is_success:
return True, None
else:
return False, response.error_message
if not wait_for_finalization and not wait_for_inclusion:
return True, None

return make_substrate_call_with_retry()
response.process_events()
if response.is_success:
return True, None
else:
return False, response.error_message


def commit_weights_extrinsic(
Expand Down Expand Up @@ -174,40 +168,36 @@ def do_reveal_weights(
This method ensures that the weight revelation is securely recorded on the Bittensor blockchain, providing transparency and accountability for the neuron's weight distribution.
"""

@retry(delay=1, tries=3, backoff=2, max_delay=4)
def make_substrate_call_with_retry():
call = self.substrate.compose_call(
call_module="SubtensorModule",
call_function="reveal_weights",
call_params={
"netuid": netuid,
"uids": uids,
"values": values,
"salt": salt,
"version_key": version_key,
},
)
extrinsic = self.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
)
response = submit_extrinsic(
substrate=self.substrate,
extrinsic=extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

if not wait_for_finalization and not wait_for_inclusion:
return True, None
call = self.substrate.compose_call(
call_module="SubtensorModule",
call_function="reveal_weights",
call_params={
"netuid": netuid,
"uids": uids,
"values": values,
"salt": salt,
"version_key": version_key,
},
)
extrinsic = self.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.hotkey,
)
response = submit_extrinsic(
substrate=self.substrate,
extrinsic=extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

response.process_events()
if response.is_success:
return True, None
else:
return False, response.error_message
if not wait_for_finalization and not wait_for_inclusion:
return True, None

return make_substrate_call_with_retry()
response.process_events()
if response.is_success:
return True, None
else:
return False, response.error_message


def reveal_weights_extrinsic(
Expand Down
132 changes: 60 additions & 72 deletions bittensor/core/extrinsics/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from typing import Union, Optional, TYPE_CHECKING

from bittensor_wallet.errors import KeyFileError
from retry import retry

from bittensor.utils import format_error_message
from bittensor.utils.btlogging import logging
Expand Down Expand Up @@ -59,46 +58,39 @@ def _do_pow_register(
success (bool): ``True`` if the extrinsic was included in a block.
error (Optional[str]): ``None`` on success or not waiting for inclusion/finalization, otherwise the error message.
"""
# create extrinsic call
call = self.substrate.compose_call(
call_module="SubtensorModule",
call_function="register",
call_params={
"netuid": netuid,
"block_number": pow_result.block_number,
"nonce": pow_result.nonce,
"work": [int(byte_) for byte_ in pow_result.seal],
"hotkey": wallet.hotkey.ss58_address,
"coldkey": wallet.coldkeypub.ss58_address,
},
)
extrinsic = self.substrate.create_signed_extrinsic(call=call, keypair=wallet.hotkey)
response = self.substrate.submit_extrinsic(
extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

@retry(delay=1, tries=3, backoff=2, max_delay=4)
def make_substrate_call_with_retry():
# create extrinsic call
call = self.substrate.compose_call(
call_module="SubtensorModule",
call_function="register",
call_params={
"netuid": netuid,
"block_number": pow_result.block_number,
"nonce": pow_result.nonce,
"work": [int(byte_) for byte_ in pow_result.seal],
"hotkey": wallet.hotkey.ss58_address,
"coldkey": wallet.coldkeypub.ss58_address,
},
)
extrinsic = self.substrate.create_signed_extrinsic(
call=call, keypair=wallet.hotkey
)
response = self.substrate.submit_extrinsic(
extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

# We only wait here if we expect finalization.
if not wait_for_finalization and not wait_for_inclusion:
return True, None

# process if registration successful, try again if pow is still valid
response.process_events()
if not response.is_success:
return False, format_error_message(
response.error_message, substrate=self.substrate
)
# Successful registration
else:
return True, None
# We only wait here if we expect finalization.
if not wait_for_finalization and not wait_for_inclusion:
return True, None

return make_substrate_call_with_retry()
# process if registration successful, try again if pow is still valid
response.process_events()
if not response.is_success:
return False, format_error_message(
response.error_message, substrate=self.substrate
)
# Successful registration
else:
return True, None


def register_extrinsic(
Expand Down Expand Up @@ -297,41 +289,37 @@ def _do_burned_register(
Tuple[bool, Optional[str]]: A tuple containing a boolean indicating success or failure, and an optional error message.
"""

@retry(delay=1, tries=3, backoff=2, max_delay=4)
def make_substrate_call_with_retry():
# create extrinsic call
call = self.substrate.compose_call(
call_module="SubtensorModule",
call_function="burned_register",
call_params={
"netuid": netuid,
"hotkey": wallet.hotkey.ss58_address,
},
)
extrinsic = self.substrate.create_signed_extrinsic(
call=call, keypair=wallet.coldkey
)
response = self.substrate.submit_extrinsic(
extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

# We only wait here if we expect finalization.
if not wait_for_finalization and not wait_for_inclusion:
return True, None
# create extrinsic call
call = self.substrate.compose_call(
call_module="SubtensorModule",
call_function="burned_register",
call_params={
"netuid": netuid,
"hotkey": wallet.hotkey.ss58_address,
},
)
extrinsic = self.substrate.create_signed_extrinsic(
call=call, keypair=wallet.coldkey
)
response = self.substrate.submit_extrinsic(
extrinsic,
wait_for_inclusion=wait_for_inclusion,
wait_for_finalization=wait_for_finalization,
)

# process if registration successful, try again if pow is still valid
response.process_events()
if not response.is_success:
return False, format_error_message(
response.error_message, substrate=self.substrate
)
# Successful registration
else:
return True, None
# We only wait here if we expect finalization.
if not wait_for_finalization and not wait_for_inclusion:
return True, None

return make_substrate_call_with_retry()
# process if registration successful, try again if pow is still valid
response.process_events()
if not response.is_success:
return False, format_error_message(
response.error_message, substrate=self.substrate
)
# Successful registration
else:
return True, None


def burned_register_extrinsic(
Expand Down
Loading

0 comments on commit 2276f7f

Please sign in to comment.