From 7d9936e55cce653329864a2d60b296e68457089d Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 5 May 2018 16:50:15 -0500 Subject: [PATCH] work on proof of work --- onionr/communicator.py | 31 +++++++++++++++++++++++++++++++ onionr/core.py | 1 - onionr/onionrcrypto.py | 12 ++++++++++-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/onionr/communicator.py b/onionr/communicator.py index 7bbfe87c..00f4d31c 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -561,6 +561,12 @@ class OnionrCommunicate: blockMeta2 = {'type': ''} pass blockContent = blockContent[blockContent.rfind(b'}') + 1:] + + if not self.verifyPow(blockContent, blockMeta2): + logger.warn(i + " has invalid or insufficient proof of work token, deleting") + self._core.removeBlock(i) + continue + try: blockMetadata['sig'] blockMeta2['id'] @@ -656,6 +662,31 @@ class OnionrCommunicate: peerTryCount += 1 return retVal + + def verifyPow(self, blockContent, metadata): + ''' + Verifies the proof of work associated with a block + ''' + retData = False + try: + metadata['pow'] + token = metadata['pow'] + except KeyError: + return False + dataLen = len(blockContent) + expectedHash = self._crypto.blake2bHash(blockContent) + difficulty = 0 + if token == expectedHash: + difficulty = math.floor(dataLen/1000000) + + mainHash = '0000000000000000000000000000000000000000000000000000000000000000'#nacl.hash.blake2b(nacl.utils.random()).decode() + puzzle = mainHash[0:difficulty] + + if token[0:difficulty] == puzzle: + logger.info('Validated block pow') + retData = True + + return retData def urlencode(self, data): ''' diff --git a/onionr/core.py b/onionr/core.py index dae25bc3..74a703a4 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -638,7 +638,6 @@ class Core: if powToken != False: break time.sleep(0.3) - try: data.decode() diff --git a/onionr/onionrcrypto.py b/onionr/onionrcrypto.py index 3a45eeda..d420cbd4 100644 --- a/onionr/onionrcrypto.py +++ b/onionr/onionrcrypto.py @@ -17,7 +17,7 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import nacl.signing, nacl.encoding, nacl.public, nacl.secret, os, binascii, base64, hashlib, logger +import nacl.signing, nacl.encoding, nacl.public, nacl.hash, nacl.secret, os, binascii, base64, hashlib, logger class OnionrCrypto: def __init__(self, coreInstance): @@ -209,4 +209,12 @@ class OnionrCrypto: hasher.update(pubkey + prev) prev = hasher.hexdigest() result = prev - return result \ No newline at end of file + return result + + def sha3Hash(self, data): + hasher = hashlib.sha3_256() + hasher.update(data) + return hasher.hexdigest() + + def blake2bHash(self, data): + return nacl.hash.blake2b(data) \ No newline at end of file