From 9c3416e707aa9d4430cf76b0b0a61cf7b50934ff Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 5 May 2018 18:32:10 -0500 Subject: [PATCH] work on proof of work --- onionr/communicator.py | 15 +++++++++++++-- onionr/onionrcrypto.py | 4 ++++ onionr/onionrproofs.py | 5 +++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/onionr/communicator.py b/onionr/communicator.py index 565b59cf..c70afc0f 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -60,7 +60,7 @@ class OnionrCommunicate: #logger.fatal('Failed to start Bitcoin Node, exiting...') #exit(1) - blockProcessTimer = 0 + blockProcessTimer = 19 blockProcessAmount = 20 highFailureTimer = 0 highFailureRate = 10 @@ -561,6 +561,10 @@ class OnionrCommunicate: blockMeta2 = {'type': ''} pass blockContent = blockContent[blockContent.rfind(b'}') + 1:] + try: + blockContent = blockContent.decode() + except AttributeError: + pass if not self.verifyPow(blockContent, blockMeta2): logger.warn(i + " has invalid or insufficient proof of work token, deleting") @@ -675,8 +679,13 @@ class OnionrCommunicate: except KeyError: return False dataLen = len(blockContent) - expectedHash = self._crypto.blake2bHash(metadata['powToken'] + blockContent) + print(blockContent) + expectedHash = self._crypto.blake2bHash(base64.b64decode(metadata['powToken']) + blockContent.encode()) difficulty = 0 + try: + expectedHash = expectedHash.decode() + except AttributeError: + pass if metadata['powHash'] == expectedHash: difficulty = math.floor(dataLen/1000000) @@ -689,6 +698,8 @@ class OnionrCommunicate: else: logger.warn("Invalid token") else: + logger.warn('expected hash ' + expectedHash) + logger.warn('got hash ' + metadata['powHash']) logger.warn("Invalid token2") return retData diff --git a/onionr/onionrcrypto.py b/onionr/onionrcrypto.py index d420cbd4..487e192a 100644 --- a/onionr/onionrcrypto.py +++ b/onionr/onionrcrypto.py @@ -217,4 +217,8 @@ class OnionrCrypto: return hasher.hexdigest() def blake2bHash(self, data): + try: + data = data.encode() + except AttributeError: + pass return nacl.hash.blake2b(data) \ No newline at end of file diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index d9ece436..0ac9b95c 100644 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -19,7 +19,7 @@ ''' import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger, sys -import btc +import btc, core class POW: def pow(self, reporting = False): @@ -34,6 +34,7 @@ class POW: blockCheckCount = 0 block = '' #self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight()) #logger.debug('thread started') + myCore = core.Core() while self.hashing: ''' if blockCheckCount == blockCheck: @@ -45,7 +46,7 @@ class POW: hbCount += 1 ''' rand = nacl.utils.random() - token = nacl.hash.blake2b(nacl.utils.random() + self.data).decode() + token = nacl.hash.blake2b(rand + self.data).decode() #print(token) if self.puzzle == token[0:self.difficulty]: self.hashing = False