From cf3af5b8c6630bf1da0c98f5d67002e86b7bb161 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sun, 25 Feb 2018 20:30:43 -0600 Subject: [PATCH] work on pow --- onionr/bitpeer | 2 +- onionr/btc.py | 6 ++++++ onionr/core.py | 2 +- onionr/pow.py | 28 +++++++++++++++++++++++----- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/onionr/bitpeer b/onionr/bitpeer index d179f625..90d5edad 160000 --- a/onionr/bitpeer +++ b/onionr/bitpeer @@ -1 +1 @@ -Subproject commit d179f625b3a1bf1b6fc544e65a81c103ab01ec7c +Subproject commit 90d5edad0c017cec1a3b190d9b0cd08321840ed8 diff --git a/onionr/btc.py b/onionr/btc.py index 47ad0acb..bf4549f2 100644 --- a/onionr/btc.py +++ b/onionr/btc.py @@ -36,3 +36,9 @@ class OnionrBTC: self.node.connect () self.node.loop () +if __name__ == "__main__": + torPort = int(sys.argv[1]) + bitcoin = OnionrBTC(torPort) + while True: + print(bitcoin.node.getBlockHash(bitcoin.node.getLastBlockHeight())) # Using print on purpose, do not change to logger + time.sleep(5) \ No newline at end of file diff --git a/onionr/core.py b/onionr/core.py index c1ff0457..e070218a 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -22,7 +22,7 @@ import sqlite3, os, sys, time, math, base64, tarfile, getpass, simplecrypt, hash #from Crypto import Random import netcontroller -import onionrutils, onionrcrypto +import onionrutils, onionrcrypto, btc if sys.version_info < (3, 6): try: diff --git a/onionr/pow.py b/onionr/pow.py index 7398efb0..3caefbd9 100644 --- a/onionr/pow.py +++ b/onionr/pow.py @@ -18,6 +18,7 @@ along with this program. If not, see . ''' import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger +import btc class POW: def pow(self): startTime = math.floor(time.time()) @@ -27,11 +28,13 @@ class POW: heartbeat = 200000 hbCount = 0 while self.hashing: + block = self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight()) if hbCount == heartbeat: - logger.info('hb') + logger.debug('hb') + logger.debug('using bitcoin block: ' + block) hbCount = 0 hbCount += 1 - token = nacl.hash.blake2b(nacl.utils.random()).decode() + token = nacl.hash.blake2b(nacl.utils.random() + block.encode()).decode() if self.mainHash[0:self.difficulty] == token[0:self.difficulty]: self.hashing = False iFound = True @@ -40,16 +43,18 @@ class POW: logger.info('Found token ' + token) endTime = math.floor(time.time()) logger.info('took ' + str(endTime - startTime)) + self.result = token - def __init__(self, difficulty): + def __init__(self, difficulty, bitcoinNode): self.foundHash = False self.difficulty = difficulty - logger.info('Computing difficulty of ' + str(self.difficulty)) + logger.debug('Computing difficulty of ' + str(self.difficulty)) self.mainHash = nacl.hash.blake2b(nacl.utils.random()).decode() self.puzzle = self.mainHash[0:self.difficulty] - logger.info('trying to find ' + str(self.mainHash)) + self.bitcoinNode = bitcoinNode + logger.debug('trying to find ' + str(self.mainHash)) tOne = threading.Thread(name='one', target=self.pow) tTwo = threading.Thread(name='two', target=self.pow) tThree = threading.Thread(name='three', target=self.pow) @@ -60,3 +65,16 @@ class POW: 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 \ No newline at end of file