2018-02-22 22:55:42 +00:00
|
|
|
'''
|
2018-11-11 02:10:58 +00:00
|
|
|
Onionr - P2P Anonymous Storage Network
|
2018-02-22 22:55:42 +00:00
|
|
|
|
|
|
|
Proof of work module
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
'''
|
2018-04-23 03:49:53 +00:00
|
|
|
|
2018-07-08 07:51:23 +00:00
|
|
|
import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger, sys, base64, json
|
2018-12-24 06:12:46 +00:00
|
|
|
import core, onionrutils, config
|
|
|
|
import onionrblockapi
|
|
|
|
|
|
|
|
def getDifficultyModifier(coreOrUtilsInst=None):
|
|
|
|
'''Accepts a core or utils instance returns
|
|
|
|
the difficulty modifier for block storage based
|
|
|
|
on a variety of factors, currently only disk use.
|
|
|
|
'''
|
|
|
|
classInst = coreOrUtilsInst
|
|
|
|
retData = 0
|
|
|
|
if isinstance(classInst, core.Core):
|
|
|
|
useFunc = classInst._utils.storageCounter.getPercent
|
|
|
|
elif isinstance(classInst, onionrutils.OnionrUtils):
|
|
|
|
useFunc = classInst.storageCounter.getPercent
|
|
|
|
else:
|
|
|
|
useFunc = core.Core()._utils.storageCounter.getPercent
|
|
|
|
|
|
|
|
percentUse = useFunc()
|
|
|
|
|
|
|
|
if percentUse >= 0.50:
|
|
|
|
retData += 1
|
|
|
|
elif percentUse >= 0.75:
|
2019-02-07 18:12:04 +00:00
|
|
|
retData += 2
|
2018-12-24 06:12:46 +00:00
|
|
|
elif percentUse >= 0.95:
|
2019-02-07 18:12:04 +00:00
|
|
|
retData += 3
|
2018-12-24 06:12:46 +00:00
|
|
|
|
|
|
|
return retData
|
|
|
|
|
|
|
|
def getDifficultyForNewBlock(data, ourBlock=True):
|
|
|
|
'''
|
|
|
|
Get difficulty for block. Accepts size in integer, Block instance, or str/bytes full block contents
|
|
|
|
'''
|
|
|
|
retData = 0
|
|
|
|
dataSize = 0
|
|
|
|
if isinstance(data, onionrblockapi.Block):
|
|
|
|
dataSize = len(data.getRaw().encode('utf-8'))
|
|
|
|
elif isinstance(data, str):
|
|
|
|
dataSize = len(data.encode('utf-8'))
|
2018-12-26 06:14:05 +00:00
|
|
|
elif isinstance(data, bytes):
|
|
|
|
dataSize = len(data)
|
2018-12-24 06:12:46 +00:00
|
|
|
elif isinstance(data, int):
|
|
|
|
dataSize = data
|
|
|
|
else:
|
|
|
|
raise ValueError('not Block, str, or int')
|
|
|
|
if ourBlock:
|
2019-02-16 04:08:03 +00:00
|
|
|
minDifficulty = config.get('general.minimum_send_pow', 4)
|
2018-12-24 06:12:46 +00:00
|
|
|
else:
|
2019-02-16 04:08:03 +00:00
|
|
|
minDifficulty = config.get('general.minimum_block_pow', 4)
|
2018-12-24 06:12:46 +00:00
|
|
|
|
2019-02-07 18:12:04 +00:00
|
|
|
retData = max(minDifficulty, math.floor(dataSize / 100000)) + getDifficultyModifier()
|
2019-02-17 05:20:47 +00:00
|
|
|
|
2018-12-24 06:12:46 +00:00
|
|
|
return retData
|
2018-11-11 02:10:58 +00:00
|
|
|
|
|
|
|
def getHashDifficulty(h):
|
|
|
|
'''
|
|
|
|
Return the amount of leading zeroes in a hex hash string (h)
|
|
|
|
'''
|
|
|
|
difficulty = 0
|
|
|
|
assert type(h) is str
|
|
|
|
for character in h:
|
|
|
|
if character == '0':
|
|
|
|
difficulty += 1
|
2018-12-09 17:29:39 +00:00
|
|
|
else:
|
|
|
|
break
|
2018-11-11 02:10:58 +00:00
|
|
|
return difficulty
|
|
|
|
|
|
|
|
def hashMeetsDifficulty(h):
|
|
|
|
'''
|
|
|
|
Return bool for a hash string to see if it meets pow difficulty defined in config
|
|
|
|
'''
|
|
|
|
config.reload()
|
|
|
|
hashDifficulty = getHashDifficulty(h)
|
2018-12-09 17:29:39 +00:00
|
|
|
try:
|
|
|
|
expected = int(config.get('general.minimum_block_pow'))
|
|
|
|
except TypeError:
|
|
|
|
raise ValueError('Missing general.minimum_block_pow config')
|
2018-11-11 02:10:58 +00:00
|
|
|
if hashDifficulty >= expected:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2018-04-23 03:49:53 +00:00
|
|
|
|
2018-07-09 07:02:33 +00:00
|
|
|
class DataPOW:
|
2018-07-22 00:20:28 +00:00
|
|
|
def __init__(self, data, forceDifficulty=0, threadCount = 5):
|
2018-07-09 07:02:33 +00:00
|
|
|
self.foundHash = False
|
|
|
|
self.difficulty = 0
|
|
|
|
self.data = data
|
|
|
|
self.threadCount = threadCount
|
2018-12-24 06:12:46 +00:00
|
|
|
self.rounds = 0
|
2018-11-11 02:10:58 +00:00
|
|
|
config.reload()
|
2018-07-09 07:02:33 +00:00
|
|
|
|
2018-07-22 00:20:28 +00:00
|
|
|
if forceDifficulty == 0:
|
|
|
|
dataLen = sys.getsizeof(data)
|
|
|
|
self.difficulty = math.floor(dataLen / 1000000)
|
|
|
|
if self.difficulty <= 2:
|
|
|
|
self.difficulty = 4
|
|
|
|
else:
|
|
|
|
self.difficulty = forceDifficulty
|
2018-07-09 07:02:33 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
self.data = self.data.encode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
self.data = nacl.hash.blake2b(self.data)
|
|
|
|
|
|
|
|
logger.info('Computing POW (difficulty: %s)...' % self.difficulty)
|
|
|
|
|
|
|
|
self.mainHash = '0' * 70
|
|
|
|
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
|
|
|
|
|
|
|
|
myCore = core.Core()
|
|
|
|
for i in range(max(1, threadCount)):
|
|
|
|
t = threading.Thread(name = 'thread%s' % i, target = self.pow, args = (True,myCore))
|
|
|
|
t.start()
|
|
|
|
|
|
|
|
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()
|
2018-12-24 06:12:46 +00:00
|
|
|
self.rounds += 1
|
2018-07-09 07:02:33 +00:00
|
|
|
#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)
|
2018-12-24 06:12:46 +00:00
|
|
|
logger.debug('Round count: %s' % (self.rounds,))
|
2018-07-09 07:02:33 +00:00
|
|
|
self.result = (token, rand)
|
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
self.hashing = False
|
|
|
|
self.puzzle = ''
|
|
|
|
|
|
|
|
def changeDifficulty(self, newDiff):
|
|
|
|
self.difficulty = newDiff
|
|
|
|
|
|
|
|
def getResult(self):
|
|
|
|
'''
|
|
|
|
Returns the result then sets to false, useful to automatically clear the result
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
retVal = self.result
|
|
|
|
except AttributeError:
|
|
|
|
retVal = False
|
|
|
|
|
|
|
|
self.result = False
|
|
|
|
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:
|
2019-02-10 18:43:45 +00:00
|
|
|
time.sleep(1)
|
2018-07-09 07:02:33 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
self.shutdown()
|
|
|
|
logger.warn('Got keyboard interrupt while waiting for POW result, stopping')
|
|
|
|
return result
|
|
|
|
|
2018-02-22 22:55:42 +00:00
|
|
|
class POW:
|
2018-12-24 06:12:46 +00:00
|
|
|
def __init__(self, metadata, data, threadCount = 5, forceDifficulty=0, coreInst=None):
|
2018-02-22 22:55:42 +00:00
|
|
|
self.foundHash = False
|
2018-05-05 20:07:32 +00:00
|
|
|
self.difficulty = 0
|
|
|
|
self.data = data
|
2018-07-08 07:51:23 +00:00
|
|
|
self.metadata = metadata
|
2018-06-01 04:25:28 +00:00
|
|
|
self.threadCount = threadCount
|
2018-05-05 20:07:32 +00:00
|
|
|
|
2018-12-24 06:12:46 +00:00
|
|
|
try:
|
|
|
|
assert isinstance(coreInst, core.Core)
|
|
|
|
except AssertionError:
|
|
|
|
myCore = core.Core()
|
|
|
|
else:
|
|
|
|
myCore = coreInst
|
|
|
|
|
2018-07-08 07:51:23 +00:00
|
|
|
dataLen = len(data) + len(json.dumps(metadata))
|
2018-05-05 20:07:32 +00:00
|
|
|
|
2018-12-24 06:12:46 +00:00
|
|
|
if forceDifficulty > 0:
|
|
|
|
self.difficulty = forceDifficulty
|
|
|
|
else:
|
|
|
|
# Calculate difficulty. Dumb for now, may use good algorithm in the future.
|
|
|
|
self.difficulty = getDifficultyForNewBlock(dataLen)
|
|
|
|
|
2018-05-05 20:07:32 +00:00
|
|
|
try:
|
|
|
|
self.data = self.data.encode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2018-06-01 04:25:28 +00:00
|
|
|
|
|
|
|
logger.info('Computing POW (difficulty: %s)...' % self.difficulty)
|
2018-02-22 22:55:42 +00:00
|
|
|
|
2018-07-08 07:51:23 +00:00
|
|
|
self.mainHash = '0' * 64
|
2018-06-01 04:25:28 +00:00
|
|
|
self.puzzle = self.mainHash[0:min(self.difficulty, len(self.mainHash))]
|
2018-12-24 06:12:46 +00:00
|
|
|
|
2018-06-01 04:25:28 +00:00
|
|
|
for i in range(max(1, threadCount)):
|
|
|
|
t = threading.Thread(name = 'thread%s' % i, target = self.pow, args = (True,myCore))
|
|
|
|
t.start()
|
2018-11-11 02:10:58 +00:00
|
|
|
self.myCore = myCore
|
2018-02-22 22:55:42 +00:00
|
|
|
return
|
|
|
|
|
2018-06-20 20:56:28 +00:00
|
|
|
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
|
2019-02-17 05:20:47 +00:00
|
|
|
nonce = int(binascii.hexlify(nacl.utils.random(2)), 16)
|
2018-06-20 20:56:28 +00:00
|
|
|
while self.hashing:
|
2018-07-08 07:51:23 +00:00
|
|
|
#token = nacl.hash.blake2b(rand + self.data).decode()
|
2019-02-17 05:20:47 +00:00
|
|
|
self.metadata['powRandomToken'] = nonce
|
2018-07-08 07:51:23 +00:00
|
|
|
payload = json.dumps(self.metadata).encode() + b'\n' + self.data
|
|
|
|
token = myCore._crypto.sha3Hash(payload)
|
2018-07-19 20:12:48 +00:00
|
|
|
try:
|
|
|
|
# on some versions, token is bytes
|
|
|
|
token = token.decode()
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
2018-06-20 20:56:28 +00:00
|
|
|
if self.puzzle == token[0:self.difficulty]:
|
|
|
|
self.hashing = False
|
|
|
|
iFound = True
|
2018-07-08 07:51:23 +00:00
|
|
|
self.result = payload
|
2018-06-20 20:56:28 +00:00
|
|
|
break
|
2019-02-17 05:20:47 +00:00
|
|
|
nonce += 1
|
2018-06-20 20:56:28 +00:00
|
|
|
|
|
|
|
if iFound:
|
|
|
|
endTime = math.floor(time.time())
|
|
|
|
if self.reporting:
|
|
|
|
logger.debug('Found token after %s seconds: %s' % (endTime - startTime, token), timestamp=True)
|
|
|
|
|
2018-02-22 22:55:42 +00:00
|
|
|
def shutdown(self):
|
|
|
|
self.hashing = False
|
2018-02-26 02:30:43 +00:00
|
|
|
self.puzzle = ''
|
2018-04-19 01:47:35 +00:00
|
|
|
|
2018-02-26 02:30:43 +00:00
|
|
|
def changeDifficulty(self, newDiff):
|
|
|
|
self.difficulty = newDiff
|
|
|
|
|
|
|
|
def getResult(self):
|
2018-04-23 03:49:53 +00:00
|
|
|
'''
|
|
|
|
Returns the result then sets to false, useful to automatically clear the result
|
|
|
|
'''
|
2018-06-01 04:25:28 +00:00
|
|
|
|
2018-02-26 02:30:43 +00:00
|
|
|
try:
|
|
|
|
retVal = self.result
|
|
|
|
except AttributeError:
|
|
|
|
retVal = False
|
2018-06-01 04:25:28 +00:00
|
|
|
|
2018-02-26 02:30:43 +00:00
|
|
|
self.result = False
|
2018-04-19 01:47:35 +00:00
|
|
|
return retVal
|
2018-06-20 20:56:28 +00:00
|
|
|
|
|
|
|
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:
|
2019-02-10 18:43:45 +00:00
|
|
|
time.sleep(1)
|
2018-06-20 20:56:28 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
self.shutdown()
|
|
|
|
logger.warn('Got keyboard interrupt while waiting for POW result, stopping')
|
|
|
|
return result
|