-
Notifications
You must be signed in to change notification settings - Fork 32
/
solo_miner.py
365 lines (221 loc) · 9.6 KB
/
solo_miner.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
#!/usr/bin/env python
# Copyright (c) 2021-2022 iceland
# Copyright (c) 2022-2023 Papa Crouz
# Distributed under the MIT/X11 software license, see the accompanying
# file license http://www.opensource.org/licenses/mit-license.php.
from signal import signal, SIGINT
import context as ctx
import traceback
import threading
import requests
import binascii
import hashlib
import logging
import random
import socket
import time
import json
import sys
import os
# Replace this with your Bitcoin Address
address = '1Q1Ten9ASaVMswFmvu64spJi96SojHCNWv'
def handler(signal_received, frame):
# Handle any cleanup here
ctx.fShutdown = True
print('Terminating miner, please wait..')
def logg(msg):
# basic logging
logging.basicConfig(level=logging.INFO, filename="miner.log", format='%(asctime)s %(message)s') # include timestamp
logging.info(msg)
def get_current_block_height():
# returns the current network height
r = requests.get('https://blockchain.info/latestblock')
return int(r.json()['height'])
def calculate_hashrate(nonce, last_updated):
if nonce % 1000000 == 999999:
now = time.time()
hashrate = round(1000000/(now - last_updated))
sys.stdout.write("\r%s hash/s"%(str(hashrate)))
sys.stdout.flush()
return now
else:
return last_updated
def check_for_shutdown(t):
# handle shutdown
n = t.n
if ctx.fShutdown:
if n != -1:
ctx.listfThreadRunning[n] = False
t.exit = True
class ExitedThread(threading.Thread):
def __init__(self, arg, n):
super(ExitedThread, self).__init__()
self.exit = False
self.arg = arg
self.n = n
def run(self):
self.thread_handler(self.arg, self.n)
pass
def thread_handler(self, arg, n):
while True:
check_for_shutdown(self)
if self.exit:
break
ctx.listfThreadRunning[n] = True
try:
self.thread_handler2(arg)
except Exception as e:
logg("ThreadHandler()")
logg(e)
ctx.listfThreadRunning[n] = False
time.sleep(5)
pass
def thread_handler2(self, arg):
raise NotImplementedError("must impl this func")
def check_self_shutdown(self):
check_for_shutdown(self)
def try_exit(self):
self.exit = True
ctx.listfThreadRunning[self.n] = False
pass
def bitcoin_miner(t, restarted=False):
if restarted:
logg('[*] Bitcoin Miner restarted')
time.sleep(10)
target = (ctx.nbits[2:]+'00'*(int(ctx.nbits[:2],16) - 3)).zfill(64)
ctx.extranonce2 = hex(random.randint(0,2**32-1))[2:].zfill(2*ctx.extranonce2_size) # create random
coinbase = ctx.coinb1 + ctx.extranonce1 + ctx.extranonce2 + ctx.coinb2
coinbase_hash_bin = hashlib.sha256(hashlib.sha256(binascii.unhexlify(coinbase)).digest()).digest()
merkle_root = coinbase_hash_bin
for h in ctx.merkle_branch:
merkle_root = hashlib.sha256(hashlib.sha256(merkle_root + binascii.unhexlify(h)).digest()).digest()
merkle_root = binascii.hexlify(merkle_root).decode()
#little endian
merkle_root = ''.join([merkle_root[i]+merkle_root[i+1] for i in range(0,len(merkle_root),2)][::-1])
work_on = get_current_block_height()
ctx.nHeightDiff[work_on+1] = 0
_diff = int("00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16)
logg('[*] Working to solve block with height {}'.format(work_on+1))
if len(sys.argv) > 1:
random_nonce = False
else:
random_nonce = True
nNonce = 0
last_updated = int(time.time())
while True:
t.check_self_shutdown()
if t.exit:
break
if ctx.prevhash != ctx.updatedPrevHash:
logg('[*] New block {} detected on network '.format(ctx.prevhash))
logg('[*] Best difficulty will trying to solve block {} was {}'.format(work_on+1, ctx.nHeightDiff[work_on+1]))
ctx.updatedPrevHash = ctx.prevhash
bitcoin_miner(t, restarted=True)
break
if random_nonce:
nonce = hex(random.randint(0,2**32-1))[2:].zfill(8) # nNonce #hex(int(nonce,16)+1)[2:]
else:
nonce = hex(nNonce)[2:].zfill(8)
blockheader = ctx.version + ctx.prevhash + merkle_root + ctx.ntime + ctx.nbits + nonce +\
'000000800000000000000000000000000000000000000000000000000000000000000000000000000000000080020000'
hash = hashlib.sha256(hashlib.sha256(binascii.unhexlify(blockheader)).digest()).digest()
hash = binascii.hexlify(hash).decode()
# Logg all hashes that start with 7 zeros or more
if hash.startswith('0000000'): logg('[*] New hash: {} for block {}'.format(hash, work_on+1))
this_hash = int(hash, 16)
difficulty = _diff / this_hash
if ctx.nHeightDiff[work_on+1] < difficulty:
# new best difficulty for block at x height
ctx.nHeightDiff[work_on+1] = difficulty
if not random_nonce:
# hash meter, only works with regular nonce.
last_updated = calculate_hashrate(nNonce, last_updated)
if hash < target :
logg('[*] Block {} solved.'.format(work_on+1))
logg('[*] Block hash: {}'.format(hash))
logg('[*] Blockheader: {}'.format(blockheader))
payload = bytes('{"params": ["'+address+'", "'+ctx.job_id+'", "'+ctx.extranonce2 \
+'", "'+ctx.ntime+'", "'+nonce+'"], "id": 1, "method": "mining.submit"}\n', 'utf-8')
logg('[*] Payload: {}'.format(payload))
ctx.sock.sendall(payload)
ret = ctx.sock.recv(1024)
logg('[*] Pool response: {}'.format(ret))
return True
# increment nonce by 1, in case we don't want random
nNonce +=1
def block_listener(t):
# init a connection to ckpool
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('solo.ckpool.org', 3333))
# send a handle subscribe message
sock.sendall(b'{"id": 1, "method": "mining.subscribe", "params": []}\n')
lines = sock.recv(1024).decode().split('\n')
response = json.loads(lines[0])
ctx.sub_details,ctx.extranonce1,ctx.extranonce2_size = response['result']
# send and handle authorize message
sock.sendall(b'{"params": ["'+address.encode()+b'", "password"], "id": 2, "method": "mining.authorize"}\n')
response = b''
while response.count(b'\n') < 4 and not(b'mining.notify' in response):response += sock.recv(1024)
responses = [json.loads(res) for res in response.decode().split('\n') if len(res.strip())>0 and 'mining.notify' in res]
ctx.job_id, ctx.prevhash, ctx.coinb1, ctx.coinb2, ctx.merkle_branch, ctx.version, ctx.nbits, ctx.ntime, ctx.clean_jobs = responses[0]['params']
# do this one time, will be overwriten by mining loop when new block is detected
ctx.updatedPrevHash = ctx.prevhash
# set sock
ctx.sock = sock
while True:
t.check_self_shutdown()
if t.exit:
break
# check for new block
response = b''
while response.count(b'\n') < 4 and not(b'mining.notify' in response):response += sock.recv(1024)
responses = [json.loads(res) for res in response.decode().split('\n') if len(res.strip())>0 and 'mining.notify' in res]
if responses[0]['params'][1] != ctx.prevhash:
# new block detected on network
# update context job data
ctx.job_id, ctx.prevhash, ctx.coinb1, ctx.coinb2, ctx.merkle_branch, ctx.version, ctx.nbits, ctx.ntime, ctx.clean_jobs = responses[0]['params']
class CoinMinerThread(ExitedThread):
def __init__(self, arg=None):
super(CoinMinerThread, self).__init__(arg, n=0)
def thread_handler2(self, arg):
self.thread_bitcoin_miner(arg)
def thread_bitcoin_miner(self, arg):
ctx.listfThreadRunning[self.n] = True
check_for_shutdown(self)
try:
ret = bitcoin_miner(self)
logg("[*] Miner returned %s\n\n" % "true" if ret else"false")
except Exception as e:
logg("[*] Miner()")
logg(e)
traceback.print_exc()
ctx.listfThreadRunning[self.n] = False
pass
class NewSubscribeThread(ExitedThread):
def __init__(self, arg=None):
super(NewSubscribeThread, self).__init__(arg, n=1)
def thread_handler2(self, arg):
self.thread_new_block(arg)
def thread_new_block(self, arg):
ctx.listfThreadRunning[self.n] = True
check_for_shutdown(self)
try:
ret = block_listener(self)
except Exception as e:
logg("[*] Subscribe thread()")
logg(e)
traceback.print_exc()
ctx.listfThreadRunning[self.n] = False
pass
def StartMining():
subscribe_t = NewSubscribeThread(None)
subscribe_t.start()
logg("[*] Subscribe thread started.")
time.sleep(4)
miner_t = CoinMinerThread(None)
miner_t.start()
logg("[*] Bitcoin miner thread started")
print('Bitcoin Miner started')
if __name__ == '__main__':
signal(SIGINT, handler)
StartMining()