communicator now starts tor node

This commit is contained in:
Kevin Froman 2018-02-27 03:33:26 -06:00
parent cf3af5b8c6
commit 3033de1d9e
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
2 changed files with 27 additions and 11 deletions

View File

@ -20,7 +20,7 @@ and code to operate as a daemon, getting commands from the command queue databas
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
''' '''
import sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse import sqlite3, requests, hmac, hashlib, time, sys, os, math, logger, urllib.parse
import core, onionrutils, onionrcrypto import core, onionrutils, onionrcrypto, pow, btc
class OnionrCommunicate: class OnionrCommunicate:
def __init__(self, debug, developmentMode): def __init__(self, debug, developmentMode):
@ -32,10 +32,20 @@ class OnionrCommunicate:
self._core = core.Core() self._core = core.Core()
self._utils = onionrutils.OnionrUtils(self._core) self._utils = onionrutils.OnionrUtils(self._core)
self._crypto = onionrcrypto.OnionrCrypto(self._core) self._crypto = onionrcrypto.OnionrCrypto(self._core)
logger.info('Starting Bitcoin Node... with Tor socks port:' + str(sys.argv[2]))
while True:
try:
self.bitcoin = btc.OnionrBTC(torP=int(sys.argv[2]))
except:
# ugly but needed
pass
else:
break
logger.info('Bitcoin Node started, on block: ' + self.bitcoin.node.getBlockHash(self.bitcoin.node.getLastBlockHeight()))
blockProcessTimer = 0 blockProcessTimer = 0
blockProcessAmount = 5 blockProcessAmount = 5
heartBeatTimer = 0 heartBeatTimer = 0
heartBeatRate = 100 heartBeatRate = 5
logger.debug('Communicator debugging enabled.') logger.debug('Communicator debugging enabled.')
torID = open('data/hs/hostname').read() torID = open('data/hs/hostname').read()

View File

@ -20,19 +20,24 @@
import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger
import btc import btc
class POW: class POW:
def pow(self): def pow(self, reporting=False):
startTime = math.floor(time.time()) startTime = math.floor(time.time())
self.hashing = True self.hashing = True
self.reporting = reporting
iFound = False # if current thread is the one that found the answer iFound = False # if current thread is the one that found the answer
answer = '' answer = ''
heartbeat = 200000 heartbeat = 200000
hbCount = 0 hbCount = 0
blockCheck = 300000 # How often the hasher should check if the bitcoin block is updated (slows hashing but prevents less wasted work)
blockCheckCount = 0
block = self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight())
while self.hashing: while self.hashing:
block = self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight()) if blockCheckCount == blockCheck:
if hbCount == heartbeat: if self.reporting:
logger.debug('hb') logger.debug('Refreshing Bitcoin block')
logger.debug('using bitcoin block: ' + block) block = self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight())
hbCount = 0 blockCheckCount = 0
blockCheckCount += 1
hbCount += 1 hbCount += 1
token = nacl.hash.blake2b(nacl.utils.random() + block.encode()).decode() token = nacl.hash.blake2b(nacl.utils.random() + block.encode()).decode()
if self.mainHash[0:self.difficulty] == token[0:self.difficulty]: if self.mainHash[0:self.difficulty] == token[0:self.difficulty]:
@ -40,9 +45,10 @@ class POW:
iFound = True iFound = True
break break
if iFound: if iFound:
logger.info('Found token ' + token)
endTime = math.floor(time.time()) endTime = math.floor(time.time())
logger.info('took ' + str(endTime - startTime)) if self.reporting:
logger.info('Found token ' + token)
logger.info('took ' + str(endTime - startTime))
self.result = token self.result = token
def __init__(self, difficulty, bitcoinNode): def __init__(self, difficulty, bitcoinNode):
@ -55,7 +61,7 @@ class POW:
self.puzzle = self.mainHash[0:self.difficulty] self.puzzle = self.mainHash[0:self.difficulty]
self.bitcoinNode = bitcoinNode self.bitcoinNode = bitcoinNode
logger.debug('trying to find ' + str(self.mainHash)) logger.debug('trying to find ' + str(self.mainHash))
tOne = threading.Thread(name='one', target=self.pow) tOne = threading.Thread(name='one', target=self.pow, args=(True,))
tTwo = threading.Thread(name='two', target=self.pow) tTwo = threading.Thread(name='two', target=self.pow)
tThree = threading.Thread(name='three', target=self.pow) tThree = threading.Thread(name='three', target=self.pow)
tOne.start() tOne.start()