remove DataPOW and make POW use non hard coded start int value
This commit is contained in:
parent
7db8193bf6
commit
f3b22dad56
@ -24,10 +24,12 @@ import config, logger
|
|||||||
from onionrblocks import onionrblockapi, storagecounter
|
from onionrblocks import onionrblockapi, storagecounter
|
||||||
from onionrutils import bytesconverter
|
from onionrutils import bytesconverter
|
||||||
from onionrcrypto import hashers
|
from onionrcrypto import hashers
|
||||||
|
|
||||||
|
from .blocknoncestart import BLOCK_NONCE_START_INT
|
||||||
config.reload()
|
config.reload()
|
||||||
|
|
||||||
def getDifficultyModifier():
|
def getDifficultyModifier():
|
||||||
'''returns the difficulty modifier for block storage based
|
'''returns the difficulty modifier for block storage based
|
||||||
on a variety of factors, currently only disk use.
|
on a variety of factors, currently only disk use.
|
||||||
'''
|
'''
|
||||||
percentUse = storagecounter.StorageCounter().get_percent()
|
percentUse = storagecounter.StorageCounter().get_percent()
|
||||||
@ -70,90 +72,6 @@ def hashMeetsDifficulty(hexHash):
|
|||||||
|
|
||||||
return hashDifficulty >= expected
|
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:
|
class POW:
|
||||||
def __init__(self, metadata, data, threadCount = 1, minDifficulty=0):
|
def __init__(self, metadata, data, threadCount = 1, minDifficulty=0):
|
||||||
@ -170,13 +88,13 @@ class POW:
|
|||||||
self.data = self.data.encode()
|
self.data = self.data.encode()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if minDifficulty > 0:
|
if minDifficulty > 0:
|
||||||
self.difficulty = minDifficulty
|
self.difficulty = minDifficulty
|
||||||
else:
|
else:
|
||||||
# Calculate difficulty. Dumb for now, may use good algorithm in the future.
|
# Calculate difficulty. Dumb for now, may use good algorithm in the future.
|
||||||
self.difficulty = getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data))
|
self.difficulty = getDifficultyForNewBlock(bytes(json_metadata + b'\n' + self.data))
|
||||||
|
|
||||||
logger.info('Computing POW (difficulty: %s)...' % (self.difficulty,))
|
logger.info('Computing POW (difficulty: %s)...' % (self.difficulty,))
|
||||||
|
|
||||||
self.mainHash = '0' * 64
|
self.mainHash = '0' * 64
|
||||||
@ -191,9 +109,8 @@ class POW:
|
|||||||
self.hashing = True
|
self.hashing = True
|
||||||
self.reporting = reporting
|
self.reporting = reporting
|
||||||
iFound = False # if current thread is the one that found the answer
|
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:
|
while self.hashing:
|
||||||
#token = nacl.hash.blake2b(rand + self.data).decode()
|
|
||||||
self.metadata['pow'] = nonce
|
self.metadata['pow'] = nonce
|
||||||
payload = json.dumps(self.metadata).encode() + b'\n' + self.data
|
payload = json.dumps(self.metadata).encode() + b'\n' + self.data
|
||||||
token = hashers.sha3_hash(payload)
|
token = hashers.sha3_hash(payload)
|
||||||
@ -208,7 +125,7 @@ class POW:
|
|||||||
self.result = payload
|
self.result = payload
|
||||||
break
|
break
|
||||||
nonce += 1
|
nonce += 1
|
||||||
|
|
||||||
if iFound:
|
if iFound:
|
||||||
endTime = math.floor(time.time())
|
endTime = math.floor(time.time())
|
||||||
if self.reporting:
|
if self.reporting:
|
||||||
@ -225,12 +142,12 @@ class POW:
|
|||||||
'''
|
'''
|
||||||
Returns the result then sets to false, useful to automatically clear the result
|
Returns the result then sets to false, useful to automatically clear the result
|
||||||
'''
|
'''
|
||||||
|
|
||||||
try:
|
try:
|
||||||
retVal = self.result
|
retVal = self.result
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
retVal = False
|
retVal = False
|
||||||
|
|
||||||
self.result = False
|
self.result = False
|
||||||
return retVal
|
return retVal
|
||||||
|
|
||||||
|
@ -15,6 +15,7 @@ import logger
|
|||||||
import onionrproofs
|
import onionrproofs
|
||||||
import onionrcrypto as crypto
|
import onionrcrypto as crypto
|
||||||
from onionrutils import bytesconverter
|
from onionrutils import bytesconverter
|
||||||
|
from .blocknoncestart import BLOCK_NONCE_START_INT
|
||||||
|
|
||||||
"""
|
"""
|
||||||
This program is free software: you can redistribute it and/or modify
|
This program is free software: you can redistribute it and/or modify
|
||||||
@ -108,7 +109,7 @@ class SubprocessPOW:
|
|||||||
|
|
||||||
def do_pow(self, pipe):
|
def do_pow(self, pipe):
|
||||||
"""find partial hash colision generating nonce for a block"""
|
"""find partial hash colision generating nonce for a block"""
|
||||||
nonce = -10000000
|
nonce = BLOCK_NONCE_START_INT
|
||||||
data = self.data
|
data = self.data
|
||||||
metadata = self.metadata
|
metadata = self.metadata
|
||||||
puzzle = self.puzzle
|
puzzle = self.puzzle
|
||||||
|
Loading…
Reference in New Issue
Block a user