* Pretty much done with new POW format
This commit is contained in:
parent
31236eea84
commit
f027202ac9
@ -168,7 +168,7 @@ class OnionrCommunicatorDaemon:
|
|||||||
metadata = metas[0]
|
metadata = metas[0]
|
||||||
meta = metas[1]
|
meta = metas[1]
|
||||||
if self._core._utils.validateMetadata(metadata): # check if metadata is valid
|
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.')
|
logger.info('Block passed proof, saving.')
|
||||||
self._core.setData(content)
|
self._core.setData(content)
|
||||||
self._core.addToBlockDB(blockHash, dataSaved=True)
|
self._core.addToBlockDB(blockHash, dataSaved=True)
|
||||||
|
@ -733,7 +733,9 @@ class Core:
|
|||||||
metadata['signer'] = signer
|
metadata['signer'] = signer
|
||||||
metadata['time'] = str(self._utils.getEpoch())
|
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)
|
retData = self.setData(payload)
|
||||||
self.addToBlockDB(retData, selfInsert=True, dataSaved=True)
|
self.addToBlockDB(retData, selfInsert=True, dataSaved=True)
|
||||||
|
@ -249,31 +249,27 @@ class OnionrCrypto:
|
|||||||
pass
|
pass
|
||||||
return nacl.hash.blake2b(data)
|
return nacl.hash.blake2b(data)
|
||||||
|
|
||||||
def verifyPow(self, blockContent, metadata):
|
def verifyPow(self, blockContent):
|
||||||
'''
|
'''
|
||||||
Verifies the proof of work associated with a block
|
Verifies the proof of work associated with a block
|
||||||
'''
|
'''
|
||||||
retData = False
|
retData = False
|
||||||
|
|
||||||
if not 'powRandomToken' in metadata:
|
|
||||||
logger.warn('No powRandomToken')
|
|
||||||
return False
|
|
||||||
|
|
||||||
dataLen = len(blockContent)
|
dataLen = len(blockContent)
|
||||||
|
|
||||||
expectedHash = self.blake2bHash(base64.b64decode(metadata['powRandomToken']) + self.blake2bHash(blockContent.encode()))
|
|
||||||
difficulty = 0
|
|
||||||
try:
|
try:
|
||||||
expectedHash = expectedHash.decode()
|
blockContent = blockContent.encode()
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
blockHash = self.sha3Hash(blockContent)
|
||||||
|
|
||||||
difficulty = math.floor(dataLen / 1000000)
|
difficulty = math.floor(dataLen / 1000000)
|
||||||
|
|
||||||
mainHash = '0000000000000000000000000000000000000000000000000000000000000000'#nacl.hash.blake2b(nacl.utils.random()).decode()
|
mainHash = '0000000000000000000000000000000000000000000000000000000000000000'#nacl.hash.blake2b(nacl.utils.random()).decode()
|
||||||
puzzle = mainHash[:difficulty]
|
puzzle = mainHash[:difficulty]
|
||||||
|
|
||||||
if metadata['powRandomToken'][:difficulty] == puzzle:
|
if blockHash[:difficulty] == puzzle:
|
||||||
# logger.debug('Validated block pow')
|
# logger.debug('Validated block pow')
|
||||||
retData = True
|
retData = True
|
||||||
else:
|
else:
|
||||||
|
@ -18,17 +18,18 @@
|
|||||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
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
|
import core
|
||||||
|
|
||||||
class POW:
|
class POW:
|
||||||
def __init__(self, data, threadCount = 5):
|
def __init__(self, metadata, data, threadCount = 5):
|
||||||
self.foundHash = False
|
self.foundHash = False
|
||||||
self.difficulty = 0
|
self.difficulty = 0
|
||||||
self.data = data
|
self.data = data
|
||||||
|
self.metadata = metadata
|
||||||
self.threadCount = threadCount
|
self.threadCount = threadCount
|
||||||
|
|
||||||
dataLen = sys.getsizeof(data)
|
dataLen = len(data) + len(json.dumps(metadata))
|
||||||
self.difficulty = math.floor(dataLen / 1000000)
|
self.difficulty = math.floor(dataLen / 1000000)
|
||||||
if self.difficulty <= 2:
|
if self.difficulty <= 2:
|
||||||
self.difficulty = 4
|
self.difficulty = 4
|
||||||
@ -38,11 +39,9 @@ class POW:
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
self.data = nacl.hash.blake2b(self.data)
|
|
||||||
|
|
||||||
logger.info('Computing POW (difficulty: %s)...' % self.difficulty)
|
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))]
|
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
|
||||||
|
|
||||||
myCore = core.Core()
|
myCore = core.Core()
|
||||||
@ -63,11 +62,15 @@ class POW:
|
|||||||
|
|
||||||
while self.hashing:
|
while self.hashing:
|
||||||
rand = nacl.utils.random()
|
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)
|
#print(token)
|
||||||
if self.puzzle == token[0:self.difficulty]:
|
if self.puzzle == token[0:self.difficulty]:
|
||||||
self.hashing = False
|
self.hashing = False
|
||||||
iFound = True
|
iFound = True
|
||||||
|
self.result = payload
|
||||||
break
|
break
|
||||||
|
|
||||||
if iFound:
|
if iFound:
|
||||||
@ -75,7 +78,6 @@ class POW:
|
|||||||
if self.reporting:
|
if self.reporting:
|
||||||
logger.debug('Found token after %s seconds: %s' % (endTime - startTime, token), timestamp=True)
|
logger.debug('Found token after %s seconds: %s' % (endTime - startTime, token), timestamp=True)
|
||||||
logger.debug('Random value was: %s' % base64.b64encode(rand).decode())
|
logger.debug('Random value was: %s' % base64.b64encode(rand).decode())
|
||||||
self.result = (token, rand)
|
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
self.hashing = False
|
self.hashing = False
|
||||||
|
Loading…
Reference in New Issue
Block a user