-
Notifications
You must be signed in to change notification settings - Fork 5
/
cluster_worker.py
433 lines (358 loc) · 12.1 KB
/
cluster_worker.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
import hashlib
import xxhash
import socket
import threading
import time
import struct
import traceback
import logging
import json
import types
logger = logging.getLogger('Cluster_Client')
logger.setLevel(logging.DEBUG)
#fh = logging.FileHandler('Cluster_Client.log')
#fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
# create formatter and add it to the handlers
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
#fh.setFormatter(formatter)
ch.setFormatter(formatter)
# add the handlers to the logger
#logger.addHandler(fh)
logger.addHandler(ch)
WORKER_NAME = 'TEST'
CLUSTER_SERVER_ADDRESS = ('127.0.0.1',9090)
client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client_socket.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
client_socket.setblocking(False)
END_JOB = True
calculation_result = [None,0,0,0,None]
calculation_thread = None
EXPECTED_HASH = None
START_END = None
JOB_WAS_TERMINATED = False
ping_delay = 30
last_ping = 0
def update_last_ping():
global last_ping
last_ping = time.time()
def to_ping():
global ping_delay
global last_ping
return time.time()-last_ping>ping_delay
def ducos1(
lastBlockHash,
expectedHash,
start,
end):
global END_JOB,calculation_result
hashcount = 0
base_hash = hashlib.sha1(str(lastBlockHash).encode('ascii'))
temp_hash = None
for ducos1xxres in range(int(start),int(end)):
if END_JOB:
logger.info('JOB TERMINATED')
calculation_result = [None,0,0,0,None]
return None
temp_hash = base_hash.copy()
temp_hash.update(str(ducos1xxres).encode('ascii'))
ducos1xx = temp_hash.hexdigest()
# Increment hash counter for hashrate calculator
hashcount += 1
# Check if result was found
if ducos1xx == expectedHash:
END_JOB = True
logger.debug('LEFT '+str(ducos1xxres))
calculation_result = [ducos1xxres, hashcount,start,end,expectedHash]
return None
logger.info('Empty block')
END_JOB = True
calculation_result = [None,hashcount,start,end,expectedHash]
def ducos1xxh(
lastBlockHash,
expectedHash,
start,
end):
global END_JOB,calculation_result
hashcount = 0
base_hash = xxhash.xxh64(str(lastBlockHash),seed=2811)
temp_hash = None
for ducos1xxres in range(int(start),int(end)):
if END_JOB:
logger.info('JOB TERMINATED')
calculation_result = [None,0,0,0,None]
return None
temp_hash = base_hash.copy()
temp_hash.update(str(ducos1xxres))
ducos1xx = temp_hash.hexdigest()
# Increment hash counter for hashrate calculator
hashcount += 1
# Check if result was found
if ducos1xx == expectedHash:
END_JOB = True
logger.debug('LEFT '+str(ducos1xxres))
calculation_result = [ducos1xxres, hashcount,start,end,expectedHash]
return None
logger.info('Empty block')
END_JOB = True
calculation_result = [None,hashcount,start,end,expectedHash]
def get_job():
global client_socket
global CLUSTER_SERVER_ADDRESS
global END_JOB
END_JOB = False
data = json.dumps({'t':'e',
'event':'get_job'}).encode('ascii')
client_socket.sendto(data,CLUSTER_SERVER_ADDRESS)
def ping(dispatcher,event):
'''
event - {'t':'e',
'event':'ping',
'address':(1,1)}
'''
global client_socket
update_last_ping()
logger.info('Pinging master server')
data = b'{"t":"e","event":"ping"}'
client_socket.sendto(data,event.address)
def register(dispatcher,event):
'''
event = {'t':'e',
'event':'register',
'address':(127.0.0.1,1234),
'callback':socket}
'''
global WORKER_NAME
dispatcher.clear_queue()
logger.info('Registering worker')
END_JOB = False
calculation_result = [None,0,0,0,None]
message = {'t':'e',
'event':'register',
'name':WORKER_NAME}
data = json.dumps(message).encode('ascii')
event.callback.sendto(data,event.address)
get_job()
def start_job(dispatcher,event):
'''
event = {'t':'e',
'event':'start_job',
'lastBlockHash':JOB[0],
'expectedHash':JOB[1],
'start':JOB_START,
'end':JOB_END,
'algorithm':algorithm,
'address':(),
'callback':socket}
'''
global calculation_thread
global END_JOB
global calculation_result
global EXPECTED_HASH
global START_END
logger.info('Starting job')
arguments = (event.lastBlockHash,
event.expectedHash,
event.start,
event.end)
func = None
if event.algorithm == 'XXHASH':
logger.info('Using XXHASH algorithm')
func = ducos1xxh
elif event.algorithm == 'DUCO-S1':
logger.info('Using DUCO-S1 algorithm')
func = ducos1
else:
logger.warning('Algorithm not implemented')
logger.debug(str(event.algorithm))
return
END_JOB = True
#yield
try:
calculation_thread.join()
except:
pass
EXPECTED_HASH = event.expectedHash
START_END = (event.start,event.end)
if func == None:
return None
END_JOB = False
calculation_result = [None,0,0,0,None]
calculation_thread = threading.Thread(target=func,args=arguments,name='calculation thread')
calculation_thread.start()
data = json.dumps({'t':'a',
'status':'ok',
'message':'Job accepted'})
event.callback.sendto(data.encode('ascii'),event.address)
update_last_ping()
def stop_job(dispatcher,event):
'''
event = {'t':'e',
'event':'stop_job',
'expected_hash':JOB[1],
'start_end':event.start_end,
'message':'another device already solved hash'}
'''
global END_JOB
global calculation_result
global calculation_thread
global EXPECTED_HASH
global START_END
global JOB_WAS_TERMINATED
if EXPECTED_HASH != event.expected_hash\
or event.start_end[0] != START_END[0]\
or event.start_end[1] != START_END[1]:
logger.warning('Trying to stop wrong job')
return
JOB_WAS_TERMINATED = True
logger.info('Terminating job')
END_JOB = True
try:
calculation_thread.join()
except:
pass
END_JOB = False
#calculation_result = [None,0,0,0]
#calculation_thread = None
data = json.dumps({'t':'a',
'status':'ok',
'message':'Job terminated'})
event.callback.sendto(data.encode('ascii'),event.address)
update_last_ping()
def send_result():
global calculation_result
global calculation_thread
global END_JOB
global client_socket
global CLUSTER_SERVER_ADDRESS
global JOB_WAS_TERMINATED
logger.info('Sending result')
logger.debug(str(calculation_result))
data = json.dumps({'t':'e',
'event':'job_done',
'result':calculation_result[:2],
'start_end':calculation_result[2:4],
'expected_hash':calculation_result[4]})
client_socket.sendto(data.encode('ascii'),CLUSTER_SERVER_ADDRESS)
calculation_result = [None,0,0,0,None]
calculation_thread = None
END_JOB = False
class Event(object):
def __init__(self,input:dict):
self.dict_representation = input
def __dict__(self):
return super(Event, self).__getattribute__('dict_representation')
#def event_name(self) -> str:
# return self.dict_representation['event']
def __getattribute__(self, item):
# Calling the super class to avoid recursion
return super(Event, self).__getattribute__(item)
def __getattr__(self, item):
try:
return super(Event, self).__getattribute__('dict_representation')[item]
except:
logger.warning('NO SUCH ELEMENT AS '+str(item))
pass
def __str__(self):
return str(self.dict_representation)
class Dispatcher:
def __init__(self):
self.actions = {}
self.queue = []
self.active_loop = []
def register(self,event_name,action):
self.actions[event_name] = action
def add_to_queue(self,event:Event):
logger.debug('added event')
self.queue.append(event)
def clear_queue(self):
self.queue = []
def iter_through_active_list(self):
counter = 0
while counter<len(self.active_loop):
try:
next(self.active_loop[counter])
except StopIteration:
self.active_loop.pop(counter)
continue
counter += 1
def dispatch_event(self,count=1):
for i in range(count):
try:
event = self.queue.pop(0)
except:
return None
logger.debug('dispatching event')
func = self.actions.get(event.event,None)
if func == None:
logger.warning('NO SUCH ACTION '+event.event)
return None
activity = self.actions[event.event](self,event)
if isinstance(activity,types.GeneratorType):
self.active_loop.append(activity)
def client():
global client_socket
global END_JOB
global calculation_thread
global JOB_WAS_TERMINATED
logger.debug('Initializing dispatcher')
event_dispatcher = Dispatcher()
event_dispatcher.register('register',register)
event_dispatcher.register('stop_job',stop_job)
event_dispatcher.register('start_job',start_job)
event_dispatcher.register('ping',ping)
logger.debug('Dispatcher initialized')
END_JOB = True
while True:
data = None
try:
data, address = client_socket.recvfrom(1024)
except:
pass
if data != None:
data_is_ok = False
try:
message = json.loads(data.decode('ascii'))
data_is_ok = True
except:
logger.warning("can't parse packet")
logger.debug(str(data))
if data_is_ok:
logger.debug('accepted packet')
logger.debug(str(message))
if message['t'] == 'e':
message['address'] = address
message['callback'] = client_socket
event = Event(message)
event_dispatcher.add_to_queue(event)
else:
pass
try:
event_dispatcher.dispatch_event()
except Exception as e:
logger.error('CANT DISPATCH EVENT')
logger.debug('Traceback',exc_info=e)
try:
event_dispatcher.iter_through_active_list()
except Exception as e:
logger.error('CANT EXECUTE')
logger.debug('Traceback',exc_info=e)
if to_ping():
event = Event({'t':'e',
'event':'ping',
'address':CLUSTER_SERVER_ADDRESS})
event_dispatcher.add_to_queue(event)
if END_JOB:
if calculation_result[4] != None:
send_result()
get_job()
time.sleep(0.5)
if __name__ == '__main__':
try:
client()
except Exception as e:
#tr = traceback.format_exc()
logger.warning('ERROR ACCURED',exc_info=e)
input()