This repository has been archived on 2024-12-03. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-12-03/dev/powinterrupttest.py
Peter Surda 3033520995
Directory maintenance
- rearrange obsolete build scripts
- rearrange working build scripts
- add build / devel scripts I use
- add existing development tests (bloom filter, message encoding, thread
  interrupts)
- add directory descriptions
2016-12-01 16:45:04 +01:00

50 lines
1.3 KiB
Python

import ctypes
import hashlib
from multiprocessing import current_process
import os
import signal
from struct import unpack, pack
from threading import current_thread
shutdown = 0
def signal_handler(signal, frame):
global shutdown
print "Got signal %i in %s/%s" % (signal, current_process().name, current_thread().name)
if current_process().name != "MainProcess":
raise StopIteration("Interrupted")
if current_thread().name != "MainThread":
return
shutdown = 1
def _doCPoW(target, initialHash):
# global shutdown
h = initialHash
m = target
out_h = ctypes.pointer(ctypes.create_string_buffer(h, 64))
out_m = ctypes.c_ulonglong(m)
print "C PoW start"
for c in range(0, 200000):
print "Iter: %i" % (c)
nonce = bmpow(out_h, out_m)
if shutdown:
break
trialValue, = unpack('>Q', hashlib.sha512(hashlib.sha512(pack('>Q', nonce) + initialHash).digest()).digest()[0:8])
if shutdown != 0:
raise StopIteration("Interrupted")
print "C PoW done"
return [trialValue, nonce]
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
bso = ctypes.CDLL(os.path.join("bitmsghash", "bitmsghash.so"))
bmpow = bso.BitmessagePOW
bmpow.restype = ctypes.c_ulonglong
_doCPoW(2**44, "")