Bugfix: Check fd still has a handler object, in case it has been destroyed in the...
[bitcoin:eloipool.git] / util.py
1 from hashlib import sha256
2 from struct import unpack
3 import traceback
4
5 def dblsha(b):
6         return sha256(sha256(b).digest()).digest()
7
8 def swap32(b):
9         o = b''
10         for i in range(0, len(b), 4):
11                 o += b[i + 3:i - 1 if i else None:-1]
12         return o
13
14 def Bits2Target(bits):
15         return unpack('<L', bits[:3] + b'\0')[0] * 2**(8*(bits[3] - 3))
16
17 def hash2int(h):
18         n = unpack('<QQQQ', h)
19         n = (n[3] << 192) | (n[2] << 128) | (n[1] << 64) | n[0]
20         return n
21
22 def tryErr(logger, func, *a, **kw):
23         try:
24                 return func(*a, **kw)
25         except:
26                 logger.error(traceback.format_exc())
27                 return None
28
29 class RejectedShare(ValueError):
30         pass