Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
revoxhere
GitHub Repository: revoxhere/duino-coin
Path: blob/master/Unofficial miners/Minimal_PC_Miner.py
920 views
1
#!/usr/bin/env python3
2
# Minimal version of Duino-Coin PC Miner, useful for developing own apps.
3
# Created by revox 2020-2022
4
# Modifications made by Robert Furr (robtech21) and YeahNotSewerSide
5
# Mining Pools added by mkursadulusoy - 2022-09-06
6
7
import hashlib
8
import os
9
from socket import socket
10
import sys # Only python3 included libraries
11
import time
12
from urllib.request import Request, urlopen
13
from json import loads
14
15
16
soc = socket()
17
18
19
def current_time():
20
t = time.localtime()
21
current_time = time.strftime("%H:%M:%S", t)
22
return current_time
23
24
username = input('Username?\n> ')
25
mining_key = input("Mining key? ['None' for no key]\n> ")
26
diff_choice = input(
27
'Use lower difficulty? (Y/N) [Leave empty for default of True]\n> ')
28
if diff_choice.lower == "n":
29
UseLowerDiff = False
30
else:
31
UseLowerDiff = True
32
33
def fetch_pools():
34
while True:
35
try:
36
response = loads(urlopen(Request("https://server.duinocoin.com/getPool")).read().decode())
37
NODE_ADDRESS = response["ip"]
38
NODE_PORT = response["port"]
39
40
return NODE_ADDRESS, NODE_PORT
41
except Exception as e:
42
print (f'{current_time()} : Error retrieving mining node, retrying in 15s')
43
time.sleep(15)
44
45
while True:
46
try:
47
print(f'{current_time()} : Searching for fastest connection to the server')
48
try:
49
NODE_ADDRESS, NODE_PORT = fetch_pools()
50
except Exception as e:
51
NODE_ADDRESS = "server.duinocoin.com"
52
NODE_PORT = 2813
53
print(f'{current_time()} : Using default server port and address')
54
soc.connect((str(NODE_ADDRESS), int(NODE_PORT)))
55
print(f'{current_time()} : Fastest connection found')
56
server_version = soc.recv(100).decode()
57
print (f'{current_time()} : Server Version: '+ server_version)
58
# Mining section
59
while True:
60
if UseLowerDiff:
61
# Send job request for lower diff
62
soc.send(bytes(
63
"JOB,"
64
+ str(username)
65
+ ",LOW,"
66
+ str(mining_key),
67
encoding="utf8"))
68
else:
69
# Send job request
70
soc.send(bytes(
71
"JOB,"
72
+ str(username)
73
+ ",MEDIUM,"
74
+ str(mining_key),
75
encoding="utf8"))
76
77
# Receive work
78
job = soc.recv(1024).decode().rstrip("\n")
79
# Split received data to job and difficulty
80
job = job.split(",")
81
difficulty = job[2]
82
83
hashingStartTime = time.time()
84
base_hash = hashlib.sha1(str(job[0]).encode('ascii'))
85
temp_hash = None
86
87
for result in range(100 * int(difficulty) + 1):
88
# Calculate hash with difficulty
89
temp_hash = base_hash.copy()
90
temp_hash.update(str(result).encode('ascii'))
91
ducos1 = temp_hash.hexdigest()
92
93
# If hash is even with expected hash result
94
if job[1] == ducos1:
95
hashingStopTime = time.time()
96
timeDifference = hashingStopTime - hashingStartTime
97
hashrate = result / timeDifference
98
99
# Send numeric result to the server
100
soc.send(bytes(
101
str(result)
102
+ ","
103
+ str(hashrate)
104
+ ",Minimal_PC_Miner",
105
encoding="utf8"))
106
107
# Get feedback about the result
108
feedback = soc.recv(1024).decode().rstrip("\n")
109
# If result was good
110
if feedback == "GOOD":
111
print(f'{current_time()} : Accepted share',
112
result,
113
"Hashrate",
114
int(hashrate/1000),
115
"kH/s",
116
"Difficulty",
117
difficulty)
118
break
119
# If result was incorrect
120
elif feedback == "BAD":
121
print(f'{current_time()} : Rejected share',
122
result,
123
"Hashrate",
124
int(hashrate/1000),
125
"kH/s",
126
"Difficulty",
127
difficulty)
128
break
129
130
except Exception as e:
131
print(f'{current_time()} : Error occured: ' + str(e) + ", restarting in 5s.")
132
time.sleep(5)
133
os.execl(sys.executable, sys.executable, *sys.argv)
134
135