From 66e55d5a50112726a20bd3e02895e029b82b0910 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Wed, 25 Apr 2018 17:42:42 -0500 Subject: [PATCH] efficiency improvements --- onionr/communicator.py | 17 +++++++++++++++-- onionr/core.py | 9 +++------ onionr/onionrblocks.py | 26 ++++++++++++++++++++++++++ onionr/onionrutils.py | 2 ++ 4 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 onionr/onionrblocks.py diff --git a/onionr/communicator.py b/onionr/communicator.py index a0ec9a0c..79719ceb 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -36,7 +36,7 @@ class OnionrCommunicate: self._netController = netcontroller.NetController(0) # arg is the HS port but not needed rn in this file self.newHashes = {} # use this to not keep hashes around too long if we cant get their data - self.keepNewHash = 20 + self.keepNewHash = 12 self.ignoredHashes = [] self.highFailureAmount = 7 @@ -182,6 +182,11 @@ class OnionrCommunicate: peerList = self._core.listAdders() blocks = '' for i in peerList: + try: + if self.peerData[i]['failCount'] >= self.highFailureAmount: + continue + except KeyError: + pass lastDB = self._core.getAddressInfo(i, 'DBHash') if lastDB == None: logger.debug('Fetching hash from ' + i + ' No previous known.') @@ -199,6 +204,7 @@ class OnionrCommunicate: blocks += self.performGet('getBlockHashes', i) except TypeError: logger.warn('Failed to get data hash from ' + i) + self.peerData[peer]['failCount'] -= 1 if self._utils.validateHash(currentDB): self._core.setAddressInfo(i, "DBHash", currentDB) if len(blocks.strip()) != 0: @@ -252,17 +258,23 @@ class OnionrCommunicate: del self.newHashes[i] return - def downloadBlock(self, hash): + def downloadBlock(self, hash, peerTries=3): ''' Download a block from random order of peers ''' retVal = False peerList = self._core.listAdders() blocks = '' + peerTryCount = 0 for i in peerList: + if self.peerData[i]['failCount'] >= self.highFailureAmount: + continue + if peerTryCount >= peerTries: + break hasher = hashlib.sha3_256() data = self.performGet('getData', i, hash, skipHighFailureAddress=True) if data == False or len(data) > 10000000 or data == '': + peerTryCount += 1 continue try: data = base64.b64decode(data) @@ -282,6 +294,7 @@ class OnionrCommunicate: logger.debug('Block text:\n' + data.decode()) else: logger.warn("Failed to validate " + hash + " " + " hash calculated was " + digest) + peerTryCount += 1 return retVal diff --git a/onionr/core.py b/onionr/core.py index 6771e03e..cd3e4b6c 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -552,9 +552,9 @@ class Core: c = conn.cursor() retData = '' if unsaved: - execute = 'SELECT hash FROM hashes WHERE dataSaved != 1;' + execute = 'SELECT hash FROM hashes WHERE dataSaved != 1 ORDER BY RANDOM();' else: - execute = 'SELECT hash FROM hashes;' + execute = 'SELECT hash FROM hashes ORDER BY RANDOM();' for row in c.execute(execute): for i in row: retData += i + "\n" @@ -594,10 +594,7 @@ class Core: Inserts a block into the network ''' retData = '' - metadata = header - if sign: - metadata += '-' + self._crypto.pubKeyHashID() + '-' - metadata += self._crypto.edSign(data, encodeResult=True) + '-' + metadata = '-' + header + '-' if len(data) == 0: logger.error('Will not insert empty block') else: diff --git a/onionr/onionrblocks.py b/onionr/onionrblocks.py new file mode 100644 index 00000000..ffa8207c --- /dev/null +++ b/onionr/onionrblocks.py @@ -0,0 +1,26 @@ +''' +Onionr - P2P Microblogging Platform & Social network. + +This class contains the OnionrBlocks class which is a class for working with Onionr blocks +''' +''' + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +''' +import json +class OnionrBlocks: + def __init__(self, coreInstance): + return + def metadataGenerate(self): + return + diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index b7534c42..af1dc440 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -333,6 +333,8 @@ class OnionrUtils: pass else: logger.info('Recieved message: ' + message.decode()) + except FileNotFoundError: + pass except Exception as error: logger.error('Failed to open block ' + str(i) + '.', error=error) return \ No newline at end of file