Merge branch 'checkShare_vars' into serve_getmemorypool
[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 dblsha(b):
22         return sha256(sha256(b).digest()).digest()
23
24 def swap32(b):
25         o = b''
26         for i in range(0, len(b), 4):
27                 o += b[i + 3:i - 1 if i else None:-1]
28         return o
29
30 def Bits2Target(bits):
31         return unpack('<L', bits[:3] + b'\0')[0] * 2**(8*(bits[3] - 3))
32
33 def hash2int(h):
34         n = unpack('<QQQQ', h)
35         n = (n[3] << 192) | (n[2] << 128) | (n[1] << 64) | n[0]
36         return n
37
38 def tryErr(func, *a, **kw):
39         IE = kw.pop('IgnoredExceptions', BaseException)
40         logger = kw.pop('Logger', None)
41         emsg = kw.pop('ErrorMsg', None)
42         try:
43                 return func(*a, **kw)
44         except IE:
45                 if logger:
46                         emsg = "%s\n" % (emsg,) if emsg else ""
47                         emsg += traceback.format_exc()
48                         logger.error(emsg)
49                 return None
50
51 class RejectedShare(ValueError):
52         pass
53
54
55 import heapq
56
57 class ScheduleDict:
58         def __init__(self):
59                 self._dict = {}
60                 self._build_heap()
61         
62         def _build_heap(self):
63                 newheap = list((v[0], k, v[1]) for k, v in self._dict.items())
64                 heapq.heapify(newheap)
65                 self._heap = newheap
66         
67         def nextTime(self):
68                 while True:
69                         (t, k, o) = self._heap[0]
70                         if k in self._dict:
71                                 break
72                         heapq.heappop(self._heap)
73                 return t
74         
75         def shift(self):
76                 while True:
77                         (t, k, o) = heapq.heappop(self._heap)
78                         if k in self._dict:
79                                 break
80                 del self._dict[k]
81                 return o
82         
83         def __setitem__(self, o, t):
84                 k = id(o)
85                 self._dict[k] = (t, o)
86                 if len(self._heap) / 2 > len(self._dict):
87                         self._build_heap()
88                 else:
89                         heapq.heappush(self._heap, (t, k, o))
90         
91         def __getitem__(self, o):
92                 return self._dict[id(o)][0]
93         
94         def __delitem__(self, o):
95                 del self._dict[id(o)]
96                 if len(self._dict) < 2:
97                         self._build_heap()
98         
99         def __len__(self):
100                 return len(self._dict)