Onionr/onionr/onionrproofs.py

98 lines
3.4 KiB
Python
Raw Normal View History

2018-02-22 22:55:42 +00:00
'''
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/>.
'''
2018-04-23 03:49:53 +00:00
import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger, sys, base64
import core
2018-04-23 03:49:53 +00:00
2018-02-22 22:55:42 +00:00
class POW:
2018-04-23 03:49:53 +00:00
def pow(self, reporting = False):
2018-02-22 22:55:42 +00:00
startTime = math.floor(time.time())
self.hashing = True
2018-02-27 09:33:26 +00:00
self.reporting = reporting
2018-02-22 22:55:42 +00:00
iFound = False # if current thread is the one that found the answer
answer = ''
heartbeat = 200000
hbCount = 0
2018-05-05 23:32:10 +00:00
myCore = core.Core()
2018-02-22 22:55:42 +00:00
while self.hashing:
2018-05-05 22:36:03 +00:00
rand = nacl.utils.random()
2018-05-05 23:32:10 +00:00
token = nacl.hash.blake2b(rand + self.data).decode()
2018-05-02 07:31:33 +00:00
#print(token)
if self.puzzle == token[0:self.difficulty]:
2018-02-22 22:55:42 +00:00
self.hashing = False
iFound = True
break
2018-05-02 07:31:33 +00:00
else:
logger.debug('POW thread exiting, another thread found result')
2018-02-22 22:55:42 +00:00
if iFound:
endTime = math.floor(time.time())
2018-02-27 09:33:26 +00:00
if self.reporting:
2018-04-19 01:47:35 +00:00
logger.info('Found token ' + token, timestamp=True)
logger.info('rand value: ' + base64.b64encode(rand).decode())
2018-05-02 07:31:33 +00:00
logger.info('took ' + str(endTime - startTime) + ' seconds', timestamp=True)
2018-05-05 22:36:03 +00:00
self.result = (token, rand)
2018-04-19 01:47:35 +00:00
def __init__(self, data):
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
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)
2018-02-22 22:55:42 +00:00
2018-02-26 02:30:43 +00:00
logger.debug('Computing difficulty of ' + str(self.difficulty))
2018-02-22 22:55:42 +00:00
2018-05-02 07:31:33 +00:00
self.mainHash = '0000000000000000000000000000000000000000000000000000000000000000'#nacl.hash.blake2b(nacl.utils.random()).decode()
2018-02-22 22:55:42 +00:00
self.puzzle = self.mainHash[0:self.difficulty]
2018-05-02 07:31:33 +00:00
#logger.debug('trying to find ' + str(self.mainHash))
2018-02-27 09:33:26 +00:00
tOne = threading.Thread(name='one', target=self.pow, args=(True,))
2018-05-02 07:31:33 +00:00
tTwo = threading.Thread(name='two', target=self.pow, args=(True,))
tThree = threading.Thread(name='three', target=self.pow, args=(True,))
2018-02-22 22:55:42 +00:00
tOne.start()
tTwo.start()
tThree.start()
return
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-02-26 02:30:43 +00:00
try:
retVal = self.result
except AttributeError:
retVal = False
self.result = False
2018-04-19 01:47:35 +00:00
return retVal