added hasBlock function

This commit is contained in:
Kevin Froman 2018-01-28 20:52:48 -06:00
parent b4d61d3598
commit 1c3e886ba0
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
2 changed files with 19 additions and 1 deletions

View File

@ -108,6 +108,8 @@ class OnionrCommunicate:
logger.debug('BLOCKS: \n' + blocks)
blockList = blocks.split('\n')
for i in blockList:
if self._utils.hasBlock(i):
continue
logger.debug('Exchanged block (blockList): ' + i)
if not self._utils.validateHash(i):
# skip hash if it isn't valid

View File

@ -18,7 +18,7 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
# Misc functions that do not fit in the main api, but are useful
import getpass, sys, requests, configparser, os, socket, gnupg, hashlib, logger
import getpass, sys, requests, configparser, os, socket, gnupg, hashlib, logger, sqlite3
if sys.version_info < (3, 6):
try:
import sha3
@ -96,6 +96,22 @@ class OnionrUtils:
dataHash = hasher.hexdigest()
return dataHash
def hasBlock(self, hash):
'''detect if we have a block in the list or not'''
conn = sqlite3.connect(self._core.blockDB)
c = conn.cursor()
if not self.validateHash(hash):
raise Exception("Invalid hash")
for result in c.execute("SELECT COUNT() FROM hashes where hash='" + hash + "'"):
if result[0] >= 1:
conn.commit()
conn.close()
return True
else:
conn.commit()
conn.close()
return False
def validateHash(self, data, length=64):
'''Validate if a string is a valid hex formatted hash'''
retVal = True