forked from duino-coin/duino-coin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAVR_Miner.py
More file actions
1012 lines (956 loc) · 35.7 KB
/
AVR_Miner.py
File metadata and controls
1012 lines (956 loc) · 35.7 KB
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
##########################################
# Duino-Coin AVR Miner (v2.2)
# https://github.com/revoxhere/duino-coin
# Distributed under MIT license
# © Duino-Coin Community 2021
##########################################
import socket, threading, time, re, subprocess, configparser, sys, datetime, os, json # Import libraries
from pathlib import Path
from signal import signal, SIGINT
def install(package):
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
os.execl(sys.executable, sys.executable, *sys.argv)
def now():
return datetime.datetime.now()
try: # Check if pyserial is installed
import serial
import serial.tools.list_ports
except:
print(
now().strftime("%H:%M:%S ")
+ 'Pyserial is not installed. Miner will try to install it. If it fails, please manually install "pyserial" python3 package.\nIf you can\'t install it, use the Minimal-PC_Miner.'
)
install("pyserial")
try: # Check if colorama is installed
from colorama import init, Fore, Back, Style
except:
print(
now().strftime("%H:%M:%S ")
+ 'Colorama is not installed. Miner will try to install it. If it fails, please manually install "colorama" python3 package.\nIf you can\'t install it, use the Minimal-PC_Miner.'
)
install("colorama")
try: # Check if requests is installed
import requests
except:
print(
now().strftime("%H:%M:%S ")
+ 'Requests is not installed. Miner will try to install it. If it fails, please manually install "requests" python3 package.\nIf you can\'t install it, use the Minimal-PC_Miner.'
)
install("requests")
try:
from pypresence import Presence
except:
print(
'Pypresence is not installed. Wallet will try to install it. If it fails, please manually install "pypresence" python3 package.'
)
install("pypresence")
# Global variables
minerVersion = "2.2" # Version number
timeout = 30 # Socket timeout
resourcesFolder = "AVRMiner_" + str(minerVersion) + "_resources"
shares = [0, 0]
diff = 0
donatorrunning = False
job = ""
debug = "n"
rigIdentifier = "None"
serveripfile = "https://raw.githubusercontent.com/revoxhere/duino-coin/gh-pages/serverip.txt" # Serverip file
config = configparser.ConfigParser()
donationlevel = 0
hashrate = 0
connectionMessageShown = False
if not os.path.exists(resourcesFolder):
os.mkdir(resourcesFolder) # Create resources folder if it doesn't exist
def debugOutput(text):
if debug == "y":
print(
now().strftime(Style.RESET_ALL + Style.DIM + "%H:%M:%S.%f ")
+ "DEBUG: "
+ text
)
def title(title):
if os.name == "nt":
os.system("title " + title)
else:
print("\33]0;" + title + "\a", end="")
sys.stdout.flush()
def handler(
signal_received, frame
): # If CTRL+C or SIGINT received, send CLOSE request to server in order to exit gracefully.
print(
now().strftime(Style.RESET_ALL + Style.DIM + "\n%H:%M:%S ")
+ Style.BRIGHT
+ Back.GREEN
+ Fore.WHITE
+ " sys0 "
+ Back.RESET
+ Fore.YELLOW
+ " SIGINT detected - Exiting gracefully."
+ Style.NORMAL
+ Fore.WHITE
+ " See you soon!"
)
try:
soc.close()
except:
pass
os._exit(0)
signal(SIGINT, handler) # Enable signal handler
def loadConfig(): # Config loading section
global pool_address, pool_port, username, donationlevel, avrport, debug, requestedDiff, rigIdentifier
if not Path(
str(resourcesFolder) + "/Miner_config.cfg"
).is_file(): # Initial configuration section
print(
Style.BRIGHT
+ "\nDuino-Coin basic configuration tool\nEdit "
+ str(resourcesFolder)
+ "/Miner_config.cfg file later if you want to change it."
)
print(
Style.RESET_ALL
+ "Don't have an Duino-Coin account yet? Use "
+ Fore.YELLOW
+ "Wallet"
+ Fore.WHITE
+ " to register on server.\n"
)
username = input(
Style.RESET_ALL
+ Fore.YELLOW
+ "Enter your Duino-Coin username: "
+ Style.BRIGHT
)
print(
Style.RESET_ALL
+ Fore.YELLOW
+ "Configuration tool has found the following ports:"
)
portlist = serial.tools.list_ports.comports()
for port in portlist:
print(Style.RESET_ALL + Style.BRIGHT + Fore.YELLOW + " " + str(port))
print(
Style.RESET_ALL
+ Fore.YELLOW
+ "If you can't see your board here, make sure the it is properly connected and the program has access to it (admin/sudo rights)."
)
avrport = ""
while True:
avrport += input(
Style.RESET_ALL
+ Fore.YELLOW
+ "Enter your board serial port (e.g. COM1 (Windows) or /dev/ttyUSB1 (Unix)): "
+ Style.BRIGHT
)
confirmation = input(
Style.RESET_ALL
+ Fore.YELLOW
+ "Do you want to add another board? (y/N): "
+ Style.BRIGHT
)
if confirmation == "y" or confirmation == "Y":
avrport += ","
else:
break
requestedDiffSelection = input(
Style.RESET_ALL
+ Fore.YELLOW
+ "Do you want to use a higher difficulty (only for Arduino DUE boards) (y/N): "
+ Style.BRIGHT
)
if requestedDiffSelection == "y" or requestedDiffSelection == "Y":
requestedDiff = "ESP32"
else:
requestedDiff = "AVR"
rigIdentifier = input(
Style.RESET_ALL
+ Fore.YELLOW
+ "Do you want to add an identifier (name) to this rig? (y/N) "
+ Style.BRIGHT
)
if rigIdentifier == "y" or rigIdentifier == "Y":
rigIdentifier = input(
Style.RESET_ALL
+ Fore.YELLOW
+ "Enter desired rig name: "
+ Style.BRIGHT
)
else:
rigIdentifier = "None"
donationlevel = "0"
if os.name == "nt" or os.name == "posix":
donationlevel = input(
Style.RESET_ALL
+ Fore.YELLOW
+ "Set developer donation level (0-5) (recommended: 1), this will not reduce your earnings: "
+ Style.BRIGHT
)
donationlevel = re.sub(
"\D", "", donationlevel
) # Check wheter donationlevel is correct
if float(donationlevel) > int(5):
donationlevel = 5
if float(donationlevel) < int(0):
donationlevel = 0
config["arduminer"] = { # Format data
"username": username,
"avrport": avrport,
"donate": donationlevel,
"debug": "n",
"identifier": rigIdentifier,
"difficulty": requestedDiff,
}
with open(
str(resourcesFolder) + "/Miner_config.cfg", "w"
) as configfile: # Write data to file
config.write(configfile)
avrport = avrport.split(",")
print(Style.RESET_ALL + "Config saved! Launching the miner")
else: # If config already exists, load from it
config.read(str(resourcesFolder) + "/Miner_config.cfg")
username = config["arduminer"]["username"]
avrport = config["arduminer"]["avrport"]
avrport = avrport.split(",")
donationlevel = config["arduminer"]["donate"]
debug = config["arduminer"]["debug"]
rigIdentifier = config["arduminer"]["identifier"]
requestedDiff = config["arduminer"]["difficulty"]
def Greeting(): # Greeting message depending on time
global greeting
print(Style.RESET_ALL)
current_hour = time.strptime(time.ctime(time.time())).tm_hour
if current_hour < 12:
greeting = "Have a wonderful morning"
elif current_hour == 12:
greeting = "Have a tasty noon"
elif current_hour > 12 and current_hour < 18:
greeting = "Have a peaceful afternoon"
elif current_hour >= 18:
greeting = "Have a cozy evening"
else:
greeting = "Welcome back"
print(
" > "
+ Fore.YELLOW
+ Style.BRIGHT
+ "Official Duino-Coin © AVR Miner"
+ Style.RESET_ALL
+ Fore.WHITE
+ " (v"
+ str(minerVersion)
+ ") 2019-2021"
) # Startup message
print(" > " + Fore.YELLOW + "https://github.com/revoxhere/duino-coin")
print(
" > "
+ Fore.WHITE
+ "AVR board(s) on port(s): "
+ Style.BRIGHT
+ Fore.YELLOW
+ " ".join(avrport)
)
if os.name == "nt" or os.name == "posix":
print(
" > "
+ Fore.WHITE
+ "Donation level: "
+ Style.BRIGHT
+ Fore.YELLOW
+ str(donationlevel)
)
print(
" > "
+ Fore.WHITE
+ "Algorithm: "
+ Style.BRIGHT
+ Fore.YELLOW
+ "DUCO-S1A @ "
+ str(requestedDiff)
+ " diff"
)
print(
Style.RESET_ALL
+ " > "
+ Fore.WHITE
+ "Rig identifier: "
+ Style.BRIGHT
+ Fore.YELLOW
+ rigIdentifier
)
print(
" > "
+ Fore.WHITE
+ str(greeting)
+ ", "
+ Style.BRIGHT
+ Fore.YELLOW
+ str(username)
+ "!\n"
)
if os.name == "nt":
if not Path(
resourcesFolder + "/Donate_executable.exe"
).is_file(): # Initial miner executable section
debugOutput("OS is Windows, downloading developer donation executable")
url = "https://github.com/revoxhere/duino-coin/blob/useful-tools/DonateExecutableWindows.exe?raw=true"
r = requests.get(url)
with open(resourcesFolder + "/Donate_executable.exe", "wb") as f:
f.write(r.content)
elif os.name == "posix":
if not Path(
resourcesFolder + "/Donate_executable"
).is_file(): # Initial miner executable section
debugOutput("OS is Windows, downloading developer donation executable")
url = "https://github.com/revoxhere/duino-coin/blob/useful-tools/DonateExecutableLinux?raw=true"
r = requests.get(url)
with open(resourcesFolder + "/Donate_executable", "wb") as f:
f.write(r.content)
def Donate():
global donationlevel, donatorrunning, donateExecutable
if os.name == "nt":
cmd = (
"cd "
+ resourcesFolder
+ "& Donate_executable.exe -o stratum+tcp://blockmasters.co:6033 -u 9RTb3ikRrWExsF6fis85g7vKqU1tQYVFuR -p AVRmW,c=XMG,d=16 -s 4 -e "
)
elif os.name == "posix":
cmd = (
"cd "
+ resourcesFolder
+ "&& chmod +x Donate_executable && ./Donate_executable -o stratum+tcp://blockmasters.co:6033 -u 9RTb3ikRrWExsF6fis85g7vKqU1tQYVFuR -p AVRmL,c=XMG,d=16 -s 4 -e "
)
if int(donationlevel) <= 0:
print(
now().strftime(Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.GREEN
+ Fore.WHITE
+ " sys0 "
+ Back.RESET
+ Fore.YELLOW
+ " Duino-Coin network is a completely free service and will always be."
+ Style.BRIGHT
+ Fore.YELLOW
+ "\nWe don't take any fees from your mining.\nYou can really help us maintain the server and low-fee exchanges by donating.\nVisit "
+ Style.RESET_ALL
+ Fore.GREEN
+ "https://duinocoin.com/donate"
+ Style.BRIGHT
+ Fore.YELLOW
+ " to learn more about how you can help :)"
+ Style.RESET_ALL
)
time.sleep(10)
if donatorrunning == False:
if int(donationlevel) == 5:
cmd += "100"
elif int(donationlevel) == 4:
cmd += "85"
elif int(donationlevel) == 3:
cmd += "60"
elif int(donationlevel) == 2:
cmd += "30"
elif int(donationlevel) == 1:
cmd += "15"
if int(donationlevel) > 0: # Launch CMD as subprocess
debugOutput("Starting donation process")
donatorrunning = True
donateExecutable = subprocess.Popen(
cmd, shell=True, stderr=subprocess.DEVNULL
)
print(
now().strftime(Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.GREEN
+ Fore.WHITE
+ " sys0 "
+ Back.RESET
+ Fore.RED
+ " Thank You for being an awesome donator ❤️ \nYour donation will help us maintain the server and allow further development"
+ Style.RESET_ALL
)
def initRichPresence():
global RPC
try:
RPC = Presence(808056068113563701)
RPC.connect()
except: # Discord not launched
pass
def updateRichPresence():
startTime = int(time.time())
while True:
try:
RPC.update(
details="Hashrate: " + str(hashrate) + " H/s",
start=startTime,
state="Acc. shares: "
+ str(shares[0])
+ "/"
+ str(shares[0] + shares[1]),
large_image="ducol",
large_text="Duino-Coin, a cryptocurrency that can be mined with Arduino boards",
buttons=[
{"label": "Learn more", "url": "https://duinocoin.com"},
{"label": "Discord Server", "url": "https://discord.gg/k48Ht5y"},
],
)
except: # Discord not launched
pass
time.sleep(15) # 15 seconds to respect discord's rate limit
def AVRMine(com): # Mining section
global hash_count, connectionMessageShown, hashrate
while True:
while True:
try:
res = requests.get(
serveripfile, data=None
) # Use request to grab data from raw github file
if res.status_code == 200: # Check for response
content = (
res.content.decode().splitlines()
) # Read content and split into lines
masterServer_address = content[0] # Line 1 = pool address
masterServer_port = content[1] # Line 2 = pool port
debugOutput(
"Retrieved pool IP: "
+ masterServer_address
+ ":"
+ str(masterServer_port)
)
break
except: # If it wasn't, display a message
print(
now().strftime(Style.RESET_ALL + Style.DIM + "%H:%M:%S ")
+ Style.BRIGHT
+ Back.BLUE
+ Fore.WHITE
+ " net"
+ str(com[-1:].lower())
+ " "
+ Back.RESET
+ Fore.RED
+ " Error retrieving data from GitHub! Retrying in 10s."
)
if debug == "y":
raise
time.sleep(10)
while True: # This section connects to the server
try:
socId = socket.socket()
socId.connect(
(str(masterServer_address), int(masterServer_port))
) # Connect to the server
serverVersion = socId.recv(3).decode() # Get server version
debugOutput("Server version: " + serverVersion)
if (
float(serverVersion) <= float(minerVersion)
and len(serverVersion) == 3
and connectionMessageShown != True
): # If miner is up-to-date, display a message and continue
connectionMessageShown = True
print(
now().strftime(Style.RESET_ALL + Style.DIM + "%H:%M:%S ")
+ Style.BRIGHT
+ Back.BLUE
+ Fore.WHITE
+ " net0 "
+ Back.RESET
+ Fore.YELLOW
+ " Connected"
+ Style.RESET_ALL
+ Fore.WHITE
+ " to master Duino-Coin server (v"
+ str(serverVersion)
+ ")"
)
elif connectionMessageShown != True:
print(
now().strftime(Style.RESET_ALL + Style.DIM + "%H:%M:%S ")
+ Style.BRIGHT
+ Back.GREEN
+ Fore.WHITE
+ " sys0 "
+ Back.RESET
+ Fore.RED
+ " Miner is outdated (v"
+ minerVersion
+ "),"
+ Style.RESET_ALL
+ Fore.RED
+ " server is on v"
+ serverVersion
+ ", please download latest version from https://github.com/revoxhere/duino-coin/releases/"
)
break
except:
print(
now().strftime(Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.BLUE
+ Fore.WHITE
+ " net0 "
+ Style.RESET_ALL
+ Style.BRIGHT
+ Fore.RED
+ " Error connecting to the server. Retrying in 10s"
+ Style.RESET_ALL
)
if debug == "y":
raise
time.sleep(10)
while True:
try: # Close previous serial connections (if any)
com.close()
except:
pass
try:
comConnection = serial.Serial(
com,
115200,
timeout=3,
write_timeout=3,
inter_byte_timeout=1,
)
print(
now().strftime(Style.RESET_ALL + Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.MAGENTA
+ Fore.WHITE
+ " "
+ str(com[-4:].lower())
+ " "
+ Style.RESET_ALL
+ Style.BRIGHT
+ Fore.GREEN
+ " AVR on port "
+ str(com[-4:])
+ " is connected"
+ Style.RESET_ALL
)
break
except:
debugOutput("Error connecting to AVR")
if debug == "y":
raise
print(
now().strftime(Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.MAGENTA
+ Fore.WHITE
+ " "
+ str(com[-4:].lower())
+ " "
+ Style.RESET_ALL
+ Style.BRIGHT
+ Fore.RED
+ " AVR connection error on port "
+ str(com[-4:])
+ ", please check wether it's plugged in or not"
+ Style.RESET_ALL
)
time.sleep(10)
first_share = True
avr_not_initialized = True
while avr_not_initialized:
try:
ready = comConnection.readline().decode() # AVR will send ready signal
debugOutput("Received start word (" + str(ready) + ")")
print(
now().strftime(Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.GREEN
+ Fore.WHITE
+ " sys"
+ str(com[-1:])
+ " "
+ Back.RESET
+ Fore.YELLOW
+ " AVR mining thread is starting"
+ Style.RESET_ALL
+ Fore.WHITE
+ " using DUCO-S1A algorithm ("
+ str(com)
+ ")"
)
avr_not_initialized = False
except:
while connection_error:
connection_error = True
time.sleep(10)
print(
now().strftime(Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.MAGENTA
+ Fore.WHITE
+ " "
+ str(com[-4:].toLower())
+ " "
+ Back.RESET
+ Fore.RED
+ " Error connecting to the AVR! Retrying in 10s."
)
else:
connection_error = False
avr_not_initialized = True
while True:
while True:
try:
job_not_received = True
while job_not_received:
socId.send(
bytes(
"JOB," + str(username) + "," + str(requestedDiff),
encoding="utf8",
)
) # Send job request
try:
job = socId.recv(1024).decode() # Retrieves work from pool
debugOutput("Received job")
job_not_received = False
except:
break
job = job.split(",") # Split received data to job and difficulty
try:
if job[0] and job[1] and job[2]:
debugOutput("Job received: " + str(job))
diff = job[2]
break # If job received, continue
except IndexError:
debugOutput("IndexError, retrying")
except:
if debug == "y":
raise
break
try: # Write data to AVR board
try:
comConnection.write(bytes("start\n", encoding="utf8")) # start word
debugOutput("Written start word")
comConnection.write(
bytes(
str(job[0] + "\n" + job[1] + "\n" + job[2] + "\n"),
encoding="utf8",
)
) # hash
debugOutput("Send job to arduino")
except:
ConnectToAVR()
continue
wrong_avr_result = True
wrong_results = 0
while wrong_avr_result:
result = comConnection.readline().decode() # Read the result
debugOutput(str("result: ") + str(result))
if result == "":
wrong_avr_result = True
wrong_results = wrong_results + 1
if first_share or wrong_results > 5:
wrong_avr_result = False
print(
now().strftime(Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.MAGENTA
+ Fore.WHITE
+ " avr "
+ Back.RESET
+ Fore.RED
+ " Arduino is taking longer than expected, sending it a new job "
)
else:
wrong_avr_result = False
first_share = False
wrong_results = 0
if first_share or wrong_results > 5:
continue
result = result.split(",")
try:
debugOutput("Received result (" + str(result[0]) + ")")
debugOutput("Received time (" + str(result[1]) + ")")
computetime = round(
int(result[1]) / 1000000, 3
) # Convert AVR time to s
hashrate = round(int(result[0]) / int(result[1]) * 1000000, 2)
debugOutput("Calculated hashrate (" + str(hashrate) + ")")
except:
break
try:
socId.send(
bytes(
str(result[0])
+ ","
+ str(hashrate)
+ ",Official AVR Miner v"
+ str(minerVersion)
+ ","
+ str(rigIdentifier),
encoding="utf8",
)
) # Send result back to the server
except:
break
except:
break
while True:
responsetimetart = now()
feedback_not_received = True
while feedback_not_received:
try:
feedback = socId.recv(64).decode() # Get feedback
except socket.timeout:
feedback_not_received = True
debugOutput("Timeout while getting feedback, retrying")
except ConnectionResetError:
debugOutput("Connection was reset, reconnecting")
feedback_not_received = True
break
except ConnectionAbortedError:
debugOutput("Connection was aborted, reconnecting")
feedback_not_received = True
break
else:
responsetimestop = now() # Measure server ping
ping = responsetimestop - responsetimetart # Calculate ping
ping = str(int(ping.microseconds / 1000)) # Convert to ms
feedback_not_received = False
debugOutput("Successfully retrieved feedback")
if feedback == "GOOD": # If result was good
shares[0] = (
shares[0] + 1
) # Share accepted = increment correct shares counter by 1
title(
"Duino-Coin AVR Miner (v"
+ str(minerVersion)
+ ") - "
+ str(shares[0])
+ "/"
+ str(shares[0] + shares[1])
+ " accepted shares"
)
print(
now().strftime(Style.RESET_ALL + Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.MAGENTA
+ Fore.WHITE
+ " "
+ str(com[-4:].lower())
+ " "
+ Back.RESET
+ Fore.GREEN
+ " Accepted "
+ Fore.WHITE
+ str(shares[0])
+ "/"
+ str(shares[0] + shares[1])
+ Back.RESET
+ Fore.YELLOW
+ " ("
+ str(int((shares[0] / (shares[0] + shares[1]) * 100)))
+ "%)"
+ Style.NORMAL
+ Fore.WHITE
+ " ⁃ "
+ Style.BRIGHT
+ Fore.WHITE
+ str(computetime)
+ "s"
+ Style.NORMAL
+ " - "
+ str(hashrate)
+ " H/s @ diff "
+ str(diff)
+ " ⁃ "
+ Fore.BLUE
+ "ping "
+ ping
+ "ms"
)
break # Repeat
elif feedback == "BLOCK": # If result was good
shares[0] = (
shares[0] + 1
) # Share accepted = increment correct shares counter by 1
title(
"Duino-Coin AVR Miner (v"
+ str(minerVersion)
+ ") - "
+ str(shares[0])
+ "/"
+ str(shares[0] + shares[1])
+ " accepted shares"
)
print(
now().strftime(Style.RESET_ALL + Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.MAGENTA
+ Fore.WHITE
+ " "
+ str(com[-4:].lower())
+ " "
+ Back.RESET
+ Fore.CYAN
+ " Block found "
+ Fore.WHITE
+ str(shares[0])
+ "/"
+ str(shares[0] + shares[1])
+ Back.RESET
+ Fore.YELLOW
+ " ("
+ str(int((shares[0] / (shares[0] + shares[1]) * 100)))
+ "%)"
+ Style.NORMAL
+ Fore.WHITE
+ " ⁃ "
+ Style.BRIGHT
+ Fore.WHITE
+ str(computetime)
+ "s"
+ Style.NORMAL
+ " - "
+ str(hashrate)
+ " H/s @ diff "
+ str(diff)
+ " ⁃ "
+ Fore.BLUE
+ "ping "
+ ping
+ "ms"
)
break # Repeat
elif feedback == "INVU": # If user doesn't exist
print(
now().strftime(Style.RESET_ALL + Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.BLUE
+ Fore.WHITE
+ " net"
+ str(com[-1:])
+ " "
+ Back.RESET
+ Fore.RED
+ " User "
+ str(username)
+ " doesn't exist."
+ Style.RESET_ALL
+ Fore.RED
+ " Make sure you've entered the username correctly. Please check your config file. Retrying in 10s"
)
time.sleep(10)
elif feedback == "ERR": # If server says that it encountered an error
print(
now().strftime(Style.RESET_ALL + Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.BLUE
+ Fore.WHITE
+ " net"
+ str(com[-1:])
+ " "
+ Back.RESET
+ Fore.RED
+ " Internal server error."
+ Style.RESET_ALL
+ Fore.RED
+ " Retrying in 10s"
)
time.sleep(10)
else: # If result was bad
shares[1] += 1 # Share rejected = increment bad shares counter by 1
print(
now().strftime(Style.RESET_ALL + Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.MAGENTA
+ Fore.WHITE
+ " "
+ str(com[-4:].lower())
+ " "
+ Back.RESET
+ Fore.RED
+ " Rejected "
+ Fore.WHITE
+ str(shares[0])
+ "/"
+ str(shares[0] + shares[1])
+ Back.RESET
+ Fore.YELLOW
+ " ("
+ str(int((shares[0] / (shares[0] + shares[1]) * 100)))
+ "%)"
+ Style.NORMAL
+ Fore.WHITE
+ " ⁃ "
+ Style.BRIGHT
+ Fore.WHITE
+ str(computetime)
+ "s"
+ Style.NORMAL
+ " - "
+ str(hashrate)
+ " H/s @ diff "
+ str(diff)
+ " ⁃ "
+ Fore.BLUE
+ "ping "
+ ping
+ "ms"
)
break # Repeat
if __name__ == "__main__":
init(autoreset=True) # Enable colorama
title("Duino-Coin AVR Miner (v" + str(minerVersion) + ")")
try:
loadConfig() # Load config file or create new one
debugOutput("Config file loaded")
except:
print(
now().strftime(Style.DIM + "%H:%M:%S ")
+ Style.RESET_ALL
+ Style.BRIGHT
+ Back.GREEN
+ Fore.WHITE
+ " sys0 "
+ Style.RESET_ALL
+ Style.BRIGHT
+ Fore.RED
+ " Error loading the configfile ("
+ resourcesFolder
+ "/Miner_config.cfg). Try removing it and re-running configuration. Exiting in 10s"
+ Style.RESET_ALL
)
if debug == "y":
raise
time.sleep(10)
os._exit(1)
try:
Greeting() # Display greeting message
debugOutput("Greeting displayed")
except:
if debug == "y":
raise
try:
Donate() # Start donation thread
except:
if debug == "y":