2019-07-22 05:24:42 +00:00
|
|
|
from .. import hashers
|
|
|
|
import config, onionrproofs, logger
|
2019-09-12 06:28:20 +00:00
|
|
|
import onionrexceptions
|
2019-07-22 05:24:42 +00:00
|
|
|
def verify_POW(blockContent):
|
|
|
|
'''
|
|
|
|
Verifies the proof of work associated with a block
|
|
|
|
'''
|
|
|
|
retData = False
|
|
|
|
|
|
|
|
dataLen = len(blockContent)
|
|
|
|
|
|
|
|
try:
|
|
|
|
blockContent = blockContent.encode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
blockHash = hashers.sha3_hash(blockContent)
|
|
|
|
try:
|
|
|
|
blockHash = blockHash.decode() # bytes on some versions for some reason
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
2019-08-13 21:54:03 +00:00
|
|
|
difficulty = onionrproofs.getDifficultyForNewBlock(blockContent)
|
2019-07-22 05:24:42 +00:00
|
|
|
|
|
|
|
if difficulty < int(config.get('general.minimum_block_pow')):
|
|
|
|
difficulty = int(config.get('general.minimum_block_pow'))
|
|
|
|
mainHash = '0000000000000000000000000000000000000000000000000000000000000000'#nacl.hash.blake2b(nacl.utils.random()).decode()
|
|
|
|
puzzle = mainHash[:difficulty]
|
|
|
|
|
|
|
|
if blockHash[:difficulty] == puzzle:
|
|
|
|
# logger.debug('Validated block pow')
|
|
|
|
retData = True
|
|
|
|
else:
|
2020-06-27 23:05:39 +00:00
|
|
|
logger.debug(f"Invalid token, bad proof for {blockHash} {puzzle}")
|
2019-09-12 06:28:20 +00:00
|
|
|
raise onionrexceptions.InvalidProof('Proof for %s needs to be %s' % (blockHash, puzzle))
|
2019-07-22 05:24:42 +00:00
|
|
|
|
|
|
|
return retData
|
|
|
|
|