From daff149acc71cb5a37f0f4ea27c80938422d7669 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Fri, 9 Aug 2019 15:41:27 -0500 Subject: [PATCH] refactoring --- onionr/communicatorutils/announcenode.py | 17 ++++++++--------- onionr/etc/onionrvalues.py | 12 +++++------- onionr/httpapi/miscpublicapi/announce.py | 2 +- onionr/onionrblocks/insert.py | 1 - onionr/onionrutils/basicrequests.py | 2 +- onionr/onionrutils/blockmetadata/process.py | 2 +- onionr/onionrutils/bytesconverter.py | 2 +- onionr/onionrutils/checkcommunicator.py | 2 +- onionr/onionrutils/epoch.py | 2 +- onionr/onionrutils/escapeansi.py | 2 +- onionr/onionrutils/getclientapiserver.py | 2 +- onionr/onionrutils/importnewblocks.py | 2 +- onionr/onionrutils/localcommand.py | 8 ++++---- onionr/onionrutils/validatemetadata.py | 21 ++++++++++----------- 14 files changed, 36 insertions(+), 41 deletions(-) diff --git a/onionr/communicatorutils/announcenode.py b/onionr/communicatorutils/announcenode.py index 84ca248c..aa391c62 100755 --- a/onionr/communicatorutils/announcenode.py +++ b/onionr/communicatorutils/announcenode.py @@ -27,9 +27,8 @@ from communicator import onlinepeers from coredb import keydb def announce_node(daemon): '''Announce our node to our peers''' - ov = onionrvalues.OnionrValues() - retData = False - announceFail = False + ret_data = False + announce_fail = False # Do not let announceCache get too large if len(daemon.announceCache) >= 10000: @@ -57,7 +56,7 @@ def announce_node(daemon): if ourID != 1: existingRand = bytesconverter.bytes_to_str(keydb.transportinfo.get_address_info(peer, 'powValue')) # Reset existingRand if it no longer meets the minimum POW - if type(existingRand) is type(None) or not existingRand.endswith('0' * ov.announce_pow): + if type(existingRand) is type(None) or not existingRand.endswith('0' * onionrvalues.ANNOUNCE_POW): existingRand = '' if peer in daemon.announceCache: @@ -66,22 +65,22 @@ def announce_node(daemon): data['random'] = existingRand else: daemon.announceProgress[peer] = True - proof = onionrproofs.DataPOW(combinedNodes, forceDifficulty=ov.announce_pow) + proof = onionrproofs.DataPOW(combinedNodes, forceDifficulty=onionrvalues.ANNOUNCE_POW) del daemon.announceProgress[peer] try: data['random'] = base64.b64encode(proof.waitForResult()[1]) except TypeError: # Happens when we failed to produce a proof logger.error("Failed to produce a pow for announcing to " + peer) - announceFail = True + announce_fail = True else: daemon.announceCache[peer] = data['random'] - if not announceFail: + if not announce_fail: logger.info('Announcing node to ' + url) if basicrequests.do_post_request(url, data, port=daemon.shared_state.get(NetController).socksPort) == 'Success': logger.info('Successfully introduced node to ' + peer, terminal=True) - retData = True + ret_data = True keydb.transportinfo.set_address_info(peer, 'introduced', 1) keydb.transportinfo.set_address_info(peer, 'powValue', data['random']) daemon.decrementThreadCount('announce_node') - return retData \ No newline at end of file + return ret_data \ No newline at end of file diff --git a/onionr/etc/onionrvalues.py b/onionr/etc/onionrvalues.py index c70ff0c6..f135e0d9 100755 --- a/onionr/etc/onionrvalues.py +++ b/onionr/etc/onionrvalues.py @@ -29,15 +29,13 @@ DEVELOPMENT_MODE = True MAX_BLOCK_TYPE_LENGTH = 15 +# Begin OnionrValues migrated values +ANNOUNCE_POW = 5 +DEFAULT_EXPIRE = 2592000 +BLOCK_METADATA_LENGTHS = {'meta': 1000, 'sig': 200, 'signer': 200, 'time': 10, 'pow': 1000, 'encryptType': 4, 'expire': 14} + platform = platform.system() if platform == 'Windows': SCRIPT_NAME = 'run-windows.bat' else: SCRIPT_NAME = 'onionr.sh' - -class OnionrValues: - def __init__(self): - self.passwordLength = 20 - self.blockMetadataLengths = {'meta': 1000, 'sig': 200, 'signer': 200, 'time': 10, 'pow': 1000, 'encryptType': 4, 'expire': 14} #TODO properly refine values to minimum needed - self.default_expire = 2592000 - self.announce_pow = 5 \ No newline at end of file diff --git a/onionr/httpapi/miscpublicapi/announce.py b/onionr/httpapi/miscpublicapi/announce.py index 53fb96e8..7a4fcad7 100755 --- a/onionr/httpapi/miscpublicapi/announce.py +++ b/onionr/httpapi/miscpublicapi/announce.py @@ -55,7 +55,7 @@ def handle_announce(request): powHash = powHash.decode() except AttributeError: pass - if powHash.startswith('0' * onionrvalues.OnionrValues().announce_pow): + if powHash.startswith('0' * onionrvalues.ANNOUNCE_POW): newNode = bytesconverter.bytes_to_str(newNode) announce_queue = deadsimplekv.DeadSimpleKV(filepaths.announce_cache) announce_queue_list = announce_queue.get('new_peers') diff --git a/onionr/onionrblocks/insert.py b/onionr/onionrblocks/insert.py index fbaadcc7..d297f706 100644 --- a/onionr/onionrblocks/insert.py +++ b/onionr/onionrblocks/insert.py @@ -13,7 +13,6 @@ def insert_block(data, header='txt', sign=False, encryptType='', symKey='', asym encryptType must be specified to encrypt a block ''' use_subprocess = powchoice.use_subprocess(config) - requirements = onionrvalues.OnionrValues() storage_counter = storagecounter.StorageCounter() allocationReachedMessage = 'Cannot insert block, disk allocation reached.' if storage_counter.isFull(): diff --git a/onionr/onionrutils/basicrequests.py b/onionr/onionrutils/basicrequests.py index 3bd6a75d..4ea13f5b 100644 --- a/onionr/onionrutils/basicrequests.py +++ b/onionr/onionrutils/basicrequests.py @@ -90,4 +90,4 @@ def do_get_request(url, port=0, proxyType='tor', ignoreAPI=False, returnHeaders= if returnHeaders: return (retData, response_headers) else: - return retData \ No newline at end of file + return retData diff --git a/onionr/onionrutils/blockmetadata/process.py b/onionr/onionrutils/blockmetadata/process.py index ee68d690..abcf5c71 100644 --- a/onionr/onionrutils/blockmetadata/process.py +++ b/onionr/onionrutils/blockmetadata/process.py @@ -57,7 +57,7 @@ def process_block_metadata(blockHash: str): expireTime = myBlock.getHeader('expire') assert len(str(int(expireTime))) < 20 # test that expire time is an integer of sane length (for epoch) except (AssertionError, ValueError, TypeError) as e: - expireTime = onionrvalues.OnionrValues().default_expire + curTime + expireTime = onionrvalues.DEFAULT_EXPIRE + curTime finally: blockmetadb.update_block_info(blockHash, 'expire', expireTime) onionrevents.event('processblocks', data = {'block': myBlock, 'type': blockType, 'signer': signer, 'validSig': valid}) diff --git a/onionr/onionrutils/bytesconverter.py b/onionr/onionrutils/bytesconverter.py index 5df4d673..b6ef9741 100644 --- a/onionr/onionrutils/bytesconverter.py +++ b/onionr/onionrutils/bytesconverter.py @@ -11,4 +11,4 @@ def bytes_to_str(data): data = data.decode('UTF-8') except AttributeError: pass - return data \ No newline at end of file + return data diff --git a/onionr/onionrutils/checkcommunicator.py b/onionr/onionrutils/checkcommunicator.py index 2e300f57..7fc69904 100644 --- a/onionr/onionrutils/checkcommunicator.py +++ b/onionr/onionrutils/checkcommunicator.py @@ -36,4 +36,4 @@ def is_communicator_running(timeout = 5, interval = 0.1): elif time.time() - starttime >= timeout: return False except: - return False \ No newline at end of file + return False diff --git a/onionr/onionrutils/epoch.py b/onionr/onionrutils/epoch.py index 01922a4c..62e82d34 100644 --- a/onionr/onionrutils/epoch.py +++ b/onionr/onionrutils/epoch.py @@ -27,4 +27,4 @@ def get_rounded_epoch(roundS=60): def get_epoch(): '''returns epoch''' - return math.floor(time.time()) \ No newline at end of file + return math.floor(time.time()) diff --git a/onionr/onionrutils/escapeansi.py b/onionr/onionrutils/escapeansi.py index 1f88e149..ca635097 100644 --- a/onionr/onionrutils/escapeansi.py +++ b/onionr/onionrutils/escapeansi.py @@ -7,4 +7,4 @@ def escape_ANSI(line): cc-by-sa-3 license https://creativecommons.org/licenses/by-sa/3.0/ ''' ansi_escape = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]') - return ansi_escape.sub('', line) \ No newline at end of file + return ansi_escape.sub('', line) diff --git a/onionr/onionrutils/getclientapiserver.py b/onionr/onionrutils/getclientapiserver.py index 2338080c..1d196eae 100644 --- a/onionr/onionrutils/getclientapiserver.py +++ b/onionr/onionrutils/getclientapiserver.py @@ -34,4 +34,4 @@ def get_client_API_server(): raise FileNotFoundError else: retData += '%s:%s' % (hostname, port) - return retData \ No newline at end of file + return retData diff --git a/onionr/onionrutils/importnewblocks.py b/onionr/onionrutils/importnewblocks.py index 80bfdc02..20270af9 100644 --- a/onionr/onionrutils/importnewblocks.py +++ b/onionr/onionrutils/importnewblocks.py @@ -46,4 +46,4 @@ def import_new_blocks(scanDir=''): else: logger.warn('Failed to verify hash for %s' % block, terminal=True) if not exist: - logger.info('No blocks found to import', terminal=True) \ No newline at end of file + logger.info('No blocks found to import', terminal=True) diff --git a/onionr/onionrutils/localcommand.py b/onionr/onionrutils/localcommand.py index 073182bd..0aab7dc3 100644 --- a/onionr/onionrutils/localcommand.py +++ b/onionr/onionrutils/localcommand.py @@ -59,12 +59,12 @@ def local_command(command, data='', silent = True, post=False, postData = {}, ma try: if post: - retData = requests.post(payload, data=postData, headers={'token': config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text + ret_data = requests.post(payload, data=postData, headers={'token': config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text else: - retData = requests.get(payload, headers={'token': config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text + ret_data = requests.get(payload, headers={'token': config.get('client.webpassword'), 'Connection':'close'}, timeout=(maxWait, maxWait)).text except Exception as error: if not silent: logger.error('Failed to make local request (command: %s):%s' % (command, error), terminal=True) - retData = False + ret_data = False - return retData \ No newline at end of file + return ret_data diff --git a/onionr/onionrutils/validatemetadata.py b/onionr/onionrutils/validatemetadata.py index f1c58f81..51723824 100644 --- a/onionr/onionrutils/validatemetadata.py +++ b/onionr/onionrutils/validatemetadata.py @@ -24,9 +24,8 @@ from onionrutils import stringvalidators, epoch, bytesconverter import config, filepaths, onionrcrypto def validate_metadata(metadata, blockData): '''Validate metadata meets onionr spec (does not validate proof value computation), take in either dictionary or json string''' - # TODO, make this check sane sizes - requirements = onionrvalues.OnionrValues() - retData = False + + ret_data = False maxClockDifference = 120 # convert to dict if it is json string @@ -37,11 +36,11 @@ def validate_metadata(metadata, blockData): pass # Validate metadata dict for invalid keys to sizes that are too large - maxAge = config.get("general.max_block_age", onionrvalues.OnionrValues().default_expire) + maxAge = config.get("general.max_block_age", onionrvalues.DEFAULT_EXPIRE) if type(metadata) is dict: for i in metadata: try: - requirements.blockMetadataLengths[i] + onionrvalues.BLOCK_METADATA_LENGTHS[i] except KeyError: logger.warn('Block has invalid metadata key ' + i) break @@ -51,7 +50,7 @@ def validate_metadata(metadata, blockData): testData = len(testData) except (TypeError, AttributeError) as e: testData = len(str(testData)) - if requirements.blockMetadataLengths[i] < testData: + if onionrvalues.BLOCK_METADATA_LENGTHS[i] < testData: logger.warn('Block metadata key ' + i + ' exceeded maximum size') break if i == 'time': @@ -84,16 +83,16 @@ def validate_metadata(metadata, blockData): try: with open(filepaths.data_nonce_file, 'r') as nonceFile: if nonce in nonceFile.read(): - retData = False # we've seen that nonce before, so we can't pass metadata + ret_data = False # we've seen that nonce before, so we can't pass metadata raise onionrexceptions.DataExists except FileNotFoundError: - retData = True + ret_data = True except onionrexceptions.DataExists: - # do not set retData to True, because nonce has been seen before + # do not set ret_data to True, because nonce has been seen before pass else: - retData = True + ret_data = True else: logger.warn('In call to utils.validateMetadata, metadata must be JSON string or a dictionary object') - return retData \ No newline at end of file + return ret_data