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:
parent
5adc4429f0
commit
2f27d43e7e
|
@ -89,15 +89,30 @@ class singleWorker(threading.Thread, StoppableThread):
|
||||||
while shared.shutdown == 0:
|
while shared.shutdown == 0:
|
||||||
command, data = shared.workerQueue.get()
|
command, data = shared.workerQueue.get()
|
||||||
if command == 'sendmessage':
|
if command == 'sendmessage':
|
||||||
|
try:
|
||||||
self.sendMsg()
|
self.sendMsg()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
elif command == 'sendbroadcast':
|
elif command == 'sendbroadcast':
|
||||||
|
try:
|
||||||
self.sendBroadcast()
|
self.sendBroadcast()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
elif command == 'doPOWForMyV2Pubkey':
|
elif command == 'doPOWForMyV2Pubkey':
|
||||||
|
try:
|
||||||
self.doPOWForMyV2Pubkey(data)
|
self.doPOWForMyV2Pubkey(data)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
elif command == 'sendOutOrStoreMyV3Pubkey':
|
elif command == 'sendOutOrStoreMyV3Pubkey':
|
||||||
|
try:
|
||||||
self.sendOutOrStoreMyV3Pubkey(data)
|
self.sendOutOrStoreMyV3Pubkey(data)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
elif command == 'sendOutOrStoreMyV4Pubkey':
|
elif command == 'sendOutOrStoreMyV4Pubkey':
|
||||||
|
try:
|
||||||
self.sendOutOrStoreMyV4Pubkey(data)
|
self.sendOutOrStoreMyV4Pubkey(data)
|
||||||
|
except:
|
||||||
|
pass
|
||||||
elif command == 'stopThread':
|
elif command == 'stopThread':
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -5,7 +5,7 @@ import hashlib
|
||||||
import random
|
import random
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from shared import codePath, safeConfigGetBoolean
|
from shared import codePath, safeConfigGetBoolean, shutdown
|
||||||
from debug import logger
|
from debug import logger
|
||||||
|
|
||||||
libAvailable = True
|
libAvailable = True
|
||||||
|
@ -69,7 +69,7 @@ def do_opencl_pow(hash, target):
|
||||||
progress = 0
|
progress = 0
|
||||||
globamt = worksize*2000
|
globamt = worksize*2000
|
||||||
|
|
||||||
while output[0][0] == 0:
|
while output[0][0] == 0 and shutdown == 0:
|
||||||
kernel.set_arg(2, pack("<Q", progress))
|
kernel.set_arg(2, pack("<Q", progress))
|
||||||
cl.enqueue_nd_range_kernel(queue, kernel, (globamt,), (worksize,))
|
cl.enqueue_nd_range_kernel(queue, kernel, (globamt,), (worksize,))
|
||||||
cl.enqueue_read_buffer(queue, dest_buf, output)
|
cl.enqueue_read_buffer(queue, dest_buf, output)
|
||||||
|
@ -77,6 +77,8 @@ def do_opencl_pow(hash, target):
|
||||||
progress += globamt
|
progress += globamt
|
||||||
sofar = time.time() - start
|
sofar = time.time() - start
|
||||||
# logger.debug("Working for %.3fs, %.2f Mh/s", sofar, (progress / sofar) / 1000000)
|
# logger.debug("Working for %.3fs, %.2f Mh/s", sofar, (progress / sofar) / 1000000)
|
||||||
|
if shutdown != 0:
|
||||||
|
raise Exception ("Interrupted")
|
||||||
taken = time.time() - start
|
taken = time.time() - start
|
||||||
# logger.debug("Took %d tries.", progress)
|
# logger.debug("Took %d tries.", progress)
|
||||||
return output[0][0]
|
return output[0][0]
|
||||||
|
|
|
@ -29,7 +29,7 @@ def _set_idle():
|
||||||
def _pool_worker(nonce, initialHash, target, pool_size):
|
def _pool_worker(nonce, initialHash, target, pool_size):
|
||||||
_set_idle()
|
_set_idle()
|
||||||
trialValue = float('inf')
|
trialValue = float('inf')
|
||||||
while trialValue > target:
|
while trialValue > target and shutdown == 0:
|
||||||
nonce += pool_size
|
nonce += 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]
|
||||||
|
@ -38,9 +38,11 @@ def _doSafePoW(target, initialHash):
|
||||||
logger.debug("Safe PoW start")
|
logger.debug("Safe PoW start")
|
||||||
nonce = 0
|
nonce = 0
|
||||||
trialValue = float('inf')
|
trialValue = float('inf')
|
||||||
while trialValue > target:
|
while trialValue > target and shutdown == 0:
|
||||||
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])
|
||||||
|
if shutdown != 0:
|
||||||
|
raise Exception("Interrupted")
|
||||||
logger.debug("Safe PoW done")
|
logger.debug("Safe PoW done")
|
||||||
return [trialValue, nonce]
|
return [trialValue, nonce]
|
||||||
|
|
||||||
|
@ -65,9 +67,7 @@ def _doFastPoW(target, initialHash):
|
||||||
while True:
|
while True:
|
||||||
if shutdown >= 1:
|
if shutdown >= 1:
|
||||||
pool.terminate()
|
pool.terminate()
|
||||||
while True:
|
raise Exception("Interrupted")
|
||||||
time.sleep(10) # Don't let this thread return here; it will return nothing and cause an exception in bitmessagemain.py
|
|
||||||
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()
|
||||||
|
@ -85,6 +85,8 @@ def _doCPoW(target, initialHash):
|
||||||
logger.debug("C PoW start")
|
logger.debug("C PoW start")
|
||||||
nonce = bmpow(out_h, out_m)
|
nonce = bmpow(out_h, out_m)
|
||||||
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])
|
||||||
|
if shutdown != 0:
|
||||||
|
raise Exception("Interrupted")
|
||||||
logger.debug("C PoW done")
|
logger.debug("C PoW done")
|
||||||
return [trialValue, nonce]
|
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)
|
logger.error("Your GPUs (%s) did not calculate correctly, disabling OpenCL. Please report to the developers.", deviceNames)
|
||||||
openclpow.ctx = False
|
openclpow.ctx = False
|
||||||
raise Exception("GPU did not calculate correctly.")
|
raise Exception("GPU did not calculate correctly.")
|
||||||
|
if shutdown != 0:
|
||||||
|
raise Exception("Interrupted")
|
||||||
logger.debug("GPU PoW done")
|
logger.debug("GPU PoW done")
|
||||||
return [trialValue, nonce]
|
return [trialValue, nonce]
|
||||||
|
|
||||||
|
@ -138,11 +142,15 @@ def run(target, initialHash):
|
||||||
try:
|
try:
|
||||||
return _doGPUPoW(target, initialHash)
|
return _doGPUPoW(target, initialHash)
|
||||||
except:
|
except:
|
||||||
|
if shutdown != 0:
|
||||||
|
raise
|
||||||
pass # fallback
|
pass # fallback
|
||||||
if bmpow:
|
if bmpow:
|
||||||
try:
|
try:
|
||||||
return _doCPoW(target, initialHash)
|
return _doCPoW(target, initialHash)
|
||||||
except:
|
except:
|
||||||
|
if shutdown != 0:
|
||||||
|
raise
|
||||||
pass # fallback
|
pass # fallback
|
||||||
if frozen == "macosx_app" or not frozen:
|
if frozen == "macosx_app" or not frozen:
|
||||||
# on my (Peter Surda) Windows 10, Windows Defender
|
# on my (Peter Surda) Windows 10, Windows Defender
|
||||||
|
@ -152,8 +160,15 @@ def run(target, initialHash):
|
||||||
try:
|
try:
|
||||||
return _doFastPoW(target, initialHash)
|
return _doFastPoW(target, initialHash)
|
||||||
except:
|
except:
|
||||||
|
if shutdown != 0:
|
||||||
|
raise
|
||||||
pass #fallback
|
pass #fallback
|
||||||
|
try:
|
||||||
return _doSafePoW(target, initialHash)
|
return _doSafePoW(target, initialHash)
|
||||||
|
except:
|
||||||
|
if shutdown != 0:
|
||||||
|
raise
|
||||||
|
pass #fallback
|
||||||
|
|
||||||
# init
|
# init
|
||||||
bitmsglib = 'bitmsghash.so'
|
bitmsglib = 'bitmsghash.so'
|
||||||
|
|
Reference in New Issue
Block a user