diff --git a/onionr/api.py b/onionr/api.py index 90a3b83e..34c320d9 100755 --- a/onionr/api.py +++ b/onionr/api.py @@ -24,7 +24,7 @@ from gevent.wsgi import WSGIServer import sys, random, threading, hmac, hashlib, base64, time, math, os, json from core import Core from onionrblockapi import Block -import onionrutils, onionrcrypto, blockimporter, onionrevents as events, logger, config +import onionrutils, onionrexceptions, onionrcrypto, blockimporter, onionrevents as events, logger, config class API: ''' @@ -119,9 +119,7 @@ class API: ''' Simply define the request as not having yet failed, before every request. ''' - self.requestFailed = False - return @app.after_request @@ -397,10 +395,14 @@ class API: pass else: if sys.getsizeof(data) < 100000000: - if blockimporter.importBlockFromData(data, self._core): - resp = 'success' - else: - logger.warn('Error encountered importing uploaded block') + try: + if blockimporter.importBlockFromData(data, self._core): + resp = 'success' + else: + logger.warn('Error encountered importing uploaded block') + except onionrexceptions.BlacklistedBlock: + logger.debug('uploaded block is blacklisted') + pass resp = Response(resp) return resp diff --git a/onionr/blockimporter.py b/onionr/blockimporter.py index a2695093..f5698d00 100644 --- a/onionr/blockimporter.py +++ b/onionr/blockimporter.py @@ -20,6 +20,12 @@ import core, onionrexceptions, logger def importBlockFromData(content, coreInst): retData = False + + dataHash = coreInst._getSha3Hash(content) + + if coreInst._blacklist.inBlacklist(dataHash): + raise onionrexceptions.BlacklistedBlock('%s is a blacklisted block' % (dataHash,)) + if not isinstance(coreInst, core.Core): raise Exception("coreInst must be an Onionr core instance") diff --git a/onionr/core.py b/onionr/core.py index 440f31e0..8f517e82 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -133,7 +133,6 @@ class Core: for i in c.execute("SELECT * FROM adders where address = '" + address + "';"): try: if i[0] == address: - logger.warn('Not adding existing address') conn.close() return False except ValueError: @@ -311,16 +310,24 @@ class Core: return data - def setData(self, data): - ''' - Set the data assciated with a hash - ''' - data = data + def _getSha3Hash(self, data): hasher = hashlib.sha3_256() if not type(data) is bytes: data = data.encode() hasher.update(data) dataHash = hasher.hexdigest() + return dataHash + + def setData(self, data): + ''' + Set the data assciated with a hash + ''' + data = data + if not type(data) is bytes: + data = data.encode() + + dataHash = self._getSha3Hash(data) + if type(dataHash) is bytes: dataHash = dataHash.decode() blockFileName = self.blockDataLocation + dataHash + '.dat' diff --git a/onionr/onionrexceptions.py b/onionr/onionrexceptions.py index 2817d419..7b0de6c7 100644 --- a/onionr/onionrexceptions.py +++ b/onionr/onionrexceptions.py @@ -38,6 +38,9 @@ class InvalidPubkey(Exception): class InvalidMetadata(Exception): pass +class BlacklistedBlock(Exception): + pass + class InvalidHexHash(Exception): '''When a string is not a valid hex string of appropriate length for a hash value''' pass diff --git a/onionr/onionrpeers.py b/onionr/onionrpeers.py index 21697033..2762277c 100644 --- a/onionr/onionrpeers.py +++ b/onionr/onionrpeers.py @@ -72,7 +72,7 @@ def getScoreSortedPeerList(coreInst): return peerList def peerCleanup(coreInst): - '''Removes peers who have been offline too long''' + '''Removes peers who have been offline too long or score too low''' if not type(coreInst is core.Core): raise TypeError('coreInst must be instance of core.Core')