diff --git a/bittensor/core/extrinsics/registration.py b/bittensor/core/extrinsics/registration.py index eef316f387..d5211ee199 100644 --- a/bittensor/core/extrinsics/registration.py +++ b/bittensor/core/extrinsics/registration.py @@ -11,12 +11,12 @@ from bittensor.utils import unlock_key from bittensor.utils.btlogging import logging -from bittensor.utils.registration import log_no_torch_error, torch +from bittensor.utils.registration import create_pow, log_no_torch_error, torch if TYPE_CHECKING: from bittensor_wallet import Wallet from bittensor.core.subtensor import Subtensor - from bittensor.utils.registration.pow import POWSolution, create_pow + from bittensor.utils.registration.pow import POWSolution def _do_burned_register( diff --git a/tests/unit_tests/extrinsics/test_commit_reveal.py b/tests/unit_tests/extrinsics/test_commit_reveal.py index e1f7c9f877..c0beedc6ab 100644 --- a/tests/unit_tests/extrinsics/test_commit_reveal.py +++ b/tests/unit_tests/extrinsics/test_commit_reveal.py @@ -1,10 +1,12 @@ +import numpy as np +import pytest +import torch +from bittensor_wallet import Wallet + from bittensor.core import subtensor as subtensor_module from bittensor.core.chain_data import SubnetHyperparameters -from bittensor.core.subtensor import Subtensor from bittensor.core.extrinsics import commit_reveal -import pytest -import torch -import numpy as np +from bittensor.core.subtensor import Subtensor @pytest.fixture @@ -53,7 +55,7 @@ def hyperparams(): def test_do_commit_reveal_v3_success(mocker, subtensor): """Test successful commit-reveal with wait for finalization.""" # Preps - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) + fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit = b"fake_commit" fake_reveal_round = 1 @@ -62,11 +64,11 @@ def test_do_commit_reveal_v3_success(mocker, subtensor): mocked_create_signed_extrinsic = mocker.patch.object( subtensor.substrate, "create_signed_extrinsic" ) - mocked_submit_extrinsic = mocker.patch.object(commit_reveal, "submit_extrinsic") + mocked_submit_extrinsic = mocker.patch.object(subtensor.substrate, "submit_extrinsic") # Call result = commit_reveal._do_commit_reveal_v3( - self=subtensor, + subtensor=subtensor, wallet=fake_wallet, netuid=fake_netuid, commit=fake_commit, @@ -87,18 +89,17 @@ def test_do_commit_reveal_v3_success(mocker, subtensor): call=mocked_compose_call.return_value, keypair=fake_wallet.hotkey ) mocked_submit_extrinsic.assert_called_once_with( - subtensor=subtensor, - extrinsic=mocked_create_signed_extrinsic.return_value, + mocked_create_signed_extrinsic.return_value, wait_for_inclusion=False, wait_for_finalization=False, ) - assert result == (True, "Not waiting for finalization or inclusion.") + assert result == (True, "") def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor): """Test commit-reveal fails due to an error in submission.""" # Preps - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) + fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_commit = b"fake_commit" fake_reveal_round = 1 @@ -108,17 +109,17 @@ def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor): subtensor.substrate, "create_signed_extrinsic" ) mocked_submit_extrinsic = mocker.patch.object( - commit_reveal, + subtensor.substrate, "submit_extrinsic", return_value=mocker.Mock(is_success=False, error_message="Mocked error"), ) mocked_format_error_message = mocker.patch.object( - commit_reveal, "format_error_message", return_value="Formatted error" + subtensor_module, "format_error_message", return_value="Formatted error" ) # Call result = commit_reveal._do_commit_reveal_v3( - self=subtensor, + subtensor=subtensor, wallet=fake_wallet, netuid=fake_netuid, commit=fake_commit, @@ -141,8 +142,7 @@ def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor): call=mocked_compose_call.return_value, keypair=fake_wallet.hotkey ) mocked_submit_extrinsic.assert_called_once_with( - subtensor=subtensor, - extrinsic=mocked_create_signed_extrinsic.return_value, + mocked_create_signed_extrinsic.return_value, wait_for_inclusion=True, wait_for_finalization=True, ) @@ -153,7 +153,7 @@ def test_do_commit_reveal_v3_failure_due_to_error(mocker, subtensor): def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor, hyperparams): """Test successful commit-reveal with torch tensors.""" # Preps - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) + fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = torch.tensor([1, 2, 3], dtype=torch.int64) fake_weights = torch.tensor([0.1, 0.2, 0.7], dtype=torch.float32) @@ -214,7 +214,7 @@ def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor, hyperp current_block=mock_block.return_value, ) mock_do_commit_reveal_v3.assert_called_once_with( - self=subtensor, + subtensor=subtensor, wallet=fake_wallet, netuid=fake_netuid, commit=fake_commit_for_reveal, @@ -227,7 +227,7 @@ def test_commit_reveal_v3_extrinsic_success_with_torch(mocker, subtensor, hyperp def test_commit_reveal_v3_extrinsic_success_with_numpy(mocker, subtensor, hyperparams): """Test successful commit-reveal with numpy arrays.""" # Preps - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) + fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = np.array([1, 2, 3], dtype=np.int64) fake_weights = np.array([0.1, 0.2, 0.7], dtype=np.float32) @@ -272,7 +272,7 @@ def test_commit_reveal_v3_extrinsic_success_with_numpy(mocker, subtensor, hyperp def test_commit_reveal_v3_extrinsic_response_false(mocker, subtensor, hyperparams): """Test unsuccessful commit-reveal with torch.""" # Preps - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) + fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = torch.tensor([1, 2, 3], dtype=torch.int64) fake_weights = torch.tensor([0.1, 0.2, 0.7], dtype=torch.float32) @@ -315,7 +315,7 @@ def test_commit_reveal_v3_extrinsic_response_false(mocker, subtensor, hyperparam assert success is False assert message == "Failed" mock_do_commit_reveal_v3.assert_called_once_with( - self=subtensor, + subtensor=subtensor, wallet=fake_wallet, netuid=fake_netuid, commit=fake_commit_for_reveal, @@ -328,7 +328,7 @@ def test_commit_reveal_v3_extrinsic_response_false(mocker, subtensor, hyperparam def test_commit_reveal_v3_extrinsic_exception(mocker, subtensor): """Test exception handling in commit-reveal.""" # Preps - fake_wallet = mocker.Mock(autospec=subtensor_module.Wallet) + fake_wallet = mocker.Mock(autospec=Wallet) fake_netuid = 1 fake_uids = [1, 2, 3] fake_weights = [0.1, 0.2, 0.7] diff --git a/tests/unit_tests/extrinsics/test_commit_weights.py b/tests/unit_tests/extrinsics/test_commit_weights.py index 57d78a8013..42cc1f2311 100644 --- a/tests/unit_tests/extrinsics/test_commit_weights.py +++ b/tests/unit_tests/extrinsics/test_commit_weights.py @@ -1,11 +1,12 @@ import pytest +from bittensor_wallet import Wallet from bittensor.core import subtensor as subtensor_module from bittensor.core.settings import version_as_int from bittensor.core.subtensor import Subtensor from bittensor.core.extrinsics.commit_weights import ( - do_commit_weights, - do_reveal_weights, + _do_commit_weights, + _do_reveal_weights, ) @@ -30,12 +31,15 @@ def test_do_commit_weights(subtensor, mocker): subtensor.substrate.submit_extrinsic.return_value.is_success = None - mocked_format_error_message = mocker.MagicMock() - subtensor_module.format_error_message = mocked_format_error_message + mocked_format_error_message = mocker.Mock() + mocker.patch( + "bittensor.core.extrinsics.commit_weights.format_error_message", + mocked_format_error_message, + ) # Call - result = do_commit_weights( - self=subtensor, + result = _do_commit_weights( + subtensor=subtensor, wallet=fake_wallet, netuid=netuid, commit_hash=commit_hash, @@ -59,24 +63,26 @@ def test_do_commit_weights(subtensor, mocker): assert kwargs["keypair"] == fake_wallet.hotkey subtensor.substrate.submit_extrinsic.assert_called_once_with( - subtensor.substrate.create_signed_extrinsic.return_value, + extrinsic=subtensor.substrate.create_signed_extrinsic.return_value, wait_for_inclusion=wait_for_inclusion, wait_for_finalization=wait_for_finalization, ) - subtensor.substrate.submit_extrinsic.return_value.process_events.assert_called_once() + mocked_format_error_message.assert_called_once_with( + subtensor.substrate.submit_extrinsic.return_value.error_message, + ) assert result == ( False, - subtensor.substrate.submit_extrinsic.return_value.error_message, + mocked_format_error_message.return_value, ) def test_do_reveal_weights(subtensor, mocker): """Verifies that the `_do_reveal_weights` method interacts with the right substrate methods.""" # Preps - fake_wallet = mocker.MagicMock() - fake_wallet.hotkey = "hotkey" + fake_wallet = mocker.MagicMock(autospec=Wallet) + fake_wallet.hotkey.ss58_address = "hotkey" netuid = 1 uids = [1, 2, 3, 4] @@ -87,12 +93,15 @@ def test_do_reveal_weights(subtensor, mocker): subtensor.substrate.submit_extrinsic.return_value.is_success = None - mocked_format_error_message = mocker.MagicMock() - subtensor_module.format_error_message = mocked_format_error_message + mocked_format_error_message = mocker.Mock() + mocker.patch( + "bittensor.core.extrinsics.commit_weights.format_error_message", + mocked_format_error_message, + ) # Call - result = do_reveal_weights( - self=subtensor, + result = _do_reveal_weights( + subtensor=subtensor, wallet=fake_wallet, netuid=netuid, uids=uids, @@ -117,18 +126,22 @@ def test_do_reveal_weights(subtensor, mocker): ) subtensor.substrate.create_signed_extrinsic.assert_called_once_with( - call=subtensor.substrate.compose_call.return_value, keypair=fake_wallet.hotkey + call=subtensor.substrate.compose_call.return_value, + keypair=fake_wallet.hotkey, + nonce=subtensor.substrate.get_account_next_index.return_value, ) subtensor.substrate.submit_extrinsic.assert_called_once_with( - subtensor.substrate.create_signed_extrinsic.return_value, + extrinsic=subtensor.substrate.create_signed_extrinsic.return_value, wait_for_inclusion=wait_for_inclusion, wait_for_finalization=wait_for_finalization, ) - subtensor.substrate.submit_extrinsic.return_value.process_events.assert_called_once() + mocked_format_error_message.assert_called_once_with( + subtensor.substrate.submit_extrinsic.return_value.error_message, + ) assert result == ( False, - subtensor.substrate.submit_extrinsic.return_value.error_message, + mocked_format_error_message.return_value, ) diff --git a/tests/unit_tests/extrinsics/test_serving.py b/tests/unit_tests/extrinsics/test_serving.py index 46eef17888..6d00e97629 100644 --- a/tests/unit_tests/extrinsics/test_serving.py +++ b/tests/unit_tests/extrinsics/test_serving.py @@ -291,7 +291,7 @@ def test_serve_axon_extrinsic( serving.do_serve_axon = mocker.MagicMock(return_value=(serve_success, "")) # Act if not external_ip_success: - with pytest.raises(RuntimeError): + with pytest.raises(ConnectionError): serving.serve_axon_extrinsic( mock_subtensor, netuid, @@ -364,7 +364,7 @@ def test_publish_metadata( ): # Act result = serving.publish_metadata( - self=mock_subtensor, + subtensor=mock_subtensor, wallet=mock_wallet, netuid=net_uid, data_type=type_u, diff --git a/tests/unit_tests/extrinsics/test_set_weights.py b/tests/unit_tests/extrinsics/test_set_weights.py index 6c070bf5c4..a2aaa4aaab 100644 --- a/tests/unit_tests/extrinsics/test_set_weights.py +++ b/tests/unit_tests/extrinsics/test_set_weights.py @@ -6,7 +6,7 @@ from bittensor.core import subtensor as subtensor_module from bittensor.core.extrinsics.set_weights import ( - do_set_weights, + _do_set_weights, set_weights_extrinsic, ) from bittensor.core.settings import version_as_int @@ -81,9 +81,9 @@ def test_set_weights_extrinsic( "bittensor.utils.weight_utils.convert_weights_and_uids_for_emit", return_value=(uids_tensor, weights_tensor), ), patch( - "bittensor.core.extrinsics.set_weights.do_set_weights", + "bittensor.core.extrinsics.set_weights._do_set_weights", return_value=(expected_success, "Mock error message"), - ) as mock_do_set_weights: + ): result, message = set_weights_extrinsic( subtensor=mock_subtensor, wallet=mock_wallet, @@ -112,8 +112,8 @@ def test_do_set_weights_is_success(mock_subtensor, mocker): mock_subtensor.substrate.submit_extrinsic.return_value.is_success = True # Call - result = do_set_weights( - self=mock_subtensor, + result = _do_set_weights( + subtensor=mock_subtensor, wallet=fake_wallet, uids=fake_uids, vals=fake_vals, @@ -141,7 +141,6 @@ def test_do_set_weights_is_success(mock_subtensor, mocker): 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.") @@ -160,8 +159,8 @@ def test_do_set_weights_is_not_success(mock_subtensor, mocker): subtensor_module.format_error_message = mocked_format_error_message # Call - result = do_set_weights( - self=mock_subtensor, + result = _do_set_weights( + subtensor=mock_subtensor, wallet=fake_wallet, uids=fake_uids, vals=fake_vals, @@ -190,12 +189,11 @@ def test_do_set_weights_is_not_success(mock_subtensor, mocker): assert kwargs["era"] == {"period": 5} mock_subtensor.substrate.submit_extrinsic.assert_called_once_with( - mock_subtensor.substrate.create_signed_extrinsic.return_value, + extrinsic=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.submit_extrinsic.return_value.process_events.assert_called_once() assert result == ( False, "Subtensor returned `UnknownError(UnknownType)` error. This means: `Unknown Description`.", @@ -213,8 +211,8 @@ def test_do_set_weights_no_waits(mock_subtensor, mocker): fake_wait_for_finalization = False # Call - result = do_set_weights( - self=mock_subtensor, + result = _do_set_weights( + subtensor=mock_subtensor, wallet=fake_wallet, uids=fake_uids, vals=fake_vals, @@ -243,7 +241,7 @@ def test_do_set_weights_no_waits(mock_subtensor, mocker): assert kwargs["era"] == {"period": 5} mock_subtensor.substrate.submit_extrinsic.assert_called_once_with( - mock_subtensor.substrate.create_signed_extrinsic.return_value, + extrinsic=mock_subtensor.substrate.create_signed_extrinsic.return_value, wait_for_inclusion=fake_wait_for_inclusion, wait_for_finalization=fake_wait_for_finalization, ) diff --git a/tests/unit_tests/extrinsics/test_transfer.py b/tests/unit_tests/extrinsics/test_transfer.py index af59d5769b..b5cb97483d 100644 --- a/tests/unit_tests/extrinsics/test_transfer.py +++ b/tests/unit_tests/extrinsics/test_transfer.py @@ -1,7 +1,7 @@ import pytest from bittensor.core import subtensor as subtensor_module -from bittensor.core.extrinsics.transfer import do_transfer +from bittensor.core.extrinsics.transfer import _do_transfer from bittensor.core.subtensor import Subtensor from bittensor.utils.balance import Balance @@ -28,7 +28,7 @@ def test_do_transfer_is_success_true(subtensor, mocker): subtensor.substrate.submit_extrinsic.return_value.is_success = True # Call - result = do_transfer( + result = _do_transfer( subtensor, fake_wallet, fake_dest, @@ -47,15 +47,15 @@ def test_do_transfer_is_success_true(subtensor, mocker): call=subtensor.substrate.compose_call.return_value, keypair=fake_wallet.coldkey ) subtensor.substrate.submit_extrinsic.assert_called_once_with( - subtensor.substrate.create_signed_extrinsic.return_value, + extrinsic=subtensor.substrate.create_signed_extrinsic.return_value, wait_for_inclusion=fake_wait_for_inclusion, wait_for_finalization=fake_wait_for_finalization, ) - subtensor.substrate.submit_extrinsic.return_value.process_events.assert_called_once() + # subtensor.substrate.submit_extrinsic.return_value.process_events.assert_called_once() assert result == ( True, subtensor.substrate.submit_extrinsic.return_value.block_hash, - None, + "Success with response.", ) @@ -70,11 +70,14 @@ def test_do_transfer_is_success_false(subtensor, mocker): subtensor.substrate.submit_extrinsic.return_value.is_success = False - mocked_format_error_message = mocker.MagicMock() - subtensor_module.format_error_message = mocked_format_error_message + mocked_format_error_message = mocker.Mock() + mocker.patch( + "bittensor.core.extrinsics.transfer.format_error_message", + mocked_format_error_message, + ) # Call - result = do_transfer( + result = _do_transfer( subtensor, fake_wallet, fake_dest, @@ -93,16 +96,16 @@ def test_do_transfer_is_success_false(subtensor, mocker): call=subtensor.substrate.compose_call.return_value, keypair=fake_wallet.coldkey ) subtensor.substrate.submit_extrinsic.assert_called_once_with( - subtensor.substrate.create_signed_extrinsic.return_value, + extrinsic=subtensor.substrate.create_signed_extrinsic.return_value, wait_for_inclusion=fake_wait_for_inclusion, wait_for_finalization=fake_wait_for_finalization, ) - subtensor.substrate.submit_extrinsic.return_value.process_events.assert_called_once() + mocked_format_error_message.assert_called_once_with(subtensor.substrate.submit_extrinsic.return_value.error_message) assert result == ( False, - None, - subtensor.substrate.submit_extrinsic.return_value.error_message, + "", + mocked_format_error_message.return_value, ) @@ -116,7 +119,7 @@ def test_do_transfer_no_waits(subtensor, mocker): fake_wait_for_finalization = False # Call - result = do_transfer( + result = _do_transfer( subtensor, fake_wallet, fake_dest, @@ -135,8 +138,8 @@ def test_do_transfer_no_waits(subtensor, mocker): call=subtensor.substrate.compose_call.return_value, keypair=fake_wallet.coldkey ) subtensor.substrate.submit_extrinsic.assert_called_once_with( - subtensor.substrate.create_signed_extrinsic.return_value, + extrinsic=subtensor.substrate.create_signed_extrinsic.return_value, wait_for_inclusion=fake_wait_for_inclusion, wait_for_finalization=fake_wait_for_finalization, ) - assert result == (True, None, None) + assert result == (True, "", "Success, extrinsic submitted without waiting.")