refactoring

This commit is contained in:
Kevin Froman 2019-08-09 15:41:27 -05:00
parent 7c02f6fff1
commit daff149acc
14 changed files with 36 additions and 41 deletions

View File

@ -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
return ret_data

View File

@ -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

View File

@ -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')

View File

@ -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():

View File

@ -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
return retData

View File

@ -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})

View File

@ -11,4 +11,4 @@ def bytes_to_str(data):
data = data.decode('UTF-8')
except AttributeError:
pass
return data
return data

View File

@ -36,4 +36,4 @@ def is_communicator_running(timeout = 5, interval = 0.1):
elif time.time() - starttime >= timeout:
return False
except:
return False
return False

View File

@ -27,4 +27,4 @@ def get_rounded_epoch(roundS=60):
def get_epoch():
'''returns epoch'''
return math.floor(time.time())
return math.floor(time.time())

View File

@ -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)
return ansi_escape.sub('', line)

View File

@ -34,4 +34,4 @@ def get_client_API_server():
raise FileNotFoundError
else:
retData += '%s:%s' % (hostname, port)
return retData
return retData

View File

@ -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)
logger.info('No blocks found to import', terminal=True)

View File

@ -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
return ret_data

View File

@ -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
return ret_data