1+ #Setting
12# Mining Address **Change Me**
23address = 'bc1qwp44lvxgrhh42de507kezjspcyh8cvw6tvuykp'
3- # Mining Pool
4+ # Mining Pool **Consider Before Change**
45pool = "solo.ckpool.org"
56port = 3333
67
1011import json
1112import hashlib
1213import binascii
13- import logging
1414import random
1515import time
1616import traceback
1717import context as ctx
18- import psutil
1918from datetime import datetime
2019from signal import SIGINT , signal
2120from colorama import Back , Fore , Style
22- from tabulate import tabulate
23- from tqdm import tqdm
2421
2522sock = None
2623best_difficulty = 0
@@ -52,7 +49,7 @@ def show_loading_splash():
5249⠀⠀⠀⠙⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠋⠀⠀⠀
5350⠀⠀⠀⠀⠀⠙⠻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠋⠀⠀⠀⠀⠀
5451⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠻⠿⢿⣿⣿⣿⣿⡿⠿⠟⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀
55- WE ARE ALL SATOSHI
52+ M I N I N G
5653 B I T C O I N
5754 """
5855 # ANSI escape code for orange text
@@ -104,16 +101,6 @@ def handler(signal_received, frame):
104101 ctx .fShutdown = True
105102 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .YELLOW , 'Force Close, Please Wait..' )
106103
107-
108- # Define the logger with the desired format
109- logging .basicConfig (level = logging .INFO , filename = "miner.log" , format = '%(asctime)s %(message)s' )
110- logger = logging .getLogger ("miner_logger" )
111-
112- # Update the logg function to use the logger
113- def logg (msg ):
114- logger .info (msg )
115-
116-
117104 ## This code is used to get the current block height of the Bitcoin network.
118105 ## The request is made to the blockchain.info API and the response is parsed to get the height field.
119106 ## The height is then converted to an integer and returned.
@@ -158,9 +145,7 @@ def thread_handler(self, arg, n):
158145 try :
159146 self .thread_handler2 (arg )
160147 except Exception as e :
161- logg ("ThreadHandler()" )
162148 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .WHITE , 'ThreadHandler()' )
163- logg (str (e ))
164149 print (Fore .GREEN , str (e ))
165150 ctx .listfThreadRunning [n ] = False
166151 time .sleep (2 )
@@ -180,7 +165,6 @@ def try_exit(self):
180165 ## It will then log and print that the miner has started. It then runs a loop which checks if the miner thread is still alive and if the subscribe thread is running.
181166 ## If either of these conditions are not true, the loop will break. Otherwise,
182167 ## it will set the miner thread to be running, call the mining method on the miner thread, and set the miner thread to be not running.
183- ## If an exception occurs, it is logged and the traceback is printed, and the loop breaks.
184168# Initialize best difficulty outside the loop
185169best_difficulty = 16 # Add this line to initialize best_difficulty
186170
@@ -189,7 +173,6 @@ def bitcoin_miner(t, restarted=False):
189173 start_time = time .time () # Start time for performance metrics
190174 total_hashes = 0 # Initialize total_hashes
191175 if restarted :
192- logg ('\n [*] Bitcoin Miner restarted' )
193176 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .YELLOW , 'Solo Miner Active' )
194177 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .BLUE , '[*] Bitcoin Miner Restarted' )
195178
@@ -246,7 +229,6 @@ def bitcoin_miner(t, restarted=False):
246229
247230 # Print and log the difficulty value
248231 print (Fore .YELLOW , '[*] Diff:' , Fore .YELLOW , '[' , int (_diff ), ']' )
249- logg ('[*] Working to solve block at block height {}' .format (work_on + 1 ))
250232 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .YELLOW , '[*] Working to solve block at ' , Fore .GREEN , 'height {}' .format (work_on + 1 ))
251233
252234
@@ -256,7 +238,6 @@ def calculate_difficulty(target, hash_hex):
256238 target_int = int (target , 16 )
257239 return target_int / max (hash_int , 1 ) # Avoid division by zero
258240
259- # Enhanced metrics logging
260241 def log_metrics (start_time , nonce , hashes_computed , best_difficulty , best_hash ):
261242 elapsed_time = time .time () - start_time
262243 hash_rate = hashes_computed / max (elapsed_time , 1 ) # Avoid division by zero
@@ -274,7 +255,6 @@ def log_metrics(start_time, nonce, hashes_computed, best_difficulty, best_hash):
274255 # Initialize hash_rate
275256 hash_rate = 0.0
276257
277- ## Modify bitcoin_miner function to include improved difficulty analysis and metrics logging
278258 ## This code is a while loop which checks if the thread should be shut down and if so, it breaks out of the loop.
279259 ## It then checks if a new block has been detected and if so, it logs and prints that a new block has been detected,
280260 ## logs and prints the difficulty of the block, restarts the bitcoin miner, and continues the loop.
@@ -284,10 +264,8 @@ def log_metrics(start_time, nonce, hashes_computed, best_difficulty, best_hash):
284264 break
285265
286266 if ctx .prevhash != ctx .updatedPrevHash :
287- logg ('[*] NEW BLOCK {} DETECTED ON NETWORK' .format (ctx .prevhash ))
288267 print (Fore .YELLOW , '[' , timer (), ']' , Fore .MAGENTA , '[*] New block {} detected on' , Fore .BLUE ,
289268 ' network ' .format (ctx .prevhash ))
290- logg ('[*] Best difficulty previous block {} was {}' .format (work_on + 1 , ctx .nHeightDiff [work_on + 1 ]))
291269 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .GREEN , '[*] Best Diff Trying Block' , Fore .YELLOW , ' {} ' ,
292270 Fore .BLUE , 'was {}' .format (work_on + 1 , ctx .nHeightDiff [work_on + 1 ]))
293271 ctx .updatedPrevHash = ctx .prevhash
@@ -324,7 +302,6 @@ def log_metrics(start_time, nonce, hashes_computed, best_difficulty, best_hash):
324302
325303 # Check if the current hash meets or exceeds the target difficulty
326304 if this_hash <= int (target_difficulty , 16 ):
327- logg (f'[*] New hash: { hash } for block { work_on + 1 } ' )
328305 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .GREEN , f'[*] New hash: { hash } for block' , Fore .YELLOW , work_on + 1 )
329306 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .YELLOW , 'Hash:' , hash .format (work_on + 1 ))
330307
@@ -340,7 +317,6 @@ def log_metrics(start_time, nonce, hashes_computed, best_difficulty, best_hash):
340317 if difficulty > best_difficulty :
341318 best_difficulty = difficulty
342319 best_hash = hash
343- logg (f'[BEST HASH UPDATE] New best hash: { best_hash } with difficulty: { best_difficulty } ' )
344320 print (f'[BEST HASH UPDATE] New best hash: { best_hash } with difficulty: { best_difficulty } ' )
345321
346322 # Update the difficulty for the current block in the context
@@ -363,29 +339,24 @@ def log_metrics(start_time, nonce, hashes_computed, best_difficulty, best_hash):
363339 ## If the hash is less than the target, it means the block has been successfully solved and the ctx.solved flag is set to True.
364340
365341 if hash < target :
366- logg ('[*] Share found for block {}.' .format (work_on + 1 ))
367342 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .YELLOW , '[*] Share found for block {}.' .format (work_on + 1 ))
368343 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .YELLOW , 'Share:' , hash .format (work_on + 1 ))
369- logg ('[*] Block hash: {}' .format (hash ))
370344 print (Fore .YELLOW )
371345 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .YELLOW , '[*] Block hash: {}' .format (hash ))
372- logg ('[*] Blockheader: {}' .format (blockheader ))
373346 print (Fore .BLUE , '--------------~~( ' , Fore .GREEN , 'BLOCK SOLVED CHECK WALLET!' , Fore .ORANGE , ' )~~--------------' )
374347 print (Fore .YELLOW , '[*] Blockheader: {}' .format (blockheader ))
375348
376349 # Print nonce value when a new share is found
377- logg ('[*] Nonce Value: {}' .format (nonce ))
378350 print (Fore .YELLOW , '[*] Nonce Value: {}' .format (nonce ))
379-
380351 payload = bytes ('{"params": ["' + address + '", "' + ctx .job_id + '", "' + ctx .extranonce2 + '", "' + ctx .ntime + '", "' + nonce + '"], "id": 1, "method": "mining.submit"}\n ' , 'utf-8' )
381- logg ('[*] Payload: {}' .format (payload ))
382352 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .BLUE , '[*] Payload:' , Fore .GREEN , ' {}' .format (payload ))
383353 sock .sendall (payload )
384354 ret = sock .recv (1024 )
385- logg ('[*] Pool response: {}' .format (ret ))
386355 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .GREEN , '[*] Pool Response:' , Fore .CYAN , ' {}' .format (ret ))
387356 print (payload )
388357 block_found_splash (block_found_ascii_art )
358+ time .sleep (1 )
359+ print (Back .BLUE , Fore .WHITE , 'Donate BTC to HCMLXOX?:' , Fore .GREEN , "bc1qnk0ftxa4ep296phhnxl5lv9c2s5f8xakpcxmth" , Style .RESET_ALL )
389360 return True
390361
391362 if difficulty >= 16 :
@@ -404,7 +375,6 @@ def log_metrics(start_time, nonce, hashes_computed, best_difficulty, best_hash):
404375 response = sock .recv (1024 ).decode ()
405376
406377 # Log and print the response for monitoring purposes
407- logg ('[*] Pool response for share submission: {}' .format (response ))
408378 print (Fore .MAGENTA , '[' , timer (), ']' , Fore .GREEN , '[*] Pool Response for share submission:' , Fore .CYAN , ' {}' .format (response ))
409379
410380 # Calculate the difficulty for the current share
@@ -414,7 +384,6 @@ def log_metrics(start_time, nonce, hashes_computed, best_difficulty, best_hash):
414384 if share_difficulty < best_share_difficulty :
415385 best_share_difficulty = share_difficulty
416386 best_share_hash = hash
417- logg (f'[BEST SHARE UPDATE] New best share hash: { best_share_hash } with difficulty: { best_share_difficulty } ' )
418387 print (f'[BEST SHARE UPDATE] New best share hash: { best_share_hash } with difficulty: { best_share_difficulty } ' )
419388
420389 # Update the difficulty for the current block in the context
@@ -448,7 +417,6 @@ def block_listener(t) :
448417 sock .sendall (b'{"params": ["' + address .encode () + b'", "x"], "id": 2, "method": "mining.authorize"}\n ' )
449418 response = b''
450419 while response .count (b'\n ' ) < 4 and not (b'mining.notify' in response ) : response += sock .recv (1024 )
451- print (response )
452420
453421 ## This code is used to parse the response from the ckpool server and get the necessary fields for the mining context (ctx).
454422 ## The response is split into individual lines and only lines that contain the 'mining.notify' string are parsed.
@@ -489,22 +457,9 @@ def block_listener(t) :
489457 # Call the splash screen function here
490458 show_loading_splash ()
491459
492- # Log the new mining context
493- logger .info (f"New Work Received from Pool:\n "
494- f"Job ID: { ctx .job_id } \n "
495- f"Previous Block Hash: { ctx .prevhash } \n "
496- f"Coinbase 1: { ctx .coinb1 } \n "
497- f"Coinbase 2: { ctx .coinb2 } \n "
498- f"Merkle Branch: { ctx .merkle_branch } \n "
499- f"Version: { ctx .version } \n "
500- f"nBits: { ctx .nbits } \n "
501- f"nTime: { ctx .ntime } \n "
502- f"Clean Jobs: { ctx .clean_jobs } " )
503-
504460 ## This code is defining a custom thread class called CoinMinerThread which is a subclass of ExitedThread.
505461 ## The class has two methods, __init__ and thread_handler2. The __init__ method is used to initialize the class and set the n attribute to 0.
506462 ## The thread_handler2 method calls the thread_bitcoin_miner method with the arg parameter. The thread_bitcoin_miner method is used to check for shutdown,
507- ## and then calls the bitcoin_miner function with the current thread object as the parameter. The result of the bitcoin_miner function is logged to the console.
508463
509464class CoinMinerThread (ExitedThread ) :
510465 def __init__ (self , arg = None ) :
@@ -518,12 +473,9 @@ def thread_bitcoin_miner(self , arg) :
518473 check_for_shutdown (self )
519474 try :
520475 ret = bitcoin_miner (self )
521- logg (Fore .MAGENTA , "[" , timer () , "] [*] Miner returned %s\n \n " % "true" if ret else "false" )
522476 print (Fore .LIGHTCYAN_EX , "[*] Miner returned %s\n \n " % "true" if ret else "false" )
523477 except Exception as e :
524- logg ("[*] Miner()" )
525478 print (Back .WHITE , Fore .MAGENTA , "[" , timer () , "]" , Fore .BLUE , "[*] Miner()" )
526- logg (e )
527479 traceback .print_exc ()
528480 ctx .listfThreadRunning [self .n ] = False
529481
@@ -533,7 +485,6 @@ def thread_bitcoin_miner(self , arg) :
533485 ## __init__ and thread_handler2. The __init__ method sets up the thread with the specified argument and sets the number of threads to 1.
534486 ## The thread_handler2 method calls the thread_new_block method with the specified argument.
535487 ## The thread_new_block method sets the thread to be running, checks for shutdown, and then calls the block_listener function. If an exception occurs,
536- ## it is logged and the traceback is printed. Finally, the thread is set to be not running.
537488
538489class NewSubscribeThread (ExitedThread ) :
539490 def __init__ (self , arg = None ) :
@@ -548,9 +499,7 @@ def thread_new_block(self , arg) :
548499 try :
549500 ret = block_listener (self )
550501 except Exception as e :
551- logg ("[*] Subscribe thread()" )
552502 print (Fore .MAGENTA , "[" , timer () , "]" , Fore .YELLOW , "[*] Subscribe thread()" )
553- logg (e )
554503 traceback .print_exc ()
555504 ctx .listfThreadRunning [self .n ] = False
556505
@@ -565,20 +514,14 @@ def thread_new_block(self , arg) :
565514def StartMining () :
566515 subscribe_t = NewSubscribeThread (None )
567516 subscribe_t .start ()
568- logg ("[*]£££ Bitcoin Solo Miner Started £££" )
569- print (Fore .BLUE , '--------------~~( ' , Fore .GREEN , 'SOLO MINER STARTED' , Fore .BLUE , ' )~~--------------' )
570517 time .sleep (4 )
571- logg ("[*] Subscribe thread started." )
572518 print (Fore .MAGENTA , "[" , timer () , "]" , Fore .GREEN , "[*] Subscribe thread started." )
573519 miner_t = CoinMinerThread (None )
574520 miner_t .start ()
575- time .sleep (1 )
576-
577521
578522if __name__ == '__main__' :
579523 # Initialize performance metrics in context
580524 ctx .total_hashes_computed = 0
581525 ctx .mining_time_per_block = []
582-
583526 signal (SIGINT , handler )
584527 StartMining ()
0 commit comments