work on proof of work

master
Kevin Froman 2018-05-05 15:07:32 -05:00
parent 908ccbe664
commit e2cc375b1a
No known key found for this signature in database
GPG Key ID: 0D414D0FE405B63B
4 changed files with 35 additions and 46 deletions

View File

@ -548,6 +548,10 @@ class OnionrCommunicate:
# deal with block metadata # deal with block metadata
blockContent = self._core.getData(i) blockContent = self._core.getData(i)
try:
blockContent = blockContent.encode()
except AttributeError:
pass
try: try:
#blockMetadata = json.loads(self._core.getData(i)).split('}')[0] + '}' #blockMetadata = json.loads(self._core.getData(i)).split('}')[0] + '}'
blockMetadata = json.loads(blockContent[:blockContent.rfind(b'}') + 1]) blockMetadata = json.loads(blockContent[:blockContent.rfind(b'}') + 1])

View File

@ -17,11 +17,11 @@
You should have received a copy of the GNU General Public License You should have received a copy of the GNU General Public License
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, 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.Cipher import AES
#from Crypto import Random #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): if sys.version_info < (3, 6):
try: try:
@ -630,13 +630,23 @@ class Core:
Inserts a block into the network 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: try:
data.decode() data.decode()
except AttributeError: except AttributeError:
data = data.encode() data = data.encode()
retData = '' retData = ''
metadata = {'type': header} metadata = {'type': header, 'pow': powToken}
sig = {} sig = {}
metadata = json.dumps(metadata) metadata = json.dumps(metadata)

View File

@ -18,7 +18,7 @@
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 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 import btc
class POW: 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) blockCheck = 300000 # How often the hasher should check if the bitcoin block is updated (slows hashing but prevents less wasted work)
blockCheckCount = 0 blockCheckCount = 0
block = '' #self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight()) block = '' #self.bitcoinNode.getBlockHash(self.bitcoinNode.getLastBlockHeight())
print('thread started') #logger.debug('thread started')
while self.hashing: while self.hashing:
''' '''
if blockCheckCount == blockCheck: if blockCheckCount == blockCheck:
@ -44,7 +44,7 @@ class POW:
blockCheckCount += 1 blockCheckCount += 1
hbCount += 1 hbCount += 1
''' '''
token = nacl.hash.blake2b(nacl.utils.random()).decode() token = nacl.hash.blake2b(nacl.utils.random() + self.data).decode()
#print(token) #print(token)
if self.puzzle == token[0:self.difficulty]: if self.puzzle == token[0:self.difficulty]:
self.hashing = False self.hashing = False
@ -59,9 +59,21 @@ class POW:
logger.info('took ' + str(endTime - startTime) + ' seconds', timestamp=True) logger.info('took ' + str(endTime - startTime) + ' seconds', timestamp=True)
self.result = token self.result = token
def __init__(self, difficulty, bitcoinNode=''): def __init__(self, data, bitcoinNode=''):
self.foundHash = False 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)) logger.debug('Computing difficulty of ' + str(self.difficulty))

View File

@ -370,43 +370,6 @@ class OnionrUtils:
else: else:
logger.warn("Bad sender id: " + signer) 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: except FileNotFoundError:
pass pass
except Exception as error: except Exception as error:
@ -452,4 +415,4 @@ class OnionrUtils:
return False return False
def token(self, size = 32): def token(self, size = 32):
return binascii.hexlify(os.urandom(size)) return binascii.hexlify(os.urandom(size))