From 2fb639a7b9299a893b10204294f84358cff014d4 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Wed, 5 Jul 2023 15:40:07 -0400 Subject: [PATCH] move mocks to bt_wallet package --- tests/helpers.py | 45 +--------- tests/mocks/__init__.py | 19 ----- tests/mocks/keyfile_mock.py | 82 ------------------- tests/mocks/wallet_mock.py | 79 ------------------ .../bittensor_tests/utils/test_utils.py | 3 +- 5 files changed, 5 insertions(+), 223 deletions(-) delete mode 100644 tests/mocks/__init__.py delete mode 100644 tests/mocks/keyfile_mock.py delete mode 100644 tests/mocks/wallet_mock.py diff --git a/tests/helpers.py b/tests/helpers.py index 771880dfa3..68d5407a1e 100644 --- a/tests/helpers.py +++ b/tests/helpers.py @@ -15,15 +15,13 @@ # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER # DEALINGS IN THE SOFTWARE. -from typing import Union, Optional -from bittensor import Balance, NeuronInfo, axon_info, PrometheusInfo, Keypair, __ss58_format__ -from scalecodec import ss58_encode +from typing import Union +from bittensor import Balance, NeuronInfo, axon_info, PrometheusInfo, __ss58_format__ from rich.console import Console from rich.text import Text -from tests.mocks.wallet_mock import MockWallet - -from Crypto.Hash import keccak +from bittensor_wallet.mock import MockWallet as MockWallet +from bittensor_wallet.mock.utils import get_mock_wallet as get_mock_wallet, get_mock_hotkey, get_mock_coldkey class CLOSE_IN_VALUE(): value: Union[float, int, Balance] @@ -40,24 +38,7 @@ def __eq__(self, __o: Union[float, int, Balance]) -> bool: ((__o - self.tolerance) <= self.value and self.value <= (__o + self.tolerance)) -def get_mock_keypair( uid: int, test_name: Optional[str] = None ) -> Keypair: - """ - Returns a mock keypair from a uid and optional test_name. - If test_name is not provided, the uid is the only seed. - If test_name is provided, the uid is hashed with the test_name to create a unique seed for the test. - """ - if test_name is not None: - hashed_test_name: bytes = keccak.new(digest_bits=256, data=test_name.encode('utf-8')).digest() - hashed_test_name_as_int: int = int.from_bytes(hashed_test_name, byteorder='big', signed=False) - uid = uid + hashed_test_name_as_int - - return Keypair.create_from_seed( seed_hex = int.to_bytes(uid, 32, 'big', signed=False), ss58_format = __ss58_format__) - -def get_mock_hotkey( uid: int ) -> str: - return get_mock_keypair(uid).ss58_address -def get_mock_coldkey( uid: int ) -> str: - return get_mock_keypair(uid).ss58_address def get_mock_neuron(**kwargs) -> NeuronInfo: """ @@ -129,24 +110,6 @@ def get_mock_neuron_by_uid( uid: int, **kwargs ) -> NeuronInfo: **kwargs ) -def get_mock_wallet(coldkey: "Keypair" = None, hotkey: "Keypair" = None): - wallet = MockWallet( - name = 'mock_wallet', - hotkey = 'mock', - path = '/tmp/mock_wallet', - ) - - if not coldkey: - coldkey = Keypair.create_from_mnemonic(Keypair.generate_mnemonic()) - if not hotkey: - hotkey = Keypair.create_from_mnemonic(Keypair.generate_mnemonic()) - - wallet.set_coldkey(coldkey, encrypt=False, overwrite=True) - wallet.set_coldkeypub(coldkey, encrypt=False, overwrite=True) - wallet.set_hotkey(hotkey, encrypt=False, overwrite=True) - - return wallet - class MockStatus: def __enter__(self): return self diff --git a/tests/mocks/__init__.py b/tests/mocks/__init__.py deleted file mode 100644 index a5edffa7bd..0000000000 --- a/tests/mocks/__init__.py +++ /dev/null @@ -1,19 +0,0 @@ -# The MIT License (MIT) -# Copyright © 2023 Opentensor Technologies - -# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated -# documentation files (the “Software”), to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, -# and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -# The above copyright notice and this permission notice shall be included in all copies or substantial portions of -# the Software. - -# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO -# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. - -from .wallet_mock import MockWallet as MockWallet -from .keyfile_mock import MockKeyfile as MockKeyfile \ No newline at end of file diff --git a/tests/mocks/keyfile_mock.py b/tests/mocks/keyfile_mock.py deleted file mode 100644 index 92b40d8bd3..0000000000 --- a/tests/mocks/keyfile_mock.py +++ /dev/null @@ -1,82 +0,0 @@ -# The MIT License (MIT) - -# Copyright © 2021 Yuma Rao -# Copyright © 2022 Opentensor Foundation -# Copyright © 2023 Opentensor Technologies - -# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated -# documentation files (the “Software”), to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, -# and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -# The above copyright notice and this permission notice shall be included in all copies or substantial portions of -# the Software. - -# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO -# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. - -from bittensor_wallet import serialized_keypair_to_keyfile_data, Keyfile -from bittensor_wallet import Keypair - -class MockKeyfile( Keyfile ): - """ Defines an interface to a mocked keyfile object (nothing is created on device) keypair is treated as non encrypted and the data is just the string version. - """ - def __init__( self, path: str ): - super().__init__( path ) - - self._mock_keypair = Keypair.create_from_mnemonic( mnemonic = 'arrive produce someone view end scout bargain coil slight festival excess struggle' ) - self._mock_data = serialized_keypair_to_keyfile_data( self._mock_keypair ) - - def __str__(self): - if not self.exists_on_device(): - return "Keyfile (empty, {})>".format( self.path ) - if self.is_encrypted(): - return "Keyfile (encrypted, {})>".format( self.path ) - else: - return "Keyfile (decrypted, {})>".format( self.path ) - - def __repr__(self): - return self.__str__() - - @property - def keypair( self ) -> 'Keypair': - return self._mock_keypair - - @property - def data( self ) -> bytes: - return bytes(self._mock_data) - - @property - def keyfile_data( self ) -> bytes: - return bytes( self._mock_data) - - def set_keypair ( self, keypair: 'Keypair', encrypt: bool = True, overwrite: bool = False, password:str = None): - self._mock_keypair = keypair - self._mock_data = serialized_keypair_to_keyfile_data( self._mock_keypair ) - - def get_keypair(self, password: str = None) -> 'Keypair': - return self._mock_keypair - - def make_dirs( self ): - return - - def exists_on_device( self ) -> bool: - return True - - def is_readable( self ) -> bool: - return True - - def is_writable( self ) -> bool: - return True - - def is_encrypted ( self ) -> bool: - return False - - def encrypt( self, password: str = None): - raise ValueError('Cannot encrypt a mock keyfile') - - def decrypt( self, password: str = None): - return diff --git a/tests/mocks/wallet_mock.py b/tests/mocks/wallet_mock.py deleted file mode 100644 index 6e7a941ea9..0000000000 --- a/tests/mocks/wallet_mock.py +++ /dev/null @@ -1,79 +0,0 @@ -# The MIT License (MIT) - -# Copyright © 2021 Yuma Rao -# Copyright © 2022 Opentensor Foundation -# Copyright © 2023 Opentensor Technologies - -# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated -# documentation files (the “Software”), to deal in the Software without restriction, including without limitation -# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, -# and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -# The above copyright notice and this permission notice shall be included in all copies or substantial portions of -# the Software. - -# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO -# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL -# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -# DEALINGS IN THE SOFTWARE. - -import os -import bittensor -import bittensor_wallet - -from .keyfile_mock import MockKeyfile - -class MockWallet(bittensor_wallet.Wallet): - """ - Mocked Version of the bittensor wallet class, meant to be used for testing - """ - def __init__( - self, - **kwargs, - ): - r""" Init bittensor wallet object containing a hot and coldkey. - Args: - _mock (required=True, default=False): - If true creates a mock wallet with random keys. - """ - super().__init__(**kwargs) - # For mocking. - self._is_mock = True - self._mocked_coldkey_keyfile = None - self._mocked_hotkey_keyfile = None - - print("---- MOCKED WALLET INITIALIZED- ---") - - @property - def hotkey_file(self) -> 'bittensor_wallet.Keyfile': - if self._is_mock: - if self._mocked_hotkey_keyfile == None: - self._mocked_hotkey_keyfile = MockKeyfile(path='MockedHotkey') - return self._mocked_hotkey_keyfile - else: - wallet_path = os.path.expanduser(os.path.join(self.path, self.name)) - hotkey_path = os.path.join(wallet_path, "hotkeys", self.hotkey_str) - return bittensor.keyfile( path = hotkey_path ) - - @property - def coldkey_file(self) -> 'bittensor_wallet.Keyfile': - if self._is_mock: - if self._mocked_coldkey_keyfile == None: - self._mocked_coldkey_keyfile = MockKeyfile(path='MockedColdkey') - return self._mocked_coldkey_keyfile - else: - wallet_path = os.path.expanduser(os.path.join(self.path, self.name)) - coldkey_path = os.path.join(wallet_path, "coldkey") - return bittensor.keyfile( path = coldkey_path ) - - @property - def coldkeypub_file(self) -> 'bittensor_wallet.Keyfile': - if self._is_mock: - if self._mocked_coldkey_keyfile == None: - self._mocked_coldkey_keyfile = MockKeyfile(path='MockedColdkeyPub') - return self._mocked_coldkey_keyfile - else: - wallet_path = os.path.expanduser(os.path.join(self.path, self.name)) - coldkeypub_path = os.path.join(wallet_path, "coldkeypub.txt") - return bittensor_wallet.Keyfile( path = coldkeypub_path ) \ No newline at end of file diff --git a/tests/unit_tests/bittensor_tests/utils/test_utils.py b/tests/unit_tests/bittensor_tests/utils/test_utils.py index bd396690f4..9d665a5e27 100644 --- a/tests/unit_tests/bittensor_tests/utils/test_utils.py +++ b/tests/unit_tests/bittensor_tests/utils/test_utils.py @@ -26,8 +26,7 @@ from bittensor.utils.registration import _CUDASolver, _SolverBase from bittensor._subtensor.subtensor_mock import MockSubtensor -from tests.mocks.wallet_mock import MockWallet -from tests.helpers import get_mock_wallet as generate_wallet, get_mock_keypair +from tests.helpers import get_mock_wallet as generate_wallet, get_mock_keypair, MockWallet @fixture(scope="function")