Started reducing unnecessary disk i/o:

* Spawn core.Core() much less
master
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
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 import Timeout
import flask, cgi, uuid
import flask
from flask import request, Response, abort, send_from_directory
import sys, random, threading, hmac, base64, time, os, json, socket
import core
from onionrblockapi import Block
import onionrutils, onionrexceptions, onionrcrypto, blockimporter, onionrevents as events, logger, config
@ -29,6 +29,7 @@ import httpapi
from httpapi import friendsapi, profilesapi, configapi, miscpublicapi
from onionrservices import httpheaders
import onionr
config.reload()
class FDSafeHandler(WSGIHandler):
'''Our WSGI handler. Doesn't do much non-default except timeouts'''
@ -57,7 +58,6 @@ def setBindIP(filePath=''):
else:
data = '127.0.0.1'
if filePath != '':
print(filePath)
with open(filePath, 'w') as bindFile:
bindFile.write(data)
return data
@ -332,7 +332,6 @@ class API:
if self._core._utils.validateHash(name):
try:
resp = Block(name, decrypt=True).bcontent
#resp = cgi.escape(Block(name, decrypt=True).bcontent, quote=True)
except TypeError:
pass
else:

View File

@ -371,7 +371,7 @@ class OnionrCommunicatorDaemon:
time.sleep(1)
else:
# 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...')
self.shutdown = True
self.decrementThreadCount('detectAPICrash')

View File

@ -23,7 +23,7 @@ def handle_daemon_commands(comm_inst):
cmd = comm_inst._core.daemonQueue()
response = ''
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':
comm_inst.shutdown = True
elif cmd[0] == 'announceNode':

View File

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

View File

@ -246,7 +246,7 @@ class OnionrCrypto:
except AttributeError:
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')):
difficulty = int(config.get('general.minimum_block_pow'))

View File

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

View File

@ -45,7 +45,7 @@ def getDifficultyModifier(coreOrUtilsInst=None):
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
'''
@ -61,7 +61,7 @@ def getDifficultyForNewBlock(data, ourBlock=True):
else:
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
@ -216,7 +216,7 @@ class POW:
self.difficulty = forceDifficulty
else:
# 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)

View File

@ -185,7 +185,7 @@ class OnionrUtils:
self._core.updateBlockInfo(blockHash, 'expire', expireTime)
if not blockType is None:
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:
pass
#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)
# 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)