work on proof of work

master
Kevin Froman 2018-05-05 17:36:03 -05:00
parent 7d9936e55c
commit 1f8eb925c6
3 changed files with 24 additions and 10 deletions

View File

@ -669,22 +669,27 @@ 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

View File

@ -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)

View File

@ -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