Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
latentvector committed Aug 31, 2024
1 parent 58dd5ab commit 55f664a
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 351 deletions.
17 changes: 2 additions & 15 deletions commune/module/_task.py → commune/executor/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import threading
from typing import *
import threading



class Task:

import commune as c
class Executor(c.Module):

thread_map = {}

Expand Down Expand Up @@ -225,16 +222,6 @@ def tasks(cls, task = None, mode='pm2',**kwargs) -> List[str]:
return tasks


@classmethod
def asubmit(cls, fn:str, *args, **kwargs):

async def _asubmit():
kwargs.update(kwargs.pop('kwargs',{}))
return fn(*args, **kwargs)
return _asubmit()



thread_map = {}

@classmethod
Expand Down
30 changes: 0 additions & 30 deletions commune/module/_crypto.py

This file was deleted.

161 changes: 0 additions & 161 deletions commune/module/_endpoint.py

This file was deleted.

2 changes: 1 addition & 1 deletion commune/module/_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ def get_module(cls,
except Exception as e:
return c.detailed_error(e)
if path in ['module', 'c']:
return c
return c.Module
# if the module is a valid import path
shortcuts = c.shortcuts()
if path in shortcuts:
Expand Down
28 changes: 28 additions & 0 deletions commune/module/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1018,3 +1018,31 @@ def pip_install(cls,
def pip_exists(cls, lib:str, verbose:str=True):
return bool(lib in cls.pip_libs())


@classmethod
def hash(cls, x, mode: str='sha256',*args,**kwargs) -> str:
import hashlib
x = cls.python2str(x)
if mode == 'keccak':
return cls.import_object('web3.main.Web3').keccak(text=x, *args, **kwargs).hex()
elif mode == 'ss58':
return cls.import_object('scalecodec.utils.ss58.ss58_encode')(x, *args,**kwargs)
elif mode == 'python':
return hash(x)
elif mode == 'md5':
return hashlib.md5(x.encode()).hexdigest()
elif mode == 'sha256':
return hashlib.sha256(x.encode()).hexdigest()
elif mode == 'sha512':
return hashlib.sha512(x.encode()).hexdigest()
elif mode =='sha3_512':
return hashlib.sha3_512(x.encode()).hexdigest()
else:
raise ValueError(f'unknown mode {mode}')

@classmethod
def hash_modes(cls):
return ['keccak', 'ss58', 'python', 'md5', 'sha256', 'sha512', 'sha3_512']

str2hash = hash

54 changes: 1 addition & 53 deletions commune/module/_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,59 +156,7 @@ def external_ip(cls, default_ip='0.0.0.0') -> str:
return ip

return default_ip

@staticmethod
def upnpc_create_port_map(port: int):
r""" Creates a upnpc port map on your router from passed external_port to local port.
Args:
port (int, `required`):
The local machine port to map from your external port.
Return:
external_port (int, `required`):
The external port mappeclass to the local port on your machine.
Raises:
Exception (Exception):
Raised if UPNPC port mapping fails, for instance, if upnpc is not enabled on your router.
"""

try:
import miniupnpc
upnp = miniupnpc.UPnP()
upnp.discoverdelay = 200
logger.debug('UPNPC: Using UPnP to open a port on your router ...')
logger.debug('UPNPC: Discovering... delay={}ms', upnp.discoverdelay)
ndevices = upnp.discover()
upnp.selectigd()
logger.debug('UPNPC: ' + str(ndevices) + ' device(s) detected')

ip = upnp.lanaddr
external_ip = upnp.externalipaddress()

logger.debug('UPNPC: your local ip address: ' + str(ip))
logger.debug('UPNPC: your external ip address: ' + str(external_ip))
logger.debug('UPNPC: status = ' + str(upnp.statusinfo()) + " connection type = " + str(upnp.connectiontype()))

# find a free port for the redirection
external_port = port
rc = upnp.getspecificportmapping(external_port, 'TCP')
while rc != None and external_port < 65536:
external_port += 1
rc = upnp.getspecificportmapping(external_port, 'TCP')
if rc != None:
raise Exception("UPNPC: No available external ports for port mapping.")

logger.info('UPNPC: trying to redirect remote: {}:{} => local: {}:{} over TCP', external_ip, external_port, ip, port)
upnp.addportmapping(external_port, 'TCP', ip, port, 'Bittensor: %u' % external_port, '')
logger.info('UPNPC: Create Success')

return external_port

except Exception as e:
raise Exception(e) from e


@classmethod
def unreserve_port(cls,port:int,
var_path='reserved_ports'):
Expand Down
Loading

0 comments on commit 55f664a

Please sign in to comment.