Skip to content

Commit

Permalink
test: fix old (sync subtensor) unittests
Browse files Browse the repository at this point in the history
  • Loading branch information
zyzniewski-reef committed Jan 24, 2025
1 parent ef50ff5 commit b9e70f0
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 73 deletions.
4 changes: 2 additions & 2 deletions bittensor/core/extrinsics/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
44 changes: 22 additions & 22 deletions tests/unit_tests/extrinsics/test_commit_reveal.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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,
)
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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]
Expand Down
51 changes: 32 additions & 19 deletions tests/unit_tests/extrinsics/test_commit_weights.py
Original file line number Diff line number Diff line change
@@ -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,
)


Expand All @@ -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,
Expand All @@ -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]
Expand All @@ -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,
Expand All @@ -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,
)
4 changes: 2 additions & 2 deletions tests/unit_tests/extrinsics/test_serving.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 11 additions & 13 deletions tests/unit_tests/extrinsics/test_set_weights.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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.")


Expand All @@ -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,
Expand Down Expand Up @@ -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`.",
Expand All @@ -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,
Expand Down Expand Up @@ -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,
)
Expand Down
Loading

0 comments on commit b9e70f0

Please sign in to comment.