Merge branch 'master' of http://github.com/Arceliar/PyBitmessage into Arceliar-master

This commit is contained in:
Jonathan Warren 2013-06-18 12:45:55 -04:00
commit 952524f7d5

View File

@ -3,23 +3,23 @@
#from multiprocessing import Pool, cpu_count #from multiprocessing import Pool, cpu_count
import hashlib import hashlib
from struct import unpack, pack from struct import unpack, pack
#import sys import sys
#import os #import os
def _set_idle(): def _set_idle():
try: if 'linux' in sys.platform:
import os
os.nice(20)
else:
try:
sys.getwindowsversion() sys.getwindowsversion()
import win32api,win32process,win32con import win32api,win32process,win32con
pid = win32api.GetCurrentProcessId() pid = win32api.GetCurrentProcessId()
handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid) handle = win32api.OpenProcess(win32con.PROCESS_ALL_ACCESS, True, pid)
win32process.SetPriorityClass(handle, win32process.IDLE_PRIORITY_CLASS) win32process.SetPriorityClass(handle, win32process.IDLE_PRIORITY_CLASS)
except: except:
try: #Windows 64-bit
#Linux pass
os.nice(20)
except:
#Windows 64-bit
pass
def _pool_worker(nonce, initialHash, target, pool_size): def _pool_worker(nonce, initialHash, target, pool_size):
_set_idle() _set_idle()
@ -29,25 +29,28 @@ def _pool_worker(nonce, initialHash, target, pool_size):
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8]) trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
return [trialValue, nonce] return [trialValue, nonce]
def run(target, initialHash): def _doSafePoW(target, initialHash):
nonce = 0 nonce = 0
trialValue = 99999999999999999999 trialValue = 99999999999999999999
while trialValue > target: while trialValue > target:
nonce += 1 nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8]) trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
return [trialValue, nonce] return [trialValue, nonce]
"""try:
def _doFastPoW(target, initialHash):
import shared
import time
from multiprocessing import Pool, cpu_count
try:
pool_size = cpu_count() pool_size = cpu_count()
except: except:
pool_size = 4 pool_size = 4
try: try:
maxCores = config.getint('bitmessagesettings', 'maxcores') maxCores = config.getint('bitmessagesettings', 'maxcores')
except: except:
maxCores = 99999 maxCores = 99999
if pool_size > maxCores: if pool_size > maxCores:
pool_size = maxCores pool_size = maxCores
pool = Pool(processes=pool_size) pool = Pool(processes=pool_size)
result = [] result = []
for i in range(pool_size): for i in range(pool_size):
@ -55,12 +58,18 @@ def run(target, initialHash):
while True: while True:
if shared.shutdown: if shared.shutdown:
pool.terminate() pool.terminate()
time.sleep(5) #Don't return anything (doing so will cause exceptions because we'll return an unusable response). Sit here and wait for this thread to close. pool.join() #Don't return anything (doing so will cause exceptions because we'll return an unusable response). Sit here and wait for this thread to close.
return return
for i in range(pool_size): for i in range(pool_size):
if result[i].ready(): if result[i].ready():
result = result[i].get() result = result[i].get()
pool.terminate() pool.terminate()
pool.join() #Wait for the workers to exit...
return result[0], result[1] return result[0], result[1]
time.sleep(0.2)""" time.sleep(0.2)
def run(target, initialHash):
if 'linux' in sys.platform:
return _doFastPoW(target, initialHash)
else:
return _doSafePoW(target, initialHash)