-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSoloMiner.py
200 lines (174 loc) · 7.43 KB
/
SoloMiner.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
import socket
import json
import hashlib
import struct
import time
import multiprocessing
import os
def get_input(prompt, data_type=str):
while True:
try:
value = data_type(input(prompt))
return value
except ValueError:
print(f"Invalid input. Please enter a valid {data_type.__name__}.")
if os.path.isfile('config.json'):
print("config.json found,start mining")
with open('config.json','r') as file:
config = json.load(file)
pool_address = config['pool_address']
pool_port = config["pool_port"]
username = config["user_name"]
password = config["password"]
min_diff = config["min_diff"]
else:
print("config.json doesn't exist,generating now")
pool_address = get_input("Enter the pool address: ")
pool_port = get_input("Enter the pool port: ", int)
user_name = get_input("Enter the user name: ")
password = get_input("Enter the password: ")
min_diff = get_input("Enter the minimum difficulty: ", float)
config_data = {
"pool_address": pool_address,
"pool_port": pool_port,
"user_name": user_name,
"password": password,
"min_diff": min_diff
}
with open("config.json", "w") as config_file:
json.dump(config_data, config_file, indent=4)
print("Configuration data has been written to config.json")
def connect_to_pool(pool_address, pool_port, timeout=30, retries=5):
for attempt in range(retries):
try:
print(f"Attempting to connect to pool (Attempt {attempt + 1}/{retries})...")
sock = socket.create_connection((pool_address, pool_port), timeout)
print("Connected to pool!")
return sock
except socket.gaierror as e:
print(f"Address-related error connecting to server: {e}")
except socket.timeout as e:
print(f"Connection timed out: {e}")
except socket.error as e:
print(f"Socket error: {e}")
print(f"Retrying in 5 seconds...")
time.sleep(5)
raise Exception("Failed to connect to the pool after multiple attempts")
def send_message(sock, message):
print(f"Sending message: {message}")
sock.sendall((json.dumps(message) + '\n').encode('utf-8'))
def receive_messages(sock, timeout=30):
buffer = b''
sock.settimeout(timeout)
while True:
try:
chunk = sock.recv(1024)
if not chunk:
break
buffer += chunk
while b'\n' in buffer:
line, buffer = buffer.split(b'\n', 1)
print(f"Received message: {line.decode('utf-8')}")
yield json.loads(line.decode('utf-8'))
except socket.timeout:
print("Receive operation timed out. Retrying...")
continue
def subscribe(sock):
message = {
"id": 1,
"method": "mining.subscribe",
"params": []
}
send_message(sock, message)
for response in receive_messages(sock):
if response['id'] == 1:
print(f"Subscribe response: {response}")
return response['result']
def authorize(sock, username, password):
message = {
"id": 2,
"method": "mining.authorize",
"params": [username, password]
}
send_message(sock, message)
for response in receive_messages(sock):
if response['id'] == 2:
print(f"Authorize response: {response}")
return response['result']
def calculate_difficulty(hash_result):
hash_int = int.from_bytes(hash_result[::-1], byteorder='big')
max_target = 0xffff * (2**208)
difficulty = max_target / hash_int
return difficulty
def mine_worker(job, target, extranonce1, extranonce2_size, nonce_start, nonce_end, result_queue, stop_event):
job_id, prevhash, coinb1, coinb2, merkle_branch, version, nbits, ntime, clean_jobs = job
extranonce2 = struct.pack('<Q', 0)[:extranonce2_size]
coinbase = (coinb1 + extranonce1 + extranonce2.hex() + coinb2).encode('utf-8')
coinbase_hash_bin = hashlib.sha256(hashlib.sha256(coinbase).digest()).digest()
merkle_root = coinbase_hash_bin
for branch in merkle_branch:
merkle_root = hashlib.sha256(hashlib.sha256((merkle_root + bytes.fromhex(branch))).digest()).digest()
block_header = (version + prevhash + merkle_root[::-1].hex() + ntime + nbits).encode('utf-8')
target_bin = bytes.fromhex(target)[::-1]
for nonce in range(nonce_start, nonce_end):
if stop_event.is_set():
return
nonce_bin = struct.pack('<I', nonce)
hash_result = hashlib.sha256(hashlib.sha256(hashlib.sha256(hashlib.sha256(block_header + nonce_bin).digest()).digest()).digest()).digest()
if hash_result[::-1] < target_bin:
difficulty = calculate_difficulty(hash_result)
if difficulty > min_diff:
print(f"Nonce found: {nonce}, Difficulty: {difficulty}")
print(f"Hash: {hash_result[::-1].hex()}")
result_queue.put((job_id, extranonce2, ntime, nonce))
stop_event.set()
return
def mine(sock, job, target, extranonce1, extranonce2_size):
num_processes = multiprocessing.cpu_count()
nonce_range = 2**32 // num_processes
result_queue = multiprocessing.Queue()
stop_event = multiprocessing.Event()
while not stop_event.is_set():
processes = []
for i in range(num_processes):
nonce_start = i * nonce_range
nonce_end = (i + 1) * nonce_range
p = multiprocessing.Process(target=mine_worker, args=(job, target, extranonce1, extranonce2_size, nonce_start, nonce_end, result_queue, stop_event))
processes.append(p)
p.start()
for p in processes:
p.join()
if not result_queue.empty():
return result_queue.get()
def submit_solution(sock, job_id, extranonce2, ntime, nonce):
message = {
"id": 4,
"method": "mining.submit",
"params": [username, job_id, extranonce2.hex(), ntime, struct.pack('<I', nonce).hex()]
}
send_message(sock, message)
for response in receive_messages(sock):
if response['id'] == 4:
print("Submission response:", response)
if response['result'] == False and response['error']['code'] == 23:
print(f"Low difficulty share: {response['error']['message']}")
return
if __name__ == "__main__":
if pool_address.startswith("stratum+tcp://"):
pool_address = pool_address[len("stratum+tcp://"):]
while True:
try:
sock = connect_to_pool(pool_address, pool_port)
extranonce = subscribe(sock)
extranonce1, extranonce2_size = extranonce[1], extranonce[2]
authorize(sock, username, password)
while True:
for response in receive_messages(sock):
if response['method'] == 'mining.notify':
job = response['params']
result = mine(sock, job, job[6], extranonce1, extranonce2_size)
if result:
submit_solution(sock, *result)
except Exception as e:
print(f"An error occurred: {e}. Reconnecting...")
time.sleep(5)