Upload shibber.py
Browse files- shibber.py +207 -0
shibber.py
ADDED
@@ -0,0 +1,207 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import hashlib
|
2 |
+
import json
|
3 |
+
import requests
|
4 |
+
import time
|
5 |
+
import numpy as np
|
6 |
+
import threading
|
7 |
+
import logging
|
8 |
+
import yaml
|
9 |
+
from web3 import Web3
|
10 |
+
from bayes_opt import BayesianOptimization
|
11 |
+
from deap import base, creator, tools
|
12 |
+
import smtplib
|
13 |
+
from email.mime.text import MIMEText
|
14 |
+
|
15 |
+
# Configure logging
|
16 |
+
logging.basicConfig(filename='shib_miner.log', level=logging.INFO,
|
17 |
+
format='%(asctime)s - %(levelname)s - %(message)s')
|
18 |
+
|
19 |
+
# Email notification function
|
20 |
+
def send_email(subject, body):
|
21 |
+
"""Send email notifications."""
|
22 |
+
sender_email = "[email protected]"
|
23 |
+
receiver_email = "[email protected]"
|
24 |
+
password = "your_email_password" # Use environment variables for security
|
25 |
+
|
26 |
+
msg = MIMEText(body)
|
27 |
+
msg['Subject'] = subject
|
28 |
+
msg['From'] = sender_email
|
29 |
+
msg['To'] = receiver_email
|
30 |
+
|
31 |
+
with smtplib.SMTP('smtp.gmail.com', 587) as server:
|
32 |
+
server.starttls()
|
33 |
+
server.login(sender_email, password)
|
34 |
+
server.send_message(msg)
|
35 |
+
logging.info("Notification email sent.")
|
36 |
+
|
37 |
+
# SHIB Miner Class
|
38 |
+
class ShibMiner:
|
39 |
+
def __init__(self, wallet_address, mining_pool_url, eth_provider_url, config):
|
40 |
+
self.wallet_address = wallet_address
|
41 |
+
self.mining_pool_url = mining_pool_url
|
42 |
+
self.web3 = Web3(Web3.HTTPProvider(eth_provider_url))
|
43 |
+
self.difficulty = None
|
44 |
+
self.nonce = 0
|
45 |
+
self.best_solution = None
|
46 |
+
self.hash_rates = []
|
47 |
+
self.max_threads = config['max_threads']
|
48 |
+
self.thread_lock = threading.Lock()
|
49 |
+
|
50 |
+
def get_pool_data(self):
|
51 |
+
"""Fetch pool data including difficulty."""
|
52 |
+
try:
|
53 |
+
response = requests.get(f'{self.mining_pool_url}/getDifficulty')
|
54 |
+
if response.status_code == 200:
|
55 |
+
self.difficulty = response.json()['difficulty']
|
56 |
+
logging.info(f"Retrieved difficulty: {self.difficulty}")
|
57 |
+
else:
|
58 |
+
logging.error("Could not retrieve pool data")
|
59 |
+
except Exception as e:
|
60 |
+
logging.error(f"Error fetching pool data: {e}")
|
61 |
+
|
62 |
+
def sha256(self, data):
|
63 |
+
return hashlib.sha256(data.encode('utf-8')).hexdigest()
|
64 |
+
|
65 |
+
def mine(self):
|
66 |
+
"""Start the mining process."""
|
67 |
+
self.get_pool_data()
|
68 |
+
block_data = f"{self.wallet_address}"
|
69 |
+
|
70 |
+
while True:
|
71 |
+
with self.thread_lock:
|
72 |
+
block_hash = self.sha256(f"{block_data}{self.nonce}")
|
73 |
+
if int(block_hash, 16) < self.difficulty:
|
74 |
+
logging.info(f'Mined a block: {block_hash} with nonce: {self.nonce}')
|
75 |
+
self.submit_work(block_hash, self.nonce)
|
76 |
+
break
|
77 |
+
self.nonce += 1
|
78 |
+
|
79 |
+
def submit_work(self, block_hash, nonce):
|
80 |
+
"""Submit mined work to the pool."""
|
81 |
+
payload = {
|
82 |
+
'wallet': self.wallet_address,
|
83 |
+
'blockHash': block_hash,
|
84 |
+
'nonce': nonce,
|
85 |
+
}
|
86 |
+
try:
|
87 |
+
response = requests.post(f'{self.mining_pool_url}/submitWork', json=payload)
|
88 |
+
if response.status_code == 200:
|
89 |
+
logging.info("Work submitted successfully.")
|
90 |
+
send_email("Mining Success", f"Successfully mined block: {block_hash}")
|
91 |
+
else:
|
92 |
+
logging.error(f"Error submitting work: {response.json()}")
|
93 |
+
# Retry logic
|
94 |
+
self.retry_submission(payload)
|
95 |
+
except Exception as e:
|
96 |
+
logging.error(f"Error submitting work: {e}")
|
97 |
+
|
98 |
+
def retry_submission(self, payload, attempts=3):
|
99 |
+
"""Retry submission of mined work."""
|
100 |
+
for attempt in range(attempts):
|
101 |
+
try:
|
102 |
+
response = requests.post(f'{self.mining_pool_url}/submitWork', json=payload)
|
103 |
+
if response.status_code == 200:
|
104 |
+
logging.info("Work submitted successfully on retry.")
|
105 |
+
send_email("Mining Success", f"Successfully mined block on retry: {payload['blockHash']}")
|
106 |
+
return
|
107 |
+
except Exception as e:
|
108 |
+
logging.error(f"Retry attempt {attempt + 1} failed: {e}")
|
109 |
+
time.sleep(5) # Wait before retrying
|
110 |
+
logging.error("All retry attempts failed.")
|
111 |
+
|
112 |
+
def optimize_parameters(self, iterations=10):
|
113 |
+
"""Optimize mining parameters using Bayesian Optimization."""
|
114 |
+
def black_box_function(param1, param2):
|
115 |
+
# Simulated hash rate based on parameters
|
116 |
+
return np.sin(param1) * np.cos(param2) + 0.5 # Simulated function
|
117 |
+
|
118 |
+
pbounds = {'param1': (0, 10), 'param2': (0, 10)}
|
119 |
+
optimizer = BayesianOptimization(
|
120 |
+
f=black_box_function,
|
121 |
+
pbounds=pbounds,
|
122 |
+
random_state=1,
|
123 |
+
)
|
124 |
+
optimizer.maximize(init_points=5, n_iter=iterations)
|
125 |
+
|
126 |
+
# Get the best parameters
|
127 |
+
self.best_solution = optimizer.max
|
128 |
+
logging.info("Best parameters from Bayesian Optimization: %s", self.best_solution)
|
129 |
+
|
130 |
+
def genetic_algorithm(self, iterations=10):
|
131 |
+
"""Optimize using a Genetic Algorithm."""
|
132 |
+
creator.create("FitnessMax", base.Fitness, weights=(1.0,))
|
133 |
+
creator.create("Individual", list, fitness=creator.FitnessMax)
|
134 |
+
|
135 |
+
toolbox = base.Toolbox()
|
136 |
+
toolbox.register("attr_float", np.random.uniform, 0, 10)
|
137 |
+
toolbox.register("individual", tools.initRepeat, creator.Individual, toolbox.attr_float, n=2)
|
138 |
+
toolbox.register("population", tools.initRepeat, list, toolbox.individual)
|
139 |
+
|
140 |
+
def eval_individual(individual):
|
141 |
+
return (np.sin(individual[0]) * np.cos(individual[1]) + 0.5,)
|
142 |
+
|
143 |
+
toolbox.register("evaluate", eval_individual)
|
144 |
+
toolbox.register("mate", tools.cxBlend, alpha=0.5)
|
145 |
+
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=1, indpb=0.2)
|
146 |
+
toolbox.register("select", tools.selTournament, tournsize=3)
|
147 |
+
|
148 |
+
population = toolbox.population(n=50)
|
149 |
+
for gen in range(iterations):
|
150 |
+
offspring = toolbox.select(population, len(population))
|
151 |
+
offspring = list(map(toolbox.clone, offspring))
|
152 |
+
|
153 |
+
for child1, child2 in zip(offspring[::2], offspring[1::2]):
|
154 |
+
if np.random.rand() < 0.5:
|
155 |
+
toolbox.mate(child1, child2)
|
156 |
+
del child1.fitness.values
|
157 |
+
del child2.fitness.values
|
158 |
+
|
159 |
+
for mutant in offspring:
|
160 |
+
if np.random.rand() < 0.2:
|
161 |
+
toolbox.mutate(mutant)
|
162 |
+
del mutant.fitness.values
|
163 |
+
|
164 |
+
invalid_ind = [ind for ind in offspring if not ind.fitness.valid]
|
165 |
+
fitnesses = map(toolbox.evaluate, invalid_ind)
|
166 |
+
for ind, fit in zip(invalid_ind, fitnesses):
|
167 |
+
ind.fitness.values = fit
|
168 |
+
|
169 |
+
population[:] = offspring
|
170 |
+
|
171 |
+
fits = [ind.fitness.values[0] for ind in population]
|
172 |
+
self.best_solution = population[np.argmax(fits)]
|
173 |
+
logging.info("Best parameters from Genetic Algorithm: %s", self.best_solution)
|
174 |
+
|
175 |
+
def trinary_qubit_optimization(self):
|
176 |
+
"""Simulated Trinary Qubit Optimization."""
|
177 |
+
# A placeholder for trinary qubit logic
|
178 |
+
qubits = [np.random.choice([0, 1, 2]) for _ in range(10)]
|
179 |
+
logging.info("Simulated Trinary Qubits: %s", qubits)
|
180 |
+
|
181 |
+
def start_mining_threads(self):
|
182 |
+
"""Start multiple mining threads."""
|
183 |
+
threads = []
|
184 |
+
for _ in range(self.max_threads):
|
185 |
+
thread = threading.Thread(target=self.mine)
|
186 |
+
thread.start()
|
187 |
+
threads.append(thread)
|
188 |
+
|
189 |
+
for thread in threads:
|
190 |
+
thread.join()
|
191 |
+
|
192 |
+
if __name__ == "__main__":
|
193 |
+
|
194 |
+
|
195 |
+
wallet_address = config['wallet_address']
|
196 |
+
mining_pool_url = config['mining_pool_url']
|
197 |
+
eth_provider_url = config['eth_provider_url']
|
198 |
+
|
199 |
+
miner = ShibMiner(wallet_address, mining_pool_url, eth_provider_url, config)
|
200 |
+
|
201 |
+
# Perform optimizations
|
202 |
+
miner.optimize_parameters(iterations=config['bayesian_iterations'])
|
203 |
+
miner.genetic_algorithm(iterations=config['ga_iterations'])
|
204 |
+
miner.trinary_qubit_optimization()
|
205 |
+
|
206 |
+
# Start mining with multiple threads
|
207 |
+
miner.start_mining_threads()
|