This repository has been archived on 2024-12-18. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-18/src/proofofwork.py

139 lines
4.5 KiB
Python
Raw Normal View History

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
import sys
from shared import config, frozen
2014-08-27 09:14:32 +02:00
import shared
import openclpow
import os
import ctypes
curdir = os.path.dirname(__file__)
bitmsglib = 'bitmsghash.so'
if "win32" == sys.platform:
if ctypes.sizeof(ctypes.c_voidp) == 4:
bitmsglib = 'bitmsghash32.dll'
else:
bitmsglib = 'bitmsghash64.dll'
try:
bso = ctypes.WinDLL(os.path.join(curdir, bitmsglib))
except:
bso = None
else:
try:
bso = ctypes.CDLL(os.path.join(curdir, bitmsglib))
except:
bso = None
if bso:
try:
bmpow = bso.BitmessagePOW
bmpow.restype = ctypes.c_ulonglong
except:
bmpow = None
2013-06-03 07:04:22 +02:00
def _set_idle():
if 'linux' in sys.platform:
2013-06-18 18:56:03 +02:00
import os
os.nice(20) # @UndefinedVariable
else:
2013-06-18 18:56:03 +02:00
try:
sys.getwindowsversion()
import win32api,win32process,win32con # @UnresolvedImport
2013-06-18 18:56:03 +02:00
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
def _pool_worker(nonce, initialHash, target, pool_size):
2013-06-03 07:04:22 +02:00
_set_idle()
trialValue = float('inf')
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])
return [trialValue, nonce]
def _doSafePoW(target, initialHash):
print "Safe POW\n"
2013-06-04 05:14:24 +02:00
nonce = 0
trialValue = float('inf')
2013-06-04 05:14:24 +02:00
while trialValue > target:
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
return [trialValue, nonce]
def _doFastPoW(target, initialHash):
print "Fast POW\n"
import time
from multiprocessing import Pool, cpu_count
try:
2013-05-30 22:25:42 +02:00
pool_size = cpu_count()
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
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)))
while True:
if shared.shutdown >= 1:
2013-05-30 22:42:24 +02:00
pool.terminate()
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()
pool.join() #Wait for the workers to exit...
2013-05-30 22:25:42 +02:00
return result[0], result[1]
time.sleep(0.2)
def _doCPoW(target, initialHash):
h = initialHash
m = target
out_h = ctypes.pointer(ctypes.create_string_buffer(h, 64))
out_m = ctypes.c_ulonglong(m)
print "C PoW start"
nonce = bmpow(out_h, out_m)
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
print "C PoW done"
return [trialValue, nonce]
def _doGPUPoW(target, initialHash):
print "GPU PoW start"
nonce = openclpow.do_opencl_pow(initialHash.encode("hex"), target)
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
#print "{} - value {} < {}".format(nonce, trialValue, target)
print "GPU PoW done"
return [trialValue, nonce]
def run(target, initialHash):
2015-09-30 10:22:41 +02:00
target = int(target)
if shared.safeConfigGetBoolean('bitmessagesettings', 'opencl') and openclpow.has_opencl():
# trialvalue1, nonce1 = _doGPUPoW(target, initialHash)
# trialvalue, nonce = _doFastPoW(target, initialHash)
# print "GPU: %s, %s" % (trialvalue1, nonce1)
# print "Fast: %s, %s" % (trialvalue, nonce)
# return [trialvalue, nonce]
try:
return _doGPUPoW(target, initialHash)
except:
pass # fallback to normal PoW
if frozen == "macosx_app" or not frozen:
if bmpow:
try:
return _doCPoW(target, initialHash)
except:
pass # fallback to normal PoW
2013-06-18 18:56:03 +02:00
return _doFastPoW(target, initialHash)
else:
2013-06-18 18:56:03 +02:00
return _doSafePoW(target, initialHash)