Merge branch 'bugfix_YN_None' into bugfix_YN_None_2
[bitcoin:eloipool.git] / util.py
1 # Eloipool - Python Bitcoin pool server
2 # Copyright (C) 2011-2012  Luke Dashjr <luke-jr+eloipool@utopios.org>
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as
6 # published by the Free Software Foundation, either version 3 of the
7 # License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 from hashlib import sha256
18 from struct import unpack
19 import traceback
20
21 def YN(b):
22         if b is None:
23                 return None
24         return 'Y' if b else 'N'
25
26 def dblsha(b):
27         return sha256(sha256(b).digest()).digest()
28
29 def swap32(b):
30         o = b''
31         for i in range(0, len(b), 4):
32                 o += b[i + 3:i - 1 if i else None:-1]
33         return o
34
35 def Bits2Target(bits):
36         return unpack('<L', bits[:3] + b'\0')[0] * 2**(8*(bits[3] - 3))
37
38 def hash2int(h):
39         n = unpack('<QQQQ', h)
40         n = (n[3] << 192) | (n[2] << 128) | (n[1] << 64) | n[0]
41         return n
42
43 def tryErr(func, *a, **kw):
44         IE = kw.pop('IgnoredExceptions', BaseException)
45         logger = kw.pop('Logger', None)
46         emsg = kw.pop('ErrorMsg', None)
47         try:
48                 return func(*a, **kw)
49         except IE:
50                 if logger:
51                         emsg = "%s\n" % (emsg,) if emsg else ""
52                         emsg += traceback.format_exc()
53                         logger.error(emsg)
54                 return None
55
56 class RejectedShare(ValueError):
57         pass
58
59
60 import heapq
61
62 class ScheduleDict:
63         def __init__(self):
64                 self._dict = {}
65                 self._build_heap()
66         
67         def _build_heap(self):
68                 newheap = list((v[0], k, v[1]) for k, v in self._dict.items())
69                 heapq.heapify(newheap)
70                 self._heap = newheap
71         
72         def nextTime(self):
73                 while True:
74                         (t, k, o) = self._heap[0]
75                         if k in self._dict:
76                                 break
77                         heapq.heappop(self._heap)
78                 return t
79         
80         def shift(self):
81                 while True:
82                         (t, k, o) = heapq.heappop(self._heap)
83                         if k in self._dict:
84                                 break
85                 del self._dict[k]
86                 return o
87         
88         def __setitem__(self, o, t):
89                 k = id(o)
90                 self._dict[k] = (t, o)
91                 if len(self._heap) / 2 > len(self._dict):
92                         self._build_heap()
93                 else:
94                         heapq.heappush(self._heap, (t, k, o))
95         
96         def __getitem__(self, o):
97                 return self._dict[id(o)][0]
98         
99         def __delitem__(self, o):
100                 del self._dict[id(o)]
101                 if len(self._dict) < 2:
102                         self._build_heap()
103         
104         def __len__(self):
105                 return len(self._dict)