work on new blocks and encryption
This commit is contained in:
parent
b3cbdbaceb
commit
0087e04f57
@ -696,6 +696,7 @@ class Core:
|
|||||||
if len(jsonMeta) > 1000:
|
if len(jsonMeta) > 1000:
|
||||||
raise onionrexceptions.InvalidMetadata('meta in json encoded form must not exceed 1000 bytes')
|
raise onionrexceptions.InvalidMetadata('meta in json encoded form must not exceed 1000 bytes')
|
||||||
|
|
||||||
|
# encrypt block metadata/sig/content
|
||||||
if encryptType == 'sym':
|
if encryptType == 'sym':
|
||||||
if len(symKey) < self.requirements.passwordLength:
|
if len(symKey) < self.requirements.passwordLength:
|
||||||
raise onionrexceptions.SecurityError('Weak encryption key')
|
raise onionrexceptions.SecurityError('Weak encryption key')
|
||||||
@ -711,19 +712,11 @@ class Core:
|
|||||||
else:
|
else:
|
||||||
raise onionrexceptions.InvalidPubkey(asymPeer + ' is not a valid base32 encoded ed25519 key')
|
raise onionrexceptions.InvalidPubkey(asymPeer + ' is not a valid base32 encoded ed25519 key')
|
||||||
|
|
||||||
metadata['meta'] = jsonMeta
|
|
||||||
metadata['sig'] = signature
|
|
||||||
metadata['signer'] = signer
|
|
||||||
|
|
||||||
powProof = onionrproofs.POW(data)
|
powProof = onionrproofs.POW(data)
|
||||||
powToken = ''
|
|
||||||
# wait for proof to complete
|
# wait for proof to complete
|
||||||
try:
|
powToken = powProof.waitForResult()
|
||||||
while True:
|
|
||||||
powToken = powProof.getResult()
|
|
||||||
if powToken == False:
|
|
||||||
time.sleep(0.3)
|
|
||||||
continue
|
|
||||||
powToken = base64.b64encode(powToken[1])
|
powToken = base64.b64encode(powToken[1])
|
||||||
try:
|
try:
|
||||||
powToken = powToken.decode()
|
powToken = powToken.decode()
|
||||||
@ -731,11 +724,13 @@ class Core:
|
|||||||
pass
|
pass
|
||||||
finally:
|
finally:
|
||||||
break
|
break
|
||||||
except KeyboardInterrupt:
|
|
||||||
logger.warn("Got keyboard interrupt while working on inserting block, stopping.")
|
|
||||||
powProof.shutdown()
|
|
||||||
return ''
|
|
||||||
|
|
||||||
|
# compile metadata
|
||||||
|
metadata['meta'] = jsonMeta
|
||||||
|
metadata['sig'] = signature
|
||||||
|
metadata['signer'] = signer
|
||||||
|
metadata['powRandomToken'] = powToken
|
||||||
|
metadata['time'] = str(self._utils.getEpoch())
|
||||||
|
|
||||||
return retData
|
return retData
|
||||||
|
|
||||||
|
@ -22,31 +22,6 @@ import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, lo
|
|||||||
import core
|
import core
|
||||||
|
|
||||||
class POW:
|
class POW:
|
||||||
def pow(self, reporting = False, myCore = None):
|
|
||||||
startTime = math.floor(time.time())
|
|
||||||
self.hashing = True
|
|
||||||
self.reporting = reporting
|
|
||||||
iFound = False # if current thread is the one that found the answer
|
|
||||||
answer = ''
|
|
||||||
heartbeat = 200000
|
|
||||||
hbCount = 0
|
|
||||||
|
|
||||||
while self.hashing:
|
|
||||||
rand = nacl.utils.random()
|
|
||||||
token = nacl.hash.blake2b(rand + self.data).decode()
|
|
||||||
#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('Random value was: %s' % base64.b64encode(rand).decode())
|
|
||||||
self.result = (token, rand)
|
|
||||||
|
|
||||||
def __init__(self, data, threadCount = 5):
|
def __init__(self, data, threadCount = 5):
|
||||||
self.foundHash = False
|
self.foundHash = False
|
||||||
self.difficulty = 0
|
self.difficulty = 0
|
||||||
@ -77,6 +52,31 @@ class POW:
|
|||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
|
def pow(self, reporting = False, myCore = None):
|
||||||
|
startTime = math.floor(time.time())
|
||||||
|
self.hashing = True
|
||||||
|
self.reporting = reporting
|
||||||
|
iFound = False # if current thread is the one that found the answer
|
||||||
|
answer = ''
|
||||||
|
heartbeat = 200000
|
||||||
|
hbCount = 0
|
||||||
|
|
||||||
|
while self.hashing:
|
||||||
|
rand = nacl.utils.random()
|
||||||
|
token = nacl.hash.blake2b(rand + self.data).decode()
|
||||||
|
#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('Random value was: %s' % base64.b64encode(rand).decode())
|
||||||
|
self.result = (token, rand)
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
self.hashing = False
|
self.hashing = False
|
||||||
self.puzzle = ''
|
self.puzzle = ''
|
||||||
@ -96,3 +96,20 @@ class POW:
|
|||||||
|
|
||||||
self.result = False
|
self.result = False
|
||||||
return retVal
|
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(2)
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
self.shutdown()
|
||||||
|
logger.warn('Got keyboard interrupt while waiting for POW result, stopping')
|
||||||
|
return result
|
Loading…
Reference in New Issue
Block a user