From 2f27d43e7ed415aa28d55ff9ef95ca87d7e40e64 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Sun, 17 Apr 2016 20:31:25 +0200 Subject: [PATCH] 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. --- src/class_singleWorker.py | 25 ++++++++++++++++++++----- src/openclpow.py | 6 ++++-- src/proofofwork.py | 27 +++++++++++++++++++++------ 3 files changed, 45 insertions(+), 13 deletions(-) diff --git a/src/class_singleWorker.py b/src/class_singleWorker.py index 169b40c0..322b2390 100644 --- a/src/class_singleWorker.py +++ b/src/class_singleWorker.py @@ -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: diff --git a/src/openclpow.py b/src/openclpow.py index 426913a4..b643ca57 100644 --- a/src/openclpow.py +++ b/src/openclpow.py @@ -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(" 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'