Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mocks, update serving #21

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,5 @@ replicate.yaml

# Notebooks
*.ipynb

temp/
48 changes: 46 additions & 2 deletions cybertensor/cwtensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,9 +763,20 @@ def _do_serve_axon(
PrivateKey(wallet.hotkey.private_key), self.address_prefix
)

msg = {"serve_axon": {
"netuid": call_params['netuid'],
"version": call_params['version'],
"ip": str(call_params['ip']),
"port": call_params['port'],
"ip_type": call_params['ip_type'],
"protocol": call_params['protocol'],
"placeholder1": call_params['placeholder1'],
"placeholder2": call_params['placeholder2'],
}}

return self.make_call_with_retry_2(
wait_for_finalization=wait_for_finalization,
msg=call_params,
msg=msg,
signer_wallet=signer_wallet)

def serve_prometheus(
Expand Down Expand Up @@ -804,9 +815,17 @@ def _do_serve_prometheus(
PrivateKey(wallet.hotkey.private_key), self.address_prefix
)

msg = {"serve_prometheus": {
"netuid": call_params['netuid'],
"version": call_params['version'],
"ip": str(call_params['ip']),
"port": call_params['port'],
"ip_type": call_params['ip_type'],
}}

return self.make_call_with_retry_2(
wait_for_finalization=wait_for_finalization,
msg=call_params,
msg=msg,
signer_wallet=signer_wallet)

#################
Expand Down Expand Up @@ -1056,6 +1075,23 @@ def max_weight_limit(

return U16_NORMALIZED_FLOAT(max_weight_limit)

""" Returns network SubnetworkN hyper parameter """

def subnetwork_n(self, netuid: int, block: Optional[int] = None) -> Optional[int]:
# TODO replace with direct query
# subnetwork_n = self.contract.query({"get_subnetwork_n": {"netuid": netuid}})
# if subnetwork_n is None:
# return None
#
# return subnetwork_n

subnet_info = self.get_subnet_info(netuid)
if subnet_info is None:
return None

return subnet_info.subnetwork_n


""" Returns network Tempo hyper parameter """

def tempo(self, netuid: int, block: Optional[int] = None) -> Optional[int]:
Expand Down Expand Up @@ -1230,6 +1266,14 @@ def get_all_subnets_info(self, block: Optional[int] = None) -> List[SubnetInfo]:

return SubnetInfo.list_from_list_any(result)

def get_subnet_info(self, netuid: int, block: Optional[int] = None) -> Optional[SubnetInfo]:
result = self.contract.query({"get_subnet_info": {"netuid": netuid}})

if result is None:
return None

return SubnetInfo.fix_decoded_values(result)

def get_subnet_hyperparameters(
self, netuid: int, block: Optional[int] = None
) -> Optional[SubnetHyperparameters]:
Expand Down
18 changes: 18 additions & 0 deletions cybertensor/mock/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# The MIT License (MIT)
# Copyright © 2023 Opentensor Technologies Inc

# 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 .cwtensor_mock import MockCwtensor as MockCwtensor
90 changes: 90 additions & 0 deletions cybertensor/mock/keyfile_mock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# 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 cybertensor.keyfile import serialized_keypair_to_keyfile_data, keyfile, 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
Loading
Loading