forked from c00w/bitHopper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
353 lines (310 loc) · 14.1 KB
/
api.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#Copyright (C) 2011,2012 Colin Rice
#This software is licensed under an included MIT license.
#See the file entitled LICENSE
#If you were not provided with a copy of the license please contact:
# Colin Rice [email protected]
import json, re, logging
import gevent, traceback
import time, socket
# Global timeout for sockets in case something leaks
socket.setdefaulttimeout(900)
class API():
def __init__(self, bitHopper):
self.bitHopper = bitHopper
self.api_lock = {}
self.pool = self.bitHopper.pool
self.api_pull = ['mine', 'info', 'mine_c', 'mine_charity', 'mine_lp', 'backup', 'backup_latehop']
self.api_pull.extend(['mine_' + coin["short_name"] for coin in self.bitHopper.altercoins.itervalues() if coin["short_name"] != 'btc'])
self.api_disable_sec = 7200
try:
self.api_disable_sec = self.bitHopper.config.getint('main', 'api_disable_sec')
except Exception, e:
logging.debug("Error getting value for api_disablesec")
logging.debug(e)
def UpdateShares(self, server, shares):
#Actual Share Update
prev_shares = self.pool.servers[server]['shares']
#Mark it as unlagged
self.pool.servers[server]['api_lag'] = False
self.pool.servers[server]['init'] = True
#If we have the same amount of shares query it in a bit.
if shares == prev_shares:
self.pool.servers[server]['refresh_time'] += .10*self.pool.servers[server]['refresh_time']
update_time = .10*self.pool.servers[server]['refresh_time']
else:
self.pool.servers[server]['refresh_time'] -= .10*self.pool.servers[server]['refresh_time']
update_time = self.pool.servers[server]['refresh_time']
if update_time <= self.pool.servers[server]['refresh_limit']:
update_time = self.pool.servers[server]['refresh_limit']
#Figure out what we should print
try:
k = str('{0:d}'.format(int(shares)))
if self.pool.servers[server]['ghash'] > 0:
k += '\t\t' + str('{0:.1f}gh/s '.format( self.pool.servers[server]['ghash'] ))
if self.pool.servers[server]['duration'] > 0:
k += '\t' + str('{0:d}min.'.format( (self.pool.servers[server]['duration']/60) ))
except Exception, e:
logging.debug("Error formatting")
logging.debug(e)
k = str(shares)
#Display output to user when shares change
if shares != prev_shares:
if len(server) == 12:
logging.info(str(server) +":"+ k)
elif len(server) <= 3:
logging.info(str(server) +":\t\t"+ k)
else:
logging.info(str(server) +":\t"+ k)
#If the shares indicate we found a block tell LP
coin_type = self.pool.servers[server]['coin']
for attr_coin in self.bitHopper.altercoins.itervalues():
if coin_type == attr_coin['short_name'] and shares < prev_shares and shares < 0.10 * self.bitHopper.difficulty[coin_type]:
self.bitHopper.lp.set_owner(server)
break
self.pool.servers[server]['shares'] = int(shares)
self.pool.servers[server]['err_api_count'] = 0
if self.pool.servers[server]['refresh_time'] > self.api_disable_sec and self.pool.servers[server]['role'] not in ['info','backup','backup_latehop']:
logging.info('Disabled due to unchanging api: ' + server)
self.pool.servers[server]['role'] = 'api_disable'
return update_time
def errsharesResponse(self, error, server_name):
logging.info(server_name + ' api error:' + str(error))
pool = server_name
self.pool.servers[pool]['err_api_count'] += 1
self.pool.servers[pool]['init'] = True
if self.pool.servers[pool]['err_api_count'] > 1:
self.pool.servers[pool]['api_lag'] = True
update_time = self.pool.servers[pool]['refresh_time']
if update_time < self.pool.servers[pool]['refresh_limit']:
update_time = self.pool.servers[pool]['refresh_limit']
return update_time
def selectsharesResponse(self, response, server_name):
#logging.debug('Calling sharesResponse for '+ args)
server = self.pool.servers[server_name]
old_duration = server['duration']
if server['role'] not in self.api_pull:
return -1
ghash = self.get_ghash(server, response, server['api_method'])
if ghash > 0:
server['ghash'] = ghash
if 'api_key_duration' in server:
if 'json' in server['api_method'] and 'api_key_duration' in server:
dur = json.loads(response)
for value in server['api_key_duration'].split(','):
dur = dur[value]
else:
dur = response
duration = self.get_duration(server, str(dur))
if duration > 0:
server['duration'] = duration
else:
duration = self.get_duration(server, response)
if duration < 0:
duration = 7*24*3600
else:
server['duration'] = duration
if server['api_method'] == 'json':
try:
info = json.loads(response)
except ValueError, e:
logging.debug(str(server_name) + " - unable to extract JSON from response")
raise e
for value in server['api_key'].split(','):
info = info[value]
elif server['api_method'] == 'json_ec':
try:
info = json.loads(response[:response.find('}')+1])
except ValueError, e:
logging.debug(str(server_name) + " - unable to extract JSON from response")
raise e
for value in server['api_key'].split(','):
info = info[value]
elif server['api_method'] == 're':
output = re.search(server['api_key'],response)
if output == None:
raise Exception(str(server_name) + " - unable to extract shares from response using regexp")
if 'api_group' in server:
info = output.group(int(server['api_group']))
else:
info = output.group(1)
elif server['api_method'] == 're_rateduration':
server['isDurationEstimated'] = True
#Check and assume
if ghash < 0:
ghash = 1
old = server['last_pulled']
server['last_pulled'] = time.time()
diff = server['last_pulled'] - old
server['duration_temporal'] = server['duration_temporal'] + diff
# new round started or initial estimation
rate = 0.25 * ghash
if duration < old_duration or old_duration < 0:
round_shares = int(rate * duration)
else:
round_shares = server['shares'] + int(rate * diff)
info = str(round_shares)
elif server['api_method'] == 're_shareestimate':
# get share count based on user shares and user reward estimate
output = re.search(server['api_key_shares'],response)
shares = output.group(1)
output = re.search(server['api_key_estimate'],response)
estimate = output.group(1)
round_shares = int(50.0 * float(shares) / float(estimate))
info = str(round_shares)
elif server['api_method'] == 're_rate':
output = re.search(server['api_key'],response)
if 'api_group' in server:
output = output.group(int(server['api_group']))
else:
output = output.group(1)
if 'api_strip' in server:
strip_str = server['api_strip'][1:-1]
output = output.replace(strip_str,'')
if 're_rate_type' in server:
prefix = server['re_rate_type']
if prefix == 'GH':
mult = 1000**3
if prefix == 'MH':
mult = 1000**2
if prefix == 'KH':
mult = 1000
if prefix == 'None':
mult = 1
else:
mult = 1000**3
rate = int(output)
rate = rate * mult
server['ghash'] = float(rate)/(1000**3)
rate = float(rate)/2**32
old = server['last_pulled']
server['last_pulled'] = time.time()
diff = server['last_pulled'] - old
shares = int(rate * diff)
info = str(shares + server['shares'])
#Disable api scraping
elif server['api_method'] == 'disable':
info = '0'
logging.info('Share estimation disabled for: ' + server['name'])
else:
logging.info('Unrecognized api method: ' + str(server))
if 'api_strip' in server:
strip_char = server['api_strip'][1:-1]
info = info.replace(strip_char,'')
if info == None:
round_shares = int(self.bitHopper.difficulty['btc'])
else:
round_shares = int(info)
update_time = self.UpdateShares(server_name, round_shares)
self.bitHopper.server_update()
return update_time
def get_ghash(self, server, response, role):
if role.find('json') >= 0 :
info = json.loads(response)
if 'api_key_ghashrate' in server:
for value in server['api_key_ghashrate'].split(','):
info = info[value]
return float(info)
if 'api_key_mhashrate' in server:
for value in server['api_key_mhashrate'].split(','):
info = info[value]
return float(info) / 1000.0
if 'api_key_khashrate' in server:
for value in server['api_key_khashrate'].split(','):
info = info[value]
return float(info) / 1000000.0
if 'api_key_hashrate' in server:
for value in server['api_key_hashrate'].split(','):
info = info[value]
return float(info) / 1000000000.0
if 'api_key_ghashrate' in server:
output = re.search(server['api_key_ghashrate'], response)
return float(output.group(1).replace(' ', ''))
if 'api_key_mhashrate' in server:
output = re.search(server['api_key_mhashrate'], response)
return float(output.group(1).replace(' ', '')) / 1000.0
if 'api_key_khashrate' in server:
output = re.search(server['api_key_khashrate'], response)
return float(output.group(1).replace(' ', '')) / 1000000.0
if 'api_key_hashrate' in server:
output = re.search(server['api_key_hashrate'], response)
return float(output.group(1).replace(' ', '')) / 1000000000.0
return -1
def get_duration(self, server, response):
duration = -1
if 'api_key_duration_day_hour_min' in server:
output = re.search(server['api_key_duration_day_hour_min'], response)
try:
day = int(output.group(1).replace(' ', ''))
except AttributeError:
day = 0
try:
hour = int(output.group(2).replace(' ', ''))
except AttributeError:
hour = 0
try:
minute = int(output.group(3).replace(' ', ''))
except AttributeError:
minute = 0
if day == 0:
if hour == 0:
if minute == 0:
duration = -1
else:
duration = minute * 60
else:
duration = hour * 3600 + minute * 60
else:
duration = day*24*3600 + hour * 3600 + minute * 60
elif 'api_key_duration_hour_min' in server:
output = re.search(server['api_key_duration_hour_min'], response)
try:
hour = int(output.group(1).replace(' ', ''))
except AttributeError:
hour = 0
try:
minute = int(output.group(2).replace(' ', ''))
except AttributeError:
minute = 0
if hour == 0:
if minute == 0:
duration = -1
else:
duration = minute * 60
else:
duration = hour * 3600 + minute * 60
elif 'api_key_duration_min' in server:
output = re.search(server['api_key_duration_min'], response)
try:
minute = int(output.group(1).replace(' ', ''))
except AttributeError:
minute = 0
if minute == 0:
duration = -1
else:
duration = minute * 60
elif 'api_key_duration_sec' in server:
output = re.search(server['api_key_duration_sec'], response)
try:
duration = int(output.group(1).replace(' ', ''))
except AttributeError:
duration = -1
return duration
def update_api_server(self,server):
update_time = self.pool.servers[server]['refresh_limit']
try:
if self.pool.servers[server]['role'] not in self.api_pull:
return
info = self.pool.servers[server]
self.bitHopper.scheduler.update_api_server(server)
user_agent = None
if 'user_agent' in info:
user_agent = info['user_agent']
value = self.bitHopper.work.get(info['api_address'], user_agent)
update_time = self.selectsharesResponse(value, server)
except Exception, e:
update_time = self.errsharesResponse(e, server)
finally:
gevent.spawn_later(update_time, self.update_api_server, server)
def update_api_servers(self):
for server in self.pool.servers:
gevent.spawn(self.update_api_server, server)