* Pretty much done with new POW format

This commit is contained in:
Kevin Froman 2018-07-08 02:51:23 -05:00
parent 31236eea84
commit f027202ac9
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
4 changed files with 19 additions and 19 deletions

View File

@ -168,7 +168,7 @@ class OnionrCommunicatorDaemon:
metadata = metas[0]
meta = metas[1]
if self._core._utils.validateMetadata(metadata): # check if metadata is valid
if self._core._crypto.verifyPow(metas[2], metadata): # check if POW is enough/correct
if self._core._crypto.verifyPow(content): # check if POW is enough/correct
logger.info('Block passed proof, saving.')
self._core.setData(content)
self._core.addToBlockDB(blockHash, dataSaved=True)

View File

@ -733,7 +733,9 @@ class Core:
metadata['signer'] = signer
metadata['time'] = str(self._utils.getEpoch())
payload = onionrproofs.POW(metadata, data)
# send block data (and metadata) to POW module to get tokenized block data
proof = onionrproofs.POW(metadata, data)
payload = proof.waitForResult()
retData = self.setData(payload)
self.addToBlockDB(retData, selfInsert=True, dataSaved=True)

View File

@ -249,31 +249,27 @@ class OnionrCrypto:
pass
return nacl.hash.blake2b(data)
def verifyPow(self, blockContent, metadata):
def verifyPow(self, blockContent):
'''
Verifies the proof of work associated with a block
'''
retData = False
if not 'powRandomToken' in metadata:
logger.warn('No powRandomToken')
return False
dataLen = len(blockContent)
expectedHash = self.blake2bHash(base64.b64decode(metadata['powRandomToken']) + self.blake2bHash(blockContent.encode()))
difficulty = 0
try:
expectedHash = expectedHash.decode()
blockContent = blockContent.encode()
except AttributeError:
pass
blockHash = self.sha3Hash(blockContent)
difficulty = math.floor(dataLen / 1000000)
mainHash = '0000000000000000000000000000000000000000000000000000000000000000'#nacl.hash.blake2b(nacl.utils.random()).decode()
puzzle = mainHash[:difficulty]
if metadata['powRandomToken'][:difficulty] == puzzle:
if blockHash[:difficulty] == puzzle:
# logger.debug('Validated block pow')
retData = True
else:

View File

@ -18,17 +18,18 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger, sys, base64
import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger, sys, base64, json
import core
class POW:
def __init__(self, data, threadCount = 5):
def __init__(self, metadata, data, threadCount = 5):
self.foundHash = False
self.difficulty = 0
self.data = data
self.metadata = metadata
self.threadCount = threadCount
dataLen = sys.getsizeof(data)
dataLen = len(data) + len(json.dumps(metadata))
self.difficulty = math.floor(dataLen / 1000000)
if self.difficulty <= 2:
self.difficulty = 4
@ -38,11 +39,9 @@ class POW:
except AttributeError:
pass
self.data = nacl.hash.blake2b(self.data)
logger.info('Computing POW (difficulty: %s)...' % self.difficulty)
self.mainHash = '0' * 70
self.mainHash = '0' * 64
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
myCore = core.Core()
@ -63,11 +62,15 @@ class POW:
while self.hashing:
rand = nacl.utils.random()
token = nacl.hash.blake2b(rand + self.data).decode()
#token = nacl.hash.blake2b(rand + self.data).decode()
self.metadata['powRandomToken'] = base64.b64encode(rand).decode()
payload = json.dumps(self.metadata).encode() + b'\n' + self.data
token = myCore._crypto.sha3Hash(payload)
#print(token)
if self.puzzle == token[0:self.difficulty]:
self.hashing = False
iFound = True
self.result = payload
break
if iFound:
@ -75,7 +78,6 @@ class POW:
if self.reporting:
logger.debug('Found token after %s seconds: %s' % (endTime - startTime, token), timestamp=True)
logger.debug('Random value was: %s' % base64.b64encode(rand).decode())
self.result = (token, rand)
def shutdown(self):
self.hashing = False