-
Notifications
You must be signed in to change notification settings - Fork 575
/
powinterrupttest.py
49 lines (39 loc) · 1.3 KB
/
powinterrupttest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 != "PyBitmessage":
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, "")