From 79ea823c6df1ce1e5f4d9052af10f138911acd99 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Mon, 16 Dec 2024 15:02:41 -0800 Subject: [PATCH 1/2] Adds retry in crv3 --- bittensor/core/subtensor.py | 39 ++++++++++++++++++++---------- tests/unit_tests/test_subtensor.py | 6 +++++ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index 4b699f09d5..261221db4e 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1812,23 +1812,36 @@ def set_weights( This function is crucial in shaping the network's collective intelligence, where each neuron's learning and contribution are influenced by the weights it sets towards others【81†source】. """ + retries = 0 + success = False + uid = self.get_uid_for_hotkey_on_subnet(wallet.hotkey.ss58_address, netuid) + if self.commit_reveal_enabled(netuid=netuid) is True: # go with `commit reveal v3` extrinsic - return commit_reveal_v3_extrinsic( - subtensor=self, - wallet=wallet, - netuid=netuid, - uids=uids, - weights=weights, - version_key=version_key, - wait_for_inclusion=wait_for_inclusion, - wait_for_finalization=wait_for_finalization, - ) + message = "No attempt made. Perhaps it is too soon to commit weights!" + while ( + self.blocks_since_last_update(netuid, uid) + > self.weights_rate_limit(netuid) + and retries < max_retries + and success is False + ): + logging.info( + f"Committing weights for subnet #{netuid}. Attempt {retries + 1} of {max_retries}." + ) + success, message = commit_reveal_v3_extrinsic( + subtensor=self, + wallet=wallet, + netuid=netuid, + uids=uids, + weights=weights, + version_key=version_key, + wait_for_inclusion=wait_for_inclusion, + wait_for_finalization=wait_for_finalization, + ) + retries += 1 + return success, message else: # go with classic `set weights` logic - uid = self.get_uid_for_hotkey_on_subnet(wallet.hotkey.ss58_address, netuid) - retries = 0 - success = False message = "No attempt made. Perhaps it is too soon to set weights!" while ( self.blocks_since_last_update(netuid, uid) # type: ignore diff --git a/tests/unit_tests/test_subtensor.py b/tests/unit_tests/test_subtensor.py index dea9d66ed0..bf156e2122 100644 --- a/tests/unit_tests/test_subtensor.py +++ b/tests/unit_tests/test_subtensor.py @@ -2850,6 +2850,12 @@ def test_set_weights_with_commit_reveal_enabled(subtensor, mocker): mocked_commit_reveal_v3_extrinsic = mocker.patch.object( subtensor_module, "commit_reveal_v3_extrinsic" ) + mocked_commit_reveal_v3_extrinsic.return_value = ( + True, + "Weights committed successfully", + ) + mocker.patch.object(subtensor, "blocks_since_last_update", return_value=181) + mocker.patch.object(subtensor, "weights_rate_limit", return_value=180) # Call result = subtensor.set_weights( From 67395431850c7a6d2283a83d2231b3c344accf14 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Mon, 16 Dec 2024 15:14:06 -0800 Subject: [PATCH 2/2] MyPy --- bittensor/core/subtensor.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index 261221db4e..584ec3b23a 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1820,8 +1820,8 @@ def set_weights( # go with `commit reveal v3` extrinsic message = "No attempt made. Perhaps it is too soon to commit weights!" while ( - self.blocks_since_last_update(netuid, uid) - > self.weights_rate_limit(netuid) + self.blocks_since_last_update(netuid, uid) # type: ignore + > self.weights_rate_limit(netuid) # type: ignore and retries < max_retries and success is False ):