Compare commits
1 Commits
wot
...
refactorin
Author | SHA1 | Date | |
---|---|---|---|
|
39ac87b6ad |
@ -32,6 +32,7 @@ class colors:
|
|||||||
strikethrough='\033[09m'
|
strikethrough='\033[09m'
|
||||||
invisible='\033[08m'
|
invisible='\033[08m'
|
||||||
italics='\033[3m'
|
italics='\033[3m'
|
||||||
|
|
||||||
class fg:
|
class fg:
|
||||||
black='\033[30m'
|
black='\033[30m'
|
||||||
red='\033[31m'
|
red='\033[31m'
|
||||||
@ -64,9 +65,7 @@ class colors:
|
|||||||
'''
|
'''
|
||||||
Use the bitwise operators to merge these settings
|
Use the bitwise operators to merge these settings
|
||||||
'''
|
'''
|
||||||
USE_ANSI = 0b100
|
USE_ANSI = 0b000 if os.name == 'nt' else 0b100
|
||||||
if os.name == 'nt':
|
|
||||||
USE_ANSI = 0b000
|
|
||||||
OUTPUT_TO_CONSOLE = 0b010
|
OUTPUT_TO_CONSOLE = 0b010
|
||||||
OUTPUT_TO_FILE = 0b001
|
OUTPUT_TO_FILE = 0b001
|
||||||
|
|
||||||
@ -150,6 +149,7 @@ def log(prefix, data, color = '', timestamp=True, fd = sys.stdout, prompt = True
|
|||||||
data : The actual data to output
|
data : The actual data to output
|
||||||
color : The color to output before the data
|
color : The color to output before the data
|
||||||
'''
|
'''
|
||||||
|
|
||||||
curTime = ''
|
curTime = ''
|
||||||
if timestamp:
|
if timestamp:
|
||||||
curTime = time.strftime("%m-%d %H:%M:%S") + ' '
|
curTime = time.strftime("%m-%d %H:%M:%S") + ' '
|
||||||
@ -210,33 +210,33 @@ def confirm(default = 'y', message = 'Are you sure %s? '):
|
|||||||
return default == 'y'
|
return default == 'y'
|
||||||
|
|
||||||
# debug: when there is info that could be useful for debugging purposes only
|
# debug: when there is info that could be useful for debugging purposes only
|
||||||
def debug(data, error = None, timestamp = True, prompt = True, terminal = False, level = LEVEL_DEBUG):
|
def debug(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_DEBUG):
|
||||||
if get_level() <= level:
|
if get_level() <= level:
|
||||||
log('/', data, timestamp = timestamp, prompt = prompt, terminal = terminal)
|
log('/', data, timestamp = timestamp, prompt = prompt, terminal = terminal)
|
||||||
if not error is None:
|
if not error is None:
|
||||||
debug('Error: ' + str(error) + parse_error())
|
debug('Error: ' + str(error) + parse_error())
|
||||||
|
|
||||||
# info: when there is something to notify the user of, such as the success of a process
|
# info: when there is something to notify the user of, such as the success of a process
|
||||||
def info(data, timestamp = False, prompt = True, terminal = False, level = LEVEL_INFO):
|
def info(data, timestamp = False, prompt = True, terminal = True, level = LEVEL_INFO):
|
||||||
if get_level() <= level:
|
if get_level() <= level:
|
||||||
log('+', data, colors.fg.green, timestamp = timestamp, prompt = prompt, terminal = terminal)
|
log('+', data, colors.fg.green, timestamp = timestamp, prompt = prompt, terminal = terminal)
|
||||||
|
|
||||||
# warn: when there is a potential for something bad to happen
|
# warn: when there is a potential for something bad to happen
|
||||||
def warn(data, error = None, timestamp = True, prompt = True, terminal = False, level = LEVEL_WARN):
|
def warn(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_WARN):
|
||||||
if not error is None:
|
if not error is None:
|
||||||
debug('Error: ' + str(error) + parse_error())
|
debug('Error: ' + str(error) + parse_error())
|
||||||
if get_level() <= level:
|
if get_level() <= level:
|
||||||
log('!', data, colors.fg.orange, timestamp = timestamp, prompt = prompt, terminal = terminal)
|
log('!', data, colors.fg.orange, timestamp = timestamp, prompt = prompt, terminal = terminal)
|
||||||
|
|
||||||
# error: when only one function, module, or process of the program encountered a problem and must stop
|
# error: when only one function, module, or process of the program encountered a problem and must stop
|
||||||
def error(data, error = None, timestamp = True, prompt = True, terminal = False, level = LEVEL_ERROR):
|
def error(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_ERROR):
|
||||||
if get_level() <= level:
|
if get_level() <= level:
|
||||||
log('-', data, colors.fg.red, timestamp = timestamp, fd = sys.stderr, prompt = prompt, terminal = terminal)
|
log('-', data, colors.fg.red, timestamp = timestamp, fd = sys.stderr, prompt = prompt, terminal = terminal)
|
||||||
if not error is None:
|
if not error is None:
|
||||||
debug('Error: ' + str(error) + parse_error())
|
debug('Error: ' + str(error) + parse_error())
|
||||||
|
|
||||||
# fatal: when the something so bad has happened that the program must stop
|
# fatal: when the something so bad has happened that the program must stop
|
||||||
def fatal(data, error = None, timestamp=True, prompt = True, terminal = False, level = LEVEL_FATAL):
|
def fatal(data, error = None, timestamp = True, prompt = True, terminal = True, level = LEVEL_FATAL):
|
||||||
if not error is None:
|
if not error is None:
|
||||||
debug('Error: ' + str(error) + parse_error(), terminal = terminal)
|
debug('Error: ' + str(error) + parse_error(), terminal = terminal)
|
||||||
if get_level() <= level:
|
if get_level() <= level:
|
||||||
@ -248,6 +248,9 @@ def parse_error():
|
|||||||
output = ''
|
output = ''
|
||||||
|
|
||||||
for line in details:
|
for line in details:
|
||||||
output += '\n ... module %s in %s:%i' % (line[2], line[0], line[1])
|
output += ('\n ... ' + colors.bold + '%s' + colors.reset + ' in %s:' + colors.bold + '%i' + colors.reset) % (line[2], line[0], line[1])
|
||||||
|
|
||||||
|
if not (get_settings() & USE_ANSI):
|
||||||
|
output = colors.filter(output)
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
@ -134,13 +134,8 @@ class Onionr:
|
|||||||
# initialize plugins
|
# initialize plugins
|
||||||
events.event('init', onionr = self, threaded = False)
|
events.event('init', onionr = self, threaded = False)
|
||||||
|
|
||||||
command = ''
|
command = sys.argv[1].lower() if len(sys.argv) >= 2 else ''
|
||||||
try:
|
self.execute(command)
|
||||||
command = sys.argv[1].lower()
|
|
||||||
except IndexError:
|
|
||||||
command = ''
|
|
||||||
finally:
|
|
||||||
self.execute(command)
|
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -285,8 +280,7 @@ class Onionr:
|
|||||||
# define commands
|
# define commands
|
||||||
commands = self.getCommands()
|
commands = self.getCommands()
|
||||||
|
|
||||||
command = commands.get(argument, self.notFound)
|
return commands.get(argument, self.notFound)()
|
||||||
command()
|
|
||||||
|
|
||||||
def version(self, verbosity = 5, function = logger.info):
|
def version(self, verbosity = 5, function = logger.info):
|
||||||
'''
|
'''
|
||||||
|
@ -151,12 +151,16 @@ class Block:
|
|||||||
Outputs:
|
Outputs:
|
||||||
- (bool): indicates whether or not the operation was successful
|
- (bool): indicates whether or not the operation was successful
|
||||||
'''
|
'''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# import from string
|
# import from string
|
||||||
blockdata = data
|
blockdata = data
|
||||||
|
|
||||||
# import from file
|
# import from file
|
||||||
if blockdata is None:
|
if blockdata is None:
|
||||||
|
if self.getHash() is None:
|
||||||
|
return None
|
||||||
|
|
||||||
try:
|
try:
|
||||||
blockdata = onionrstorage.getData(self.core, self.getHash()).decode()
|
blockdata = onionrstorage.getData(self.core, self.getHash()).decode()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
@ -190,7 +194,7 @@ class Block:
|
|||||||
|
|
||||||
if len(self.getRaw()) <= config.get('allocations.blockCache', 500000):
|
if len(self.getRaw()) <= config.get('allocations.blockCache', 500000):
|
||||||
self.cache()
|
self.cache()
|
||||||
|
|
||||||
if self.autoDecrypt:
|
if self.autoDecrypt:
|
||||||
self.decrypt()
|
self.decrypt()
|
||||||
|
|
||||||
@ -677,7 +681,7 @@ class Block:
|
|||||||
'''
|
'''
|
||||||
if isinstance(bHash, Block):
|
if isinstance(bHash, Block):
|
||||||
bHash = bHash.getHash()
|
bHash = bHash.getHash()
|
||||||
|
|
||||||
ret = isinstance(onionrstorage.getData(onionrcore.Core(), bHash), type(None))
|
ret = isinstance(onionrstorage.getData(onionrcore.Core(), bHash), type(None))
|
||||||
|
|
||||||
return not ret
|
return not ret
|
||||||
|
@ -164,4 +164,4 @@ cmd_help = {
|
|||||||
'change-id': 'Change active ID',
|
'change-id': 'Change active ID',
|
||||||
'open-home': 'Open your node\'s home/info screen',
|
'open-home': 'Open your node\'s home/info screen',
|
||||||
'reset-tor': 'Delete the Tor data directory. Only do this if Tor never starts.'
|
'reset-tor': 'Delete the Tor data directory. Only do this if Tor never starts.'
|
||||||
}
|
}
|
||||||
|
@ -24,8 +24,8 @@ from onionrutils import bytesconverter
|
|||||||
config.reload()
|
config.reload()
|
||||||
|
|
||||||
def getDifficultyModifier(coreOrUtilsInst=None):
|
def getDifficultyModifier(coreOrUtilsInst=None):
|
||||||
'''Accepts a core or utils instance returns
|
'''Accepts a core or utils instance returns
|
||||||
the difficulty modifier for block storage based
|
the difficulty modifier for block storage based
|
||||||
on a variety of factors, currently only disk use.
|
on a variety of factors, currently only disk use.
|
||||||
'''
|
'''
|
||||||
classInst = coreOrUtilsInst
|
classInst = coreOrUtilsInst
|
||||||
@ -110,14 +110,14 @@ class DataPOW:
|
|||||||
self.data = self.data.encode()
|
self.data = self.data.encode()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.data = nacl.hash.blake2b(self.data)
|
self.data = nacl.hash.blake2b(self.data)
|
||||||
|
|
||||||
logger.info('Computing POW (difficulty: %s)...' % self.difficulty)
|
logger.debug('Computing POW (difficulty: %s)...' % self.difficulty)
|
||||||
|
|
||||||
self.mainHash = '0' * 70
|
self.mainHash = '0' * 70
|
||||||
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
|
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
|
||||||
|
|
||||||
myCore = core.Core()
|
myCore = core.Core()
|
||||||
for i in range(max(1, threadCount)):
|
for i in range(max(1, threadCount)):
|
||||||
t = threading.Thread(name = 'thread%s' % i, target = self.pow, args = (True,myCore))
|
t = threading.Thread(name = 'thread%s' % i, target = self.pow, args = (True,myCore))
|
||||||
@ -131,7 +131,7 @@ class DataPOW:
|
|||||||
answer = ''
|
answer = ''
|
||||||
heartbeat = 200000
|
heartbeat = 200000
|
||||||
hbCount = 0
|
hbCount = 0
|
||||||
|
|
||||||
while self.hashing:
|
while self.hashing:
|
||||||
rand = nacl.utils.random()
|
rand = nacl.utils.random()
|
||||||
token = nacl.hash.blake2b(rand + self.data).decode()
|
token = nacl.hash.blake2b(rand + self.data).decode()
|
||||||
@ -141,7 +141,7 @@ class DataPOW:
|
|||||||
self.hashing = False
|
self.hashing = False
|
||||||
iFound = True
|
iFound = True
|
||||||
break
|
break
|
||||||
|
|
||||||
if iFound:
|
if iFound:
|
||||||
endTime = math.floor(time.time())
|
endTime = math.floor(time.time())
|
||||||
if self.reporting:
|
if self.reporting:
|
||||||
@ -160,12 +160,12 @@ class DataPOW:
|
|||||||
'''
|
'''
|
||||||
Returns the result then sets to false, useful to automatically clear the result
|
Returns the result then sets to false, useful to automatically clear the result
|
||||||
'''
|
'''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
retVal = self.result
|
retVal = self.result
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
retVal = False
|
retVal = False
|
||||||
|
|
||||||
self.result = False
|
self.result = False
|
||||||
return retVal
|
return retVal
|
||||||
|
|
||||||
@ -207,15 +207,15 @@ class POW:
|
|||||||
self.data = self.data.encode()
|
self.data = self.data.encode()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if forceDifficulty > 0:
|
if forceDifficulty > 0:
|
||||||
self.difficulty = forceDifficulty
|
self.difficulty = forceDifficulty
|
||||||
else:
|
else:
|
||||||
# Calculate difficulty. Dumb for now, may use good algorithm in the future.
|
# Calculate difficulty. Dumb for now, may use good algorithm in the future.
|
||||||
self.difficulty = getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data), coreInst=myCore)
|
self.difficulty = getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data), coreInst=myCore)
|
||||||
|
|
||||||
|
|
||||||
logger.info('Computing POW (difficulty: %s)...' % self.difficulty)
|
logger.debug('Computing POW (difficulty: %s)...' % self.difficulty)
|
||||||
|
|
||||||
self.mainHash = '0' * 64
|
self.mainHash = '0' * 64
|
||||||
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
|
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
|
||||||
@ -251,7 +251,7 @@ class POW:
|
|||||||
self.result = payload
|
self.result = payload
|
||||||
break
|
break
|
||||||
nonce += 1
|
nonce += 1
|
||||||
|
|
||||||
if iFound:
|
if iFound:
|
||||||
endTime = math.floor(time.time())
|
endTime = math.floor(time.time())
|
||||||
if self.reporting:
|
if self.reporting:
|
||||||
@ -268,12 +268,12 @@ class POW:
|
|||||||
'''
|
'''
|
||||||
Returns the result then sets to false, useful to automatically clear the result
|
Returns the result then sets to false, useful to automatically clear the result
|
||||||
'''
|
'''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
retVal = self.result
|
retVal = self.result
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
retVal = False
|
retVal = False
|
||||||
|
|
||||||
self.result = False
|
self.result = False
|
||||||
return retVal
|
return retVal
|
||||||
|
|
||||||
@ -292,4 +292,4 @@ class POW:
|
|||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
self.shutdown()
|
self.shutdown()
|
||||||
logger.warn('Got keyboard interrupt while waiting for POW result, stopping')
|
logger.warn('Got keyboard interrupt while waiting for POW result, stopping')
|
||||||
return result
|
return result
|
||||||
|
@ -45,4 +45,4 @@ def import_new_blocks(core_inst=None, scanDir=''):
|
|||||||
else:
|
else:
|
||||||
logger.warn('Failed to verify hash for %s' % block, terminal=True)
|
logger.warn('Failed to verify hash for %s' % block, terminal=True)
|
||||||
if not exist:
|
if not exist:
|
||||||
logger.info('No blocks found to import', terminal=True)
|
logger.debug('No blocks found to import', terminal=True)
|
||||||
|
@ -226,7 +226,15 @@ def pluginToBlock(plugin, import_block = True):
|
|||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
metadata = {'author' : author, 'date' : str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')), 'name' : plugin, 'info' : info, 'compiled-by' : plugin_name, 'content' : data.decode('utf-8'), 'description' : description}
|
metadata = {
|
||||||
|
'author' : author,
|
||||||
|
'date' : str(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')),
|
||||||
|
'name' : plugin,
|
||||||
|
'info' : info,
|
||||||
|
'compiled-by' : plugin_name,
|
||||||
|
'content' : data.decode('utf-8'),
|
||||||
|
'description' : description
|
||||||
|
}
|
||||||
|
|
||||||
block = Block(core = pluginapi.get_core())
|
block = Block(core = pluginapi.get_core())
|
||||||
|
|
||||||
@ -250,6 +258,7 @@ def pluginToBlock(plugin, import_block = True):
|
|||||||
def installBlock(block):
|
def installBlock(block):
|
||||||
try:
|
try:
|
||||||
block = Block(block, core = pluginapi.get_core())
|
block = Block(block, core = pluginapi.get_core())
|
||||||
|
print(block.getContent())
|
||||||
blockContent = json.loads(block.getContent())
|
blockContent = json.loads(block.getContent())
|
||||||
|
|
||||||
name = sanitize(blockContent['name'])
|
name = sanitize(blockContent['name'])
|
||||||
|
@ -29,7 +29,7 @@ class SubprocessPOW:
|
|||||||
def __init__(self, data, metadata, core_inst=None, subproc_count=None):
|
def __init__(self, data, metadata, core_inst=None, subproc_count=None):
|
||||||
'''
|
'''
|
||||||
Onionr proof of work using multiple processes
|
Onionr proof of work using multiple processes
|
||||||
Accepts block data, block metadata
|
Accepts block data, block metadata
|
||||||
and optionally an onionr core library instance.
|
and optionally an onionr core library instance.
|
||||||
if subproc_count is not set, os.cpu_count() is used to determine the number of processes
|
if subproc_count is not set, os.cpu_count() is used to determine the number of processes
|
||||||
|
|
||||||
@ -55,8 +55,8 @@ class SubprocessPOW:
|
|||||||
self.data = bytesconverter.str_to_bytes(data)
|
self.data = bytesconverter.str_to_bytes(data)
|
||||||
# Calculate difficulty. Dumb for now, may use good algorithm in the future.
|
# Calculate difficulty. Dumb for now, may use good algorithm in the future.
|
||||||
self.difficulty = onionrproofs.getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data), coreInst=self.core_inst)
|
self.difficulty = onionrproofs.getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data), coreInst=self.core_inst)
|
||||||
|
|
||||||
logger.info('Computing POW (difficulty: %s)...' % self.difficulty)
|
logger.debug('Computing POW (difficulty: %s)...' % self.difficulty)
|
||||||
|
|
||||||
self.mainHash = '0' * 64
|
self.mainHash = '0' * 64
|
||||||
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
|
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
|
||||||
@ -74,7 +74,7 @@ class SubprocessPOW:
|
|||||||
else:
|
else:
|
||||||
self.shutdown = True
|
self.shutdown = True
|
||||||
return self.payload
|
return self.payload
|
||||||
|
|
||||||
def _spawn_proc(self):
|
def _spawn_proc(self):
|
||||||
# Create a child proof of work process, wait for data and send shutdown signal when its found
|
# Create a child proof of work process, wait for data and send shutdown signal when its found
|
||||||
parent_conn, child_conn = Pipe()
|
parent_conn, child_conn = Pipe()
|
||||||
@ -117,4 +117,3 @@ class SubprocessPOW:
|
|||||||
pipe.send(payload)
|
pipe.send(payload)
|
||||||
break
|
break
|
||||||
nonce += 1
|
nonce += 1
|
||||||
|
|
Loading…
Reference in New Issue
Block a user