-
Notifications
You must be signed in to change notification settings - Fork 69
/
util.py
214 lines (176 loc) · 4.79 KB
/
util.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# Eloipool - Python Bitcoin pool server
# Copyright (C) 2011-2012 Luke Dashjr <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from hashlib import sha256
from math import log
import re
import string
from struct import unpack
import traceback
def YN(b):
if b is None:
return None
return 'Y' if b else 'N'
def _maybe_int(n):
n_int = int(n)
if n == n_int:
return n_int
return n
def target2pdiff(target):
if target is None:
return None
pdiff = round(2**(224 - log(target, 2)), 8)
return _maybe_int(pdiff)
bdiff1target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
def target2bdiff(target):
bdiff = bdiff1target / target
return _maybe_int(bdiff)
class shareLogFormatter:
_re_x = re.compile(r'^\s*(\w+)\s*(?:\(\s*(.*?)\s*\))?\s*$')
def __init__(self, *a, **ka):
self._p = self.parse(*a, **ka)
# NOTE: This only works for psf='%s' (default)
def formatShare(self, *a, **ka):
(stmt, params) = self.applyToShare(*a, **ka)
return stmt % params
def applyToShare(self, share):
(stmt, stmtf) = self._p
params = []
for f in stmtf:
params.append(f(share))
params = tuple(params)
return (stmt, params)
@classmethod
def parse(self, stmt, psf = '%s'):
fmt = string.Formatter()
pstmt = tuple(fmt.parse(stmt))
stmt = ''
fmt = []
for (lit, field, fmtspec, conv) in pstmt:
stmt += lit
if not field:
continue
f = self.get_field(field)
fmt.append(f)
stmt += psf
fmt = tuple(fmt)
return (stmt, fmt)
@classmethod
def get_field(self, field):
m = self._re_x.match(field)
if m:
if m.group(2) is None:
# identifier
return lambda s: s.get(field, None)
else:
# function
fn = m.group(1)
sf = self.get_field(m.group(2))
return getattr(self, 'get_field_%s' % (fn,))(sf)
raise ValueError('Failed to parse field: %s' % (field,))
@classmethod
def get_field_not(self, subfunc):
return lambda s: not subfunc(s)
@classmethod
def get_field_Q(self, subfunc):
return lambda s: subfunc(s) or '?'
@classmethod
def get_field_dash(self, subfunc):
return lambda s: subfunc(s) or '-'
@classmethod
def get_field_YN(self, subfunc):
return lambda s: YN(subfunc(s))
@classmethod
def get_field_target2bdiff(self, subfunc):
return lambda s: target2bdiff(subfunc(s))
@classmethod
def get_field_target2pdiff(self, subfunc):
return lambda s: target2pdiff(subfunc(s))
def dblsha(b):
return sha256(sha256(b).digest()).digest()
def swap32(b):
o = b''
for i in range(0, len(b), 4):
o += b[i + 3:i - 1 if i else None:-1]
return o
def Bits2Target(bits):
return unpack('<L', bits[:3] + b'\0')[0] * 2**(8*(bits[3] - 3))
def LEhash2int(h):
n = unpack('<QQQQ', h)
n = (n[3] << 192) | (n[2] << 128) | (n[1] << 64) | n[0]
return n
def BEhash2int(h):
n = unpack('>QQQQ', h)
n = (n[0] << 192) | (n[1] << 128) | (n[2] << 64) | n[3]
return n
def tryErr(func, *a, **kw):
IE = kw.pop('IgnoredExceptions', BaseException)
logger = kw.pop('Logger', None)
emsg = kw.pop('ErrorMsg', None)
try:
return func(*a, **kw)
except IE:
if logger:
emsg = "%s\n" % (emsg,) if emsg else ""
emsg += traceback.format_exc()
logger.error(emsg)
return None
class RejectedShare(ValueError):
pass
PendingUpstream = object()
import heapq
class ScheduleDict:
def __init__(self):
self._dict = {}
self._build_heap()
def _build_heap(self):
newheap = list((v[0], k, v[1]) for k, v in self._dict.items())
heapq.heapify(newheap)
self._heap = newheap
def nextTime(self):
while True:
(t, k, o) = self._heap[0]
if k in self._dict:
break
heapq.heappop(self._heap)
return t
def shift(self):
while True:
(t, k, o) = heapq.heappop(self._heap)
if k in self._dict:
break
del self._dict[k]
return o
def __setitem__(self, o, t):
k = id(o)
self._dict[k] = (t, o)
if len(self._heap) / 2 > len(self._dict):
self._build_heap()
else:
heapq.heappush(self._heap, (t, k, o))
def __getitem__(self, o):
return self._dict[id(o)][0]
def __delitem__(self, o):
del self._dict[id(o)]
if len(self._dict) < 2:
self._build_heap()
def __len__(self):
return len(self._dict)
class WithNoop:
def __enter__(self):
pass
def __exit__(self, *a):
pass
WithNoop = WithNoop()