diff --git a/commune/client/client.py b/commune/client/client.py index 9f735600..18035213 100644 --- a/commune/client/client.py +++ b/commune/client/client.py @@ -35,6 +35,73 @@ def __init__( + async def async_forward(self, + fn: str, + args: list = None, + kwargs: dict = None, + params: dict = None, + address : str = None, + timeout: int = 10, + headers : dict ={'Content-Type': 'application/json'}, + message_type = "v0", + default_fn = 'info', + verbose = False, + debug = True, + **extra_kwargs + ): + if isinstance(args, dict): + kwargs = args + args = None + + if params != None: + assert type(params) in [list, dict], f'params must be a list or dict, not {type(params)}' + if isinstance(params, list): + args = params + elif isinstance(params, dict): + kwargs = params + kwargs = kwargs or {} + kwargs.update(extra_kwargs) + fn = fn or default_fn + + address = address or self.address + args = args if args else [] + kwargs = kwargs if kwargs else {} + + input = { + "args": args, + "kwargs": kwargs, + "ip": c.ip(), + "timestamp": c.timestamp(), + } + self.count += 1 + # serialize this into a json string + if message_type == "v0": + request = self.serializer.serialize(input) + request = self.key.sign(request, return_json=True) + # key emoji + + elif message_type == "v1": + input['ticket'] = self.key.ticket() + request = self.serializer.serialize(input) + else: + raise ValueError(f"Invalid message_type: {message_type}") + + url = f"{address}/{fn}/" + if not url.startswith('http'): + url = 'http://' + url + result = await self.process_request(url, request, headers=headers, timeout=timeout) + + c.print(f"🛰️ Call {self.address}/{fn} 🛰️ (🔑{self.key.ss58_address})", color='green', verbose=verbose) + + if self.save_history: + input['fn'] = fn + input['result'] = result + input['module'] = self.address + input['latency'] = c.time() - input['timestamp'] + path = self.history_path+ '/' + self.key.ss58_address + '/' + self.address+ '/'+ str(input['timestamp']) + self.put(path, input) + return result + def age(self): return self.start_timestamp - c.timestamp() @@ -52,6 +119,7 @@ def set_client(self, module = address # we assume its a module name namespace = c.get_namespace(search=module, network=network) module = c.choice(list(namespace.keys())) + assert module in namespace, f"Invalid module {module}" address = namespace[module] if '://' in address: mode = address.split('://')[0] @@ -118,73 +186,6 @@ async def process_request(self, url:str, request: dict, headers=None, timeout:in return result - async def async_forward(self, - fn: str, - args: list = None, - kwargs: dict = None, - params: dict = None, - address : str = None, - timeout: int = 10, - headers : dict ={'Content-Type': 'application/json'}, - message_type = "v0", - default_fn = 'info', - verbose = False, - debug = True, - **extra_kwargs - ): - if isinstance(args, dict): - kwargs = args - args = None - - if params != None: - assert type(params) in [list, dict], f'params must be a list or dict, not {type(params)}' - if isinstance(params, list): - args = params - elif isinstance(params, dict): - kwargs = params - kwargs = kwargs or {} - kwargs.update(extra_kwargs) - fn = fn or default_fn - - address = address or self.address - args = args if args else [] - kwargs = kwargs if kwargs else {} - - input = { - "args": args, - "kwargs": kwargs, - "ip": c.ip(), - "timestamp": c.timestamp(), - } - self.count += 1 - # serialize this into a json string - if message_type == "v0": - request = self.serializer.serialize(input) - request = self.key.sign(request, return_json=True) - # key emoji - - c.print(f"🛰️ Call {self.address}/{fn} 🛰️ (🔑{self.key.ss58_address})", color='green', verbose=verbose) - elif message_type == "v1": - input['ticket'] = self.key.ticket() - request = self.serializer.serialize(input) - else: - raise ValueError(f"Invalid message_type: {message_type}") - - url = f"{address}/{fn}/" - if not url.startswith('http'): - url = 'http://' + url - c.print(f"🛰️ Call {url} 🛰️ (🔑{self.key.ss58_address})", color='green') - result = await self.process_request(url, request, headers=headers, timeout=timeout) - - if self.save_history: - input['fn'] = fn - input['result'] = result - input['module'] = self.address - input['latency'] = c.time() - input['timestamp'] - path = self.history_path+ '/' + self.key.ss58_address + '/' + self.address+ '/'+ str(input['timestamp']) - self.put(path, input) - return result - @classmethod def history(cls, key=None, history_path='history'): key = c.get_key(key) diff --git a/commune/key/key.py b/commune/key/key.py index 97a40cb4..e907cc60 100644 --- a/commune/key/key.py +++ b/commune/key/key.py @@ -718,6 +718,8 @@ def create_from_seed( @classmethod def from_password(cls, password:str, **kwargs): return cls.create_from_uri(password, **kwargs) + + pwd2key = password2key = from_password @classmethod diff --git a/commune/module/module.py b/commune/module/module.py index 89928d72..9044b974 100755 --- a/commune/module/module.py +++ b/commune/module/module.py @@ -827,7 +827,14 @@ def switch_key(cls, *args, **kwargs): @classmethod def module_info(cls, *args, **kwargs): return c.module('subspace')().module_info(*args, **kwargs) + minfo = module_info + + @classmethod + def pwd2key(cls, *args, **kwargs): + return c.module('key').pwd2key(*args, **kwargs) + + password2key = pwd2key # KEY LAND @classmethod def rename_key(cls, *args, **kwargs): diff --git a/commune/subspace/subspace.py b/commune/subspace/subspace.py index 12c38756..491a7551 100644 --- a/commune/subspace/subspace.py +++ b/commune/subspace/subspace.py @@ -394,8 +394,8 @@ def query_map(self, name: str = 'StakeFrom', paths = self.glob(path + '*') update = update or len(paths) == 0 or block != None if not update: - last_path = sorted(paths, reverse=True)[-1] - value = self.get(last_path,None , max_age=max_age) + last_path = sorted(paths, reverse=True)[0] + value = self.get(last_path, None , max_age=max_age) else: value = None diff --git a/vali/vali.py b/vali/vali.py index 02ecf5f8..e6e697ae 100644 --- a/vali/vali.py +++ b/vali/vali.py @@ -94,7 +94,6 @@ def epoch_info(self): def run_info(self): return { - 'network': self.network_info(), 'epoch': self.epoch_info() , 'vote': self.vote_info(), @@ -423,7 +422,6 @@ def vote_info(self): votes = self.votes() info = { 'num_uids': len(votes.get('uids', [])), - 'timestamp': votes['timestamp'], 'staleness': self.vote_staleness, 'key': self.key.ss58_address, 'network': self.network,