diff --git a/onionr/communicator.py b/onionr/communicator.py index 00f4d31c..565b59cf 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -669,23 +669,28 @@ class OnionrCommunicate: ''' retData = False try: - metadata['pow'] - token = metadata['pow'] + metadata['powToken'] + metadata['powHash'] + token = metadata['powToken'] except KeyError: return False dataLen = len(blockContent) - expectedHash = self._crypto.blake2bHash(blockContent) + expectedHash = self._crypto.blake2bHash(metadata['powToken'] + blockContent) difficulty = 0 - if token == expectedHash: + if metadata['powHash'] == expectedHash: difficulty = math.floor(dataLen/1000000) mainHash = '0000000000000000000000000000000000000000000000000000000000000000'#nacl.hash.blake2b(nacl.utils.random()).decode() puzzle = mainHash[0:difficulty] - if token[0:difficulty] == puzzle: + if metadata['powHash'][0:difficulty] == puzzle: logger.info('Validated block pow') retData = True - + else: + logger.warn("Invalid token") + else: + logger.warn("Invalid token2") + return retData def urlencode(self, data): diff --git a/onionr/core.py b/onionr/core.py index 74a703a4..ab315041 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -635,9 +635,17 @@ class Core: # wait for proof to complete while True: powToken = powProof.getResult() - if powToken != False: + if powToken == False: + time.sleep(0.3) + continue + powHash = powToken[0] + powToken = base64.b64encode(powToken[1]) + try: + powToken = powToken.decode() + except AttributeError: + pass + finally: break - time.sleep(0.3) try: data.decode() @@ -645,7 +653,7 @@ class Core: data = data.encode() retData = '' - metadata = {'type': header, 'pow': powToken} + metadata = {'type': header, 'powHash': powHash, 'powToken': powToken} sig = {} metadata = json.dumps(metadata) diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index 02fee043..d9ece436 100644 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -44,6 +44,7 @@ class POW: blockCheckCount += 1 hbCount += 1 ''' + rand = nacl.utils.random() token = nacl.hash.blake2b(nacl.utils.random() + self.data).decode() #print(token) if self.puzzle == token[0:self.difficulty]: @@ -57,7 +58,7 @@ class POW: if self.reporting: logger.info('Found token ' + token, timestamp=True) logger.info('took ' + str(endTime - startTime) + ' seconds', timestamp=True) - self.result = token + self.result = (token, rand) def __init__(self, data, bitcoinNode=''): self.foundHash = False