Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Apr 11, 2024
1 parent 1d10bdc commit e017d85
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 71 deletions.
135 changes: 68 additions & 67 deletions commune/client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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]
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions commune/key/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions commune/module/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions commune/subspace/subspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 0 additions & 2 deletions vali/vali.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e017d85

Please sign in to comment.