/ dev / powinterrupttest.py
powinterrupttest.py
 1  import ctypes
 2  import hashlib
 3  from multiprocessing import current_process
 4  import os
 5  import signal
 6  from struct import unpack, pack
 7  from threading import current_thread
 8  
 9  shutdown = 0
10  
11  
12  def signal_handler(signal, frame):
13      global shutdown
14      print(("Got signal %i in %s/%s" % (signal, current_process().name, current_thread().name)))
15      if current_process().name != "MainProcess":
16          raise StopIteration("Interrupted")
17      if current_thread().name != "PyBitmessage":
18          return
19      shutdown = 1
20  
21  
22  def _doCPoW(target, initialHash):
23      # global shutdown
24      h = initialHash
25      m = target
26      out_h = ctypes.pointer(ctypes.create_string_buffer(h, 64))
27      out_m = ctypes.c_ulonglong(m)
28      print("C PoW start")
29      for c in range(0, 200000):
30          print(("Iter: %i" % (c)))
31          nonce = bmpow(out_h, out_m)
32          if shutdown:
33              break
34      trialValue, = unpack('>Q', hashlib.sha512(hashlib.sha512(pack('>Q', nonce) + initialHash).digest()).digest()[0:8])
35      if shutdown != 0:
36          raise StopIteration("Interrupted")
37      print("C PoW done")
38      return [trialValue, nonce]
39  
40  
41  signal.signal(signal.SIGINT, signal_handler)
42  signal.signal(signal.SIGTERM, signal_handler)
43  
44  bso = ctypes.CDLL(os.path.join("bitmsghash", "bitmsghash.so"))
45  
46  bmpow = bso.BitmessagePOW
47  bmpow.restype = ctypes.c_ulonglong
48  
49  _doCPoW(2**44, "")