From 9f5ea1e3d33aea18ec0243a1a98d09b60ba770eb Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Wed, 11 Dec 2024 21:36:34 -0500 Subject: [PATCH 01/25] use the tx pool for setting/committing weights --- bittensor/core/extrinsics/async_weights.py | 12 ++++++++++++ bittensor/core/extrinsics/commit_weights.py | 2 ++ bittensor/utils/async_substrate_interface.py | 17 +++++++++++++---- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/bittensor/core/extrinsics/async_weights.py b/bittensor/core/extrinsics/async_weights.py index 2c4f0d0a7b..85a6a0e2ae 100644 --- a/bittensor/core/extrinsics/async_weights.py +++ b/bittensor/core/extrinsics/async_weights.py @@ -58,11 +58,17 @@ async def _do_set_weights( "version_key": version_key, }, ) + + next_nonce = await subtensor.substrate.get_account_next_index( + wallet.hotkey.ss58_address + ) + # Period dictates how long the extrinsic will stay as part of waiting pool extrinsic = await subtensor.substrate.create_signed_extrinsic( call=call, keypair=wallet.hotkey, era={"period": 5}, + nonce=next_nonce, ) response = await subtensor.substrate.submit_extrinsic( extrinsic, @@ -186,9 +192,15 @@ async def _do_commit_weights( "commit_hash": commit_hash, }, ) + + next_nonce = await subtensor.substrate.get_account_next_index( + wallet.hotkey.ss58_address + ) + extrinsic = await subtensor.substrate.create_signed_extrinsic( call=call, keypair=wallet.hotkey, + nonce=next_nonce, ) response = await subtensor.substrate.submit_extrinsic( substrate=subtensor.substrate, diff --git a/bittensor/core/extrinsics/commit_weights.py b/bittensor/core/extrinsics/commit_weights.py index 0ad6ad5add..23d0f5e00e 100644 --- a/bittensor/core/extrinsics/commit_weights.py +++ b/bittensor/core/extrinsics/commit_weights.py @@ -66,9 +66,11 @@ def do_commit_weights( "commit_hash": commit_hash, }, ) + next_nonce = self.substrate.get_account_next_index(wallet.hotkey.ss58_address) extrinsic = self.substrate.create_signed_extrinsic( call=call, keypair=wallet.hotkey, + nonce=next_nonce, ) response = submit_extrinsic( subtensor=self, diff --git a/bittensor/utils/async_substrate_interface.py b/bittensor/utils/async_substrate_interface.py index 05fd963212..7182830d62 100644 --- a/bittensor/utils/async_substrate_interface.py +++ b/bittensor/utils/async_substrate_interface.py @@ -2296,10 +2296,19 @@ async def get_account_nonce(self, account_address: str) -> int: Returns: Nonce for given account address """ - nonce_obj = await self.runtime_call( - "AccountNonceApi", "account_nonce", [account_address] - ) - return nonce_obj.value + + async def get_account_next_index(self, account_address: str) -> int: + """ + Returns next index for the given account address, taking into account the transaction pool. + + Args: + account_address: SS58 formatted address + + Returns: + Next index for the given account address + """ + nonce_obj = await self.rpc_request("account_nextIndex", [account_address]) + return nonce_obj["result"] async def get_metadata_constant(self, module_name, constant_name, block_hash=None): """ From 0043ad4c88e1282e3e96d5f2b86bc98d7db91048 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Wed, 11 Dec 2024 21:41:45 -0500 Subject: [PATCH 02/25] add supports rpc method with cache --- bittensor/utils/async_substrate_interface.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/bittensor/utils/async_substrate_interface.py b/bittensor/utils/async_substrate_interface.py index 7182830d62..7a0efa2a5c 100644 --- a/bittensor/utils/async_substrate_interface.py +++ b/bittensor/utils/async_substrate_interface.py @@ -10,6 +10,7 @@ import random from collections import defaultdict from dataclasses import dataclass +from functools import lru_cache from hashlib import blake2b from typing import Optional, Any, Union, Callable, Awaitable, cast, TYPE_CHECKING @@ -1704,6 +1705,24 @@ def make_payload(id_: str, method: str, params: list) -> dict: "payload": {"jsonrpc": "2.0", "method": method, "params": params}, } + @lru_cache(maxsize=512) # RPC methods are unlikely to change often + async def supports_rpc_method(self, name: str) -> bool: + """ + Check if substrate RPC supports given method + Parameters + ---------- + name: name of method to check + + Returns + ------- + bool + """ + result = await self.rpc_request("rpc_methods", []).get("result") + if result: + self.config["rpc_methods"] = result.get("methods", []) + + return name in self.config["rpc_methods"] + async def rpc_request( self, method: str, From b7a6bf9030407d75fe6c50e86cb1aa3e460ab7c7 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Wed, 11 Dec 2024 21:42:15 -0500 Subject: [PATCH 03/25] handle when state call is not possible (unlikely) --- bittensor/utils/async_substrate_interface.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/bittensor/utils/async_substrate_interface.py b/bittensor/utils/async_substrate_interface.py index 7a0efa2a5c..180f64e7b7 100644 --- a/bittensor/utils/async_substrate_interface.py +++ b/bittensor/utils/async_substrate_interface.py @@ -2315,6 +2315,16 @@ async def get_account_nonce(self, account_address: str) -> int: Returns: Nonce for given account address """ + if await self.supports_rpc_method("state_call"): + nonce_obj = await self.runtime_call( + "AccountNonceApi", "account_nonce", [account_address] + ) + return nonce_obj + else: + response = await self.query( + module="System", storage_function="Account", params=[account_address] + ) + return response["nonce"] async def get_account_next_index(self, account_address: str) -> int: """ @@ -2326,6 +2336,10 @@ async def get_account_next_index(self, account_address: str) -> int: Returns: Next index for the given account address """ + if not await self.supports_rpc_method("account_nextIndex"): + # Unlikely to happen, this is a common RPC method + raise Exception("account_nextIndex not supported") + nonce_obj = await self.rpc_request("account_nextIndex", [account_address]) return nonce_obj["result"] From aca0ecb2cec887282f1a1b216d18385c07582fa5 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Wed, 11 Dec 2024 21:48:06 -0500 Subject: [PATCH 04/25] increment next index by one for correct nonce --- bittensor/core/extrinsics/async_weights.py | 8 ++++---- bittensor/core/extrinsics/commit_weights.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bittensor/core/extrinsics/async_weights.py b/bittensor/core/extrinsics/async_weights.py index 85a6a0e2ae..01a9a19621 100644 --- a/bittensor/core/extrinsics/async_weights.py +++ b/bittensor/core/extrinsics/async_weights.py @@ -59,8 +59,8 @@ async def _do_set_weights( }, ) - next_nonce = await subtensor.substrate.get_account_next_index( - wallet.hotkey.ss58_address + next_nonce = ( + await subtensor.substrate.get_account_next_index(wallet.hotkey.ss58_address) + 1 ) # Period dictates how long the extrinsic will stay as part of waiting pool @@ -193,8 +193,8 @@ async def _do_commit_weights( }, ) - next_nonce = await subtensor.substrate.get_account_next_index( - wallet.hotkey.ss58_address + next_nonce = ( + await subtensor.substrate.get_account_next_index(wallet.hotkey.ss58_address) + 1 ) extrinsic = await subtensor.substrate.create_signed_extrinsic( diff --git a/bittensor/core/extrinsics/commit_weights.py b/bittensor/core/extrinsics/commit_weights.py index 23d0f5e00e..6f7d19ba47 100644 --- a/bittensor/core/extrinsics/commit_weights.py +++ b/bittensor/core/extrinsics/commit_weights.py @@ -66,7 +66,7 @@ def do_commit_weights( "commit_hash": commit_hash, }, ) - next_nonce = self.substrate.get_account_next_index(wallet.hotkey.ss58_address) + next_nonce = self.substrate.get_account_next_index(wallet.hotkey.ss58_address) + 1 extrinsic = self.substrate.create_signed_extrinsic( call=call, keypair=wallet.hotkey, From 1aaca2744621b12416e13b8ea290c0327c46c929 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Wed, 11 Dec 2024 21:48:16 -0500 Subject: [PATCH 05/25] use for set sync set weights also --- bittensor/core/extrinsics/set_weights.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bittensor/core/extrinsics/set_weights.py b/bittensor/core/extrinsics/set_weights.py index 65aac03fc2..98f3cdcaf0 100644 --- a/bittensor/core/extrinsics/set_weights.py +++ b/bittensor/core/extrinsics/set_weights.py @@ -76,11 +76,13 @@ def do_set_weights( "version_key": version_key, }, ) + next_nonce = self.substrate.get_account_next_index(wallet.hotkey.ss58_address) + 1 # Period dictates how long the extrinsic will stay as part of waiting pool extrinsic = self.substrate.create_signed_extrinsic( call=call, keypair=wallet.hotkey, era={"period": period}, + nonce=next_nonce, ) response = submit_extrinsic( self, From 2a73a5049937b7deaf8c00077dedd679b5c878ac Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 12 Dec 2024 09:54:20 -0500 Subject: [PATCH 06/25] oops, define in subtensor for sync methods --- bittensor/core/extrinsics/commit_weights.py | 2 +- bittensor/core/extrinsics/set_weights.py | 2 +- bittensor/core/subtensor.py | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/bittensor/core/extrinsics/commit_weights.py b/bittensor/core/extrinsics/commit_weights.py index 6f7d19ba47..27f3a954ea 100644 --- a/bittensor/core/extrinsics/commit_weights.py +++ b/bittensor/core/extrinsics/commit_weights.py @@ -66,7 +66,7 @@ def do_commit_weights( "commit_hash": commit_hash, }, ) - next_nonce = self.substrate.get_account_next_index(wallet.hotkey.ss58_address) + 1 + next_nonce = self.get_account_next_index(wallet.hotkey.ss58_address) + 1 extrinsic = self.substrate.create_signed_extrinsic( call=call, keypair=wallet.hotkey, diff --git a/bittensor/core/extrinsics/set_weights.py b/bittensor/core/extrinsics/set_weights.py index 98f3cdcaf0..8dac09aeea 100644 --- a/bittensor/core/extrinsics/set_weights.py +++ b/bittensor/core/extrinsics/set_weights.py @@ -76,7 +76,7 @@ def do_set_weights( "version_key": version_key, }, ) - next_nonce = self.substrate.get_account_next_index(wallet.hotkey.ss58_address) + 1 + next_nonce = self.get_account_next_index(wallet.hotkey.ss58_address) + 1 # Period dictates how long the extrinsic will stay as part of waiting pool extrinsic = self.substrate.create_signed_extrinsic( call=call, diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index 441a4c033b..497faabeb9 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -660,6 +660,16 @@ def query_module( ), ) + @networking.ensure_connected + def get_account_next_index(self, address: str) -> int: + """ + Returns the next nonce for an account, taking into account the transaction pool. + """ + if not self.substrate.supports_rpc_method("account_nextIndex"): + raise Exception("account_nextIndex not supported") + + return self.substrate.rpc_request("account_nextIndex", [address])["result"] + # Common subtensor methods ========================================================================================= def metagraph( self, netuid: int, lite: bool = True, block: Optional[int] = None From 85a45fa24361ab61876b5f8a00e4f7956805d9db Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 12 Dec 2024 10:13:08 -0500 Subject: [PATCH 07/25] make tests less specific --- .../extrinsics/test_commit_weights.py | 7 ++-- .../unit_tests/extrinsics/test_set_weights.py | 36 ++++++++----------- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/tests/unit_tests/extrinsics/test_commit_weights.py b/tests/unit_tests/extrinsics/test_commit_weights.py index 35a1d4d426..57d78a8013 100644 --- a/tests/unit_tests/extrinsics/test_commit_weights.py +++ b/tests/unit_tests/extrinsics/test_commit_weights.py @@ -53,9 +53,10 @@ def test_do_commit_weights(subtensor, mocker): }, ) - subtensor.substrate.create_signed_extrinsic.assert_called_once_with( - call=subtensor.substrate.compose_call.return_value, keypair=fake_wallet.hotkey - ) + subtensor.substrate.create_signed_extrinsic.assert_called_once() + _, kwargs = subtensor.substrate.create_signed_extrinsic.call_args + assert kwargs["call"] == subtensor.substrate.compose_call.return_value + assert kwargs["keypair"] == fake_wallet.hotkey subtensor.substrate.submit_extrinsic.assert_called_once_with( subtensor.substrate.create_signed_extrinsic.return_value, diff --git a/tests/unit_tests/extrinsics/test_set_weights.py b/tests/unit_tests/extrinsics/test_set_weights.py index f447915d2f..6c070bf5c4 100644 --- a/tests/unit_tests/extrinsics/test_set_weights.py +++ b/tests/unit_tests/extrinsics/test_set_weights.py @@ -135,17 +135,11 @@ def test_do_set_weights_is_success(mock_subtensor, mocker): }, ) - mock_subtensor.substrate.create_signed_extrinsic.assert_called_once_with( - call=mock_subtensor.substrate.compose_call.return_value, - keypair=fake_wallet.hotkey, - era={"period": 5}, - ) - - mock_subtensor.substrate.submit_extrinsic.assert_called_once_with( - mock_subtensor.substrate.create_signed_extrinsic.return_value, - wait_for_inclusion=fake_wait_for_inclusion, - wait_for_finalization=fake_wait_for_finalization, - ) + mock_subtensor.substrate.create_signed_extrinsic.assert_called_once() + _, kwargs = mock_subtensor.substrate.create_signed_extrinsic.call_args + assert kwargs["call"] == mock_subtensor.substrate.compose_call.return_value + assert kwargs["keypair"] == fake_wallet.hotkey + assert kwargs["era"] == {"period": 5} mock_subtensor.substrate.submit_extrinsic.return_value.process_events.assert_called_once() assert result == (True, "Successfully set weights.") @@ -189,11 +183,11 @@ def test_do_set_weights_is_not_success(mock_subtensor, mocker): }, ) - mock_subtensor.substrate.create_signed_extrinsic.assert_called_once_with( - call=mock_subtensor.substrate.compose_call.return_value, - keypair=fake_wallet.hotkey, - era={"period": 5}, - ) + mock_subtensor.substrate.create_signed_extrinsic.assert_called_once() + _, kwargs = mock_subtensor.substrate.create_signed_extrinsic.call_args + assert kwargs["call"] == mock_subtensor.substrate.compose_call.return_value + assert kwargs["keypair"] == fake_wallet.hotkey + assert kwargs["era"] == {"period": 5} mock_subtensor.substrate.submit_extrinsic.assert_called_once_with( mock_subtensor.substrate.create_signed_extrinsic.return_value, @@ -242,11 +236,11 @@ def test_do_set_weights_no_waits(mock_subtensor, mocker): }, ) - mock_subtensor.substrate.create_signed_extrinsic.assert_called_once_with( - call=mock_subtensor.substrate.compose_call.return_value, - keypair=fake_wallet.hotkey, - era={"period": 5}, - ) + mock_subtensor.substrate.create_signed_extrinsic.assert_called_once() + _, kwargs = mock_subtensor.substrate.create_signed_extrinsic.call_args + assert kwargs["call"] == mock_subtensor.substrate.compose_call.return_value + assert kwargs["keypair"] == fake_wallet.hotkey + assert kwargs["era"] == {"period": 5} mock_subtensor.substrate.submit_extrinsic.assert_called_once_with( mock_subtensor.substrate.create_signed_extrinsic.return_value, From 13f21ccb7b139d0b2a49a4811f575f00083e1173 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 12 Dec 2024 10:20:19 -0500 Subject: [PATCH 08/25] try e2e test for commit weights --- tests/e2e_tests/test_commit_weights.py | 157 +++++++++++++++++++++++++ 1 file changed, 157 insertions(+) diff --git a/tests/e2e_tests/test_commit_weights.py b/tests/e2e_tests/test_commit_weights.py index 3e29dc56ec..eda5e79e74 100644 --- a/tests/e2e_tests/test_commit_weights.py +++ b/tests/e2e_tests/test_commit_weights.py @@ -3,6 +3,8 @@ import numpy as np import pytest +import asyncio + from bittensor.core.subtensor import Subtensor from bittensor.utils.balance import Balance from bittensor.utils.weight_utils import convert_weights_and_uids_for_emit @@ -172,3 +174,158 @@ async def test_commit_and_reveal_weights(local_chain): weight_vals[0] == revealed_weights.value[0][1] ), f"Incorrect revealed weights. Expected: {weights[0]}, Actual: {revealed_weights.value[0][1]}" print("✅ Passed test_commit_and_reveal_weights") + + +@pytest.mark.asyncio +async def test_commit_weights_uses_next_nonce(local_chain): + """ + Tests that commiting weights doesn't re-use a nonce in the transaction pool. + + Steps: + 1. Register a subnet through Alice + 2. Register Alice's neuron and add stake + 3. Enable commit-reveal mechanism on the subnet + 4. Lower the commit_reveal interval and rate limit + 5. Commit weights twice in the same block + 6. Assert that both commits succeeded + Raises: + AssertionError: If any of the checks or verifications fail + """ + netuid = 1 + utils.EXTRINSIC_SUBMISSION_TIMEOUT = 12 # handle fast blocks + print("Testing test_commit_and_reveal_weights") + # Register root as Alice + keypair, alice_wallet = setup_wallet("//Alice") + assert register_subnet(local_chain, alice_wallet), "Unable to register the subnet" + + # Verify subnet 1 created successfully + assert local_chain.query( + "SubtensorModule", "NetworksAdded", [1] + ).serialize(), "Subnet wasn't created successfully" + + subtensor = Subtensor(network="ws://localhost:9945") + + # Register Alice to the subnet + assert subtensor.burned_register( + alice_wallet, netuid + ), "Unable to register Alice as a neuron" + + # Stake to become to top neuron after the first epoch + add_stake(local_chain, alice_wallet, Balance.from_tao(100_000)) + + # Enable commit_reveal on the subnet + assert sudo_set_hyperparameter_bool( + local_chain, + alice_wallet, + "sudo_set_commit_reveal_weights_enabled", + True, + netuid, + ), "Unable to enable commit reveal on the subnet" + + assert subtensor.get_subnet_hyperparameters( + netuid=netuid, + ).commit_reveal_weights_enabled, "Failed to enable commit/reveal" + + # Lower the commit_reveal interval + assert sudo_set_hyperparameter_values( + local_chain, + alice_wallet, + call_function="sudo_set_commit_reveal_weights_interval", + call_params={"netuid": netuid, "interval": "1"}, + return_error_message=True, + ) + + assert ( + subtensor.get_subnet_hyperparameters( + netuid=netuid + ).commit_reveal_weights_interval + == 1 + ), "Failed to set commit/reveal periods" + + assert ( + subtensor.weights_rate_limit(netuid=netuid) > 0 + ), "Weights rate limit is below 0" + # Lower the rate limit + assert sudo_set_hyperparameter_values( + local_chain, + alice_wallet, + call_function="sudo_set_weights_set_rate_limit", + call_params={"netuid": netuid, "weights_set_rate_limit": "0"}, + return_error_message=True, + ) + + assert ( + subtensor.get_subnet_hyperparameters(netuid=netuid).weights_rate_limit == 0 + ), "Failed to set weights_rate_limit" + assert subtensor.weights_rate_limit(netuid=netuid) == 0 + + # Commit-reveal values + uids = np.array([0], dtype=np.int64) + weights = np.array([0.1], dtype=np.float32) + salt = [18, 179, 107, 0, 165, 211, 141, 197] + weight_uids, weight_vals = convert_weights_and_uids_for_emit( + uids=uids, weights=weights + ) + + # Make a second salt + salt2 = salt.copy() + salt2[0] += 1 # Increment the first byte to produce a different commit hash + + # Make a third salt + salt3 = salt.copy() + salt3[0] += 2 # Increment the first byte to produce a different commit hash + + # Commit all three salts + success, message = subtensor.commit_weights( + alice_wallet, + netuid, + salt=salt, + uids=weight_uids, + weights=weight_vals, + wait_for_inclusion=False, # Don't wait for inclusion, we are testing the nonce when there is a tx in the pool + wait_for_finalization=False, + ) + + assert success is True + + success, message = subtensor.commit_weights( + alice_wallet, + netuid, + salt=salt2, + uids=weight_uids, + weights=weight_vals, + wait_for_inclusion=False, + wait_for_finalization=False, + ) + + assert success is True + + # Commit the third salt + success, message = subtensor.commit_weights( + alice_wallet, + netuid, + salt=salt3, + uids=weight_uids, + weights=weight_vals, + wait_for_inclusion=False, + wait_for_finalization=False, + ) + + assert success is True + + # Wait a few blocks + await asyncio.sleep(2) # Wait for the txs to be included in the block + + # Query the WeightCommits storage map for all three salts + weight_commits = subtensor.query_module( + module="SubtensorModule", + name="WeightCommits", + params=[netuid, alice_wallet.hotkey.ss58_address], + ) + # Assert that the committed weights are set correctly + assert weight_commits.value is not None, "Weight commit not found in storage" + commit_hash, commit_block, reveal_block, expire_block = weight_commits.value[0] + assert commit_block > 0, f"Invalid block number: {commit_block}" + + # Check for three commits in the WeightCommits storage map + assert len(weight_commits.value) == 3, "Expected 3 weight commits" From d1a0c120dc42fef0f0d5900376b1cd3437ba8c90 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 12 Dec 2024 10:33:33 -0500 Subject: [PATCH 09/25] modify comments on test --- tests/e2e_tests/test_commit_weights.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e_tests/test_commit_weights.py b/tests/e2e_tests/test_commit_weights.py index eda5e79e74..3d9ab1c9e2 100644 --- a/tests/e2e_tests/test_commit_weights.py +++ b/tests/e2e_tests/test_commit_weights.py @@ -186,8 +186,8 @@ async def test_commit_weights_uses_next_nonce(local_chain): 2. Register Alice's neuron and add stake 3. Enable commit-reveal mechanism on the subnet 4. Lower the commit_reveal interval and rate limit - 5. Commit weights twice in the same block - 6. Assert that both commits succeeded + 5. Commit weights three times + 6. Assert that all commits succeeded Raises: AssertionError: If any of the checks or verifications fail """ @@ -314,7 +314,7 @@ async def test_commit_weights_uses_next_nonce(local_chain): assert success is True # Wait a few blocks - await asyncio.sleep(2) # Wait for the txs to be included in the block + await asyncio.sleep(2) # Wait for the txs to be included in the chain # Query the WeightCommits storage map for all three salts weight_commits = subtensor.query_module( From beb493c7866cc9faa11fd94e345a9a37053ae5f8 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 12 Dec 2024 10:33:44 -0500 Subject: [PATCH 10/25] add root set test helper --- tests/e2e_tests/utils/chain_interactions.py | 29 +++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/e2e_tests/utils/chain_interactions.py b/tests/e2e_tests/utils/chain_interactions.py index 9c0d9100e8..c028f3490b 100644 --- a/tests/e2e_tests/utils/chain_interactions.py +++ b/tests/e2e_tests/utils/chain_interactions.py @@ -160,3 +160,32 @@ async def wait_interval(tempo: int, subtensor: "Subtensor", netuid: int = 1): logging.info( f"Current Block: {current_block} Next tempo for netuid {netuid} at: {next_tempo_block_start}" ) + + +async def root_set_subtensor_hyperparameter_values( + substrate: "SubstrateInterface", + wallet: "Wallet", + call_function: str, + call_params: dict, + return_error_message: bool = False, +) -> Union[bool, tuple[bool, Optional[str]]]: + """ + Sets liquid alpha values using AdminUtils. Mimics setting hyperparams + """ + call = substrate.compose_call( + call_module="SubtensorModule", + call_function=call_function, + call_params=call_params, + ) + extrinsic = substrate.create_signed_extrinsic(call=call, keypair=wallet.coldkey) + response = substrate.submit_extrinsic( + extrinsic, + wait_for_inclusion=True, + wait_for_finalization=True, + ) + response.process_events() + + if return_error_message: + return response.is_success, response.error_message + + return response.is_success From 6bd685a6b56896c9ce4479feda75a3a726f16cb1 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 12 Dec 2024 10:49:01 -0500 Subject: [PATCH 11/25] try upping submit timeout --- tests/e2e_tests/test_incentive.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/e2e_tests/test_incentive.py b/tests/e2e_tests/test_incentive.py index ab557a56fd..cfafef42b5 100644 --- a/tests/e2e_tests/test_incentive.py +++ b/tests/e2e_tests/test_incentive.py @@ -15,6 +15,7 @@ templates_repo, ) from bittensor.utils.balance import Balance +from bittensor.core.extrinsics import utils from bittensor.core.extrinsics.set_weights import do_set_weights from bittensor.core.metagraph import Metagraph @@ -40,6 +41,8 @@ async def test_incentive(local_chain): print("Testing test_incentive") netuid = 1 + utils.EXTRINSIC_SUBMISSION_TIMEOUT = 12 # handle fast blocks + # Register root as Alice - the subnet owner and validator alice_keypair, alice_wallet = setup_wallet("//Alice") register_subnet(local_chain, alice_wallet) From 27251dcb048e4b7872d016be9ef052bceb7fd036 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 12 Dec 2024 10:53:46 -0500 Subject: [PATCH 12/25] add set weights e2e test --- tests/e2e_tests/test_set_weights.py | 144 ++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 tests/e2e_tests/test_set_weights.py diff --git a/tests/e2e_tests/test_set_weights.py b/tests/e2e_tests/test_set_weights.py new file mode 100644 index 0000000000..ce4833aaf7 --- /dev/null +++ b/tests/e2e_tests/test_set_weights.py @@ -0,0 +1,144 @@ +import numpy as np +import pytest + +import asyncio + +from bittensor.core.subtensor import Subtensor +from bittensor.utils.balance import Balance +from bittensor.utils.weight_utils import convert_weights_and_uids_for_emit +from bittensor.core.extrinsics import utils +from tests.e2e_tests.utils.chain_interactions import ( + add_stake, + register_subnet, + sudo_set_hyperparameter_bool, + sudo_set_hyperparameter_values, + root_set_subtensor_hyperparameter_values, +) +from tests.e2e_tests.utils.e2e_test_utils import setup_wallet + + +@pytest.mark.asyncio +async def test_set_weights_uses_next_nonce(local_chain): + """ + Tests that setting weights doesn't re-use a nonce in the transaction pool. + + Steps: + 1. Register three subnets through Alice + 2. Register Alice's neuron on each subnet and add stake + 3. Verify Alice has a vpermit on each subnet + 4. Lower the set weights rate limit on each subnet + 5. Set weights on each subnet + 6. Assert that all the set weights succeeded + Raises: + AssertionError: If any of the checks or verifications fail + """ + netuids = [1, 2, 3] + utils.EXTRINSIC_SUBMISSION_TIMEOUT = 12 # handle fast blocks + print("Testing test_commit_and_reveal_weights") + # Register root as Alice + keypair, alice_wallet = setup_wallet("//Alice") + + # Lower the network registration rate limit and cost + root_set_subtensor_hyperparameter_values( + local_chain, + alice_wallet, + call_function="sudo_set_network_rate_limit", + call_params={"rate_limit": "0"}, # No limit + return_error_message=True, + ) + # Set lock reduction interval + root_set_subtensor_hyperparameter_values( + local_chain, + alice_wallet, + call_function="sudo_set_lock_reduction_interval", + call_params={"interval": "1"}, # 1 block # reduce lock every block + return_error_message=True, + ) + # Try to register the subnets + for netuid in netuids: + assert register_subnet( + local_chain, alice_wallet, netuid + ), "Unable to register the subnet" + + # Verify all subnets created successfully + assert local_chain.query( + "SubtensorModule", "NetworksAdded", [3] + ).serialize(), "Subnet wasn't created successfully" + + subtensor = Subtensor(network="ws://localhost:9945") + + for netuid in netuids: + # Register Alice to the subnet + assert subtensor.burned_register( + alice_wallet, netuid + ), "Unable to register Alice as a neuron" + + # Stake to become to top neuron after the first epoch + add_stake(local_chain, alice_wallet, Balance.from_tao(100_000)) + + # Set weight hyperparameters per subnet + for netuid in netuids: + assert sudo_set_hyperparameter_bool( + local_chain, + alice_wallet, + "sudo_set_commit_reveal_weights_enabled", + False, + netuid, + ), "Unable to enable commit reveal on the subnet" + + assert subtensor.get_subnet_hyperparameters( + netuid=netuid, + ).commit_reveal_weights_enabled, "Failed to enable commit/reveal" + + assert ( + subtensor.weights_rate_limit(netuid=netuid) > 0 + ), "Weights rate limit is below 0" + # Lower the rate limit + assert sudo_set_hyperparameter_values( + local_chain, + alice_wallet, + call_function="sudo_set_weights_set_rate_limit", + call_params={"netuid": netuid, "weights_set_rate_limit": "0"}, + return_error_message=True, + ) + + assert ( + subtensor.get_subnet_hyperparameters(netuid=netuid).weights_rate_limit == 0 + ), "Failed to set weights_rate_limit" + assert subtensor.weights_rate_limit(netuid=netuid) == 0 + + # Weights values + uids = np.array([0], dtype=np.int64) + weights = np.array([0.1], dtype=np.float32) + weight_uids, weight_vals = convert_weights_and_uids_for_emit( + uids=uids, weights=weights + ) + + # Set weights for each subnet + for netuid in netuids: + success, message = subtensor.commit_weights( + alice_wallet, + netuid, + uids=weight_uids, + weights=weight_vals, + wait_for_inclusion=False, # Don't wait for inclusion, we are testing the nonce when there is a tx in the pool + wait_for_finalization=False, + ) + + assert success is True, f"Failed to set weights for subnet {netuid}" + + # Wait for the txs to be included in the chain + await asyncio.sleep(2) + + for netuid in netuids: + # Query the Weights storage map for all three subnets + weights = subtensor.query_module( + module="SubtensorModule", + name="Weights", + params=[netuid, 0], # Alice should be the only UID + ) + + assert weights is not None, f"Weights not found for subnet {netuid}" + assert weights == list( + zip(weight_uids, weight_vals) + ), f"Weights do not match for subnet {netuid}" From ec28ff2432f06ec564d38fdcabeebfc49787f4b2 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 12 Dec 2024 13:00:06 -0500 Subject: [PATCH 13/25] use high timeout on tests --- tests/e2e_tests/test_commit_weights.py | 2 +- tests/e2e_tests/test_incentive.py | 2 +- tests/e2e_tests/test_set_weights.py | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/e2e_tests/test_commit_weights.py b/tests/e2e_tests/test_commit_weights.py index 3d9ab1c9e2..8ded6e4f5b 100644 --- a/tests/e2e_tests/test_commit_weights.py +++ b/tests/e2e_tests/test_commit_weights.py @@ -35,7 +35,7 @@ async def test_commit_and_reveal_weights(local_chain): AssertionError: If any of the checks or verifications fail """ netuid = 1 - utils.EXTRINSIC_SUBMISSION_TIMEOUT = 12 # handle fast blocks + utils.EXTRINSIC_SUBMISSION_TIMEOUT = 500 # handle fast blocks print("Testing test_commit_and_reveal_weights") # Register root as Alice keypair, alice_wallet = setup_wallet("//Alice") diff --git a/tests/e2e_tests/test_incentive.py b/tests/e2e_tests/test_incentive.py index cfafef42b5..38a36b14da 100644 --- a/tests/e2e_tests/test_incentive.py +++ b/tests/e2e_tests/test_incentive.py @@ -41,7 +41,7 @@ async def test_incentive(local_chain): print("Testing test_incentive") netuid = 1 - utils.EXTRINSIC_SUBMISSION_TIMEOUT = 12 # handle fast blocks + utils.EXTRINSIC_SUBMISSION_TIMEOUT = 500 # handle fast blocks # Register root as Alice - the subnet owner and validator alice_keypair, alice_wallet = setup_wallet("//Alice") diff --git a/tests/e2e_tests/test_set_weights.py b/tests/e2e_tests/test_set_weights.py index ce4833aaf7..1bb72ffdc4 100644 --- a/tests/e2e_tests/test_set_weights.py +++ b/tests/e2e_tests/test_set_weights.py @@ -33,8 +33,8 @@ async def test_set_weights_uses_next_nonce(local_chain): AssertionError: If any of the checks or verifications fail """ netuids = [1, 2, 3] - utils.EXTRINSIC_SUBMISSION_TIMEOUT = 12 # handle fast blocks - print("Testing test_commit_and_reveal_weights") + utils.EXTRINSIC_SUBMISSION_TIMEOUT = 500 # handle fast blocks + print("Testing test_set_weights_uses_next_nonce") # Register root as Alice keypair, alice_wallet = setup_wallet("//Alice") @@ -55,9 +55,9 @@ async def test_set_weights_uses_next_nonce(local_chain): return_error_message=True, ) # Try to register the subnets - for netuid in netuids: + for _ in netuids: assert register_subnet( - local_chain, alice_wallet, netuid + local_chain, alice_wallet ), "Unable to register the subnet" # Verify all subnets created successfully From df88a696afe6437d6bb56df68cb01c0db6d58d4e Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 12 Dec 2024 14:06:47 -0500 Subject: [PATCH 14/25] add awaits and log netuid --- tests/e2e_tests/test_set_weights.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e_tests/test_set_weights.py b/tests/e2e_tests/test_set_weights.py index 1bb72ffdc4..95140329e4 100644 --- a/tests/e2e_tests/test_set_weights.py +++ b/tests/e2e_tests/test_set_weights.py @@ -39,7 +39,7 @@ async def test_set_weights_uses_next_nonce(local_chain): keypair, alice_wallet = setup_wallet("//Alice") # Lower the network registration rate limit and cost - root_set_subtensor_hyperparameter_values( + await root_set_subtensor_hyperparameter_values( local_chain, alice_wallet, call_function="sudo_set_network_rate_limit", @@ -47,7 +47,7 @@ async def test_set_weights_uses_next_nonce(local_chain): return_error_message=True, ) # Set lock reduction interval - root_set_subtensor_hyperparameter_values( + await root_set_subtensor_hyperparameter_values( local_chain, alice_wallet, call_function="sudo_set_lock_reduction_interval", @@ -71,7 +71,7 @@ async def test_set_weights_uses_next_nonce(local_chain): # Register Alice to the subnet assert subtensor.burned_register( alice_wallet, netuid - ), "Unable to register Alice as a neuron" + ), f"Unable to register Alice as a neuron on SN{netuid}" # Stake to become to top neuron after the first epoch add_stake(local_chain, alice_wallet, Balance.from_tao(100_000)) From aab321ab34affe3b62ab8e1195101ce393c821f2 Mon Sep 17 00:00:00 2001 From: camfairchild Date: Mon, 16 Dec 2024 11:17:18 -0500 Subject: [PATCH 15/25] chore: ruff --- tests/e2e_tests/utils/chain_interactions.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/e2e_tests/utils/chain_interactions.py b/tests/e2e_tests/utils/chain_interactions.py index aabafab1ce..bd5829e219 100644 --- a/tests/e2e_tests/utils/chain_interactions.py +++ b/tests/e2e_tests/utils/chain_interactions.py @@ -182,7 +182,7 @@ async def wait_interval( # Helper to execute sudo wrapped calls on the chain def sudo_set_admin_utils( - substrate: "SubstrateInterface", + substrate: "SubstrateInterface", wallet: "Wallet", call_function: str, call_params: dict, @@ -227,6 +227,7 @@ def sudo_set_admin_utils( return response.is_success + async def root_set_subtensor_hyperparameter_values( substrate: "SubstrateInterface", wallet: "Wallet", From 161692bcb1775ba35d888e09b878f510a456b369 Mon Sep 17 00:00:00 2001 From: camfairchild Date: Mon, 16 Dec 2024 11:21:18 -0500 Subject: [PATCH 16/25] use sudo_set_admin_utils instead --- tests/e2e_tests/test_set_weights.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/e2e_tests/test_set_weights.py b/tests/e2e_tests/test_set_weights.py index 95140329e4..d798e7acd7 100644 --- a/tests/e2e_tests/test_set_weights.py +++ b/tests/e2e_tests/test_set_weights.py @@ -13,6 +13,7 @@ sudo_set_hyperparameter_bool, sudo_set_hyperparameter_values, root_set_subtensor_hyperparameter_values, + sudo_set_admin_utils, ) from tests.e2e_tests.utils.e2e_test_utils import setup_wallet @@ -39,7 +40,7 @@ async def test_set_weights_uses_next_nonce(local_chain): keypair, alice_wallet = setup_wallet("//Alice") # Lower the network registration rate limit and cost - await root_set_subtensor_hyperparameter_values( + await sudo_set_admin_utils( local_chain, alice_wallet, call_function="sudo_set_network_rate_limit", @@ -47,7 +48,7 @@ async def test_set_weights_uses_next_nonce(local_chain): return_error_message=True, ) # Set lock reduction interval - await root_set_subtensor_hyperparameter_values( + await sudo_set_admin_utils( local_chain, alice_wallet, call_function="sudo_set_lock_reduction_interval", From b6aa92613ee30299b9813ed93a151596379a68ee Mon Sep 17 00:00:00 2001 From: camfairchild Date: Mon, 16 Dec 2024 11:38:54 -0500 Subject: [PATCH 17/25] don't increment next index for nonce --- bittensor/core/extrinsics/async_weights.py | 8 ++++---- bittensor/core/extrinsics/commit_weights.py | 2 +- bittensor/core/extrinsics/set_weights.py | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/bittensor/core/extrinsics/async_weights.py b/bittensor/core/extrinsics/async_weights.py index 01a9a19621..85a6a0e2ae 100644 --- a/bittensor/core/extrinsics/async_weights.py +++ b/bittensor/core/extrinsics/async_weights.py @@ -59,8 +59,8 @@ async def _do_set_weights( }, ) - next_nonce = ( - await subtensor.substrate.get_account_next_index(wallet.hotkey.ss58_address) + 1 + next_nonce = await subtensor.substrate.get_account_next_index( + wallet.hotkey.ss58_address ) # Period dictates how long the extrinsic will stay as part of waiting pool @@ -193,8 +193,8 @@ async def _do_commit_weights( }, ) - next_nonce = ( - await subtensor.substrate.get_account_next_index(wallet.hotkey.ss58_address) + 1 + next_nonce = await subtensor.substrate.get_account_next_index( + wallet.hotkey.ss58_address ) extrinsic = await subtensor.substrate.create_signed_extrinsic( diff --git a/bittensor/core/extrinsics/commit_weights.py b/bittensor/core/extrinsics/commit_weights.py index 27f3a954ea..4136e0c348 100644 --- a/bittensor/core/extrinsics/commit_weights.py +++ b/bittensor/core/extrinsics/commit_weights.py @@ -66,7 +66,7 @@ def do_commit_weights( "commit_hash": commit_hash, }, ) - next_nonce = self.get_account_next_index(wallet.hotkey.ss58_address) + 1 + next_nonce = self.get_account_next_index(wallet.hotkey.ss58_address) extrinsic = self.substrate.create_signed_extrinsic( call=call, keypair=wallet.hotkey, diff --git a/bittensor/core/extrinsics/set_weights.py b/bittensor/core/extrinsics/set_weights.py index b9ef75ef9d..9cb291a299 100644 --- a/bittensor/core/extrinsics/set_weights.py +++ b/bittensor/core/extrinsics/set_weights.py @@ -76,7 +76,7 @@ def do_set_weights( "version_key": version_key, }, ) - next_nonce = self.get_account_next_index(wallet.hotkey.ss58_address) + 1 + next_nonce = self.get_account_next_index(wallet.hotkey.ss58_address) # Period dictates how long the extrinsic will stay as part of waiting pool extrinsic = self.substrate.create_signed_extrinsic( call=call, From e26763fca1742094063cbf4e63936cf242379d3b Mon Sep 17 00:00:00 2001 From: camfairchild Date: Mon, 16 Dec 2024 11:43:23 -0500 Subject: [PATCH 18/25] dont await sudo_set_admin_utils --- tests/e2e_tests/test_set_weights.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/e2e_tests/test_set_weights.py b/tests/e2e_tests/test_set_weights.py index d798e7acd7..f449227705 100644 --- a/tests/e2e_tests/test_set_weights.py +++ b/tests/e2e_tests/test_set_weights.py @@ -40,7 +40,7 @@ async def test_set_weights_uses_next_nonce(local_chain): keypair, alice_wallet = setup_wallet("//Alice") # Lower the network registration rate limit and cost - await sudo_set_admin_utils( + sudo_set_admin_utils( local_chain, alice_wallet, call_function="sudo_set_network_rate_limit", @@ -48,7 +48,7 @@ async def test_set_weights_uses_next_nonce(local_chain): return_error_message=True, ) # Set lock reduction interval - await sudo_set_admin_utils( + sudo_set_admin_utils( local_chain, alice_wallet, call_function="sudo_set_lock_reduction_interval", From 1f9291118c136ad413f8ff19456ddc9569b70630 Mon Sep 17 00:00:00 2001 From: camfairchild Date: Mon, 16 Dec 2024 11:50:16 -0500 Subject: [PATCH 19/25] lower test ext timeout to 12 --- tests/e2e_tests/test_commit_weights.py | 2 +- tests/e2e_tests/test_incentive.py | 2 +- tests/e2e_tests/test_set_weights.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e_tests/test_commit_weights.py b/tests/e2e_tests/test_commit_weights.py index 8a54096b04..8a5371283e 100644 --- a/tests/e2e_tests/test_commit_weights.py +++ b/tests/e2e_tests/test_commit_weights.py @@ -35,7 +35,7 @@ async def test_commit_and_reveal_weights_legacy(local_chain): AssertionError: If any of the checks or verifications fail """ netuid = 1 - utils.EXTRINSIC_SUBMISSION_TIMEOUT = 500 # handle fast blocks + utils.EXTRINSIC_SUBMISSION_TIMEOUT = 12 # handle fast blocks print("Testing test_commit_and_reveal_weights") # Register root as Alice keypair, alice_wallet = setup_wallet("//Alice") diff --git a/tests/e2e_tests/test_incentive.py b/tests/e2e_tests/test_incentive.py index 38a36b14da..cfafef42b5 100644 --- a/tests/e2e_tests/test_incentive.py +++ b/tests/e2e_tests/test_incentive.py @@ -41,7 +41,7 @@ async def test_incentive(local_chain): print("Testing test_incentive") netuid = 1 - utils.EXTRINSIC_SUBMISSION_TIMEOUT = 500 # handle fast blocks + utils.EXTRINSIC_SUBMISSION_TIMEOUT = 12 # handle fast blocks # Register root as Alice - the subnet owner and validator alice_keypair, alice_wallet = setup_wallet("//Alice") diff --git a/tests/e2e_tests/test_set_weights.py b/tests/e2e_tests/test_set_weights.py index f449227705..a13b8b62b5 100644 --- a/tests/e2e_tests/test_set_weights.py +++ b/tests/e2e_tests/test_set_weights.py @@ -34,7 +34,7 @@ async def test_set_weights_uses_next_nonce(local_chain): AssertionError: If any of the checks or verifications fail """ netuids = [1, 2, 3] - utils.EXTRINSIC_SUBMISSION_TIMEOUT = 500 # handle fast blocks + utils.EXTRINSIC_SUBMISSION_TIMEOUT = 12 # handle fast blocks print("Testing test_set_weights_uses_next_nonce") # Register root as Alice keypair, alice_wallet = setup_wallet("//Alice") From 527d746bbc904fc45898ecb37cf120622ad12049 Mon Sep 17 00:00:00 2001 From: camfairchild Date: Mon, 16 Dec 2024 12:14:16 -0500 Subject: [PATCH 20/25] enable reg during test --- tests/e2e_tests/test_set_weights.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/e2e_tests/test_set_weights.py b/tests/e2e_tests/test_set_weights.py index a13b8b62b5..3c79bf181b 100644 --- a/tests/e2e_tests/test_set_weights.py +++ b/tests/e2e_tests/test_set_weights.py @@ -69,6 +69,14 @@ async def test_set_weights_uses_next_nonce(local_chain): subtensor = Subtensor(network="ws://localhost:9945") for netuid in netuids: + # Allow registration on the subnet + assert sudo_set_hyperparameter_bool( + local_chain, + alice_wallet, + "sudo_set_network_registration_allowed", + True, + netuid, + ) # Register Alice to the subnet assert subtensor.burned_register( alice_wallet, netuid From e3f5c6527f12d33d72c6731b1aa0e7e5feb09698 Mon Sep 17 00:00:00 2001 From: camfairchild Date: Mon, 16 Dec 2024 14:01:05 -0500 Subject: [PATCH 21/25] use set hyp values instead --- tests/e2e_tests/test_set_weights.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e_tests/test_set_weights.py b/tests/e2e_tests/test_set_weights.py index 3c79bf181b..740a893e05 100644 --- a/tests/e2e_tests/test_set_weights.py +++ b/tests/e2e_tests/test_set_weights.py @@ -70,12 +70,12 @@ async def test_set_weights_uses_next_nonce(local_chain): for netuid in netuids: # Allow registration on the subnet - assert sudo_set_hyperparameter_bool( + assert sudo_set_hyperparameter_values( local_chain, alice_wallet, "sudo_set_network_registration_allowed", - True, - netuid, + {"netuid": netuid, "registration_allowed": True}, + return_error_message=True, ) # Register Alice to the subnet assert subtensor.burned_register( From 69d07d845dd6e864a1931f73854f9aa174d03958 Mon Sep 17 00:00:00 2001 From: camfairchild Date: Mon, 16 Dec 2024 14:41:22 -0500 Subject: [PATCH 22/25] sleep after setting reg allowed --- tests/e2e_tests/test_set_weights.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/e2e_tests/test_set_weights.py b/tests/e2e_tests/test_set_weights.py index 740a893e05..6f1e30940c 100644 --- a/tests/e2e_tests/test_set_weights.py +++ b/tests/e2e_tests/test_set_weights.py @@ -77,6 +77,11 @@ async def test_set_weights_uses_next_nonce(local_chain): {"netuid": netuid, "registration_allowed": True}, return_error_message=True, ) + + # This should give a gap for the calls above to be included in the chain + await asyncio.sleep(2) + + for netuid in netuids: # Register Alice to the subnet assert subtensor.burned_register( alice_wallet, netuid From 371dd1e38edac5a4ea3037b56f4c4de566a314cb Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Tue, 17 Dec 2024 09:21:41 -0800 Subject: [PATCH 23/25] fix test_set_weights_uses_next_nonce --- tests/e2e_tests/test_set_weights.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/e2e_tests/test_set_weights.py b/tests/e2e_tests/test_set_weights.py index 6f1e30940c..ce992e368e 100644 --- a/tests/e2e_tests/test_set_weights.py +++ b/tests/e2e_tests/test_set_weights.py @@ -12,7 +12,6 @@ register_subnet, sudo_set_hyperparameter_bool, sudo_set_hyperparameter_values, - root_set_subtensor_hyperparameter_values, sudo_set_admin_utils, ) from tests.e2e_tests.utils.e2e_test_utils import setup_wallet @@ -33,7 +32,7 @@ async def test_set_weights_uses_next_nonce(local_chain): Raises: AssertionError: If any of the checks or verifications fail """ - netuids = [1, 2, 3] + netuids = [1, 2] utils.EXTRINSIC_SUBMISSION_TIMEOUT = 12 # handle fast blocks print("Testing test_set_weights_uses_next_nonce") # Register root as Alice @@ -100,7 +99,7 @@ async def test_set_weights_uses_next_nonce(local_chain): netuid, ), "Unable to enable commit reveal on the subnet" - assert subtensor.get_subnet_hyperparameters( + assert not subtensor.get_subnet_hyperparameters( netuid=netuid, ).commit_reveal_weights_enabled, "Failed to enable commit/reveal" @@ -130,7 +129,7 @@ async def test_set_weights_uses_next_nonce(local_chain): # Set weights for each subnet for netuid in netuids: - success, message = subtensor.commit_weights( + success, message = subtensor.set_weights( alice_wallet, netuid, uids=weight_uids, From a30a8ab7cd436e08bd7ce861c2f51119dbc21bdd Mon Sep 17 00:00:00 2001 From: Benjamin Himes Date: Tue, 17 Dec 2024 19:35:35 +0200 Subject: [PATCH 24/25] Use asyncstdlib for lru_cache of `AsyncSubstrateInterface.supports_rpc_method` --- bittensor/utils/async_substrate_interface.py | 4 ++-- requirements/prod.txt | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bittensor/utils/async_substrate_interface.py b/bittensor/utils/async_substrate_interface.py index 180f64e7b7..eeb5eb1068 100644 --- a/bittensor/utils/async_substrate_interface.py +++ b/bittensor/utils/async_substrate_interface.py @@ -10,10 +10,10 @@ import random from collections import defaultdict from dataclasses import dataclass -from functools import lru_cache from hashlib import blake2b from typing import Optional, Any, Union, Callable, Awaitable, cast, TYPE_CHECKING +import asyncstdlib as a from async_property import async_property from bittensor_wallet import Keypair from bt_decode import PortableRegistry, decode as decode_by_type_string, MetadataV15 @@ -1705,7 +1705,7 @@ def make_payload(id_: str, method: str, params: list) -> dict: "payload": {"jsonrpc": "2.0", "method": method, "params": params}, } - @lru_cache(maxsize=512) # RPC methods are unlikely to change often + @a.lru_cache(maxsize=512) # RPC methods are unlikely to change often async def supports_rpc_method(self, name: str) -> bool: """ Check if substrate RPC supports given method diff --git a/requirements/prod.txt b/requirements/prod.txt index c57ce611f9..12525a2305 100644 --- a/requirements/prod.txt +++ b/requirements/prod.txt @@ -1,6 +1,7 @@ wheel setuptools~=70.0.0 aiohttp~=3.9 +asyncstdlib~=3.13.0 async-property==0.2.2 bittensor-cli bt-decode==0.4.0 From cad3bd055f97fa23cbf3cfe184d85b29c05db000 Mon Sep 17 00:00:00 2001 From: ibraheem-opentensor Date: Tue, 17 Dec 2024 09:50:40 -0800 Subject: [PATCH 25/25] Increases sleep for txs in test_set_weights_uses_next_nonce --- tests/e2e_tests/test_set_weights.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e_tests/test_set_weights.py b/tests/e2e_tests/test_set_weights.py index ce992e368e..edf208ba8d 100644 --- a/tests/e2e_tests/test_set_weights.py +++ b/tests/e2e_tests/test_set_weights.py @@ -141,7 +141,7 @@ async def test_set_weights_uses_next_nonce(local_chain): assert success is True, f"Failed to set weights for subnet {netuid}" # Wait for the txs to be included in the chain - await asyncio.sleep(2) + await asyncio.sleep(4) for netuid in netuids: # Query the Weights storage map for all three subnets