From e2cc375b1ab5edcb61538afb90c17adbe2deee18 Mon Sep 17 00:00:00 2001 From: Kevin Froman Date: Sat, 5 May 2018 15:07:32 -0500 Subject: [PATCH] work on proof of work --- onionr/communicator.py | 4 ++++ onionr/core.py | 16 +++++++++++++--- onionr/onionrproofs.py | 22 +++++++++++++++++----- onionr/onionrutils.py | 39 +-------------------------------------- 4 files changed, 35 insertions(+), 46 deletions(-) diff --git a/onionr/communicator.py b/onionr/communicator.py index 0ef36b24..ef63bca3 100755 --- a/onionr/communicator.py +++ b/onionr/communicator.py @@ -548,6 +548,10 @@ class OnionrCommunicate: # deal with block metadata blockContent = self._core.getData(i) + try: + blockContent = blockContent.encode() + except AttributeError: + pass try: #blockMetadata = json.loads(self._core.getData(i)).split('}')[0] + '}' blockMetadata = json.loads(blockContent[:blockContent.rfind(b'}') + 1]) diff --git a/onionr/core.py b/onionr/core.py index 8ebb432d..dae25bc3 100644 --- a/onionr/core.py +++ b/onionr/core.py @@ -17,11 +17,11 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . ''' -import sqlite3, os, sys, time, math, base64, tarfile, getpass, simplecrypt, hashlib, nacl, logger, json, netcontroller +import sqlite3, os, sys, time, math, base64, tarfile, getpass, simplecrypt, hashlib, nacl, logger, json, netcontroller, math #from Crypto.Cipher import AES #from Crypto import Random -import onionrutils, onionrcrypto, btc, onionrevents as events +import onionrutils, onionrcrypto, onionrproofs, btc, onionrevents as events if sys.version_info < (3, 6): try: @@ -630,13 +630,23 @@ class Core: Inserts a block into the network ''' + powProof = onionrproofs.POW(data) + powToken = '' + # wait for proof to complete + while True: + powToken = powProof.getResult() + if powToken != False: + break + time.sleep(0.3) + + try: data.decode() except AttributeError: data = data.encode() retData = '' - metadata = {'type': header} + metadata = {'type': header, 'pow': powToken} sig = {} metadata = json.dumps(metadata) diff --git a/onionr/onionrproofs.py b/onionr/onionrproofs.py index 0eb860e2..02fee043 100644 --- a/onionr/onionrproofs.py +++ b/onionr/onionrproofs.py @@ -18,7 +18,7 @@ along with this program. If not, see . ''' -import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger +import nacl.encoding, nacl.hash, nacl.utils, time, math, threading, binascii, logger, sys import btc class POW: @@ -33,7 +33,7 @@ class POW: 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()) - print('thread started') + #logger.debug('thread started') while self.hashing: ''' if blockCheckCount == blockCheck: @@ -44,7 +44,7 @@ class POW: blockCheckCount += 1 hbCount += 1 ''' - token = nacl.hash.blake2b(nacl.utils.random()).decode() + token = nacl.hash.blake2b(nacl.utils.random() + self.data).decode() #print(token) if self.puzzle == token[0:self.difficulty]: self.hashing = False @@ -59,9 +59,21 @@ class POW: logger.info('took ' + str(endTime - startTime) + ' seconds', timestamp=True) self.result = token - def __init__(self, difficulty, bitcoinNode=''): + def __init__(self, data, bitcoinNode=''): self.foundHash = False - self.difficulty = difficulty + 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) logger.debug('Computing difficulty of ' + str(self.difficulty)) diff --git a/onionr/onionrutils.py b/onionr/onionrutils.py index 0ef3925d..24e549a3 100644 --- a/onionr/onionrutils.py +++ b/onionr/onionrutils.py @@ -370,43 +370,6 @@ class OnionrUtils: else: logger.warn("Bad sender id: " + signer) - ''' - data = potentialMessage.read().split('}') - message = data[1] - sigResult = '' - signer = '' - - try: - metadata = json.loads(data[0] + '}') - except json.decoder.JSONDecodeError: - metadata = {} - - try: - message = self._core._crypto.pubKeyDecrypt(message, encodedData=True, anonymous=True) - except nacl.exceptions.CryptoError as e: - pass - #logger.error('Unable to decrypt ' + i, error=e) - else: - try: - message = json.loads(message.decode()) - message['msg'] - message['id'] - message['sig'] - except json.decoder.JSONDecodeError: - logger.error('Could not decode PM JSON') - except KeyError: - logger.error('PM is missing JSON keys') - else: - if self.validatePubKey(message['id']): - sigResult = self._core._crypto.edVerify(message['msg'], message['id'], message['sig'], encodedData=True) - logger.info('-----------------------------------') - logger.info('Recieved message: ' + message['msg']) - if sigResult: - logger.info('Valid signature by ' + message['id']) - else: - logger.warn('Invalid signature by ' + message['id']) - ''' - except FileNotFoundError: pass except Exception as error: @@ -452,4 +415,4 @@ class OnionrUtils: return False def token(self, size = 32): - return binascii.hexlify(os.urandom(size)) + return binascii.hexlify(os.urandom(size)) \ No newline at end of file