Skip to content

Commit

Permalink
commune update with chain update
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed May 31, 2024
1 parent a955cc5 commit 872cd3c
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 43 deletions.
2 changes: 1 addition & 1 deletion commune/key/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -1520,7 +1520,7 @@ def test_str_signing(self):
return {'success':True}

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

def verify_ticket(self, ticket, **kwargs):
return c.module('ticket')().verify(ticket, key=self.key, **kwargs)
Expand Down
10 changes: 3 additions & 7 deletions commune/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ def namespace(cls, search=None,
network = network or 'local'
if netuid != None:
network = f'subspace.{netuid}'

path = network
namespace = cls.get(path, {}, max_age=max_age)


namespace = cls.get(network, {}, max_age=max_age)
if 'subspace' in network:
if '.' in network:
network, netuid = network.split('.')
Expand All @@ -42,7 +39,7 @@ def namespace(cls, search=None,
elif network == 'local':
if update or len(namespace) == 0:
namespace = cls.build_namespace(network=network)


namespace = {k:v for k,v in namespace.items() if 'Error' not in k}
if search != None:
Expand Down Expand Up @@ -194,8 +191,7 @@ def build_namespace(cls,
c.print(f'Timeout error {e}', color='red', verbose=verbose)

cls.put_namespace(network, namespace)



return namespace


Expand Down
19 changes: 8 additions & 11 deletions commune/subspace/subspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -2561,11 +2561,12 @@ def register(
module_key = module_key or c.get_key(name).ss58_address
netuid2subnet = self.netuid2subnet(max_age=max_age)
subnet2netuid = {v:k for k,v in netuid2subnet.items()}
netuid = netuid or subnet
assert isinstance(subnet, str), f"Subnet must be a string"
if isinstance(netuid, str):
if isinstance(subnet, str):
netuid = subnet
if isinstance(netuid, ):
subnet = netuid
netuid = subnet2netuid.get(netuid, 0)
assert isinstance(subnet, str), f"Subnet must be a string"

if netuid in netuid2subnet:
subnet = netuid2subnet[netuid]
Expand All @@ -2576,25 +2577,21 @@ def register(
return {'success': False, 'msg': 'Subnet not found and not created'}

# require prompt to create new subnet
stake = stake or 0
min_register_stake = self.min_register_stake(netuid=netuid, network=network)
stake = max(min_register_stake, stake)
stake = stake * 1e9
stake = (stake or 0) * 1e9

if '0.0.0.0' in address:
address = address.replace('0.0.0.0', c.ip(update=1))
address = address.replace('0.0.0.0', c.ip())

if len(address) > 32:
address = address[-32:]

metadata = c.dict2str(metadata or {})
params = {
'network': subnet.encode('utf-8'),
'address': address.encode('utf-8'),
'name': name.encode('utf-8'),
'stake': stake,
'module_key': module_key,
'metadata': b'{}',
'metadata': c.str2bytes(c.dict2str(metadata or {})),
}

# create extrinsic call
Expand Down Expand Up @@ -2629,7 +2626,7 @@ def transfer(

response = self.compose_call(
module='Balances',
fn='transfer',
fn='transfer_keep_alive',
params={
'dest': dest,
'value': amount
Expand Down
7 changes: 5 additions & 2 deletions commune/tests/test_commune.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import commune as c
def test_key():
c.module('key').test()
def test_ticket():
c.module('ticket').test()
def test_namespace():
c.module('namespace').test()
def test_server():
c.module('server').test()
def test_subnet():
c.module('subnet').test()
def test_validator():
c.module('vali').test()




Expand Down
4 changes: 2 additions & 2 deletions commune/ticket/ticket.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ timestamp::ticket::signature

by calling

c.ticket("alice")
c.ticket(key="alice", data="alice")

the alice key signs the current timestamp and returns the ticket.


1713500654.659339::ticket::e0559b535129037a62947c65af35f17c50d29b4a5c31df86b069d8ada5bcbb230f4c1e996393e6721f78d88f9b512b
1713500654.659339::signature=e0559b535129037a62947c65af35f17c50d29b4a5c31df86b069d8ada5bcbb230f4c1e996393e6721f78d88f9b512b
6493b5ca743d027091585366875c6bea8e

now to verify the ticket you can do so like this.
Expand Down
14 changes: 8 additions & 6 deletions commune/ticket/ticket.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class Ticket(c.Module):
# THIS CREATES A TOKEN THAT CAN BE USED TO VERIFY THE ORIGIN OF A MESSAGE, AND IS GENERATED CLIENT SIDE
# THIS USES THE SAME TECHNOLOGY AS ACCESS TOKENS, BUT IS USED FOR CLIENT SIDE VERIFICATION, AND NOT SERVER SIDE
# THIS GIVES USERS THE ABILITY TO VERIFY THE ORIGIN OF A MESSAGE, AND TO VERIFY THAT THE MESSAGE HAS NOT BEEN TAMPERED WITH
#SIGNATURE={SIGNATURE}::ADDRESS=ADDRESS TIMESTAMP=TIMESTAMP DATA=DATA
#data={DATA}::address={ADDRESS}::timestamp={TIMESTAMP}::signature={SIGNATURE}
"""

signature_seperator = '::signature='

signature_seperator = '::signature='
max_age = 5

def create(self, data=None, key=None, **kwargs):
Expand All @@ -19,21 +19,23 @@ def create(self, data=None, key=None, **kwargs):
'timestamp': c.time(),
'address': key.ss58_address,
}
c.print(ticket_dict)
data = self.dict2ticket(ticket_dict)
ticket = key.sign(data, return_string=True, seperator=self.signature_seperator)
return ticket


def dict2ticket(self, ticket):
"""
Convert a dictionary to a ticket string
"""
ticket_str = ''
for i, (k,v) in enumerate(ticket.items()):
ticket_str += (("::" if i > 0 else "") +k + '=' + str(v) )
return ticket_str



def ticket2dict(self, ticket):
"""
Convert a ticket string to a dictionary
"""
ticket_dict = {}
for item in ticket.split('::'):
k,v = item.split('=')
Expand Down
37 changes: 23 additions & 14 deletions commune/vali/vali.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def init_vali(self, config=None, module=None, score_fn=None, **kwargs):
self.init_metrics()
self.set_score_fn(score_fn)
self.futures = []
self.sync()
c.thread(self.run_loop)

init = init_vali
Expand Down Expand Up @@ -194,7 +195,7 @@ def cancel_futures(self):
epoch2results = {}

def epoch(self, **kwargs):

self.sync_network(**kwargs)
module_addresses = c.shuffle(list(self.namespace.values()))
c.print(f'Epoch {self.epochs} with {len(module_addresses)} modules', color='yellow')

Expand Down Expand Up @@ -313,7 +314,10 @@ def get_module_path(self, module):
return path


def get_module(self, module:str, network:str='local', path=None, update=False, **kwargs):
def get_module(self,
module:str,
network:str='local',
path=None, update=False, **kwargs):
network = network or self.config.network
self.sync(network=network, update=update, **kwargs)
info = {}
Expand All @@ -331,7 +335,7 @@ def get_module(self, module:str, network:str='local', path=None, update=False, *

# CONNECT TO THE MODULE
info = self.get(path, {})
if 'ss58_address' not in info:
if 'key_address' not in info:
info = module.info(timeout=self.config.timeout_info)

info['past_timestamp'] = info.get('timestamp', 0) # for the stalnesss
Expand All @@ -352,7 +356,12 @@ def get_module(self, module:str, network:str='local', path=None, update=False, *
setattr(module,'local_info', info) # set the client
return module

def eval(self, module:str, network:str=None, update=False, **kwargs):
def eval(self,
module:str,
network:str=None,
update=False,
verbose_keys= ['w', 'address', 'name', 'key_address'],
**kwargs):
"""
The following evaluates a module sver
"""
Expand All @@ -372,8 +381,8 @@ def eval(self, module:str, network:str=None, update=False, **kwargs):
c.print(f'Error (name={name}) --> {response_str}', color='red', verbose=self.config.verbose)
self.errors += 1
self.last_error = c.time()

return response
return {k:response[k] for k in verbose_keys}


def process_response(self, response:dict, info:dict ):
Expand Down Expand Up @@ -403,7 +412,7 @@ def process_response(self, response:dict, info:dict ):
# resolve the alph
info['latency'] = c.time() - info['timestamp']
info['w'] = info['w'] * info['alpha'] + info['past_w'] * (1 - info['alpha'])

info['count'] = info.get('count', 0) + 1
# store modules that have a minimum weight to save storage of stale modules
if info['w'] > self.config.min_leaderboard_weight:
self.put(info['path'], info)
Expand All @@ -412,7 +421,7 @@ def process_response(self, response:dict, info:dict ):
self.successes += 1
self.last_success = c.time()

return response
return info


@property
Expand Down Expand Up @@ -458,7 +467,7 @@ def vote_info(self):

def calculate_votes(self, df=None, **kwargs):
network = self.config.network
keys = ['name', 'w', 'staleness','latency', 'ss58_address']
keys = ['name', 'w', 'staleness','latency', 'key']
leaderboard = df or self.leaderboard(network=network,
keys=keys,
to_dict=True)
Expand All @@ -468,11 +477,11 @@ def calculate_votes(self, df=None, **kwargs):
key2uid = self.subspace.key2uid(**kwargs) if hasattr(self, 'subspace') else {}
for info in leaderboard:
## valid modules have a weight greater than 0 and a valid ss58_address
if 'ss58_address' in info and info['w'] >= 0:
if info['ss58_address'] in key2uid:
votes['keys'] += [info['ss58_address']]
if 'key' in info and info['w'] >= 0:
if info['key'] in key2uid:
votes['keys'] += [info['key']]
votes['weights'] += [info['w']]
votes['uids'] += [key2uid.get(info['ss58_address'], -1)]
votes['uids'] += [key2uid.get(info['key'], -1)]
assert len(votes['uids']) == len(votes['weights']), f'Length of uids and weights must be the same, got {len(votes["uids"])} uids and {len(votes["weights"])} weights'

return votes
Expand Down Expand Up @@ -522,7 +531,7 @@ def leaderboard(self,
# chunk the jobs into batches
for path in paths:
r = self.get(path, {}, max_age=max_age)
if isinstance(r, dict) and 'ss58_address' and r.get('w', 0) > self.config.min_leaderboard_weight :
if isinstance(r, dict) and 'key' and r.get('w', 0) > self.config.min_leaderboard_weight :
r['staleness'] = c.time() - r.get('timestamp', 0)
if not self.filter_module(r['name']):
continue
Expand Down

0 comments on commit 872cd3c

Please sign in to comment.