Skip to content

Commit

Permalink
tests fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Sep 3, 2024
1 parent b9ffee8 commit e459558
Show file tree
Hide file tree
Showing 64 changed files with 237 additions and 1,253 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion commune/executor/executor.py → commune/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import weakref
import itertools
import threading

from loguru import logger
from typing import Callable
import concurrent
Expand Down
48 changes: 45 additions & 3 deletions commune/key/key.py → commune/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,47 @@
import ed25519_zebra
import commune as c


class KeypairType:
"""
Type of cryptography, used in `Keypair` instance to encrypt and sign data
* ED25519 = 0
* SR25519 = 1
* ECDSA = 2
"""
ED25519 = 0
SR25519 = 1
ECDSA = 2


class MnemonicLanguageCode:
"""
Available language codes to generate mnemonics
* ENGLISH = 'en'
* CHINESE_SIMPLIFIED = 'zh-hans'
* CHINESE_TRADITIONAL = 'zh-hant'
* FRENCH = 'fr'
* ITALIAN = 'it'
* JAPANESE = 'ja'
* KOREAN = 'ko'
* SPANISH = 'es'
"""
ENGLISH = 'en'
CHINESE_SIMPLIFIED = 'zh-hans'
CHINESE_TRADITIONAL = 'zh-hant'
FRENCH = 'fr'
ITALIAN = 'it'
JAPANESE = 'ja'
KOREAN = 'ko'
SPANISH = 'es'




__all__ = ['Keypair', 'KeypairType', 'MnemonicLanguageCode']


Expand Down Expand Up @@ -340,6 +381,7 @@ def get_key(cls,
if hasattr(path, 'ss58_address'):
key = path
return key
path = path or 'module'
# if ss58_address is provided, get key from address
if cls.valid_ss58_address(path):
path = cls.address2key().get(path)
Expand Down Expand Up @@ -1106,7 +1148,7 @@ def resolve_aes_key(self, password = None):
if password == None:
c.print('Using the KeyPair private key as the encryption key')
password = self.mnemonic or self.private_key
return c.module('key.aes')(c.hash(password))
return c.module('aes')(c.hash(password))

def decrypt(self, data: Union[str, bytes], password=None, **kwargs) -> bytes:
aes_key = self.resolve_aes_key(password)
Expand Down Expand Up @@ -1400,10 +1442,10 @@ def id_card(self, return_json=True,**kwargs):

def ticket(self, *args, key=None, **kwargs):
key = key or self.key
return c.module('key.ticket')().ticket(*args,key=key, **kwargs)
return c.module('ticket')().ticket(*args,key=key, **kwargs)

def verify_ticket(self, ticket, **kwargs):
return c.module('key.ticket')().verify(ticket, key=self.key, **kwargs)
return c.module('ticket')().verify(ticket, key=self.key, **kwargs)

def to_mnemonic(self, password=None):
from mnemonic import Mnemonic
Expand Down
38 changes: 0 additions & 38 deletions commune/key/types.py

This file was deleted.

43 changes: 8 additions & 35 deletions commune/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,25 +453,8 @@ def is_folder_module(cls, module = None) -> bool:
is_module_folder = is_folder_module

@classmethod
def get_key(cls,key:str = None ,mode='commune', **kwargs) -> None:
mode2module = {
'commune': 'key',
'subspace': 'subspace.key',
'substrate': 'web3.account.substrate',
'evm': 'web3.account.evm',
'aes': 'key.aes',
}

key = cls.resolve_keypath(key)
if 'Keypair' in c.type_str(key):
return key
module = c.module(mode2module[mode])
if hasattr(module, 'get_key'):
key = module.get_key(key, **kwargs)
else:
key = module(key, **kwargs)

return key
def get_key(cls,key:str = None , **kwargs) -> None:
return c.module('key').get_key(key, **kwargs)

@classmethod
def id(self):
Expand Down Expand Up @@ -651,20 +634,12 @@ def local_module(cls, filename_options = ['module', 'agent', 'block'], cache=Tru

# local update
@classmethod
def update(cls,
module = None,
namespace: bool = False,
subspace: bool = False,
network: str = 'local',
**kwargs
):
responses = []
if module != None:
return c.module(module).update()
# update local namespace
if namespace:
responses.append(c.namespace(network=network, update=True))
return {'success': True, 'responses': responses}
def update(cls, ):

c.namespace(update=True)
c.ip(update=1)
return {'ip': c.ip(), 'namespace': c.namespace()}


@classmethod
def set_key(self, key:str, **kwargs) -> None:
Expand Down Expand Up @@ -6124,8 +6099,6 @@ def threads(cls, search:str = None):





c.enable_routes()
# c.add_utils()

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions commune/modules/sandbox.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import commune as c

print(dir(c.import_module('commune.module._storage')))
print('FAM')
print(c.namespace())
4 changes: 4 additions & 0 deletions commune/modules/subspace/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@




File renamed without changes.
File renamed without changes.
15 changes: 8 additions & 7 deletions commune/server/namespace.py → commune/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def clean_namespace(cls, namespace):
namespace = { k.replace('"', ''): v for k,v in namespace.items() }
return namespace
@classmethod
def update_namespace(cls, network, netuid=None, timeout=5, search=None, verbose=False):
def update_namespace(cls, network, netuid=None, timeout=1, search=None, verbose=False):
c.print(f'UPDATING --> NETWORK(network={network} netuid={netuid})', color='blue')
if 'subspace' in network:
if '.' in network:
Expand All @@ -72,13 +72,14 @@ def update_namespace(cls, network, netuid=None, timeout=5, search=None, verbose=
try:
for f in c.as_completed(futures, timeout=timeout):
address = future2address[f]
try:
name = f.result()
name = f.result()
if isinstance(name, str):
namespace[name] = address
except Exception as e:
c.print(f'Error {e} with {name} and {address}', color='red', verbose=True)
else:
print(f'Error: {name}')
except Exception as e:
c.print(f'Error: {e}', color='red', verbose=True)
print(e)

namespace = {k:v for k,v in namespace.items() if 'Error' not in k}
namespace = {k: '0.0.0.0:' + str(v.split(':')[-1]) for k,v in namespace.items() }
else:
Expand Down Expand Up @@ -159,7 +160,7 @@ def module_exists(cls, module:str, network:str=network) -> bool:
@classmethod
def check_servers(self, *args, **kwargs):
servers = c.pm2ls()
namespace = c.namespace(*args, **kwargs)
namespace = c.get_namespace(*args, **kwargs)
c.print('Checking servers', color='blue')
for server in servers:
if server in namespace:
Expand Down
2 changes: 1 addition & 1 deletion commune/serializer/serializer.py → commune/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def deserialize(self, x) -> object:
return x

def serializer_map(self):
type_path = self.dirpath() + '/types'
type_path = self.dirpath() + '/serializers'
module_paths = c.find_objects(type_path)
return {p.split('.')[-2]: c.obj(p)() for p in module_paths}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 20 additions & 4 deletions commune/server/server.py → commune/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,25 @@
import uvicorn
import json
import asyncio
from .middleware import ServerMiddleware
from fastapi.responses import JSONResponse
from starlette.middleware.base import BaseHTTPMiddleware
from sse_starlette.sse import EventSourceResponse

class ServerMiddleware(BaseHTTPMiddleware):
def __init__(self, app, max_bytes: int):
super().__init__(app)
self.max_bytes = max_bytes
async def dispatch(self, request: Request, call_next):
content_length = request.headers.get('content-length')
if content_length:
if int(content_length) > self.max_bytes:
return JSONResponse(status_code=413, content={"error": "Request too large"})
body = await request.body()
if len(body) > self.max_bytes:
return JSONResponse(status_code=413, content={"error": "Request too large"})
response = await call_next(request)
return response

class Server(c.Module):
def __init__(
self,
Expand Down Expand Up @@ -186,7 +202,7 @@ def serve(cls,
if tag != None:
name = f'{module}{tag_seperator}{tag}'
if port == None:
namespace = c.namespace()
namespace = c.get_namespace()
if name in namespace:
port = int(namespace.get(name).split(':')[-1])
else:
Expand Down Expand Up @@ -433,7 +449,7 @@ def sync(self, update=False):
else:
response['msg'] = 'Synced with the network'
response['staleness'] = 0
c.namespace(max_age=self.network_staleness)
c.get_namespace(max_age=self.network_staleness)
self.subspace = c.module('subspace')(network=network)
state['stake_from'] = self.subspace.stake_from(fmt='j', update=update, max_age=self.network_staleness)
state['stake'] = {k: sum(v.values()) for k,v in state['stake_from'].items()}
Expand Down Expand Up @@ -556,7 +572,7 @@ def wait_for_server(cls,

time_waiting = 0
while time_waiting < timeout:
namespace = c.namespace(network=network)
namespace = c.get_namespace(network=network)
if name in namespace:
c.print(f'{name} is ready', color='green')
return True
Expand Down
Loading

0 comments on commit e459558

Please sign in to comment.