Moved proof of work to child processes.

This commit is contained in:
Arceliar 2013-05-29 22:01:12 +02:00
parent fe32ed5d4c
commit 128c6916af
2 changed files with 37 additions and 36 deletions

View File

@ -47,6 +47,7 @@ from SimpleXMLRPCServer import *
import json import json
from subprocess import call #used when the API must execute an outside program from subprocess import call #used when the API must execute an outside program
import singleton import singleton
import proofofwork
#For each stream to which we connect, several outgoingSynSender threads will exist and will collectively create 8 connections with peers. #For each stream to which we connect, several outgoingSynSender threads will exist and will collectively create 8 connections with peers.
class outgoingSynSender(threading.Thread): class outgoingSynSender(threading.Thread):
@ -2744,16 +2745,12 @@ class singleWorker(threading.Thread):
payload += pubEncryptionKey[1:] payload += pubEncryptionKey[1:]
#Do the POW for this pubkey message #Do the POW for this pubkey message
nonce = 0
trialValue = 99999999999999999999
target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte)
print '(For pubkey message) Doing proof of work...' print '(For pubkey message) Doing proof of work...'
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
while trialValue > target: trialValue, nonce = proofofwork.run(target, initialHash)
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
print '(For pubkey message) Found proof of work', trialValue, 'Nonce:', nonce print '(For pubkey message) Found proof of work', trialValue, 'Nonce:', nonce
trialValue, nonce = proofofwork.run(target, initialHash)
payload = pack('>Q',nonce) + payload payload = pack('>Q',nonce) + payload
"""t = (hash,payload,embeddedTime,'no') """t = (hash,payload,embeddedTime,'no')
shared.sqlLock.acquire() shared.sqlLock.acquire()
@ -2810,14 +2807,10 @@ class singleWorker(threading.Thread):
payload += signature payload += signature
#Do the POW for this pubkey message #Do the POW for this pubkey message
nonce = 0
trialValue = 99999999999999999999
target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte)
print '(For pubkey message) Doing proof of work...' print '(For pubkey message) Doing proof of work...'
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
while trialValue > target: trialValue, nonce = proofofwork.run(target, initialHash)
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
print '(For pubkey message) Found proof of work', trialValue, 'Nonce:', nonce print '(For pubkey message) Found proof of work', trialValue, 'Nonce:', nonce
payload = pack('>Q',nonce) + payload payload = pack('>Q',nonce) + payload
@ -2885,16 +2878,12 @@ class singleWorker(threading.Thread):
payload += encodeVarint(len(signature)) payload += encodeVarint(len(signature))
payload += signature payload += signature
nonce = 0
trialValue = 99999999999999999999
target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte)
print '(For broadcast message) Doing proof of work...' print '(For broadcast message) Doing proof of work...'
#self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...')
shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...'))) shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...')))
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
while trialValue > target: trialValue, nonce = proofofwork.run(target, initialHash)
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
print '(For broadcast message) Found proof of work', trialValue, 'Nonce:', nonce print '(For broadcast message) Found proof of work', trialValue, 'Nonce:', nonce
payload = pack('>Q',nonce) + payload payload = pack('>Q',nonce) + payload
@ -2956,16 +2945,12 @@ class singleWorker(threading.Thread):
pubEncryptionKey = pointMult(privEncryptionKey) pubEncryptionKey = pointMult(privEncryptionKey)
payload += highlevelcrypto.encrypt(dataToEncrypt,pubEncryptionKey.encode('hex')) payload += highlevelcrypto.encrypt(dataToEncrypt,pubEncryptionKey.encode('hex'))
nonce = 0
trialValue = 99999999999999999999
target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte)
print '(For broadcast message) Doing proof of work...' print '(For broadcast message) Doing proof of work...'
#self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...') #self.emit(SIGNAL("updateSentItemStatusByAckdata(PyQt_PyObject,PyQt_PyObject)"),ackdata,'Doing work necessary to send broadcast...')
shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...'))) shared.UISignalQueue.put(('updateSentItemStatusByAckdata',(ackdata,'Doing work necessary to send broadcast...')))
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
while trialValue > target: trialValue, nonce = proofofwork.run(target, initialHash)
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
print '(For broadcast message) Found proof of work', trialValue, 'Nonce:', nonce print '(For broadcast message) Found proof of work', trialValue, 'Nonce:', nonce
payload = pack('>Q',nonce) + payload payload = pack('>Q',nonce) + payload
@ -3139,8 +3124,6 @@ class singleWorker(threading.Thread):
requiredPayloadLengthExtraBytes = shared.networkDefaultPayloadLengthExtraBytes requiredPayloadLengthExtraBytes = shared.networkDefaultPayloadLengthExtraBytes
encrypted = highlevelcrypto.encrypt(payload,"04"+pubEncryptionKeyBase256.encode('hex')) encrypted = highlevelcrypto.encrypt(payload,"04"+pubEncryptionKeyBase256.encode('hex'))
nonce = 0
trialValue = 99999999999999999999
#We are now dropping the unencrypted data in payload since it has already been encrypted and replacing it with the encrypted payload that we will send out. #We are now dropping the unencrypted data in payload since it has already been encrypted and replacing it with the encrypted payload that we will send out.
payload = embeddedTime + encodeVarint(toStreamNumber) + encrypted payload = embeddedTime + encodeVarint(toStreamNumber) + encrypted
target = 2**64 / ((len(payload)+requiredPayloadLengthExtraBytes+8) * requiredAverageProofOfWorkNonceTrialsPerByte) target = 2**64 / ((len(payload)+requiredPayloadLengthExtraBytes+8) * requiredAverageProofOfWorkNonceTrialsPerByte)
@ -3149,9 +3132,7 @@ class singleWorker(threading.Thread):
shared.printLock.release() shared.printLock.release()
powStartTime = time.time() powStartTime = time.time()
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
while trialValue > target: trialValue, nonce = proofofwork.run(target, initialHash)
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
print '(For msg message) Found proof of work', trialValue, 'Nonce:', nonce print '(For msg message) Found proof of work', trialValue, 'Nonce:', nonce
try: try:
print 'POW took', int(time.time()-powStartTime), 'seconds.', nonce/(time.time()-powStartTime), 'nonce trials per second.' print 'POW took', int(time.time()-powStartTime), 'seconds.', nonce/(time.time()-powStartTime), 'nonce trials per second.'
@ -3190,8 +3171,6 @@ class singleWorker(threading.Thread):
shared.printLock.acquire() shared.printLock.acquire()
print 'making request for pubkey with ripe:', ripe.encode('hex') print 'making request for pubkey with ripe:', ripe.encode('hex')
shared.printLock.release() shared.printLock.release()
nonce = 0
trialValue = 99999999999999999999
#print 'trial value', trialValue #print 'trial value', trialValue
statusbar = 'Doing the computations necessary to request the recipient\'s public key.' statusbar = 'Doing the computations necessary to request the recipient\'s public key.'
#self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar) #self.emit(SIGNAL("updateStatusBar(PyQt_PyObject)"),statusbar)
@ -3201,9 +3180,7 @@ class singleWorker(threading.Thread):
print 'Doing proof-of-work necessary to send getpubkey message.' print 'Doing proof-of-work necessary to send getpubkey message.'
target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte)
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
while trialValue > target: trialValue, nonce = proofofwork.run(target, initialHash)
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
shared.printLock.acquire() shared.printLock.acquire()
print 'Found proof of work', trialValue, 'Nonce:', nonce print 'Found proof of work', trialValue, 'Nonce:', nonce
shared.printLock.release() shared.printLock.release()
@ -3221,8 +3198,6 @@ class singleWorker(threading.Thread):
shared.UISignalQueue.put(('updateSentItemStatusByHash',(ripe,'Sending public key request. Waiting for reply. Requested at ' + unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8')))) shared.UISignalQueue.put(('updateSentItemStatusByHash',(ripe,'Sending public key request. Waiting for reply. Requested at ' + unicode(strftime(shared.config.get('bitmessagesettings', 'timeformat'),localtime(int(time.time()))),'utf-8'))))
def generateFullAckMessage(self,ackdata,toStreamNumber,embeddedTime): def generateFullAckMessage(self,ackdata,toStreamNumber,embeddedTime):
nonce = 0
trialValue = 99999999999999999999
payload = embeddedTime + encodeVarint(toStreamNumber) + ackdata payload = embeddedTime + encodeVarint(toStreamNumber) + ackdata
target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte) target = 2**64 / ((len(payload)+shared.networkDefaultPayloadLengthExtraBytes+8) * shared.networkDefaultProofOfWorkNonceTrialsPerByte)
shared.printLock.acquire() shared.printLock.acquire()
@ -3230,9 +3205,7 @@ class singleWorker(threading.Thread):
shared.printLock.release() shared.printLock.release()
powStartTime = time.time() powStartTime = time.time()
initialHash = hashlib.sha512(payload).digest() initialHash = hashlib.sha512(payload).digest()
while trialValue > target: trialValue, nonce = proofofwork.run(target, initialHash)
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
shared.printLock.acquire() shared.printLock.acquire()
print '(For ack message) Found proof of work', trialValue, 'Nonce:', nonce print '(For ack message) Found proof of work', trialValue, 'Nonce:', nonce
try: try:

28
src/proofofwork.py Normal file
View File

@ -0,0 +1,28 @@
def _pool_worker(nonce, initialHash, target, pool_size):
import hashlib
from struct import unpack, pack
trialValue = 99999999999999999999
while trialValue > target:
nonce += pool_size
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
return [trialValue, nonce]
def run(target, initialHash):
from multiprocessing import Pool, cpu_count
import time
try:
pool_size = cpu_count()
except:
pool_size = 4
pool = Pool(processes=pool_size)
result = []
for i in range(pool_size):
result.append(pool.apply_async(_pool_worker, args = (i, initialHash, target, pool_size)))
while True:
for counter in range(pool_size):
if result[counter].ready():
result = result[counter].get()
pool.terminate()
return result[0], result[1]
time.sleep(1)