Started reducing unnecessary disk i/o:

* Spawn core.Core() much less
This commit is contained in:
Kevin Froman 2019-06-15 13:56:57 -05:00
parent d054722bce
commit 7cb5f212ec
9 changed files with 17 additions and 20 deletions

View File

@ -17,11 +17,11 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import random, threading, hmac, base64, time, os, json, socket
from gevent.pywsgi import WSGIServer, WSGIHandler from gevent.pywsgi import WSGIServer, WSGIHandler
from gevent import Timeout from gevent import Timeout
import flask, cgi, uuid import flask
from flask import request, Response, abort, send_from_directory from flask import request, Response, abort, send_from_directory
import sys, random, threading, hmac, base64, time, os, json, socket
import core import core
from onionrblockapi import Block from onionrblockapi import Block
import onionrutils, onionrexceptions, onionrcrypto, blockimporter, onionrevents as events, logger, config import onionrutils, onionrexceptions, onionrcrypto, blockimporter, onionrevents as events, logger, config
@ -29,6 +29,7 @@ import httpapi
from httpapi import friendsapi, profilesapi, configapi, miscpublicapi from httpapi import friendsapi, profilesapi, configapi, miscpublicapi
from onionrservices import httpheaders from onionrservices import httpheaders
import onionr import onionr
config.reload() config.reload()
class FDSafeHandler(WSGIHandler): class FDSafeHandler(WSGIHandler):
'''Our WSGI handler. Doesn't do much non-default except timeouts''' '''Our WSGI handler. Doesn't do much non-default except timeouts'''
@ -57,7 +58,6 @@ def setBindIP(filePath=''):
else: else:
data = '127.0.0.1' data = '127.0.0.1'
if filePath != '': if filePath != '':
print(filePath)
with open(filePath, 'w') as bindFile: with open(filePath, 'w') as bindFile:
bindFile.write(data) bindFile.write(data)
return data return data
@ -332,7 +332,6 @@ class API:
if self._core._utils.validateHash(name): if self._core._utils.validateHash(name):
try: try:
resp = Block(name, decrypt=True).bcontent resp = Block(name, decrypt=True).bcontent
#resp = cgi.escape(Block(name, decrypt=True).bcontent, quote=True)
except TypeError: except TypeError:
pass pass
else: else:

View File

@ -371,7 +371,7 @@ class OnionrCommunicatorDaemon:
time.sleep(1) time.sleep(1)
else: else:
# This executes if the api is NOT detected to be running # This executes if the api is NOT detected to be running
events.event('daemon_crash', onionr = None, data = {}) events.event('daemon_crash', onionr = self._core.onionrInst, data = {})
logger.error('Daemon detected API crash (or otherwise unable to reach API after long time), stopping...') logger.error('Daemon detected API crash (or otherwise unable to reach API after long time), stopping...')
self.shutdown = True self.shutdown = True
self.decrementThreadCount('detectAPICrash') self.decrementThreadCount('detectAPICrash')

View File

@ -23,7 +23,7 @@ def handle_daemon_commands(comm_inst):
cmd = comm_inst._core.daemonQueue() cmd = comm_inst._core.daemonQueue()
response = '' response = ''
if cmd is not False: if cmd is not False:
events.event('daemon_command', onionr = None, data = {'cmd' : cmd}) events.event('daemon_command', onionr = comm_inst._core.onionrInst, data = {'cmd' : cmd})
if cmd[0] == 'shutdown': if cmd[0] == 'shutdown':
comm_inst.shutdown = True comm_inst.shutdown = True
elif cmd[0] == 'announceNode': elif cmd[0] == 'announceNode':

View File

@ -39,7 +39,6 @@ class Core:
''' '''
Initialize Core Onionr library Initialize Core Onionr library
''' '''
# set data dir # set data dir
self.dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/')) self.dataDir = os.environ.get('ONIONR_HOME', os.environ.get('DATA_DIR', 'data/'))
if not self.dataDir.endswith('/'): if not self.dataDir.endswith('/'):
@ -135,7 +134,7 @@ class Core:
if not self._utils.validatePubKey(peerID): if not self._utils.validatePubKey(peerID):
return False return False
events.event('pubkey_add', data = {'key': peerID}, onionr = None) events.event('pubkey_add', data = {'key': peerID}, onionr = self.onionrInst)
conn = sqlite3.connect(self.peerDB, timeout=30) conn = sqlite3.connect(self.peerDB, timeout=30)
hashID = self._crypto.pubKeyHashID(peerID) hashID = self._crypto.pubKeyHashID(peerID)
@ -187,7 +186,7 @@ class Core:
conn.commit() conn.commit()
conn.close() conn.close()
events.event('address_add', data = {'address': address}, onionr = None) events.event('address_add', data = {'address': address}, onionr = self.onionrInst)
return True return True
else: else:
@ -207,7 +206,7 @@ class Core:
conn.commit() conn.commit()
conn.close() conn.close()
events.event('address_remove', data = {'address': address}, onionr = None) events.event('address_remove', data = {'address': address}, onionr = self.onionrInst)
return True return True
else: else:
return False return False
@ -342,7 +341,7 @@ class Core:
conn.commit() conn.commit()
conn.close() conn.close()
events.event('queue_pop', data = {'data': retData}, onionr = None) events.event('queue_pop', data = {'data': retData}, onionr = self.onionrInst)
return retData return retData
@ -363,7 +362,7 @@ class Core:
except sqlite3.OperationalError: except sqlite3.OperationalError:
retData = False retData = False
self.daemonQueue() self.daemonQueue()
events.event('queue_push', data = {'command': command, 'data': data}, onionr = None) events.event('queue_push', data = {'command': command, 'data': data}, onionr = self.onionrInst)
conn.close() conn.close()
return retData return retData
@ -406,7 +405,7 @@ class Core:
pass pass
conn.close() conn.close()
events.event('queue_clear', onionr = None) events.event('queue_clear', onionr = self.onionrInst)
return return

View File

@ -246,7 +246,7 @@ class OnionrCrypto:
except AttributeError: except AttributeError:
pass pass
difficulty = onionrproofs.getDifficultyForNewBlock(blockContent, ourBlock=False) difficulty = onionrproofs.getDifficultyForNewBlock(blockContent, ourBlock=False, coreInst=self._core)
if difficulty < int(config.get('general.minimum_block_pow')): if difficulty < int(config.get('general.minimum_block_pow')):
difficulty = int(config.get('general.minimum_block_pow')) difficulty = int(config.get('general.minimum_block_pow'))

View File

@ -34,7 +34,6 @@ class PeerProfiles:
if not isinstance(coreInst, core.Core): if not isinstance(coreInst, core.Core):
raise TypeError("coreInst must be a type of core.Core") raise TypeError("coreInst must be a type of core.Core")
self.coreInst = coreInst self.coreInst = coreInst
assert isinstance(self.coreInst, core.Core)
self.loadScore() self.loadScore()
self.getConnectTime() self.getConnectTime()

View File

@ -45,7 +45,7 @@ def getDifficultyModifier(coreOrUtilsInst=None):
return retData return retData
def getDifficultyForNewBlock(data, ourBlock=True): def getDifficultyForNewBlock(data, ourBlock=True, coreInst=None):
''' '''
Get difficulty for block. Accepts size in integer, Block instance, or str/bytes full block contents Get difficulty for block. Accepts size in integer, Block instance, or str/bytes full block contents
''' '''
@ -61,7 +61,7 @@ def getDifficultyForNewBlock(data, ourBlock=True):
else: else:
minDifficulty = config.get('general.minimum_block_pow', 4) minDifficulty = config.get('general.minimum_block_pow', 4)
retData = max(minDifficulty, math.floor(dataSize / 100000)) + getDifficultyModifier() retData = max(minDifficulty, math.floor(dataSize / 100000)) + getDifficultyModifier(coreInst)
return retData return retData
@ -216,7 +216,7 @@ class POW:
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)) self.difficulty = getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data), coreInst=myCore)
logger.info('Computing POW (difficulty: %s)...' % self.difficulty) logger.info('Computing POW (difficulty: %s)...' % self.difficulty)

View File

@ -185,7 +185,7 @@ class OnionrUtils:
self._core.updateBlockInfo(blockHash, 'expire', expireTime) self._core.updateBlockInfo(blockHash, 'expire', expireTime)
if not blockType is None: if not blockType is None:
self._core.updateBlockInfo(blockHash, 'dataType', blockType) self._core.updateBlockInfo(blockHash, 'dataType', blockType)
onionrevents.event('processblocks', data = {'block': myBlock, 'type': blockType, 'signer': signer, 'validSig': valid}, onionr = None) onionrevents.event('processblocks', data = {'block': myBlock, 'type': blockType, 'signer': signer, 'validSig': valid}, onionr = self._core.onionrInst)
else: else:
pass pass
#logger.debug('Not processing metadata on encrypted block we cannot decrypt.') #logger.debug('Not processing metadata on encrypted block we cannot decrypt.')

View File

@ -53,7 +53,7 @@ class SubprocessPOW:
self.data = onionrutils.OnionrUtils.strToBytes(data) self.data = onionrutils.OnionrUtils.strToBytes(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)) self.difficulty = onionrproofs.getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data), coreInst=self.core_inst)
logger.info('Computing POW (difficulty: %s)...' % self.difficulty) logger.info('Computing POW (difficulty: %s)...' % self.difficulty)