diff --git a/onionr/communicator.py b/onionr/communicator.py index bdc11256..ee0fed46 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -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 diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 1f0190f1..ec7517e2 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -18,7 +18,7 @@ along with this program. If not, see . ''' # 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