8846dcc2c6
- Completes support for repositories - `./RUN-LINUX.sh create-repository [plugins...]` - `./RUN-LINUX.sh add-repository <block hash>` - `./RUN-LINUX.sh remove-repository <block hash>` - Fixes several misc bugs - Refactors code - Some messy code was rewritten - Variables renamed - Migrated old block api (insertBlock) to new Block API (onionrblockapi) - Kept to standards - Made code more reusable in `onionrproofs.py` - Improves logging messages - Added error output for some features missing it - Capitalized sentences - Added punctuation where it is missing - Switched `logger.info` and `logger.debug` in a few places, where it is logical - Removed or added timestamps depending on the circumstance - Added a few misc features - Added command aliases for `add-file` and `import-blocks` - Improved statistics menu - Displays `Known Block Count` - Calculates and displays `Percent Blocks Signed`
99 lines
3.1 KiB
Python
99 lines
3.1 KiB
Python
'''
|
|
Onionr - P2P Microblogging Platform & Social network
|
|
|
|
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/>.
|
|
'''
|
|
|
|
import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger, sys, base64
|
|
import core
|
|
|
|
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):
|
|
self.foundHash = False
|
|
self.difficulty = 0
|
|
self.data = data
|
|
self.threadCount = threadCount
|
|
|
|
dataLen = sys.getsizeof(data)
|
|
self.difficulty = math.floor(dataLen / 1000000)
|
|
if self.difficulty <= 2:
|
|
self.difficulty = 4
|
|
|
|
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 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
|