Onionr/onionr/onionrproofs.py

86 lines
3.3 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/>.
'''
import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger
2018-02-26 02:30:43 +00:00
import btc
2018-02-22 22:55:42 +00:00
class POW:
2018-02-27 09:33:26 +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-02-27 09:33:26 +00:00
blockCheck = 300000 # How often the hasher should check if the bitcoin block is updated (slows hashing but prevents less wasted work)
blockCheckCount = 0
2018-03-04 02:28:17 +00:00
block = ''#self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight())
2018-02-22 22:55:42 +00:00
while self.hashing:
2018-02-27 09:33:26 +00:00
if blockCheckCount == blockCheck:
if self.reporting:
logger.debug('Refreshing Bitcoin block')
2018-03-04 02:28:17 +00:00
block = ''#self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight())
2018-02-27 09:33:26 +00:00
blockCheckCount = 0
blockCheckCount += 1
2018-02-22 22:55:42 +00:00
hbCount += 1
2018-02-26 02:30:43 +00:00
token = nacl.hash.blake2b(nacl.utils.random() + block.encode()).decode()
2018-02-22 22:55:42 +00:00
if self.mainHash[0:self.difficulty] == token[0:self.difficulty]:
self.hashing = False
iFound = True
break
if iFound:
endTime = math.floor(time.time())
2018-02-27 09:33:26 +00:00
if self.reporting:
logger.info('Found token ' + token)
logger.info('took ' + str(endTime - startTime))
2018-02-26 02:30:43 +00:00
self.result = token
2018-02-22 22:55:42 +00:00
2018-02-26 02:30:43 +00:00
def __init__(self, difficulty, bitcoinNode):
2018-02-22 22:55:42 +00:00
self.foundHash = False
self.difficulty = difficulty
2018-02-26 02:30:43 +00:00
logger.debug('Computing difficulty of ' + str(self.difficulty))
2018-02-22 22:55:42 +00:00
self.mainHash = nacl.hash.blake2b(nacl.utils.random()).decode()
self.puzzle = self.mainHash[0:self.difficulty]
2018-02-26 02:30:43 +00:00
self.bitcoinNode = bitcoinNode
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-02-22 22:55:42 +00:00
tTwo = threading.Thread(name='two', target=self.pow)
tThree = threading.Thread(name='three', target=self.pow)
tOne.start()
tTwo.start()
tThree.start()
return
def shutdown(self):
self.hashing = False
2018-02-26 02:30:43 +00:00
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