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

50 lines
1.3 KiB
Python
Raw Normal View History

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
2021-11-08 17:09:59 +01:00
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 != "PyBitmessage":
return
shutdown = 1
def _doCPoW(target, initialHash):
2021-11-08 17:09:59 +01:00
# global shutdown
h = initialHash
m = target
out_h = ctypes.pointer(ctypes.create_string_buffer(h, 64))
out_m = ctypes.c_ulonglong(m)
2021-11-08 17:09:59 +01:00
print("C PoW start")
for c in range(0, 200000):
2021-11-08 17:09:59 +01:00
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")
2021-11-08 17:09:59 +01:00
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, "")