remove deprecated POW
parent
eeab5ef2c2
commit
5751468e8a
|
@ -72,97 +72,3 @@ def hashMeetsDifficulty(hexHash):
|
|||
|
||||
return hashDifficulty >= expected
|
||||
|
||||
|
||||
class POW:
|
||||
def __init__(self, metadata, data, threadCount = 1, minDifficulty=0):
|
||||
self.foundHash = False
|
||||
self.difficulty = 0
|
||||
self.data = data
|
||||
self.metadata = metadata
|
||||
self.threadCount = threadCount
|
||||
self.hashing = False
|
||||
|
||||
json_metadata = json.dumps(metadata).encode()
|
||||
|
||||
try:
|
||||
self.data = self.data.encode()
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
if minDifficulty > 0:
|
||||
self.difficulty = minDifficulty
|
||||
else:
|
||||
# Calculate difficulty. Dumb for now, may use good algorithm in the future.
|
||||
self.difficulty = getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data))
|
||||
|
||||
logger.info('Computing POW (difficulty: %s)...' % (self.difficulty,))
|
||||
|
||||
self.mainHash = '0' * 64
|
||||
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
|
||||
nonce = BLOCK_NONCE_START_INT
|
||||
while self.hashing:
|
||||
self.metadata['pow'] = nonce
|
||||
payload = json.dumps(self.metadata).encode() + b'\n' + self.data
|
||||
token = hashers.sha3_hash(payload)
|
||||
try:
|
||||
# on some versions, token is bytes
|
||||
token = token.decode()
|
||||
except AttributeError:
|
||||
pass
|
||||
if self.puzzle == token[0:self.difficulty]:
|
||||
self.hashing = False
|
||||
iFound = True
|
||||
self.result = payload
|
||||
break
|
||||
nonce += 1
|
||||
|
||||
if iFound:
|
||||
endTime = math.floor(time.time())
|
||||
if self.reporting:
|
||||
logger.debug('Found token after %s seconds: %s' % (endTime - startTime, token), timestamp=True)
|
||||
|
||||
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
|
Loading…
Reference in New Issue