diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index 15f5c4d953..ff17c8e896 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -1814,23 +1814,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) # type: ignore + > self.weights_rate_limit(netuid) # type: ignore + 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(