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
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])

View File

@ -17,11 +17,11 @@
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 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)

View File

@ -18,7 +18,7 @@
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
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))

View File

@ -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))