2013-06-04 05:14:24 +02:00
|
|
|
#import shared
|
|
|
|
#import time
|
|
|
|
#from multiprocessing import Pool, cpu_count
|
2013-05-30 22:25:42 +02:00
|
|
|
import hashlib
|
|
|
|
from struct import unpack, pack
|
2013-06-05 23:20:34 +02:00
|
|
|
import sys
|
2013-06-04 05:14:24 +02:00
|
|
|
#import os
|
2013-06-03 07:04:22 +02:00
|
|
|
|
|
|
|
def _set_idle():
|
2013-06-05 23:20:34 +02:00
|
|
|
if 'linux' in sys.platform:
|
2013-06-18 18:56:03 +02:00
|
|
|
import os
|
|
|
|
os.nice(20)
|
2013-06-05 23:20:34 +02:00
|
|
|
else:
|
2013-06-18 18:56:03 +02:00
|
|
|
try:
|
|
|
|
sys.getwindowsversion()
|
|
|
|
import win32api,win32process,win32con
|
|
|
|
pid = win32api.GetCurrentProcessId()
|
|
|
|
handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
|
|
|
|
win32process.SetPriorityClass(handle, win32process.IDLE_PRIORITY_CLASS)
|
|
|
|
except:
|
|
|
|
#Windows 64-bit
|
|
|
|
pass
|
2013-05-30 22:25:42 +02:00
|
|
|
|
2013-05-29 22:01:12 +02:00
|
|
|
def _pool_worker(nonce, initialHash, target, pool_size):
|
2013-06-03 07:04:22 +02:00
|
|
|
_set_idle()
|
2013-05-29 22:01:12 +02:00
|
|
|
trialValue = 99999999999999999999
|
|
|
|
while trialValue > target:
|
2013-05-30 22:25:42 +02:00
|
|
|
nonce += pool_size
|
|
|
|
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
|
2013-05-29 22:01:12 +02:00
|
|
|
return [trialValue, nonce]
|
|
|
|
|
2013-06-05 23:20:34 +02:00
|
|
|
def _doSafePoW(target, initialHash):
|
2013-06-04 05:14:24 +02:00
|
|
|
nonce = 0
|
|
|
|
trialValue = 99999999999999999999
|
|
|
|
while trialValue > target:
|
|
|
|
nonce += 1
|
|
|
|
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
|
|
|
|
return [trialValue, nonce]
|
2013-06-05 23:20:34 +02:00
|
|
|
|
|
|
|
def _doFastPoW(target, initialHash):
|
|
|
|
import shared
|
|
|
|
import time
|
|
|
|
from multiprocessing import Pool, cpu_count
|
|
|
|
try:
|
2013-05-30 22:25:42 +02:00
|
|
|
pool_size = cpu_count()
|
2013-05-29 22:01:12 +02:00
|
|
|
except:
|
2013-05-30 22:25:42 +02:00
|
|
|
pool_size = 4
|
|
|
|
try:
|
|
|
|
maxCores = config.getint('bitmessagesettings', 'maxcores')
|
|
|
|
except:
|
|
|
|
maxCores = 99999
|
|
|
|
if pool_size > maxCores:
|
|
|
|
pool_size = maxCores
|
2013-05-29 22:01:12 +02:00
|
|
|
pool = Pool(processes=pool_size)
|
|
|
|
result = []
|
|
|
|
for i in range(pool_size):
|
2013-05-30 22:25:42 +02:00
|
|
|
result.append(pool.apply_async(_pool_worker, args = (i, initialHash, target, pool_size)))
|
2013-05-29 22:01:12 +02:00
|
|
|
while True:
|
2013-05-30 22:42:24 +02:00
|
|
|
if shared.shutdown:
|
|
|
|
pool.terminate()
|
2013-06-19 06:51:31 +02:00
|
|
|
while True:
|
|
|
|
time.sleep(10) # Don't let this thread return here; it will return nothing and cause an exception in bitmessagemain.py
|
2013-05-30 22:42:24 +02:00
|
|
|
return
|
2013-05-30 22:25:42 +02:00
|
|
|
for i in range(pool_size):
|
|
|
|
if result[i].ready():
|
|
|
|
result = result[i].get()
|
|
|
|
pool.terminate()
|
2013-06-06 19:30:57 +02:00
|
|
|
pool.join() #Wait for the workers to exit...
|
2013-05-30 22:25:42 +02:00
|
|
|
return result[0], result[1]
|
2013-06-05 23:20:34 +02:00
|
|
|
time.sleep(0.2)
|
2013-05-29 22:01:12 +02:00
|
|
|
|
2013-06-05 23:20:34 +02:00
|
|
|
def run(target, initialHash):
|
2013-06-05 23:29:40 +02:00
|
|
|
if 'linux' in sys.platform:
|
2013-06-18 18:56:03 +02:00
|
|
|
return _doFastPoW(target, initialHash)
|
2013-06-05 23:20:34 +02:00
|
|
|
else:
|
2013-06-18 18:56:03 +02:00
|
|
|
return _doSafePoW(target, initialHash)
|