PoW can sometimes be shutdown-able

Python and OpenCL PoW now stop when PyBitmessage shutdowns. C PoW needs
additional support in C so it doesn't work there yet.
This commit is contained in:
Peter Šurda 2016-04-17 20:31:25 +02:00
parent 5adc4429f0
commit 2f27d43e7e
3 changed files with 45 additions and 13 deletions

View File

@ -89,15 +89,30 @@ class singleWorker(threading.Thread, StoppableThread):
while shared.shutdown == 0:
command, data = shared.workerQueue.get()
if command == 'sendmessage':
self.sendMsg()
try:
self.sendMsg()
except:
pass
elif command == 'sendbroadcast':
self.sendBroadcast()
try:
self.sendBroadcast()
except:
pass
elif command == 'doPOWForMyV2Pubkey':
self.doPOWForMyV2Pubkey(data)
try:
self.doPOWForMyV2Pubkey(data)
except:
pass
elif command == 'sendOutOrStoreMyV3Pubkey':
self.sendOutOrStoreMyV3Pubkey(data)
try:
self.sendOutOrStoreMyV3Pubkey(data)
except:
pass
elif command == 'sendOutOrStoreMyV4Pubkey':
self.sendOutOrStoreMyV4Pubkey(data)
try:
self.sendOutOrStoreMyV4Pubkey(data)
except:
pass
elif command == 'stopThread':
return
else:

View File

@ -5,7 +5,7 @@ import hashlib
import random
import os
from shared import codePath, safeConfigGetBoolean
from shared import codePath, safeConfigGetBoolean, shutdown
from debug import logger
libAvailable = True
@ -69,7 +69,7 @@ def do_opencl_pow(hash, target):
progress = 0
globamt = worksize*2000
while output[0][0] == 0:
while output[0][0] == 0 and shutdown == 0:
kernel.set_arg(2, pack("<Q", progress))
cl.enqueue_nd_range_kernel(queue, kernel, (globamt,), (worksize,))
cl.enqueue_read_buffer(queue, dest_buf, output)
@ -77,6 +77,8 @@ def do_opencl_pow(hash, target):
progress += globamt
sofar = time.time() - start
# logger.debug("Working for %.3fs, %.2f Mh/s", sofar, (progress / sofar) / 1000000)
if shutdown != 0:
raise Exception ("Interrupted")
taken = time.time() - start
# logger.debug("Took %d tries.", progress)
return output[0][0]

View File

@ -29,7 +29,7 @@ def _set_idle():
def _pool_worker(nonce, initialHash, target, pool_size):
_set_idle()
trialValue = float('inf')
while trialValue > target:
while trialValue > target and shutdown == 0:
nonce += pool_size
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
return [trialValue, nonce]
@ -38,9 +38,11 @@ def _doSafePoW(target, initialHash):
logger.debug("Safe PoW start")
nonce = 0
trialValue = float('inf')
while trialValue > target:
while trialValue > target and shutdown == 0:
nonce += 1
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
if shutdown != 0:
raise Exception("Interrupted")
logger.debug("Safe PoW done")
return [trialValue, nonce]
@ -65,9 +67,7 @@ def _doFastPoW(target, initialHash):
while True:
if shutdown >= 1:
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
return
raise Exception("Interrupted")
for i in range(pool_size):
if result[i].ready():
result = result[i].get()
@ -85,6 +85,8 @@ def _doCPoW(target, initialHash):
logger.debug("C PoW start")
nonce = bmpow(out_h, out_m)
trialValue, = unpack('>Q',hashlib.sha512(hashlib.sha512(pack('>Q',nonce) + initialHash).digest()).digest()[0:8])
if shutdown != 0:
raise Exception("Interrupted")
logger.debug("C PoW done")
return [trialValue, nonce]
@ -99,6 +101,8 @@ def _doGPUPoW(target, initialHash):
logger.error("Your GPUs (%s) did not calculate correctly, disabling OpenCL. Please report to the developers.", deviceNames)
openclpow.ctx = False
raise Exception("GPU did not calculate correctly.")
if shutdown != 0:
raise Exception("Interrupted")
logger.debug("GPU PoW done")
return [trialValue, nonce]
@ -138,11 +142,15 @@ def run(target, initialHash):
try:
return _doGPUPoW(target, initialHash)
except:
if shutdown != 0:
raise
pass # fallback
if bmpow:
try:
return _doCPoW(target, initialHash)
except:
if shutdown != 0:
raise
pass # fallback
if frozen == "macosx_app" or not frozen:
# on my (Peter Surda) Windows 10, Windows Defender
@ -152,8 +160,15 @@ def run(target, initialHash):
try:
return _doFastPoW(target, initialHash)
except:
if shutdown != 0:
raise
pass #fallback
return _doSafePoW(target, initialHash)
try:
return _doSafePoW(target, initialHash)
except:
if shutdown != 0:
raise
pass #fallback
# init
bitmsglib = 'bitmsghash.so'