-
Notifications
You must be signed in to change notification settings - Fork 3
/
ESPythonMiner.py
114 lines (103 loc) · 5.09 KB
/
ESPythonMiner.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
"""
Unofficial DuinoCoin miner for ESP32/ESP8266 running Micropython, based on official Python PC_Miner
Created FabioPolancoE - 2021
This miner works standalone, just like official PC_Miner, so you don't need to connect your device via Serial to a PC (Although you can)
NOTE: This requires you to enter your network name and password in order to establish a WiFi connection.
It's recommended to connect the device via Serial to a PC before starting mining to make sure you have no errors, once the device
starts to mine, you can close your serial monitor so the only thing that your miner takes from your PC is current. Note, that this is
not required, only recommended, you can make sure your miner is working in the DUCO Webwallet or SIUNUSDEV's DUCOMonitor.
"""
# Fill this variables to configure your miner
username = "enter your DUCO username here"
rigIdentifier = "ESP Device"
netname = "enter the name of your WiFi network here"
password = "enter the password of you WiFi network here"
import usocket as socket, uhashlib as hashlib, time, network, ubinascii
print("Almost all modules imported...")
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
print("Network interface activated")
if not wlan.isconnected():
print('Connecting to network...')
wlan.connect(netname, password)
while not wlan.isconnected():
print("Not connected to network, this happens commonly, retrying...")
time.sleep(3)
pass
print('Connected to WiFI! Network config:', wlan.ifconfig())
try:
import urequests as requests
print("uRequests imported")
except ImportError:
print("Looks like uRequests is no installed, trying to install with uPip, if installation fails, please install manually")
import upip
upip.install("urequests")
try:
import urequests as requests
except ImportError:
print("Maybe installation failed, please install urequests manually")
while True:
pass
def ducos1(lastBlockHash, expectedHash, difficulty): # Loop from 1 too 100*diff
hashcount = 0
for ducos1res in range(100 * int(difficulty) + 1):
ducos1 = hashlib.sha1(str(lastBlockHash + str(ducos1res)).encode("utf-8"))
ducos1 = ubinascii.hexlify(ducos1.digest()).decode() # Generate hash
time.sleep(0.005)
hashcount += 1 # Increment hash counter for hashrate calculator
# Next line was used for debugging, uncomment it to see the miner work :3
# print(str(ducos1) + " | " + str(expectedHash))
if ducos1 == expectedHash:
print("Hash found, sending for feedback...")
return [ducos1res, hashcount]
while True:
soc = socket.socket()
soc.settimeout(10)
try:
# This sections grabs pool adress and port from Duino-Coin GitHub file
serverip = requests.get("https://raw.githubusercontent.com/revoxhere/duino-coin/gh-pages/serverip.txt") # Serverip file
content = serverip.text.split("\n") # Read content and split into lines
pool_address = content[0] # Line 1 = pool address
pool_port = content[1] # Line 2 = pool port
# This section connects and logs user to the server
soc.connect((str(pool_address), int(pool_port))) # Connect to the server
server_version = soc.recv(3).decode() # Get server version
print("Server is on version", server_version)
# Mining section
while True:
soc.send(
bytes("JOB," + username + ",AVR", "utf8")
) # Send job request
job = soc.recv(1024).decode() # Get work from pool
job = job.split(",") # Split received data to job (job and difficulty)
difficulty = job[2]
hashingStartTime = time.time()
print("Calculating...")
result = ducos1(job[0], job[1], job[2])
hashingStopTime = time.time()
difference = hashingStopTime - hashingStartTime
try:
hashrate = result[1] / difference
except ZeroDivisionError:
hashrate = 0
while True:
soc.send(
bytes(str(result[0]) + "," + str(hashrate) + ",ESPython Miner," + rigIdentifier, "utf8")
) # Send result of hashing algorithm to pool
feedback = soc.recv(1024).decode() # Get feedback about the result
if feedback == "GOOD": # If result was good
print("Accepted share | Hashrate", int(hashrate/1000), "kH/s | Difficulty", difficulty)
break
elif feedback == "BLOCK":
print("Block found | Hashrate", int(hashrate/1000), "kH/s | Difficulty", difficulty)
break
elif feedback == "BAD": # If result was bad
print("Rejected share | Hashrate", int(hashrate/1000), "kH/s | Difficulty", difficulty)
break
else:
print(feedback)
except Exception as e:
# Uncomment the next line to get the full exception data
# raise
soc.close()
print("Error occured:\n\t" + str(e))