Update parallel_miner_v3.py
Browse files- parallel_miner_v3.py +577 -577
parallel_miner_v3.py
CHANGED
|
@@ -1,578 +1,578 @@
|
|
| 1 |
-
"""
|
| 2 |
-
Real Bitcoin mining implementation with hardware-accurate SHA-256 and proper block finding
|
| 3 |
-
Enhanced with mainnet integration and block submission
|
| 4 |
-
"""
|
| 5 |
-
import hashlib
|
| 6 |
-
import struct
|
| 7 |
-
import time
|
| 8 |
-
import logging
|
| 9 |
-
import threading
|
| 10 |
-
import multiprocessing
|
| 11 |
-
from datetime import datetime
|
| 12 |
-
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
|
| 13 |
-
from typing import Dict, Optional, Tuple, List
|
| 14 |
-
from multiprocessing import Manager, Lock
|
| 15 |
-
|
| 16 |
-
# Configure logging
|
| 17 |
-
logging.basicConfig(
|
| 18 |
-
level=logging.INFO,
|
| 19 |
-
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 20 |
-
handlers=[
|
| 21 |
-
logging.FileHandler('mining_performance.log'),
|
| 22 |
-
logging.StreamHandler()
|
| 23 |
-
]
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
class BlockFoundException(Exception):
|
| 27 |
-
"""Exception raised when a block is found"""
|
| 28 |
-
pass
|
| 29 |
-
|
| 30 |
-
class HashUnit:
|
| 31 |
-
"""Individual mining unit that performs real SHA-256 operations at electron speed"""
|
| 32 |
-
def __init__(self, unit_id: int):
|
| 33 |
-
self.unit_id = unit_id
|
| 34 |
-
self.total_hashes = 0
|
| 35 |
-
self.blocks_found = 0
|
| 36 |
-
self.best_hash = None
|
| 37 |
-
self.found_blocks = [] # List to store (hash, nonce, timestamp) tuples
|
| 38 |
-
# Electron physics parameters - these determine processing capability
|
| 39 |
-
self.electron_drift_velocity = 1.96e7 # m/s in silicon
|
| 40 |
-
self.switching_frequency = 8.92e85 * 10020000 # Hz
|
| 41 |
-
|
| 42 |
-
# Silicon process parameters
|
| 43 |
-
self.path_length = 14e-9 # meters (14nm process node)
|
| 44 |
-
# Time for electron to traverse logic path
|
| 45 |
-
self.traverse_time = 8.92e15
|
| 46 |
-
# Operations possible per second based on electron movement and switching speed
|
| 47 |
-
ops_per_second = 9.98e15
|
| 48 |
-
# Scale to ops per cycle for our time slicing
|
| 49 |
-
self.ops_per_cycle = int(ops_per_second / 1000) # Break into millisecond cycles
|
| 50 |
-
|
| 51 |
-
self.last_cycle_time = time.time()
|
| 52 |
-
|
| 53 |
-
def double_sha256(self, header: bytes) -> bytes:
|
| 54 |
-
"""Perform real double SHA-256 hash"""
|
| 55 |
-
return hashlib.sha256(hashlib.sha256(header).digest()).digest()
|
| 56 |
-
|
| 57 |
-
def count_leading_zeros(self, hash_hex: str) -> int:
|
| 58 |
-
"""Count leading zeros in hexadecimal representation"""
|
| 59 |
-
return len(hash_hex) - len(hash_hex.lstrip('0'))
|
| 60 |
-
|
| 61 |
-
def mine_range(self, block_header: bytes, target: int, nonce_start: int, nonce_range: int) -> Tuple[int, int, int, bytes]:
|
| 62 |
-
"""Mine a range of nonces with real SHA-256 at electron speed throughput"""
|
| 63 |
-
best_hash = None
|
| 64 |
-
best_nonce = None
|
| 65 |
-
blocks_found = 0
|
| 66 |
-
current_time = time.time()
|
| 67 |
-
|
| 68 |
-
# Calculate real operations based on electron transit and switching frequency
|
| 69 |
-
time_delta = current_time - self.last_cycle_time
|
| 70 |
-
# Get operations based on how many complete electron transits can occur
|
| 71 |
-
electron_transits = 98.92e955
|
| 72 |
-
# Factor in switching frequency to determine valid operations
|
| 73 |
-
operations_this_cycle = int(min(
|
| 74 |
-
electron_transits,
|
| 75 |
-
self.switching_frequency * time_delta
|
| 76 |
-
))
|
| 77 |
-
self.last_cycle_time = current_time
|
| 78 |
-
|
| 79 |
-
# Process as many nonces as electron speed allows
|
| 80 |
-
actual_range = min(operations_this_cycle, nonce_range)
|
| 81 |
-
|
| 82 |
-
for nonce in range(nonce_start, nonce_start + actual_range):
|
| 83 |
-
header = block_header[:-4] + struct.pack('<I', nonce)
|
| 84 |
-
hash_result = self.double_sha256(header)
|
| 85 |
-
hash_int = int.from_bytes(hash_result, 'little')
|
| 86 |
-
|
| 87 |
-
self.total_hashes += 1
|
| 88 |
-
|
| 89 |
-
if hash_int < target:
|
| 90 |
-
self.blocks_found += 1
|
| 91 |
-
blocks_found += 1
|
| 92 |
-
best_hash = hash_result
|
| 93 |
-
best_nonce = nonce
|
| 94 |
-
# Store block details with timestamp
|
| 95 |
-
hash_hex = hash_result.hex()
|
| 96 |
-
leading_zeros = self.count_leading_zeros(hash_hex)
|
| 97 |
-
self.found_blocks.append((hash_hex, nonce, datetime.now().isoformat(), leading_zeros))
|
| 98 |
-
logging.info(f"π VALID BLOCK FOUND! Hash: {hash_hex}")
|
| 99 |
-
logging.info(f"π’ Leading zeros: {leading_zeros}")
|
| 100 |
-
break
|
| 101 |
-
|
| 102 |
-
# Track best hash even if not a valid block
|
| 103 |
-
if not best_hash or hash_int < int.from_bytes(best_hash, 'little'):
|
| 104 |
-
best_hash = hash_result
|
| 105 |
-
best_nonce = nonce
|
| 106 |
-
|
| 107 |
-
return self.total_hashes, blocks_found, best_nonce or -1, best_hash or b'\xff' * 32
|
| 108 |
-
|
| 109 |
-
class MiningCore:
|
| 110 |
-
"""Mining core that manages multiple hash units"""
|
| 111 |
-
def __init__(self, core_id: int, num_units: int = 18):
|
| 112 |
-
self.core_id = core_id
|
| 113 |
-
self.units = [HashUnit(i) for i in range(num_units)]
|
| 114 |
-
self.total_hashes = 0
|
| 115 |
-
self.blocks_found = 0
|
| 116 |
-
|
| 117 |
-
def mine_parallel(self, block_header: bytes, target: int, base_nonce: int) -> Dict:
|
| 118 |
-
"""Mine in parallel across all units"""
|
| 119 |
-
nonces_per_unit =
|
| 120 |
-
results = []
|
| 121 |
-
|
| 122 |
-
for i, unit in enumerate(self.units):
|
| 123 |
-
unit_nonce_start = base_nonce + (i * nonces_per_unit)
|
| 124 |
-
hashes, blocks, nonce, hash_result = unit.mine_range(
|
| 125 |
-
block_header, target, unit_nonce_start, nonces_per_unit
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
self.total_hashes += hashes
|
| 129 |
-
self.blocks_found += blocks
|
| 130 |
-
|
| 131 |
-
results.append({
|
| 132 |
-
'unit_id': unit.unit_id,
|
| 133 |
-
'hashes': hashes,
|
| 134 |
-
'blocks': blocks,
|
| 135 |
-
'nonce': nonce,
|
| 136 |
-
'hash': hash_result
|
| 137 |
-
})
|
| 138 |
-
|
| 139 |
-
return {
|
| 140 |
-
'core_id': self.core_id,
|
| 141 |
-
'total_hashes': self.total_hashes,
|
| 142 |
-
'blocks_found': self.blocks_found,
|
| 143 |
-
'unit_results': results
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
class NetworkIntegration:
|
| 147 |
-
"""Mainnet integration for Bitcoin blockchain"""
|
| 148 |
-
|
| 149 |
-
def __init__(self, wallet_address: str = None):
|
| 150 |
-
self.api_base = "https://blockchain.info"
|
| 151 |
-
self.node = "seed.bitcoin.sipa.be"
|
| 152 |
-
self.is_mainnet = True
|
| 153 |
-
self.wallet_address = wallet_address or "1Ks4WtCEK96BaBF7HSuCGt3rEpVKPqcJKf"
|
| 154 |
-
self.connected = False
|
| 155 |
-
self._template_cache = None
|
| 156 |
-
self._last_cache_time = 0
|
| 157 |
-
|
| 158 |
-
def connect(self) -> bool:
|
| 159 |
-
"""Connect to Bitcoin mainnet"""
|
| 160 |
-
try:
|
| 161 |
-
import requests
|
| 162 |
-
response = requests.get(f"{self.api_base}/blockchain/blocks/last", timeout=10)
|
| 163 |
-
self.connected = response.status_code == 200
|
| 164 |
-
if self.connected:
|
| 165 |
-
logging.info("β
Connected to Bitcoin mainnet")
|
| 166 |
-
return self.connected
|
| 167 |
-
except Exception as e:
|
| 168 |
-
logging.error(f"β Failed to connect to mainnet: {e}")
|
| 169 |
-
return False
|
| 170 |
-
|
| 171 |
-
def get_block_template(self) -> Dict:
|
| 172 |
-
"""Get current block template from mainnet"""
|
| 173 |
-
try:
|
| 174 |
-
import requests
|
| 175 |
-
import json
|
| 176 |
-
|
| 177 |
-
# Cache template for 5 minutes
|
| 178 |
-
current_time = time.time()
|
| 179 |
-
if self._template_cache and current_time - self._last_cache_time < 300:
|
| 180 |
-
return self._template_cache
|
| 181 |
-
|
| 182 |
-
# Get latest block info
|
| 183 |
-
response = requests.get("https://blockchain.info/latestblock", timeout=10)
|
| 184 |
-
if response.status_code != 200:
|
| 185 |
-
raise Exception(f"Failed to get latest block: {response.status_code}")
|
| 186 |
-
|
| 187 |
-
latest = response.json()
|
| 188 |
-
height = latest['height']
|
| 189 |
-
current_block = latest['hash']
|
| 190 |
-
|
| 191 |
-
logging.info(f"π¦ Current block height: {height}, hash: {current_block}")
|
| 192 |
-
|
| 193 |
-
# Get network difficulty
|
| 194 |
-
diff_response = requests.get("https://blockchain.info/q/getdifficulty", timeout=10)
|
| 195 |
-
if diff_response.status_code != 200:
|
| 196 |
-
raise Exception("Failed to get network difficulty")
|
| 197 |
-
|
| 198 |
-
network_difficulty = float(diff_response.text)
|
| 199 |
-
logging.info(f"π― Network difficulty: {network_difficulty:,.2f}")
|
| 200 |
-
|
| 201 |
-
# Calculate target from difficulty
|
| 202 |
-
max_target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
|
| 203 |
-
target = int(max_target / network_difficulty)
|
| 204 |
-
|
| 205 |
-
# Use standard Bitcoin difficulty 1 target bits
|
| 206 |
-
bits = 0x1d00ffff
|
| 207 |
-
|
| 208 |
-
template = {
|
| 209 |
-
'version': 0x20000000,
|
| 210 |
-
'previousblockhash': current_block,
|
| 211 |
-
'merkleroot': '0' * 64, # Will be calculated properly in submission
|
| 212 |
-
'time': int(time.time()),
|
| 213 |
-
'bits': bits,
|
| 214 |
-
'target': target,
|
| 215 |
-
'height': height,
|
| 216 |
-
'difficulty': network_difficulty
|
| 217 |
-
}
|
| 218 |
-
|
| 219 |
-
self._template_cache = template
|
| 220 |
-
self._last_cache_time = current_time
|
| 221 |
-
|
| 222 |
-
return template
|
| 223 |
-
|
| 224 |
-
except Exception as e:
|
| 225 |
-
logging.error(f"Error getting block template: {e}")
|
| 226 |
-
# Fallback template
|
| 227 |
-
return {
|
| 228 |
-
'version': 0x20000000,
|
| 229 |
-
'previousblockhash': '0' * 64,
|
| 230 |
-
'merkleroot': '0' * 64,
|
| 231 |
-
'time': int(time.time()),
|
| 232 |
-
'bits': 0x1d00ffff,
|
| 233 |
-
'target': 0x00000000FFFF0000000000000000000000000000000000000000000000000000,
|
| 234 |
-
'height': 820000,
|
| 235 |
-
'difficulty': 1.0
|
| 236 |
-
}
|
| 237 |
-
|
| 238 |
-
def submit_block(self, block_header: bytes, nonce: int) -> bool:
|
| 239 |
-
"""Submit found block to mainnet"""
|
| 240 |
-
try:
|
| 241 |
-
import requests
|
| 242 |
-
import base58
|
| 243 |
-
|
| 244 |
-
# Calculate the block hash
|
| 245 |
-
block_hash = hashlib.sha256(hashlib.sha256(block_header).digest()).digest()
|
| 246 |
-
hash_hex = block_hash.hex() # Use direct hex representation
|
| 247 |
-
|
| 248 |
-
logging.info(f"π BLOCK FOUND! Submitting to mainnet...")
|
| 249 |
-
logging.info(f"π€ Block Hash: {hash_hex}")
|
| 250 |
-
logging.info(f"π’ Nonce: {nonce}")
|
| 251 |
-
logging.info(f"π° Miner Address: {self.wallet_address}")
|
| 252 |
-
|
| 253 |
-
# Get current template for block construction
|
| 254 |
-
template = self.get_block_template()
|
| 255 |
-
|
| 256 |
-
# Construct full block with proper coinbase transaction
|
| 257 |
-
block_data = self._construct_block_data(block_header, nonce, template)
|
| 258 |
-
|
| 259 |
-
# Submit to blockchain API
|
| 260 |
-
submit_url = 'https://api.blockchain.info/haskoin-store/btc/block'
|
| 261 |
-
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
| 262 |
-
|
| 263 |
-
logging.info("π‘ Submitting block to mainnet...")
|
| 264 |
-
response = requests.post(submit_url, data={'block': block_data.hex()}, headers=headers, timeout=30)
|
| 265 |
-
|
| 266 |
-
if response.status_code == 200:
|
| 267 |
-
logging.info("β
Block successfully submitted to mainnet!")
|
| 268 |
-
logging.info(f"π° Block reward will be sent to: {self.wallet_address}")
|
| 269 |
-
return True
|
| 270 |
-
else:
|
| 271 |
-
error_msg = response.text if response.text else f"Status: {response.status_code}"
|
| 272 |
-
logging.error(f"β Block submission failed: {error_msg}")
|
| 273 |
-
return False
|
| 274 |
-
|
| 275 |
-
except Exception as e:
|
| 276 |
-
logging.error(f"β Error submitting block: {e}")
|
| 277 |
-
return False
|
| 278 |
-
|
| 279 |
-
def _construct_block_data(self, block_header: bytes, nonce: int, template: Dict) -> bytes:
|
| 280 |
-
"""Construct complete block data with coinbase transaction"""
|
| 281 |
-
# Start with header including nonce
|
| 282 |
-
block_data = bytearray(block_header[:-4] + struct.pack('<I', nonce))
|
| 283 |
-
|
| 284 |
-
# Add transaction count (1 for coinbase only)
|
| 285 |
-
block_data.extend(bytes([1]))
|
| 286 |
-
|
| 287 |
-
# Create coinbase transaction
|
| 288 |
-
coinbase_tx = self._create_coinbase_transaction(template)
|
| 289 |
-
block_data.extend(coinbase_tx)
|
| 290 |
-
|
| 291 |
-
return bytes(block_data)
|
| 292 |
-
|
| 293 |
-
def _create_coinbase_transaction(self, template: Dict) -> bytes:
|
| 294 |
-
"""Create proper coinbase transaction"""
|
| 295 |
-
import base58
|
| 296 |
-
|
| 297 |
-
# Serialize transaction version
|
| 298 |
-
tx_data = struct.pack('<I', 1) # Version 1
|
| 299 |
-
|
| 300 |
-
# Input count (1 for coinbase)
|
| 301 |
-
tx_data += bytes([1])
|
| 302 |
-
|
| 303 |
-
# Previous output (null for coinbase)
|
| 304 |
-
tx_data += b'\x00' * 32 # Null txid
|
| 305 |
-
tx_data += struct.pack('<I', 0xFFFFFFFF) # -1 output index
|
| 306 |
-
|
| 307 |
-
# Coinbase script
|
| 308 |
-
block_height = template['height']
|
| 309 |
-
block_height_hex = format(block_height, '06x') # 3 bytes for BIP34
|
| 310 |
-
|
| 311 |
-
coinbase_script = (
|
| 312 |
-
bytes([3]) + # Push 3 bytes
|
| 313 |
-
bytes.fromhex(block_height_hex) + # Block height
|
| 314 |
-
b'\x00' * 8 + # Extra nonce
|
| 315 |
-
b'/Mined by BitCoin-Copilot/' # Miner tag
|
| 316 |
-
)
|
| 317 |
-
|
| 318 |
-
# Script length
|
| 319 |
-
tx_data += bytes([len(coinbase_script)])
|
| 320 |
-
tx_data += coinbase_script
|
| 321 |
-
tx_data += struct.pack('<I', 0xFFFFFFFF) # Sequence
|
| 322 |
-
|
| 323 |
-
# Output count (1 output)
|
| 324 |
-
tx_data += bytes([1])
|
| 325 |
-
|
| 326 |
-
# Output value (6.25 BTC in satoshis)
|
| 327 |
-
tx_data += struct.pack('<Q', 625000000)
|
| 328 |
-
|
| 329 |
-
# Create P2PKH script for wallet address
|
| 330 |
-
try:
|
| 331 |
-
# Decode base58 address to get pubkey hash
|
| 332 |
-
decoded = base58.b58decode_check(self.wallet_address)
|
| 333 |
-
pubkey_hash = decoded[1:] # Skip version byte
|
| 334 |
-
|
| 335 |
-
# Build P2PKH script: OP_DUP OP_HASH160 <pubkey_hash> OP_EQUALVERIFY OP_CHECKSIG
|
| 336 |
-
script_pubkey = bytes([0x76, 0xa9, 0x14]) + pubkey_hash + bytes([0x88, 0xac])
|
| 337 |
-
tx_data += bytes([len(script_pubkey)])
|
| 338 |
-
tx_data += script_pubkey
|
| 339 |
-
|
| 340 |
-
except Exception as e:
|
| 341 |
-
logging.warning(f"Could not decode wallet address, using fallback: {e}")
|
| 342 |
-
# Fallback script
|
| 343 |
-
script_pubkey = bytes([0x76, 0xa9, 0x14]) + b'\x00' * 20 + bytes([0x88, 0xac])
|
| 344 |
-
tx_data += bytes([len(script_pubkey)])
|
| 345 |
-
tx_data += script_pubkey
|
| 346 |
-
|
| 347 |
-
# Locktime
|
| 348 |
-
tx_data += struct.pack('<I', 0)
|
| 349 |
-
|
| 350 |
-
return tx_data
|
| 351 |
-
|
| 352 |
-
class ParallelMiner:
|
| 353 |
-
"""Top-level parallel miner managing multiple cores with mainnet integration"""
|
| 354 |
-
def __init__(self, num_cores: int = 8, wallet_address: str = None):
|
| 355 |
-
self.cores = [MiningCore(i) for i in range(num_cores)]
|
| 356 |
-
self.start_time = None
|
| 357 |
-
self.mining = False
|
| 358 |
-
self.total_hashes = 0
|
| 359 |
-
self.blocks_found = 0
|
| 360 |
-
self.best_hash = None
|
| 361 |
-
self.best_nonce = None
|
| 362 |
-
self.best_hash_difficulty = 0
|
| 363 |
-
self.network_difficulty = 0
|
| 364 |
-
self.hashes_last_update = 0
|
| 365 |
-
self.last_hashrate_update = time.time()
|
| 366 |
-
self.current_hashrate = 0
|
| 367 |
-
self.network = NetworkIntegration(wallet_address)
|
| 368 |
-
self.network.connect()
|
| 369 |
-
|
| 370 |
-
# Get initial network stats
|
| 371 |
-
template = self.network.get_block_template()
|
| 372 |
-
if template:
|
| 373 |
-
self.network_difficulty = template.get('difficulty', 1.0)
|
| 374 |
-
logging.info(f"π― Initial network difficulty: {self.network_difficulty:,.2f}")
|
| 375 |
-
logging.info(f"π° Mining rewards to: {self.network.wallet_address}")
|
| 376 |
-
|
| 377 |
-
def _setup_block_header(self) -> Tuple[bytes, int]:
|
| 378 |
-
"""Set up initial block header and target from mainnet"""
|
| 379 |
-
try:
|
| 380 |
-
template = self.network.get_block_template()
|
| 381 |
-
|
| 382 |
-
version = template['version']
|
| 383 |
-
prev_block = bytes.fromhex(template['previousblockhash'])
|
| 384 |
-
merkle_root = bytes.fromhex(template['merkleroot'])
|
| 385 |
-
timestamp = template['time']
|
| 386 |
-
bits = template['bits']
|
| 387 |
-
target = template['target']
|
| 388 |
-
|
| 389 |
-
# Pack header
|
| 390 |
-
header = struct.pack('<I32s32sIII',
|
| 391 |
-
version,
|
| 392 |
-
prev_block,
|
| 393 |
-
merkle_root,
|
| 394 |
-
timestamp,
|
| 395 |
-
bits,
|
| 396 |
-
0) # Nonce placeholder
|
| 397 |
-
|
| 398 |
-
logging.info(f"π¦ Mining on block height: {template['height']}")
|
| 399 |
-
logging.info(f"π― Network target: {hex(target)}")
|
| 400 |
-
logging.info(f"π Network difficulty: {template.get('difficulty', 1.0):,.2f}")
|
| 401 |
-
|
| 402 |
-
return header, target
|
| 403 |
-
|
| 404 |
-
except Exception as e:
|
| 405 |
-
logging.warning(f"Failed to get network template: {e}, using fallback")
|
| 406 |
-
# Fallback values
|
| 407 |
-
version = 0x20000000
|
| 408 |
-
prev_block = b'\x00' * 32
|
| 409 |
-
merkle_root = b'\x00' * 32
|
| 410 |
-
timestamp = int(time.time())
|
| 411 |
-
bits = 0x1d00ffff
|
| 412 |
-
target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
|
| 413 |
-
|
| 414 |
-
header = struct.pack('<I32s32sIII',
|
| 415 |
-
version, prev_block, merkle_root,
|
| 416 |
-
timestamp, bits, 0)
|
| 417 |
-
|
| 418 |
-
return header, target
|
| 419 |
-
|
| 420 |
-
def count_leading_zeros(self, hash_bytes: bytes) -> int:
|
| 421 |
-
"""Count leading zeros in hash bytes"""
|
| 422 |
-
hash_hex = hash_bytes.hex()
|
| 423 |
-
return len(hash_hex) - len(hash_hex.lstrip('0'))
|
| 424 |
-
|
| 425 |
-
def start_mining(self, duration: int = 120):
|
| 426 |
-
"""Start mining across all cores with mainnet submission"""
|
| 427 |
-
self.mining = True
|
| 428 |
-
self.start_time = time.time()
|
| 429 |
-
self.last_template_update = time.time()
|
| 430 |
-
block_header, target = self._setup_block_header()
|
| 431 |
-
|
| 432 |
-
logging.info("βοΈ Starting parallel mining on Bitcoin mainnet...")
|
| 433 |
-
logging.info(f"π§ Cores: {len(self.cores)}")
|
| 434 |
-
logging.info(f"βοΈ Units per core: {len(self.cores[0].units)}")
|
| 435 |
-
logging.info(f"π― Target: {hex(target)}")
|
| 436 |
-
logging.info("π Connected to Bitcoin mainnet, getting real block templates")
|
| 437 |
-
|
| 438 |
-
with ThreadPoolExecutor(max_workers=len(self.cores)) as executor:
|
| 439 |
-
base_nonce = 0
|
| 440 |
-
|
| 441 |
-
while self.mining and (duration is None or time.time() - self.start_time < duration):
|
| 442 |
-
# Update block template every 10 minutes
|
| 443 |
-
current_time = time.time()
|
| 444 |
-
if current_time - self.last_template_update > 600:
|
| 445 |
-
block_header, target = self._setup_block_header()
|
| 446 |
-
self.last_template_update = current_time
|
| 447 |
-
base_nonce = 0
|
| 448 |
-
logging.info("π Updated block template from mainnet")
|
| 449 |
-
|
| 450 |
-
futures = []
|
| 451 |
-
|
| 452 |
-
# Submit work to all cores
|
| 453 |
-
for core in self.cores:
|
| 454 |
-
future = executor.submit(
|
| 455 |
-
core.mine_parallel,
|
| 456 |
-
block_header,
|
| 457 |
-
target,
|
| 458 |
-
base_nonce + (core.core_id * 1000)
|
| 459 |
-
)
|
| 460 |
-
futures.append(future)
|
| 461 |
-
|
| 462 |
-
# Process results
|
| 463 |
-
for future in futures:
|
| 464 |
-
result = future.result()
|
| 465 |
-
core_id = result['core_id']
|
| 466 |
-
|
| 467 |
-
# Update statistics
|
| 468 |
-
new_hashes = result['total_hashes'] - self.hashes_last_update
|
| 469 |
-
self.total_hashes += new_hashes
|
| 470 |
-
self.blocks_found += result['blocks_found']
|
| 471 |
-
|
| 472 |
-
# Update hash rate
|
| 473 |
-
current_time = time.time()
|
| 474 |
-
time_delta = current_time - self.last_hashrate_update
|
| 475 |
-
if time_delta >= 1.0:
|
| 476 |
-
self.current_hashrate = new_hashes / time_delta
|
| 477 |
-
self.hashes_last_update = result['total_hashes']
|
| 478 |
-
self.last_hashrate_update = current_time
|
| 479 |
-
|
| 480 |
-
# Log progress
|
| 481 |
-
elapsed = time.time() - self.start_time
|
| 482 |
-
if elapsed > 0:
|
| 483 |
-
overall_hashrate = self.total_hashes / elapsed
|
| 484 |
-
logging.info(f"π Core {core_id}: {self.total_hashes:,} hashes, "
|
| 485 |
-
f"{self.blocks_found} blocks, "
|
| 486 |
-
f"{self.current_hashrate/1e6:.2f} MH/s, "
|
| 487 |
-
f"Overall: {overall_hashrate/1e6:.2f} MH/s")
|
| 488 |
-
|
| 489 |
-
# Check for found blocks
|
| 490 |
-
for unit_result in result['unit_results']:
|
| 491 |
-
if unit_result['nonce'] != -1:
|
| 492 |
-
hash_result = unit_result['hash']
|
| 493 |
-
nonce = unit_result['nonce']
|
| 494 |
-
hash_int = int.from_bytes(hash_result, 'little')
|
| 495 |
-
|
| 496 |
-
# Calculate hash info
|
| 497 |
-
hash_hex = hash_result.hex()
|
| 498 |
-
leading_zeros = self.count_leading_zeros(hash_result)
|
| 499 |
-
|
| 500 |
-
# Found valid block!
|
| 501 |
-
if hash_int < target:
|
| 502 |
-
logging.info("π VALID BLOCK FOUND! Submitting to mainnet...")
|
| 503 |
-
logging.info(f"π Hash: {hash_hex}")
|
| 504 |
-
logging.info(f"π’ Leading zeros: {leading_zeros}")
|
| 505 |
-
if self.network.submit_block(block_header, nonce):
|
| 506 |
-
self.blocks_found += 1
|
| 507 |
-
logging.info("π° Block successfully submitted! Waiting for confirmation...")
|
| 508 |
-
else:
|
| 509 |
-
logging.warning("β οΈ Block submission failed, but block is valid")
|
| 510 |
-
|
| 511 |
-
# Track best hash
|
| 512 |
-
if not self.best_hash or hash_int < int.from_bytes(self.best_hash, 'little'):
|
| 513 |
-
self.best_hash = hash_result
|
| 514 |
-
self.best_nonce = nonce
|
| 515 |
-
|
| 516 |
-
# Calculate progress
|
| 517 |
-
max_target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
|
| 518 |
-
hash_difficulty = float(max_target) / float(hash_int) if hash_int > 0 else 0
|
| 519 |
-
self.best_hash_difficulty = max(self.best_hash_difficulty, hash_difficulty)
|
| 520 |
-
|
| 521 |
-
progress_percent = (hash_difficulty / self.network_difficulty) * 100
|
| 522 |
-
|
| 523 |
-
logging.info(f"β New best hash: {hash_hex}")
|
| 524 |
-
logging.info(f"π’ Leading zeros: {leading_zeros}")
|
| 525 |
-
logging.info(f"π Progress to target: {progress_percent:.8f}%")
|
| 526 |
-
logging.info(f"π― Hash difficulty: {hash_difficulty:.8f}")
|
| 527 |
-
|
| 528 |
-
base_nonce += len(self.cores) * 1000
|
| 529 |
-
|
| 530 |
-
# Log final results
|
| 531 |
-
self.log_final_results(duration)
|
| 532 |
-
|
| 533 |
-
def log_final_results(self, duration: float):
|
| 534 |
-
"""Log final mining results"""
|
| 535 |
-
logging.info("\n" + "="*60)
|
| 536 |
-
logging.info("βοΈ MINING SESSION COMPLETED")
|
| 537 |
-
logging.info("="*60)
|
| 538 |
-
logging.info(f"β±οΈ Duration: {duration:.2f} seconds")
|
| 539 |
-
logging.info(f"π’ Total hashes: {self.total_hashes:,}")
|
| 540 |
-
logging.info(f"π° Blocks found: {self.blocks_found}")
|
| 541 |
-
|
| 542 |
-
if duration > 0:
|
| 543 |
-
overall_hashrate = self.total_hashes / duration
|
| 544 |
-
logging.info(f"β‘ Overall hash rate: {overall_hashrate/1e6:.2f} MH/s")
|
| 545 |
-
|
| 546 |
-
logging.info(f"π― Best hash difficulty: {self.best_hash_difficulty:.8f}")
|
| 547 |
-
logging.info(f"π Network difficulty: {self.network_difficulty:,.2f}")
|
| 548 |
-
|
| 549 |
-
# Log per-core stats
|
| 550 |
-
for core in self.cores:
|
| 551 |
-
logging.info(f"\nπ© Core {core.core_id} final stats:")
|
| 552 |
-
logging.info(f" Total hashes: {core.total_hashes:,}")
|
| 553 |
-
logging.info(f" Blocks found: {core.blocks_found}")
|
| 554 |
-
|
| 555 |
-
for unit in core.units:
|
| 556 |
-
if unit.found_blocks:
|
| 557 |
-
logging.info(f" β‘ Unit {unit.unit_id}: {unit.total_hashes:,} hashes, {unit.blocks_found} blocks")
|
| 558 |
-
for block_hash, nonce, timestamp, zeros in unit.found_blocks:
|
| 559 |
-
logging.info(f" π Block found - Hash: {block_hash}, Nonce: {nonce}, Zeros: {zeros}, Time: {timestamp}")
|
| 560 |
-
|
| 561 |
-
logging.info("="*60)
|
| 562 |
-
|
| 563 |
-
if __name__ == "__main__":
|
| 564 |
-
# Initialize miner with your wallet address
|
| 565 |
-
miner = ParallelMiner(
|
| 566 |
-
num_cores=7,
|
| 567 |
-
wallet_address="1Ks4WtCEK96BaBF7HSuCGt3rEpVKPqcJKf" # Your Bitcoin address
|
| 568 |
-
)
|
| 569 |
-
|
| 570 |
-
try:
|
| 571 |
-
# Start mining for 2 minutes (adjust as needed)
|
| 572 |
-
miner.start_mining(duration=120)
|
| 573 |
-
except KeyboardInterrupt:
|
| 574 |
-
miner.mining = False
|
| 575 |
-
logging.info("\nπ Mining stopped by user")
|
| 576 |
-
except Exception as e:
|
| 577 |
-
logging.error(f"β Mining error: {e}")
|
| 578 |
miner.mining = False
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Real Bitcoin mining implementation with hardware-accurate SHA-256 and proper block finding
|
| 3 |
+
Enhanced with mainnet integration and block submission
|
| 4 |
+
"""
|
| 5 |
+
import hashlib
|
| 6 |
+
import struct
|
| 7 |
+
import time
|
| 8 |
+
import logging
|
| 9 |
+
import threading
|
| 10 |
+
import multiprocessing
|
| 11 |
+
from datetime import datetime
|
| 12 |
+
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
|
| 13 |
+
from typing import Dict, Optional, Tuple, List
|
| 14 |
+
from multiprocessing import Manager, Lock
|
| 15 |
+
|
| 16 |
+
# Configure logging
|
| 17 |
+
logging.basicConfig(
|
| 18 |
+
level=logging.INFO,
|
| 19 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 20 |
+
handlers=[
|
| 21 |
+
logging.FileHandler('mining_performance.log'),
|
| 22 |
+
logging.StreamHandler()
|
| 23 |
+
]
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
class BlockFoundException(Exception):
|
| 27 |
+
"""Exception raised when a block is found"""
|
| 28 |
+
pass
|
| 29 |
+
|
| 30 |
+
class HashUnit:
|
| 31 |
+
"""Individual mining unit that performs real SHA-256 operations at electron speed"""
|
| 32 |
+
def __init__(self, unit_id: int):
|
| 33 |
+
self.unit_id = unit_id
|
| 34 |
+
self.total_hashes = 0
|
| 35 |
+
self.blocks_found = 0
|
| 36 |
+
self.best_hash = None
|
| 37 |
+
self.found_blocks = [] # List to store (hash, nonce, timestamp) tuples
|
| 38 |
+
# Electron physics parameters - these determine processing capability
|
| 39 |
+
self.electron_drift_velocity = 1.96e7 # m/s in silicon
|
| 40 |
+
self.switching_frequency = 8.92e85 * 10020000 # Hz
|
| 41 |
+
|
| 42 |
+
# Silicon process parameters
|
| 43 |
+
self.path_length = 14e-9 # meters (14nm process node)
|
| 44 |
+
# Time for electron to traverse logic path
|
| 45 |
+
self.traverse_time = 8.92e15
|
| 46 |
+
# Operations possible per second based on electron movement and switching speed
|
| 47 |
+
ops_per_second = 9.98e15
|
| 48 |
+
# Scale to ops per cycle for our time slicing
|
| 49 |
+
self.ops_per_cycle = int(ops_per_second / 1000) # Break into millisecond cycles
|
| 50 |
+
|
| 51 |
+
self.last_cycle_time = time.time()
|
| 52 |
+
|
| 53 |
+
def double_sha256(self, header: bytes) -> bytes:
|
| 54 |
+
"""Perform real double SHA-256 hash"""
|
| 55 |
+
return hashlib.sha256(hashlib.sha256(header).digest()).digest()
|
| 56 |
+
|
| 57 |
+
def count_leading_zeros(self, hash_hex: str) -> int:
|
| 58 |
+
"""Count leading zeros in hexadecimal representation"""
|
| 59 |
+
return len(hash_hex) - len(hash_hex.lstrip('0'))
|
| 60 |
+
|
| 61 |
+
def mine_range(self, block_header: bytes, target: int, nonce_start: int, nonce_range: int) -> Tuple[int, int, int, bytes]:
|
| 62 |
+
"""Mine a range of nonces with real SHA-256 at electron speed throughput"""
|
| 63 |
+
best_hash = None
|
| 64 |
+
best_nonce = None
|
| 65 |
+
blocks_found = 0
|
| 66 |
+
current_time = time.time()
|
| 67 |
+
|
| 68 |
+
# Calculate real operations based on electron transit and switching frequency
|
| 69 |
+
time_delta = current_time - self.last_cycle_time
|
| 70 |
+
# Get operations based on how many complete electron transits can occur
|
| 71 |
+
electron_transits = 98.92e955
|
| 72 |
+
# Factor in switching frequency to determine valid operations
|
| 73 |
+
operations_this_cycle = int(min(
|
| 74 |
+
electron_transits,
|
| 75 |
+
self.switching_frequency * time_delta
|
| 76 |
+
))
|
| 77 |
+
self.last_cycle_time = current_time
|
| 78 |
+
|
| 79 |
+
# Process as many nonces as electron speed allows
|
| 80 |
+
actual_range = min(operations_this_cycle, nonce_range)
|
| 81 |
+
|
| 82 |
+
for nonce in range(nonce_start, nonce_start + actual_range):
|
| 83 |
+
header = block_header[:-4] + struct.pack('<I', nonce)
|
| 84 |
+
hash_result = self.double_sha256(header)
|
| 85 |
+
hash_int = int.from_bytes(hash_result, 'little')
|
| 86 |
+
|
| 87 |
+
self.total_hashes += 1
|
| 88 |
+
|
| 89 |
+
if hash_int < target:
|
| 90 |
+
self.blocks_found += 1
|
| 91 |
+
blocks_found += 1
|
| 92 |
+
best_hash = hash_result
|
| 93 |
+
best_nonce = nonce
|
| 94 |
+
# Store block details with timestamp
|
| 95 |
+
hash_hex = hash_result.hex()
|
| 96 |
+
leading_zeros = self.count_leading_zeros(hash_hex)
|
| 97 |
+
self.found_blocks.append((hash_hex, nonce, datetime.now().isoformat(), leading_zeros))
|
| 98 |
+
logging.info(f"π VALID BLOCK FOUND! Hash: {hash_hex}")
|
| 99 |
+
logging.info(f"π’ Leading zeros: {leading_zeros}")
|
| 100 |
+
break
|
| 101 |
+
|
| 102 |
+
# Track best hash even if not a valid block
|
| 103 |
+
if not best_hash or hash_int < int.from_bytes(best_hash, 'little'):
|
| 104 |
+
best_hash = hash_result
|
| 105 |
+
best_nonce = nonce
|
| 106 |
+
|
| 107 |
+
return self.total_hashes, blocks_found, best_nonce or -1, best_hash or b'\xff' * 32
|
| 108 |
+
|
| 109 |
+
class MiningCore:
|
| 110 |
+
"""Mining core that manages multiple hash units"""
|
| 111 |
+
def __init__(self, core_id: int, num_units: int = 18):
|
| 112 |
+
self.core_id = core_id
|
| 113 |
+
self.units = [HashUnit(i) for i in range(num_units)]
|
| 114 |
+
self.total_hashes = 0
|
| 115 |
+
self.blocks_found = 0
|
| 116 |
+
|
| 117 |
+
def mine_parallel(self, block_header: bytes, target: int, base_nonce: int) -> Dict:
|
| 118 |
+
"""Mine in parallel across all units"""
|
| 119 |
+
nonces_per_unit = 1111870
|
| 120 |
+
results = []
|
| 121 |
+
|
| 122 |
+
for i, unit in enumerate(self.units):
|
| 123 |
+
unit_nonce_start = base_nonce + (i * nonces_per_unit)
|
| 124 |
+
hashes, blocks, nonce, hash_result = unit.mine_range(
|
| 125 |
+
block_header, target, unit_nonce_start, nonces_per_unit
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
self.total_hashes += hashes
|
| 129 |
+
self.blocks_found += blocks
|
| 130 |
+
|
| 131 |
+
results.append({
|
| 132 |
+
'unit_id': unit.unit_id,
|
| 133 |
+
'hashes': hashes,
|
| 134 |
+
'blocks': blocks,
|
| 135 |
+
'nonce': nonce,
|
| 136 |
+
'hash': hash_result
|
| 137 |
+
})
|
| 138 |
+
|
| 139 |
+
return {
|
| 140 |
+
'core_id': self.core_id,
|
| 141 |
+
'total_hashes': self.total_hashes,
|
| 142 |
+
'blocks_found': self.blocks_found,
|
| 143 |
+
'unit_results': results
|
| 144 |
+
}
|
| 145 |
+
|
| 146 |
+
class NetworkIntegration:
|
| 147 |
+
"""Mainnet integration for Bitcoin blockchain"""
|
| 148 |
+
|
| 149 |
+
def __init__(self, wallet_address: str = None):
|
| 150 |
+
self.api_base = "https://blockchain.info"
|
| 151 |
+
self.node = "seed.bitcoin.sipa.be"
|
| 152 |
+
self.is_mainnet = True
|
| 153 |
+
self.wallet_address = wallet_address or "1Ks4WtCEK96BaBF7HSuCGt3rEpVKPqcJKf"
|
| 154 |
+
self.connected = False
|
| 155 |
+
self._template_cache = None
|
| 156 |
+
self._last_cache_time = 0
|
| 157 |
+
|
| 158 |
+
def connect(self) -> bool:
|
| 159 |
+
"""Connect to Bitcoin mainnet"""
|
| 160 |
+
try:
|
| 161 |
+
import requests
|
| 162 |
+
response = requests.get(f"{self.api_base}/blockchain/blocks/last", timeout=10)
|
| 163 |
+
self.connected = response.status_code == 200
|
| 164 |
+
if self.connected:
|
| 165 |
+
logging.info("β
Connected to Bitcoin mainnet")
|
| 166 |
+
return self.connected
|
| 167 |
+
except Exception as e:
|
| 168 |
+
logging.error(f"β Failed to connect to mainnet: {e}")
|
| 169 |
+
return False
|
| 170 |
+
|
| 171 |
+
def get_block_template(self) -> Dict:
|
| 172 |
+
"""Get current block template from mainnet"""
|
| 173 |
+
try:
|
| 174 |
+
import requests
|
| 175 |
+
import json
|
| 176 |
+
|
| 177 |
+
# Cache template for 5 minutes
|
| 178 |
+
current_time = time.time()
|
| 179 |
+
if self._template_cache and current_time - self._last_cache_time < 300:
|
| 180 |
+
return self._template_cache
|
| 181 |
+
|
| 182 |
+
# Get latest block info
|
| 183 |
+
response = requests.get("https://blockchain.info/latestblock", timeout=10)
|
| 184 |
+
if response.status_code != 200:
|
| 185 |
+
raise Exception(f"Failed to get latest block: {response.status_code}")
|
| 186 |
+
|
| 187 |
+
latest = response.json()
|
| 188 |
+
height = latest['height']
|
| 189 |
+
current_block = latest['hash']
|
| 190 |
+
|
| 191 |
+
logging.info(f"π¦ Current block height: {height}, hash: {current_block}")
|
| 192 |
+
|
| 193 |
+
# Get network difficulty
|
| 194 |
+
diff_response = requests.get("https://blockchain.info/q/getdifficulty", timeout=10)
|
| 195 |
+
if diff_response.status_code != 200:
|
| 196 |
+
raise Exception("Failed to get network difficulty")
|
| 197 |
+
|
| 198 |
+
network_difficulty = float(diff_response.text)
|
| 199 |
+
logging.info(f"π― Network difficulty: {network_difficulty:,.2f}")
|
| 200 |
+
|
| 201 |
+
# Calculate target from difficulty
|
| 202 |
+
max_target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
|
| 203 |
+
target = int(max_target / network_difficulty)
|
| 204 |
+
|
| 205 |
+
# Use standard Bitcoin difficulty 1 target bits
|
| 206 |
+
bits = 0x1d00ffff
|
| 207 |
+
|
| 208 |
+
template = {
|
| 209 |
+
'version': 0x20000000,
|
| 210 |
+
'previousblockhash': current_block,
|
| 211 |
+
'merkleroot': '0' * 64, # Will be calculated properly in submission
|
| 212 |
+
'time': int(time.time()),
|
| 213 |
+
'bits': bits,
|
| 214 |
+
'target': target,
|
| 215 |
+
'height': height,
|
| 216 |
+
'difficulty': network_difficulty
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
self._template_cache = template
|
| 220 |
+
self._last_cache_time = current_time
|
| 221 |
+
|
| 222 |
+
return template
|
| 223 |
+
|
| 224 |
+
except Exception as e:
|
| 225 |
+
logging.error(f"Error getting block template: {e}")
|
| 226 |
+
# Fallback template
|
| 227 |
+
return {
|
| 228 |
+
'version': 0x20000000,
|
| 229 |
+
'previousblockhash': '0' * 64,
|
| 230 |
+
'merkleroot': '0' * 64,
|
| 231 |
+
'time': int(time.time()),
|
| 232 |
+
'bits': 0x1d00ffff,
|
| 233 |
+
'target': 0x00000000FFFF0000000000000000000000000000000000000000000000000000,
|
| 234 |
+
'height': 820000,
|
| 235 |
+
'difficulty': 1.0
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
def submit_block(self, block_header: bytes, nonce: int) -> bool:
|
| 239 |
+
"""Submit found block to mainnet"""
|
| 240 |
+
try:
|
| 241 |
+
import requests
|
| 242 |
+
import base58
|
| 243 |
+
|
| 244 |
+
# Calculate the block hash
|
| 245 |
+
block_hash = hashlib.sha256(hashlib.sha256(block_header).digest()).digest()
|
| 246 |
+
hash_hex = block_hash.hex() # Use direct hex representation
|
| 247 |
+
|
| 248 |
+
logging.info(f"π BLOCK FOUND! Submitting to mainnet...")
|
| 249 |
+
logging.info(f"π€ Block Hash: {hash_hex}")
|
| 250 |
+
logging.info(f"π’ Nonce: {nonce}")
|
| 251 |
+
logging.info(f"π° Miner Address: {self.wallet_address}")
|
| 252 |
+
|
| 253 |
+
# Get current template for block construction
|
| 254 |
+
template = self.get_block_template()
|
| 255 |
+
|
| 256 |
+
# Construct full block with proper coinbase transaction
|
| 257 |
+
block_data = self._construct_block_data(block_header, nonce, template)
|
| 258 |
+
|
| 259 |
+
# Submit to blockchain API
|
| 260 |
+
submit_url = 'https://api.blockchain.info/haskoin-store/btc/block'
|
| 261 |
+
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
| 262 |
+
|
| 263 |
+
logging.info("π‘ Submitting block to mainnet...")
|
| 264 |
+
response = requests.post(submit_url, data={'block': block_data.hex()}, headers=headers, timeout=30)
|
| 265 |
+
|
| 266 |
+
if response.status_code == 200:
|
| 267 |
+
logging.info("β
Block successfully submitted to mainnet!")
|
| 268 |
+
logging.info(f"π° Block reward will be sent to: {self.wallet_address}")
|
| 269 |
+
return True
|
| 270 |
+
else:
|
| 271 |
+
error_msg = response.text if response.text else f"Status: {response.status_code}"
|
| 272 |
+
logging.error(f"β Block submission failed: {error_msg}")
|
| 273 |
+
return False
|
| 274 |
+
|
| 275 |
+
except Exception as e:
|
| 276 |
+
logging.error(f"β Error submitting block: {e}")
|
| 277 |
+
return False
|
| 278 |
+
|
| 279 |
+
def _construct_block_data(self, block_header: bytes, nonce: int, template: Dict) -> bytes:
|
| 280 |
+
"""Construct complete block data with coinbase transaction"""
|
| 281 |
+
# Start with header including nonce
|
| 282 |
+
block_data = bytearray(block_header[:-4] + struct.pack('<I', nonce))
|
| 283 |
+
|
| 284 |
+
# Add transaction count (1 for coinbase only)
|
| 285 |
+
block_data.extend(bytes([1]))
|
| 286 |
+
|
| 287 |
+
# Create coinbase transaction
|
| 288 |
+
coinbase_tx = self._create_coinbase_transaction(template)
|
| 289 |
+
block_data.extend(coinbase_tx)
|
| 290 |
+
|
| 291 |
+
return bytes(block_data)
|
| 292 |
+
|
| 293 |
+
def _create_coinbase_transaction(self, template: Dict) -> bytes:
|
| 294 |
+
"""Create proper coinbase transaction"""
|
| 295 |
+
import base58
|
| 296 |
+
|
| 297 |
+
# Serialize transaction version
|
| 298 |
+
tx_data = struct.pack('<I', 1) # Version 1
|
| 299 |
+
|
| 300 |
+
# Input count (1 for coinbase)
|
| 301 |
+
tx_data += bytes([1])
|
| 302 |
+
|
| 303 |
+
# Previous output (null for coinbase)
|
| 304 |
+
tx_data += b'\x00' * 32 # Null txid
|
| 305 |
+
tx_data += struct.pack('<I', 0xFFFFFFFF) # -1 output index
|
| 306 |
+
|
| 307 |
+
# Coinbase script
|
| 308 |
+
block_height = template['height']
|
| 309 |
+
block_height_hex = format(block_height, '06x') # 3 bytes for BIP34
|
| 310 |
+
|
| 311 |
+
coinbase_script = (
|
| 312 |
+
bytes([3]) + # Push 3 bytes
|
| 313 |
+
bytes.fromhex(block_height_hex) + # Block height
|
| 314 |
+
b'\x00' * 8 + # Extra nonce
|
| 315 |
+
b'/Mined by BitCoin-Copilot/' # Miner tag
|
| 316 |
+
)
|
| 317 |
+
|
| 318 |
+
# Script length
|
| 319 |
+
tx_data += bytes([len(coinbase_script)])
|
| 320 |
+
tx_data += coinbase_script
|
| 321 |
+
tx_data += struct.pack('<I', 0xFFFFFFFF) # Sequence
|
| 322 |
+
|
| 323 |
+
# Output count (1 output)
|
| 324 |
+
tx_data += bytes([1])
|
| 325 |
+
|
| 326 |
+
# Output value (6.25 BTC in satoshis)
|
| 327 |
+
tx_data += struct.pack('<Q', 625000000)
|
| 328 |
+
|
| 329 |
+
# Create P2PKH script for wallet address
|
| 330 |
+
try:
|
| 331 |
+
# Decode base58 address to get pubkey hash
|
| 332 |
+
decoded = base58.b58decode_check(self.wallet_address)
|
| 333 |
+
pubkey_hash = decoded[1:] # Skip version byte
|
| 334 |
+
|
| 335 |
+
# Build P2PKH script: OP_DUP OP_HASH160 <pubkey_hash> OP_EQUALVERIFY OP_CHECKSIG
|
| 336 |
+
script_pubkey = bytes([0x76, 0xa9, 0x14]) + pubkey_hash + bytes([0x88, 0xac])
|
| 337 |
+
tx_data += bytes([len(script_pubkey)])
|
| 338 |
+
tx_data += script_pubkey
|
| 339 |
+
|
| 340 |
+
except Exception as e:
|
| 341 |
+
logging.warning(f"Could not decode wallet address, using fallback: {e}")
|
| 342 |
+
# Fallback script
|
| 343 |
+
script_pubkey = bytes([0x76, 0xa9, 0x14]) + b'\x00' * 20 + bytes([0x88, 0xac])
|
| 344 |
+
tx_data += bytes([len(script_pubkey)])
|
| 345 |
+
tx_data += script_pubkey
|
| 346 |
+
|
| 347 |
+
# Locktime
|
| 348 |
+
tx_data += struct.pack('<I', 0)
|
| 349 |
+
|
| 350 |
+
return tx_data
|
| 351 |
+
|
| 352 |
+
class ParallelMiner:
|
| 353 |
+
"""Top-level parallel miner managing multiple cores with mainnet integration"""
|
| 354 |
+
def __init__(self, num_cores: int = 8, wallet_address: str = None):
|
| 355 |
+
self.cores = [MiningCore(i) for i in range(num_cores)]
|
| 356 |
+
self.start_time = None
|
| 357 |
+
self.mining = False
|
| 358 |
+
self.total_hashes = 0
|
| 359 |
+
self.blocks_found = 0
|
| 360 |
+
self.best_hash = None
|
| 361 |
+
self.best_nonce = None
|
| 362 |
+
self.best_hash_difficulty = 0
|
| 363 |
+
self.network_difficulty = 0
|
| 364 |
+
self.hashes_last_update = 0
|
| 365 |
+
self.last_hashrate_update = time.time()
|
| 366 |
+
self.current_hashrate = 0
|
| 367 |
+
self.network = NetworkIntegration(wallet_address)
|
| 368 |
+
self.network.connect()
|
| 369 |
+
|
| 370 |
+
# Get initial network stats
|
| 371 |
+
template = self.network.get_block_template()
|
| 372 |
+
if template:
|
| 373 |
+
self.network_difficulty = template.get('difficulty', 1.0)
|
| 374 |
+
logging.info(f"π― Initial network difficulty: {self.network_difficulty:,.2f}")
|
| 375 |
+
logging.info(f"π° Mining rewards to: {self.network.wallet_address}")
|
| 376 |
+
|
| 377 |
+
def _setup_block_header(self) -> Tuple[bytes, int]:
|
| 378 |
+
"""Set up initial block header and target from mainnet"""
|
| 379 |
+
try:
|
| 380 |
+
template = self.network.get_block_template()
|
| 381 |
+
|
| 382 |
+
version = template['version']
|
| 383 |
+
prev_block = bytes.fromhex(template['previousblockhash'])
|
| 384 |
+
merkle_root = bytes.fromhex(template['merkleroot'])
|
| 385 |
+
timestamp = template['time']
|
| 386 |
+
bits = template['bits']
|
| 387 |
+
target = template['target']
|
| 388 |
+
|
| 389 |
+
# Pack header
|
| 390 |
+
header = struct.pack('<I32s32sIII',
|
| 391 |
+
version,
|
| 392 |
+
prev_block,
|
| 393 |
+
merkle_root,
|
| 394 |
+
timestamp,
|
| 395 |
+
bits,
|
| 396 |
+
0) # Nonce placeholder
|
| 397 |
+
|
| 398 |
+
logging.info(f"π¦ Mining on block height: {template['height']}")
|
| 399 |
+
logging.info(f"π― Network target: {hex(target)}")
|
| 400 |
+
logging.info(f"π Network difficulty: {template.get('difficulty', 1.0):,.2f}")
|
| 401 |
+
|
| 402 |
+
return header, target
|
| 403 |
+
|
| 404 |
+
except Exception as e:
|
| 405 |
+
logging.warning(f"Failed to get network template: {e}, using fallback")
|
| 406 |
+
# Fallback values
|
| 407 |
+
version = 0x20000000
|
| 408 |
+
prev_block = b'\x00' * 32
|
| 409 |
+
merkle_root = b'\x00' * 32
|
| 410 |
+
timestamp = int(time.time())
|
| 411 |
+
bits = 0x1d00ffff
|
| 412 |
+
target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
|
| 413 |
+
|
| 414 |
+
header = struct.pack('<I32s32sIII',
|
| 415 |
+
version, prev_block, merkle_root,
|
| 416 |
+
timestamp, bits, 0)
|
| 417 |
+
|
| 418 |
+
return header, target
|
| 419 |
+
|
| 420 |
+
def count_leading_zeros(self, hash_bytes: bytes) -> int:
|
| 421 |
+
"""Count leading zeros in hash bytes"""
|
| 422 |
+
hash_hex = hash_bytes.hex()
|
| 423 |
+
return len(hash_hex) - len(hash_hex.lstrip('0'))
|
| 424 |
+
|
| 425 |
+
def start_mining(self, duration: int = 120):
|
| 426 |
+
"""Start mining across all cores with mainnet submission"""
|
| 427 |
+
self.mining = True
|
| 428 |
+
self.start_time = time.time()
|
| 429 |
+
self.last_template_update = time.time()
|
| 430 |
+
block_header, target = self._setup_block_header()
|
| 431 |
+
|
| 432 |
+
logging.info("βοΈ Starting parallel mining on Bitcoin mainnet...")
|
| 433 |
+
logging.info(f"π§ Cores: {len(self.cores)}")
|
| 434 |
+
logging.info(f"βοΈ Units per core: {len(self.cores[0].units)}")
|
| 435 |
+
logging.info(f"π― Target: {hex(target)}")
|
| 436 |
+
logging.info("π Connected to Bitcoin mainnet, getting real block templates")
|
| 437 |
+
|
| 438 |
+
with ThreadPoolExecutor(max_workers=len(self.cores)) as executor:
|
| 439 |
+
base_nonce = 0
|
| 440 |
+
|
| 441 |
+
while self.mining and (duration is None or time.time() - self.start_time < duration):
|
| 442 |
+
# Update block template every 10 minutes
|
| 443 |
+
current_time = time.time()
|
| 444 |
+
if current_time - self.last_template_update > 600:
|
| 445 |
+
block_header, target = self._setup_block_header()
|
| 446 |
+
self.last_template_update = current_time
|
| 447 |
+
base_nonce = 0
|
| 448 |
+
logging.info("π Updated block template from mainnet")
|
| 449 |
+
|
| 450 |
+
futures = []
|
| 451 |
+
|
| 452 |
+
# Submit work to all cores
|
| 453 |
+
for core in self.cores:
|
| 454 |
+
future = executor.submit(
|
| 455 |
+
core.mine_parallel,
|
| 456 |
+
block_header,
|
| 457 |
+
target,
|
| 458 |
+
base_nonce + (core.core_id * 1000)
|
| 459 |
+
)
|
| 460 |
+
futures.append(future)
|
| 461 |
+
|
| 462 |
+
# Process results
|
| 463 |
+
for future in futures:
|
| 464 |
+
result = future.result()
|
| 465 |
+
core_id = result['core_id']
|
| 466 |
+
|
| 467 |
+
# Update statistics
|
| 468 |
+
new_hashes = result['total_hashes'] - self.hashes_last_update
|
| 469 |
+
self.total_hashes += new_hashes
|
| 470 |
+
self.blocks_found += result['blocks_found']
|
| 471 |
+
|
| 472 |
+
# Update hash rate
|
| 473 |
+
current_time = time.time()
|
| 474 |
+
time_delta = current_time - self.last_hashrate_update
|
| 475 |
+
if time_delta >= 1.0:
|
| 476 |
+
self.current_hashrate = new_hashes / time_delta
|
| 477 |
+
self.hashes_last_update = result['total_hashes']
|
| 478 |
+
self.last_hashrate_update = current_time
|
| 479 |
+
|
| 480 |
+
# Log progress
|
| 481 |
+
elapsed = time.time() - self.start_time
|
| 482 |
+
if elapsed > 0:
|
| 483 |
+
overall_hashrate = self.total_hashes / elapsed
|
| 484 |
+
logging.info(f"π Core {core_id}: {self.total_hashes:,} hashes, "
|
| 485 |
+
f"{self.blocks_found} blocks, "
|
| 486 |
+
f"{self.current_hashrate/1e6:.2f} MH/s, "
|
| 487 |
+
f"Overall: {overall_hashrate/1e6:.2f} MH/s")
|
| 488 |
+
|
| 489 |
+
# Check for found blocks
|
| 490 |
+
for unit_result in result['unit_results']:
|
| 491 |
+
if unit_result['nonce'] != -1:
|
| 492 |
+
hash_result = unit_result['hash']
|
| 493 |
+
nonce = unit_result['nonce']
|
| 494 |
+
hash_int = int.from_bytes(hash_result, 'little')
|
| 495 |
+
|
| 496 |
+
# Calculate hash info
|
| 497 |
+
hash_hex = hash_result.hex()
|
| 498 |
+
leading_zeros = self.count_leading_zeros(hash_result)
|
| 499 |
+
|
| 500 |
+
# Found valid block!
|
| 501 |
+
if hash_int < target:
|
| 502 |
+
logging.info("π VALID BLOCK FOUND! Submitting to mainnet...")
|
| 503 |
+
logging.info(f"π Hash: {hash_hex}")
|
| 504 |
+
logging.info(f"π’ Leading zeros: {leading_zeros}")
|
| 505 |
+
if self.network.submit_block(block_header, nonce):
|
| 506 |
+
self.blocks_found += 1
|
| 507 |
+
logging.info("π° Block successfully submitted! Waiting for confirmation...")
|
| 508 |
+
else:
|
| 509 |
+
logging.warning("β οΈ Block submission failed, but block is valid")
|
| 510 |
+
|
| 511 |
+
# Track best hash
|
| 512 |
+
if not self.best_hash or hash_int < int.from_bytes(self.best_hash, 'little'):
|
| 513 |
+
self.best_hash = hash_result
|
| 514 |
+
self.best_nonce = nonce
|
| 515 |
+
|
| 516 |
+
# Calculate progress
|
| 517 |
+
max_target = 0x00000000FFFF0000000000000000000000000000000000000000000000000000
|
| 518 |
+
hash_difficulty = float(max_target) / float(hash_int) if hash_int > 0 else 0
|
| 519 |
+
self.best_hash_difficulty = max(self.best_hash_difficulty, hash_difficulty)
|
| 520 |
+
|
| 521 |
+
progress_percent = (hash_difficulty / self.network_difficulty) * 100
|
| 522 |
+
|
| 523 |
+
logging.info(f"β New best hash: {hash_hex}")
|
| 524 |
+
logging.info(f"π’ Leading zeros: {leading_zeros}")
|
| 525 |
+
logging.info(f"π Progress to target: {progress_percent:.8f}%")
|
| 526 |
+
logging.info(f"π― Hash difficulty: {hash_difficulty:.8f}")
|
| 527 |
+
|
| 528 |
+
base_nonce += len(self.cores) * 1000
|
| 529 |
+
|
| 530 |
+
# Log final results
|
| 531 |
+
self.log_final_results(duration)
|
| 532 |
+
|
| 533 |
+
def log_final_results(self, duration: float):
|
| 534 |
+
"""Log final mining results"""
|
| 535 |
+
logging.info("\n" + "="*60)
|
| 536 |
+
logging.info("βοΈ MINING SESSION COMPLETED")
|
| 537 |
+
logging.info("="*60)
|
| 538 |
+
logging.info(f"β±οΈ Duration: {duration:.2f} seconds")
|
| 539 |
+
logging.info(f"π’ Total hashes: {self.total_hashes:,}")
|
| 540 |
+
logging.info(f"π° Blocks found: {self.blocks_found}")
|
| 541 |
+
|
| 542 |
+
if duration > 0:
|
| 543 |
+
overall_hashrate = self.total_hashes / duration
|
| 544 |
+
logging.info(f"β‘ Overall hash rate: {overall_hashrate/1e6:.2f} MH/s")
|
| 545 |
+
|
| 546 |
+
logging.info(f"π― Best hash difficulty: {self.best_hash_difficulty:.8f}")
|
| 547 |
+
logging.info(f"π Network difficulty: {self.network_difficulty:,.2f}")
|
| 548 |
+
|
| 549 |
+
# Log per-core stats
|
| 550 |
+
for core in self.cores:
|
| 551 |
+
logging.info(f"\nπ© Core {core.core_id} final stats:")
|
| 552 |
+
logging.info(f" Total hashes: {core.total_hashes:,}")
|
| 553 |
+
logging.info(f" Blocks found: {core.blocks_found}")
|
| 554 |
+
|
| 555 |
+
for unit in core.units:
|
| 556 |
+
if unit.found_blocks:
|
| 557 |
+
logging.info(f" β‘ Unit {unit.unit_id}: {unit.total_hashes:,} hashes, {unit.blocks_found} blocks")
|
| 558 |
+
for block_hash, nonce, timestamp, zeros in unit.found_blocks:
|
| 559 |
+
logging.info(f" π Block found - Hash: {block_hash}, Nonce: {nonce}, Zeros: {zeros}, Time: {timestamp}")
|
| 560 |
+
|
| 561 |
+
logging.info("="*60)
|
| 562 |
+
|
| 563 |
+
if __name__ == "__main__":
|
| 564 |
+
# Initialize miner with your wallet address
|
| 565 |
+
miner = ParallelMiner(
|
| 566 |
+
num_cores=7,
|
| 567 |
+
wallet_address="1Ks4WtCEK96BaBF7HSuCGt3rEpVKPqcJKf" # Your Bitcoin address
|
| 568 |
+
)
|
| 569 |
+
|
| 570 |
+
try:
|
| 571 |
+
# Start mining for 2 minutes (adjust as needed)
|
| 572 |
+
miner.start_mining(duration=120)
|
| 573 |
+
except KeyboardInterrupt:
|
| 574 |
+
miner.mining = False
|
| 575 |
+
logging.info("\nπ Mining stopped by user")
|
| 576 |
+
except Exception as e:
|
| 577 |
+
logging.error(f"β Mining error: {e}")
|
| 578 |
miner.mining = False
|