remove DataPOW and make POW use non hard coded start int value

master
Kevin Froman 2020-01-19 21:02:51 -06:00
parent 7db8193bf6
commit f3b22dad56
2 changed files with 11 additions and 93 deletions

View File

@ -24,6 +24,8 @@ import config, logger
from onionrblocks import onionrblockapi, storagecounter
from onionrutils import bytesconverter
from onionrcrypto import hashers
from .blocknoncestart import BLOCK_NONCE_START_INT
config.reload()
def getDifficultyModifier():
@ -70,90 +72,6 @@ def hashMeetsDifficulty(hexHash):
return hashDifficulty >= expected
class DataPOW:
def __init__(self, data, minDifficulty = 0, threadCount = 1):
self.data = data
self.threadCount = threadCount
self.difficulty = max(minDifficulty, getDifficultyForNewBlock(data))
self.rounds = 0
self.hashing = False
self.foundHash = False
try:
self.data = self.data.encode()
except AttributeError:
pass
self.data = nacl.hash.blake2b(self.data)
logger.info('Computing POW (difficulty: %s)...' % self.difficulty)
self.mainHash = '0' * 70
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
for i in range(max(1, threadCount)):
t = threading.Thread(name = 'thread%s' % i, target = self.pow, args = (True,))
t.start()
def pow(self, reporting = False):
startTime = math.floor(time.time())
self.hashing = True
self.reporting = reporting
iFound = False # if current thread is the one that found the answer
while self.hashing:
rand = nacl.utils.random()
token = nacl.hash.blake2b(rand + self.data).decode()
self.rounds += 1
#print(token)
if self.puzzle == token[0:self.difficulty]:
self.hashing = False
iFound = True
break
if iFound:
endTime = math.floor(time.time())
if self.reporting:
logger.debug('Found token after %s seconds: %s' % (endTime - startTime, token), timestamp=True)
logger.debug('Round count: %s' % (self.rounds,))
self.result = (token, rand)
def shutdown(self):
self.hashing = False
self.puzzle = ''
def changeDifficulty(self, newDiff):
self.difficulty = newDiff
def getResult(self):
'''
Returns the result then sets to false, useful to automatically clear the result
'''
try:
retVal = self.result
except AttributeError:
retVal = False
self.result = False
return retVal
def waitForResult(self):
'''
Returns the result only when it has been found, False if not running and not found
'''
result = False
try:
while True:
result = self.getResult()
if not self.hashing:
break
else:
time.sleep(1)
except KeyboardInterrupt:
self.shutdown()
logger.warn('Got keyboard interrupt while waiting for POW result, stopping')
return result
class POW:
def __init__(self, metadata, data, threadCount = 1, minDifficulty=0):
@ -191,9 +109,8 @@ class POW:
self.hashing = True
self.reporting = reporting
iFound = False # if current thread is the one that found the answer
nonce = int(binascii.hexlify(nacl.utils.random(64)), 16)
nonce = BLOCK_NONCE_START_INT
while self.hashing:
#token = nacl.hash.blake2b(rand + self.data).decode()
self.metadata['pow'] = nonce
payload = json.dumps(self.metadata).encode() + b'\n' + self.data
token = hashers.sha3_hash(payload)

View File

@ -15,6 +15,7 @@ import logger
import onionrproofs
import onionrcrypto as crypto
from onionrutils import bytesconverter
from .blocknoncestart import BLOCK_NONCE_START_INT
"""
This program is free software: you can redistribute it and/or modify
@ -108,7 +109,7 @@ class SubprocessPOW:
def do_pow(self, pipe):
"""find partial hash colision generating nonce for a block"""
nonce = -10000000
nonce = BLOCK_NONCE_START_INT
data = self.data
metadata = self.metadata
puzzle = self.puzzle