efficiency improvements
This commit is contained in:
parent
3aa539ce82
commit
66e55d5a50
@ -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
|
||||
|
||||
|
@ -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:
|
||||
|
26
onionr/onionrblocks.py
Normal file
26
onionr/onionrblocks.py
Normal file
@ -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 <https://www.gnu.org/licenses/>.
|
||||
'''
|
||||
import json
|
||||
class OnionrBlocks:
|
||||
def __init__(self, coreInstance):
|
||||
return
|
||||
def metadataGenerate(self):
|
||||
return
|
||||
|
@ -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
|
Loading…
Reference in New Issue
Block a user