Onionr/src/onionrcrypto/cryptoutils/verifypow.py

39 lines
1.2 KiB
Python
Raw Normal View History

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
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:
logger.debug("Invalid token, bad proof")
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