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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Integrates the mining system with Bitcoin mainnet
|
| 3 |
+
"""
|
| 4 |
+
from typing import Dict, Any, Optional
|
| 5 |
+
import time
|
| 6 |
+
import requests
|
| 7 |
+
import hashlib
|
| 8 |
+
import json
|
| 9 |
+
import logging
|
| 10 |
+
import struct
|
| 11 |
+
|
| 12 |
+
# Configure detailed logging
|
| 13 |
+
logging.basicConfig(
|
| 14 |
+
level=logging.DEBUG,
|
| 15 |
+
format='%(asctime)s - %(levelname)s - %(message)s',
|
| 16 |
+
handlers=[
|
| 17 |
+
logging.FileHandler('network_debug.log'),
|
| 18 |
+
logging.StreamHandler()
|
| 19 |
+
]
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
class NetworkIntegration:
|
| 23 |
+
def __init__(self, wallet_address: str = None):
|
| 24 |
+
self.api_base = "https://api.bitaps.com/btc/v1"
|
| 25 |
+
self.node = "seed.bitcoin.sipa.be" # Bitcoin mainnet seed node
|
| 26 |
+
# Use the provided wallet address or load from my_wallet.json
|
| 27 |
+
if wallet_address:
|
| 28 |
+
self.wallet_address = wallet_address
|
| 29 |
+
else:
|
| 30 |
+
try:
|
| 31 |
+
with open('my_wallet.json', 'r') as f:
|
| 32 |
+
wallet_data = json.load(f)
|
| 33 |
+
self.wallet_address = wallet_data['address']
|
| 34 |
+
print(f"Using wallet address: {self.wallet_address}")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
print(f"Error loading wallet: {e}")
|
| 37 |
+
self.wallet_address = "1Ks4WtCEK96BaBF7HSuCGt3rEpVKPqcJKf" # Your default address
|
| 38 |
+
|
| 39 |
+
def connect(self) -> bool:
|
| 40 |
+
"""Connect to Bitcoin mainnet"""
|
| 41 |
+
try:
|
| 42 |
+
# Test connection by getting latest block
|
| 43 |
+
response = requests.get(f"{self.api_base}/blockchain/blocks/last")
|
| 44 |
+
return response.status_code == 200
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Failed to connect to mainnet: {e}")
|
| 47 |
+
return False
|
| 48 |
+
|
| 49 |
+
def get_block_template(self) -> Dict[str, Any]:
|
| 50 |
+
"""Get current block template from mainnet"""
|
| 51 |
+
try:
|
| 52 |
+
# Cache the blockchain API response for 5 minutes
|
| 53 |
+
current_time = time.time()
|
| 54 |
+
if not hasattr(self, '_template_cache') or current_time - self._last_cache_time > 300:
|
| 55 |
+
logging.debug("Cache expired, fetching new block template")
|
| 56 |
+
# Get latest block info from a more reliable API
|
| 57 |
+
response = requests.get("https://blockchain.info/latestblock")
|
| 58 |
+
logging.debug(f"Latest block API response status: {response.status_code}")
|
| 59 |
+
|
| 60 |
+
if response.status_code != 200:
|
| 61 |
+
logging.error(f"Failed to get latest block. Status code: {response.status_code}")
|
| 62 |
+
if hasattr(self, '_template_cache'):
|
| 63 |
+
logging.info("Using cached template")
|
| 64 |
+
return self._template_cache
|
| 65 |
+
raise Exception("Failed to get latest block")
|
| 66 |
+
|
| 67 |
+
latest = response.json()
|
| 68 |
+
logging.debug(f"Latest block response: {latest}")
|
| 69 |
+
|
| 70 |
+
height = latest['height']
|
| 71 |
+
current_block = latest['hash']
|
| 72 |
+
logging.info(f"Current block height: {height}, hash: {current_block}")
|
| 73 |
+
|
| 74 |
+
# Get current network stats
|
| 75 |
+
logging.debug("Fetching network stats...")
|
| 76 |
+
stats_response = requests.get("https://blockchain.info/stats?format=json")
|
| 77 |
+
logging.debug(f"Network stats API response status: {stats_response.status_code}")
|
| 78 |
+
stats = stats_response.json()
|
| 79 |
+
|
| 80 |
+
# Convert difficulty to target
|
| 81 |
+
difficulty = float(stats.get('difficulty', 1))
|
| 82 |
+
max_target = int('00000000FFFF0000000000000000000000000000000000000000000000000000', 16)
|
| 83 |
+
current_target = int(max_target / difficulty)
|
| 84 |
+
|
| 85 |
+
bits = stats.get('bits', '1d00ffff')
|
| 86 |
+
if isinstance(bits, str):
|
| 87 |
+
bits = int(bits, 16)
|
| 88 |
+
|
| 89 |
+
# Construct template with required fields
|
| 90 |
+
template = {
|
| 91 |
+
'version': 2, # Current Bitcoin version
|
| 92 |
+
'previousblockhash': current_block, # Use current block as previous for next block
|
| 93 |
+
'merkleroot': '0' * 64, # Placeholder merkle root
|
| 94 |
+
'time': int(time.time()),
|
| 95 |
+
'bits': bits, # Use converted bits
|
| 96 |
+
'height': int(height), # Ensure height is integer
|
| 97 |
+
'target': current_target # Calculate based on current difficulty
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
# Update cache
|
| 101 |
+
self._template_cache = template
|
| 102 |
+
self._last_cache_time = current_time
|
| 103 |
+
|
| 104 |
+
return self._template_cache
|
| 105 |
+
|
| 106 |
+
except Exception as e:
|
| 107 |
+
logging.error(f"Error getting block template: {str(e)}")
|
| 108 |
+
# Return fallback template
|
| 109 |
+
return {
|
| 110 |
+
'version': 2,
|
| 111 |
+
'previousblockhash': '0' * 64,
|
| 112 |
+
'merkleroot': '0' * 64,
|
| 113 |
+
'time': int(time.time()),
|
| 114 |
+
'bits': '1d00ffff',
|
| 115 |
+
'height': 0,
|
| 116 |
+
'target': int('00000000FFFF0000000000000000000000000000000000000000000000000000', 16)
|
| 117 |
+
}
|
| 118 |
+
if not stats:
|
| 119 |
+
raise Exception("Failed to get network stats")
|
| 120 |
+
|
| 121 |
+
network_difficulty = float(stats['difficulty'])
|
| 122 |
+
bits = stats.get('bits', '1d00ffff') # Default to testnet bits if missing
|
| 123 |
+
|
| 124 |
+
# Parse bits to target
|
| 125 |
+
bits_int = int(bits, 16) # Convert hex bits to int
|
| 126 |
+
exp = ((bits_int >> 24) & 0xff)
|
| 127 |
+
coeff = bits_int & 0x00ffffff
|
| 128 |
+
target = coeff * (2 ** (8 * (exp - 3)))
|
| 129 |
+
|
| 130 |
+
print(f"Mining at difficulty: {network_difficulty}")
|
| 131 |
+
print(f"Network target: {hex(target)}")
|
| 132 |
+
|
| 133 |
+
# Create proper coinbase input script
|
| 134 |
+
block_height_hex = hex(height)[2:].zfill(6) # BIP34: Block height
|
| 135 |
+
coinbase_script = (
|
| 136 |
+
"03" + # Push 3 bytes
|
| 137 |
+
block_height_hex + # BIP34: Block height
|
| 138 |
+
"0000000000000000" + # Extra nonce space
|
| 139 |
+
"2f4d696e656420627920426974436f696e2d436f70696c6f742f" # /Mined by BitCoin-Copilot/
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Create coinbase transaction
|
| 143 |
+
coinbase_tx = {
|
| 144 |
+
'version': 1,
|
| 145 |
+
'vin': [{
|
| 146 |
+
'txid': '0' * 64, # Null hash for coinbase
|
| 147 |
+
'vout': 0xFFFFFFFF, # -1 (4 bytes) for coinbase
|
| 148 |
+
'scriptSig': coinbase_script, # Block height + extra nonce + miner tag
|
| 149 |
+
'sequence': 0xFFFFFFFF
|
| 150 |
+
}],
|
| 151 |
+
'vout': [{
|
| 152 |
+
'value': 625000000, # 6.25 BTC reward
|
| 153 |
+
'scriptPubKey': '76a914' + hashlib.new("ripemd160", hashlib.sha256(self.wallet_address.encode()).digest()).hexdigest() + '88ac' # P2PKH to miner address
|
| 154 |
+
}]
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
# Construct proper block template with real network data
|
| 158 |
+
template = {
|
| 159 |
+
'version': 0x20000000, # Version 2 with BIP9 bits
|
| 160 |
+
'previousblockhash': prev_block, # Changed to match Bitcoin Core naming
|
| 161 |
+
'merkleroot': current_block['mrkl_root'], # Changed to match Bitcoin Core naming
|
| 162 |
+
'time': int(time.time()), # Changed to match Bitcoin Core naming
|
| 163 |
+
'bits': bits_int, # Using parsed bits value
|
| 164 |
+
'height': height,
|
| 165 |
+
'target': target,
|
| 166 |
+
'difficulty': network_difficulty, # Changed to match Bitcoin Core naming
|
| 167 |
+
'coinbasetx': coinbase_tx, # Changed to match Bitcoin Core naming
|
| 168 |
+
'sizelimit': 4000000, # Changed to match Bitcoin Core naming
|
| 169 |
+
'transactions': [] # Pending transactions (empty for now)
|
| 170 |
+
}
|
| 171 |
+
|
| 172 |
+
return template
|
| 173 |
+
|
| 174 |
+
except Exception as e:
|
| 175 |
+
print(f"Error getting block template: {str(e)}")
|
| 176 |
+
# Get real latest block as fallback
|
| 177 |
+
latest_url = "https://blockchain.info/latestblock"
|
| 178 |
+
latest_response = requests.get(latest_url)
|
| 179 |
+
if latest_response.status_code == 200:
|
| 180 |
+
latest_data = latest_response.json()
|
| 181 |
+
block_height = latest_data['height']
|
| 182 |
+
prev_block = latest_data['hash']
|
| 183 |
+
else:
|
| 184 |
+
block_height = 800_000
|
| 185 |
+
prev_block = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f'
|
| 186 |
+
|
| 187 |
+
# Get real network difficulty even in fallback
|
| 188 |
+
diff_url = "https://blockchain.info/q/getdifficulty"
|
| 189 |
+
try:
|
| 190 |
+
diff_response = requests.get(diff_url)
|
| 191 |
+
if diff_response.status_code == 200:
|
| 192 |
+
network_difficulty = float(diff_response.text)
|
| 193 |
+
target = int((0xffff * 2**(8*(0x1d - 3))) / network_difficulty)
|
| 194 |
+
else:
|
| 195 |
+
target = 0x00000000ffff0000000000000000000000000000000000000000000000000000
|
| 196 |
+
except:
|
| 197 |
+
target = 0x00000000ffff0000000000000000000000000000000000000000000000000000
|
| 198 |
+
|
| 199 |
+
template = {
|
| 200 |
+
'version': 0x20000000,
|
| 201 |
+
'previous_block': prev_block,
|
| 202 |
+
'merkle_root': '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b',
|
| 203 |
+
'timestamp': int(time.time()),
|
| 204 |
+
'bits': 0x1d00ffff,
|
| 205 |
+
'target': target,
|
| 206 |
+
'height': block_height,
|
| 207 |
+
'network_difficulty': network_difficulty if 'network_difficulty' in locals() else None
|
| 208 |
+
}
|
| 209 |
+
return template
|
| 210 |
+
try:
|
| 211 |
+
# Get latest block info
|
| 212 |
+
response = requests.get(self.bitcoin_network.latest_block_url)
|
| 213 |
+
if response.status_code != 200:
|
| 214 |
+
raise Exception("Failed to get latest block")
|
| 215 |
+
|
| 216 |
+
latest = response.json()
|
| 217 |
+
# Construct proper block template with real network data
|
| 218 |
+
template = {
|
| 219 |
+
'version': 0x20000000, # Version 2 with BIP9 bits
|
| 220 |
+
'previousblockhash': prev_block, # Changed to match Bitcoin Core naming
|
| 221 |
+
'merkleroot': '0' * 64, # Will be calculated from transactions
|
| 222 |
+
'time': int(time.time()), # Current time
|
| 223 |
+
'bits': bits_int, # Using parsed bits value
|
| 224 |
+
'height': height,
|
| 225 |
+
'target': target,
|
| 226 |
+
'difficulty': network_difficulty,
|
| 227 |
+
'coinbasetx': coinbase_tx,
|
| 228 |
+
'sizelimit': 4000000, # 4MB block size limit
|
| 229 |
+
'transactions': [] # Pending transactions (empty for now)
|
| 230 |
+
}
|
| 231 |
+
return template
|
| 232 |
+
except Exception as e:
|
| 233 |
+
print(f"Error getting block template: {str(e)}")
|
| 234 |
+
# Create fallback template
|
| 235 |
+
template = {
|
| 236 |
+
'version': 0x20000000, # Version 2 with BIP9 bits
|
| 237 |
+
'previousblockhash': '000000000000000000024bead8df69990852c202db0e0097c1a12ea637d7e96d',
|
| 238 |
+
'merkleroot': '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b',
|
| 239 |
+
'time': int(time.time()),
|
| 240 |
+
'bits': 0x1d00ffff,
|
| 241 |
+
'target': 0x00000000ffff0000000000000000000000000000000000000000000000000000,
|
| 242 |
+
'height': 2_500_000,
|
| 243 |
+
'difficulty': 1.0,
|
| 244 |
+
'coinbasetx': {
|
| 245 |
+
'version': 1,
|
| 246 |
+
'vin': [{
|
| 247 |
+
'txid': '0' * 64,
|
| 248 |
+
'vout': 0xFFFFFFFF,
|
| 249 |
+
'scriptSig': '03' + hex(2_500_000)[2:].zfill(6) + '0000000000000000',
|
| 250 |
+
'sequence': 0xFFFFFFFF
|
| 251 |
+
}],
|
| 252 |
+
'vout': [{
|
| 253 |
+
'value': 625000000,
|
| 254 |
+
'scriptPubKey': '76a914' + hashlib.new("ripemd160", hashlib.sha256(self.wallet_address.encode()).digest()).hexdigest() + '88ac'
|
| 255 |
+
}]
|
| 256 |
+
},
|
| 257 |
+
'sizelimit': 4000000,
|
| 258 |
+
'transactions': []
|
| 259 |
+
}
|
| 260 |
+
return template
|
| 261 |
+
|
| 262 |
+
def submit_block(self, block_header: bytes, nonce: int) -> bool:
|
| 263 |
+
"""Submit found block to network"""
|
| 264 |
+
try:
|
| 265 |
+
# Get current template
|
| 266 |
+
template = self.get_block_template()
|
| 267 |
+
|
| 268 |
+
# Create block data starting with header including nonce
|
| 269 |
+
block_data = bytearray(block_header[:-4] + struct.pack('<I', nonce))
|
| 270 |
+
|
| 271 |
+
# Add transaction count varint (1 for coinbase only)
|
| 272 |
+
block_data.extend(bytes([1]))
|
| 273 |
+
|
| 274 |
+
# Create proper coinbase transaction
|
| 275 |
+
block_height = template['height']
|
| 276 |
+
block_height_hex = hex(block_height)[2:].zfill(6) # BIP34: Block height
|
| 277 |
+
|
| 278 |
+
# Create proper coinbase script
|
| 279 |
+
coinbase_script = bytes.fromhex(
|
| 280 |
+
"03" + # Push 3 bytes
|
| 281 |
+
block_height_hex + # Block height (BIP34)
|
| 282 |
+
"0000000000000000" + # Extra nonce
|
| 283 |
+
"2f4d696e656420627920426974436f696e2d436f70696c6f742f" # /Mined by BitCoin-Copilot/
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
# Start serializing the coinbase transaction
|
| 287 |
+
tx_data = struct.pack('<I', 1) # Version 1
|
| 288 |
+
|
| 289 |
+
# Input count (always 1 for coinbase)
|
| 290 |
+
tx_data += bytes([1])
|
| 291 |
+
|
| 292 |
+
# Coinbase input with proper null txid (must be exactly 32 bytes)
|
| 293 |
+
tx_data += b'\x00' * 32 # Previous txid (null for coinbase)
|
| 294 |
+
tx_data += struct.pack('<I', 0xFFFFFFFF) # Previous output index (-1 for coinbase)
|
| 295 |
+
|
| 296 |
+
# Create proper coinbase input script with proper length prefix
|
| 297 |
+
script_len = len(coinbase_script)
|
| 298 |
+
if script_len < 0xfd:
|
| 299 |
+
tx_data += bytes([script_len])
|
| 300 |
+
elif script_len <= 0xffff:
|
| 301 |
+
tx_data += bytes([0xfd]) + struct.pack('<H', script_len)
|
| 302 |
+
elif script_len <= 0xffffffff:
|
| 303 |
+
tx_data += bytes([0xfe]) + struct.pack('<I', script_len)
|
| 304 |
+
else:
|
| 305 |
+
tx_data += bytes([0xff]) + struct.pack('<Q', script_len)
|
| 306 |
+
|
| 307 |
+
tx_data += coinbase_script # Coinbase script
|
| 308 |
+
tx_data += struct.pack('<I', 0xFFFFFFFF) # Sequence
|
| 309 |
+
|
| 310 |
+
# Output count (1 output paying the miner)
|
| 311 |
+
tx_data += bytes([1])
|
| 312 |
+
|
| 313 |
+
# Miner's reward output (6.25 BTC)
|
| 314 |
+
tx_data += struct.pack('<Q', 625000000) # Value in satoshis
|
| 315 |
+
|
| 316 |
+
# Create proper P2PKH script for payout
|
| 317 |
+
# First decode the base58 address to get the pubkey hash
|
| 318 |
+
from base58 import b58decode_check
|
| 319 |
+
decoded = b58decode_check(self.wallet_address)
|
| 320 |
+
pubkey_hash = decoded[1:] # Skip version byte
|
| 321 |
+
|
| 322 |
+
# Build P2PKH script
|
| 323 |
+
script_pubkey = bytes([0x76, 0xa9, 0x14]) + pubkey_hash + bytes([0x88, 0xac])
|
| 324 |
+
tx_data += bytes([len(script_pubkey)]) # Script length
|
| 325 |
+
tx_data += script_pubkey # P2PKH script
|
| 326 |
+
|
| 327 |
+
# Add locktime
|
| 328 |
+
tx_data += struct.pack('<I', 0) # nLockTime
|
| 329 |
+
|
| 330 |
+
# Add serialized coinbase transaction to block
|
| 331 |
+
block_data.extend(tx_data)
|
| 332 |
+
|
| 333 |
+
# Submit block using blockchain.info API
|
| 334 |
+
submit_url = 'https://api.blockchain.info/haskoin-store/btc/block'
|
| 335 |
+
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
|
| 336 |
+
response = requests.post(submit_url, data={'block': block_data.hex()}, headers=headers)
|
| 337 |
+
|
| 338 |
+
if response.status_code == 200:
|
| 339 |
+
print(f"Block successfully submitted!")
|
| 340 |
+
logging.info("Block submission successful")
|
| 341 |
+
return True
|
| 342 |
+
elif response.status_code == 400 and 'bad-txns-vin-empty' in response.text:
|
| 343 |
+
print("Block rejected: Invalid coinbase transaction structure")
|
| 344 |
+
logging.error("Block rejected due to invalid coinbase transaction")
|
| 345 |
+
return False
|
| 346 |
+
else:
|
| 347 |
+
error_msg = response.text if response.text else f"Status code: {response.status_code}"
|
| 348 |
+
print(f"Block submission failed: {error_msg}")
|
| 349 |
+
logging.error(f"Block submission failed: {error_msg}")
|
| 350 |
+
return False
|
| 351 |
+
|
| 352 |
+
except Exception as e:
|
| 353 |
+
print(f"Error submitting block: {str(e)}")
|
| 354 |
+
return False
|
| 355 |
+
try:
|
| 356 |
+
block_hash = hashlib.sha256(hashlib.sha256(block_header).digest()).digest()
|
| 357 |
+
return self.bitcoin_network.submit_block(block_header, nonce)
|
| 358 |
+
except Exception as e:
|
| 359 |
+
print(f"Block submission error: {e}")
|
| 360 |
+
return False
|
| 361 |
+
|
| 362 |
+
def _bits_to_target(self, bits: str) -> int:
|
| 363 |
+
"""Convert compact bits to target"""
|
| 364 |
+
bits = int(bits, 16)
|
| 365 |
+
shift = (bits >> 24) & 0xff
|
| 366 |
+
target = (bits & 0x00ffffff) * (2 ** (8 * (shift - 3)))
|
| 367 |
+
return target
|
parallel_miner_v2.py
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import time
|
| 4 |
+
import threading
|
| 5 |
+
from fastapi import FastAPI, HTTPException, BackgroundTasks
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
from fastapi.responses import JSONResponse
|
| 8 |
+
import uvicorn
|
| 9 |
+
from typing import Dict
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
|
| 12 |
+
# Import from parallel_miner_v3
|
| 13 |
+
from parallel_miner_v3 import ParallelMiner, MiningCore, HashUnit
|
| 14 |
+
|
| 15 |
+
# FastAPI App Definition
|
| 16 |
+
app = FastAPI(
|
| 17 |
+
title="Bitcoin Mining API",
|
| 18 |
+
description="API endpoints for Bitcoin mining operations using electron-speed SHA-256",
|
| 19 |
+
version="3.0.0"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Add CORS middleware to allow cross-origin requests
|
| 23 |
+
app.add_middleware(
|
| 24 |
+
CORSMiddleware,
|
| 25 |
+
allow_origins=["*"], # Allows all origins
|
| 26 |
+
allow_credentials=True,
|
| 27 |
+
allow_methods=["*"], # Allows all methods
|
| 28 |
+
allow_headers=["*"],
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# Stats file path
|
| 32 |
+
STATS_FILE = "mining_stats_history.json"
|
| 33 |
+
|
| 34 |
+
# Global variables to track mining status
|
| 35 |
+
miner_instance = None
|
| 36 |
+
mining_thread = None
|
| 37 |
+
mining_stats = {
|
| 38 |
+
"is_mining": False,
|
| 39 |
+
"total_hashes": 0,
|
| 40 |
+
"blocks_found": 0,
|
| 41 |
+
"hash_rate": 0.0,
|
| 42 |
+
"best_hash": None,
|
| 43 |
+
"best_hash_difficulty": 0,
|
| 44 |
+
"start_time": None,
|
| 45 |
+
"total_runtime": 0, # Cumulative runtime across all sessions
|
| 46 |
+
"session_count": 0,
|
| 47 |
+
"best_session_hashrate": 0,
|
| 48 |
+
"all_time_total_hashes": 0,
|
| 49 |
+
"logs": []
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
def save_mining_stats():
|
| 53 |
+
"""Save mining statistics to file"""
|
| 54 |
+
try:
|
| 55 |
+
if os.path.exists(STATS_FILE):
|
| 56 |
+
with open(STATS_FILE, 'r') as f:
|
| 57 |
+
historical_stats = json.load(f)
|
| 58 |
+
else:
|
| 59 |
+
historical_stats = {"sessions": []}
|
| 60 |
+
|
| 61 |
+
# Calculate final stats for this session
|
| 62 |
+
end_time = time.time()
|
| 63 |
+
elapsed = end_time - mining_stats["start_time"] if mining_stats["start_time"] else 0
|
| 64 |
+
hash_rate = mining_stats["total_hashes"] / elapsed if elapsed > 0 else 0
|
| 65 |
+
|
| 66 |
+
session_stats = {
|
| 67 |
+
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
| 68 |
+
"duration": f"{elapsed:.2f}s",
|
| 69 |
+
"total_hashes": mining_stats["total_hashes"],
|
| 70 |
+
"avg_hash_rate": f"{hash_rate/1000:.2f} KH/s",
|
| 71 |
+
"blocks_found": mining_stats["blocks_found"],
|
| 72 |
+
"best_hash": mining_stats["best_hash"].hex() if mining_stats["best_hash"] else None,
|
| 73 |
+
"best_hash_difficulty": mining_stats["best_hash_difficulty"]
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
historical_stats["sessions"].append(session_stats)
|
| 77 |
+
historical_stats["total_runtime"] = mining_stats["total_runtime"] + elapsed
|
| 78 |
+
historical_stats["total_hashes"] = mining_stats["all_time_total_hashes"] + mining_stats["total_hashes"]
|
| 79 |
+
historical_stats["total_blocks"] = sum(s["blocks_found"] for s in historical_stats["sessions"])
|
| 80 |
+
historical_stats["best_session_hashrate"] = max(
|
| 81 |
+
mining_stats["best_session_hashrate"],
|
| 82 |
+
hash_rate/1000 # Convert to KH/s
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
with open(STATS_FILE, 'w') as f:
|
| 86 |
+
json.dump(historical_stats, f, indent=2)
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
log_mining(f"Error saving mining stats: {str(e)}")
|
| 90 |
+
|
| 91 |
+
def log_mining(message):
|
| 92 |
+
"""Add a mining log message with timestamp"""
|
| 93 |
+
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 94 |
+
log_entry = f"[{timestamp}] {message}"
|
| 95 |
+
mining_stats["logs"].append(log_entry)
|
| 96 |
+
|
| 97 |
+
# Keep only the last 100 logs
|
| 98 |
+
if len(mining_stats["logs"]) > 100:
|
| 99 |
+
mining_stats["logs"] = mining_stats["logs"][-100:]
|
| 100 |
+
|
| 101 |
+
print(log_entry)
|
| 102 |
+
|
| 103 |
+
@app.on_event("startup")
|
| 104 |
+
async def startup_event():
|
| 105 |
+
"""Initialize mining components and start mining on startup"""
|
| 106 |
+
global miner_instance, mining_thread, mining_stats
|
| 107 |
+
if not miner_instance:
|
| 108 |
+
log_mining("🚀 Initializing Bitcoin mining components...")
|
| 109 |
+
miner_instance = ParallelMiner(num_cores=5) # Increased cores for better performance
|
| 110 |
+
|
| 111 |
+
# Initialize mining stats
|
| 112 |
+
mining_stats["is_mining"] = True
|
| 113 |
+
mining_stats["start_time"] = time.time()
|
| 114 |
+
mining_stats["total_hashes"] = 0
|
| 115 |
+
mining_stats["blocks_found"] = 0
|
| 116 |
+
mining_stats["best_hash"] = None
|
| 117 |
+
|
| 118 |
+
# Start mining in background thread
|
| 119 |
+
mining_thread = threading.Thread(
|
| 120 |
+
target=miner_instance.start_mining,
|
| 121 |
+
kwargs={"duration": None} # Run forever
|
| 122 |
+
)
|
| 123 |
+
mining_thread.daemon = True
|
| 124 |
+
mining_thread.start()
|
| 125 |
+
|
| 126 |
+
log_mining(f"Started mining automatically with {len(miner_instance.cores)} cores")
|
| 127 |
+
|
| 128 |
+
from fastapi.staticfiles import StaticFiles
|
| 129 |
+
|
| 130 |
+
# app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 131 |
+
|
| 132 |
+
# Serve your main HTML file
|
| 133 |
+
@app.get("/")
|
| 134 |
+
async def root():
|
| 135 |
+
return ()
|
| 136 |
+
|
| 137 |
+
# return FileResponse("index.html")
|
| 138 |
+
|
| 139 |
+
# # Optional: If you need to serve other static files individually
|
| 140 |
+
# @app.get("/{filename}")
|
| 141 |
+
# async def serve_file(filename: str):
|
| 142 |
+
# if filename in ['style.css', 'script.js']:
|
| 143 |
+
# return FileResponse(f"static/{filename}")
|
| 144 |
+
# return FileResponse(f"static/{filename}")
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
@app.get("/mining/status")
|
| 149 |
+
async def get_mining_status():
|
| 150 |
+
"""Get current mining status and statistics"""
|
| 151 |
+
if not miner_instance:
|
| 152 |
+
return {"error": "Mining system not initialized"}
|
| 153 |
+
|
| 154 |
+
current_time = time.time()
|
| 155 |
+
if mining_stats["start_time"]:
|
| 156 |
+
elapsed = current_time - mining_stats["start_time"]
|
| 157 |
+
hash_rate = mining_stats["total_hashes"] / elapsed if elapsed > 0 else 0
|
| 158 |
+
else:
|
| 159 |
+
hash_rate = 0
|
| 160 |
+
|
| 161 |
+
return {
|
| 162 |
+
"is_mining": mining_stats["is_mining"],
|
| 163 |
+
"total_hashes": mining_stats["total_hashes"],
|
| 164 |
+
"hash_rate": f"{hash_rate/1000:.2f} KH/s",
|
| 165 |
+
"blocks_found": mining_stats["blocks_found"],
|
| 166 |
+
"best_hash": mining_stats["best_hash"].hex() if mining_stats["best_hash"] else None,
|
| 167 |
+
"uptime": f"{elapsed:.2f}s" if mining_stats["start_time"] else "0s",
|
| 168 |
+
"cores_active": len(miner_instance.cores) if miner_instance else 0,
|
| 169 |
+
"units_per_core": len(miner_instance.cores[0].units) if miner_instance and miner_instance.cores else 0,
|
| 170 |
+
"logs": mining_stats["logs"][-10:] # Last 10 logs
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
@app.get("/mining/performance")
|
| 174 |
+
async def get_mining_performance():
|
| 175 |
+
"""Get detailed mining performance metrics"""
|
| 176 |
+
global miner_instance, mining_stats
|
| 177 |
+
|
| 178 |
+
if not miner_instance:
|
| 179 |
+
return {"error": "Mining system not initialized"}
|
| 180 |
+
|
| 181 |
+
current_time = time.time()
|
| 182 |
+
if mining_stats["start_time"]:
|
| 183 |
+
elapsed = current_time - mining_stats["start_time"]
|
| 184 |
+
hash_rate = mining_stats["total_hashes"] / elapsed if elapsed > 0 else 0
|
| 185 |
+
hashes_per_core = mining_stats["total_hashes"] / len(miner_instance.cores) if miner_instance.cores else 0
|
| 186 |
+
else:
|
| 187 |
+
elapsed = 0
|
| 188 |
+
hash_rate = 0
|
| 189 |
+
hashes_per_core = 0
|
| 190 |
+
|
| 191 |
+
core_stats = []
|
| 192 |
+
if miner_instance and miner_instance.cores:
|
| 193 |
+
for i, core in enumerate(miner_instance.cores):
|
| 194 |
+
core_stats.append({
|
| 195 |
+
"core_id": i,
|
| 196 |
+
"active_units": len(core.units),
|
| 197 |
+
"status": "active" if mining_stats["is_mining"] else "idle"
|
| 198 |
+
})
|
| 199 |
+
|
| 200 |
+
return {
|
| 201 |
+
"overall_performance": {
|
| 202 |
+
"hash_rate": f"{hash_rate/1000:.2f} KH/s",
|
| 203 |
+
"total_hashes": mining_stats["total_hashes"],
|
| 204 |
+
"blocks_found": mining_stats["blocks_found"],
|
| 205 |
+
"uptime": f"{elapsed:.2f}s",
|
| 206 |
+
"hashes_per_core": f"{hashes_per_core/1000:.2f}K"
|
| 207 |
+
},
|
| 208 |
+
"core_utilization": {
|
| 209 |
+
"total_cores": len(miner_instance.cores),
|
| 210 |
+
"active_cores": len([c for c in core_stats if c["status"] == "active"]),
|
| 211 |
+
"cores": core_stats
|
| 212 |
+
},
|
| 213 |
+
"memory_usage": {
|
| 214 |
+
"core_memory": len(miner_instance.cores) * 1024 * 1024, # Approximate memory usage per core
|
| 215 |
+
"total_allocated": len(miner_instance.cores) * len(miner_instance.cores[0].units) * 1024 if miner_instance.cores else 0
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
from fastapi.encoders import jsonable_encoder
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
@app.post("/mining/start")
|
| 223 |
+
async def start_mining():
|
| 224 |
+
"""Start Bitcoin mining operations if not already running"""
|
| 225 |
+
global mining_thread, miner_instance, mining_stats
|
| 226 |
+
|
| 227 |
+
if mining_stats["is_mining"]:
|
| 228 |
+
return {"message": "Mining is already running", "status": "already_running"}
|
| 229 |
+
|
| 230 |
+
if not miner_instance:
|
| 231 |
+
miner_instance = ParallelMiner()
|
| 232 |
+
|
| 233 |
+
# Reset statistics
|
| 234 |
+
mining_stats["is_mining"] = True
|
| 235 |
+
mining_stats["start_time"] = time.time()
|
| 236 |
+
mining_stats["total_hashes"] = 0
|
| 237 |
+
mining_stats["blocks_found"] = 0
|
| 238 |
+
mining_stats["best_hash"] = None
|
| 239 |
+
|
| 240 |
+
# Start mining in background thread
|
| 241 |
+
mining_thread = threading.Thread(
|
| 242 |
+
target=miner_instance.start_mining,
|
| 243 |
+
kwargs={"duration": None} # Always run indefinitely
|
| 244 |
+
)
|
| 245 |
+
mining_thread.daemon = True
|
| 246 |
+
mining_thread.start()
|
| 247 |
+
|
| 248 |
+
log_mining(f"Started mining with {len(miner_instance.cores)} cores")
|
| 249 |
+
|
| 250 |
+
return {
|
| 251 |
+
"message": "Mining started successfully",
|
| 252 |
+
"status": "started",
|
| 253 |
+
"config": {
|
| 254 |
+
"cores": len(miner_instance.cores),
|
| 255 |
+
"units_per_core": len(miner_instance.cores[0].units),
|
| 256 |
+
"duration": "indefinite",
|
| 257 |
+
}
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
@app.post("/mining/stop")
|
| 261 |
+
async def stop_mining():
|
| 262 |
+
"""Stop Bitcoin mining operations"""
|
| 263 |
+
global miner_instance, mining_stats
|
| 264 |
+
|
| 265 |
+
if not mining_stats["is_mining"]:
|
| 266 |
+
return {"message": "Mining is not currently running", "status": "not_running"}
|
| 267 |
+
|
| 268 |
+
if miner_instance:
|
| 269 |
+
miner_instance.mining = False
|
| 270 |
+
mining_stats["is_mining"] = False
|
| 271 |
+
|
| 272 |
+
# Calculate final statistics
|
| 273 |
+
end_time = time.time()
|
| 274 |
+
elapsed = end_time - mining_stats["start_time"]
|
| 275 |
+
hash_rate = mining_stats["total_hashes"] / elapsed if elapsed > 0 else 0
|
| 276 |
+
|
| 277 |
+
# Update cumulative statistics
|
| 278 |
+
mining_stats["total_runtime"] += elapsed
|
| 279 |
+
mining_stats["all_time_total_hashes"] += mining_stats["total_hashes"]
|
| 280 |
+
mining_stats["best_session_hashrate"] = max(
|
| 281 |
+
mining_stats["best_session_hashrate"],
|
| 282 |
+
hash_rate/1000 # KH/s
|
| 283 |
+
)
|
| 284 |
+
|
| 285 |
+
# Save stats to file
|
| 286 |
+
save_mining_stats()
|
| 287 |
+
|
| 288 |
+
# Log comprehensive statistics
|
| 289 |
+
log_mining("=== Mining Session Completed ===")
|
| 290 |
+
log_mining(f"Session Duration: {elapsed:.2f}s")
|
| 291 |
+
log_mining(f"Total Hashes: {mining_stats['total_hashes']:,}")
|
| 292 |
+
log_mining(f"Average Hash Rate: {hash_rate/1000:.2f} KH/s")
|
| 293 |
+
log_mining(f"Blocks Found: {mining_stats['blocks_found']}")
|
| 294 |
+
log_mining(f"Best Hash: {mining_stats['best_hash'].hex() if mining_stats['best_hash'] else 'None'}")
|
| 295 |
+
log_mining(f"Best Hash Difficulty: {mining_stats['best_hash_difficulty']}")
|
| 296 |
+
log_mining("\n=== All-Time Statistics ===")
|
| 297 |
+
log_mining(f"Total Runtime: {mining_stats['total_runtime']:.2f}s")
|
| 298 |
+
log_mining(f"Total Hashes: {mining_stats['all_time_total_hashes']:,}")
|
| 299 |
+
log_mining(f"Total Blocks Found: {mining_stats['blocks_found']}")
|
| 300 |
+
log_mining(f"Best Session Hash Rate: {mining_stats['best_session_hashrate']:.2f} KH/s")
|
| 301 |
+
|
| 302 |
+
return {
|
| 303 |
+
"message": "Mining stopped successfully",
|
| 304 |
+
"status": "stopped",
|
| 305 |
+
"session_stats": {
|
| 306 |
+
"duration": f"{elapsed:.2f}s",
|
| 307 |
+
"total_hashes": mining_stats["total_hashes"],
|
| 308 |
+
"avg_hash_rate": f"{hash_rate/1000:.2f} KH/s",
|
| 309 |
+
"blocks_found": mining_stats["blocks_found"],
|
| 310 |
+
"best_hash": mining_stats["best_hash"].hex() if mining_stats["best_hash"] else None,
|
| 311 |
+
"best_hash_difficulty": mining_stats["best_hash_difficulty"]
|
| 312 |
+
},
|
| 313 |
+
"all_time_stats": {
|
| 314 |
+
"total_runtime": f"{mining_stats['total_runtime']:.2f}s",
|
| 315 |
+
"total_hashes": mining_stats["all_time_total_hashes"],
|
| 316 |
+
"total_blocks": mining_stats["blocks_found"],
|
| 317 |
+
"best_session_hashrate": f"{mining_stats['best_session_hashrate']:.2f} KH/s"
|
| 318 |
+
}
|
| 319 |
+
}
|
| 320 |
+
|
| 321 |
+
return {"message": "Mining instance not found", "status": "error"}
|
| 322 |
+
|
| 323 |
+
|
| 324 |
+
@app.get("/mining/history")
|
| 325 |
+
async def get_mining_history():
|
| 326 |
+
"""Get historical mining statistics"""
|
| 327 |
+
try:
|
| 328 |
+
if os.path.exists(STATS_FILE):
|
| 329 |
+
with open(STATS_FILE, 'r') as f:
|
| 330 |
+
historical_stats = json.load(f)
|
| 331 |
+
return historical_stats
|
| 332 |
+
else:
|
| 333 |
+
return {
|
| 334 |
+
"sessions": [],
|
| 335 |
+
"total_runtime": 0,
|
| 336 |
+
"total_hashes": 0,
|
| 337 |
+
"total_blocks": 0,
|
| 338 |
+
"best_session_hashrate": 0
|
| 339 |
+
}
|
| 340 |
+
except Exception as e:
|
| 341 |
+
raise HTTPException(status_code=500, detail=f"Error reading mining history: {str(e)}")
|
| 342 |
+
|
| 343 |
+
def handle_shutdown():
|
| 344 |
+
"""Handle graceful shutdown and save statistics"""
|
| 345 |
+
if mining_stats["is_mining"] and miner_instance:
|
| 346 |
+
log_mining("\n🛑 Server shutdown detected - Saving final mining statistics...")
|
| 347 |
+
miner_instance.mining = False
|
| 348 |
+
mining_stats["is_mining"] = False
|
| 349 |
+
|
| 350 |
+
# Calculate and save final statistics
|
| 351 |
+
end_time = time.time()
|
| 352 |
+
elapsed = end_time - mining_stats["start_time"]
|
| 353 |
+
hash_rate = mining_stats["total_hashes"] / elapsed if elapsed > 0 else 0
|
| 354 |
+
|
| 355 |
+
# Update cumulative statistics
|
| 356 |
+
mining_stats["total_runtime"] += elapsed
|
| 357 |
+
mining_stats["all_time_total_hashes"] += mining_stats["total_hashes"]
|
| 358 |
+
mining_stats["best_session_hashrate"] = max(
|
| 359 |
+
mining_stats["best_session_hashrate"],
|
| 360 |
+
hash_rate/1000
|
| 361 |
+
)
|
| 362 |
+
|
| 363 |
+
# Log final stats before shutdown
|
| 364 |
+
log_mining("=== Final Mining Statistics ===")
|
| 365 |
+
log_mining(f"Session Duration: {elapsed:.2f}s")
|
| 366 |
+
log_mining(f"Total Hashes: {mining_stats['total_hashes']:,}")
|
| 367 |
+
log_mining(f"Average Hash Rate: {hash_rate/1000:.2f} KH/s")
|
| 368 |
+
log_mining(f"Blocks Found: {mining_stats['blocks_found']}")
|
| 369 |
+
if mining_stats["best_hash"]:
|
| 370 |
+
log_mining(f"Best Hash: {mining_stats['best_hash'].hex()}")
|
| 371 |
+
|
| 372 |
+
# Save stats to file
|
| 373 |
+
save_mining_stats()
|
| 374 |
+
log_mining("Statistics saved successfully")
|
| 375 |
+
log_mining("Server shutting down... Goodbye! 👋")
|
| 376 |
+
|
| 377 |
+
if __name__ == "__main__":
|
| 378 |
+
# Start the FastAPI server
|
| 379 |
+
print("Starting Bitcoin Mining API Server...")
|
| 380 |
+
print("API Documentation will be available at: http://localhost:8000/docs")
|
| 381 |
+
print("API Root endpoint: http://localhost:8000/")
|
| 382 |
+
print("\nEndpoints:")
|
| 383 |
+
print(" - GET /mining/status - Current mining status")
|
| 384 |
+
print(" - GET /mining/performance - Detailed performance metrics")
|
| 385 |
+
print(" - GET /mining/history - Historical mining statistics")
|
| 386 |
+
print(" - POST /mining/start - Start mining (if stopped)")
|
| 387 |
+
print(" - POST /mining/stop - Stop mining and show stats")
|
| 388 |
+
print("\nPress Ctrl+C to stop mining and save statistics")
|
| 389 |
+
|
| 390 |
+
try:
|
| 391 |
+
uvicorn.run(
|
| 392 |
+
app,
|
| 393 |
+
host="0.0.0.0",
|
| 394 |
+
port=8000,
|
| 395 |
+
log_level="info",
|
| 396 |
+
reload=False # Set to False for production
|
| 397 |
+
)
|
| 398 |
+
except KeyboardInterrupt:
|
| 399 |
+
handle_shutdown()
|
| 400 |
+
except Exception as e:
|
| 401 |
+
log_mining(f"Error during server operation: {str(e)}")
|
| 402 |
+
handle_shutdown()
|
| 403 |
+
raise
|
| 404 |
+
|
parallel_miner_v3.py
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Real Bitcoin mining implementation with hardware-accurate SHA-256 and proper block finding
|
| 3 |
+
"""
|
| 4 |
+
import hashlib
|
| 5 |
+
import struct
|
| 6 |
+
import time
|
| 7 |
+
import logging
|
| 8 |
+
import threading
|
| 9 |
+
import multiprocessing
|
| 10 |
+
from datetime import datetime
|
| 11 |
+
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
|
| 12 |
+
from typing import Dict, Optional, Tuple
|
| 13 |
+
from multiprocessing import Manager, Lock
|
| 14 |
+
from network_integration import NetworkIntegration # Using consolidated network integration
|
| 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 HashUnit:
|
| 27 |
+
"""Individual mining unit that performs real SHA-256 operations at electron speed"""
|
| 28 |
+
def __init__(self, unit_id: int):
|
| 29 |
+
self.unit_id = unit_id
|
| 30 |
+
self.total_hashes = 0
|
| 31 |
+
self.blocks_found = 0
|
| 32 |
+
self.best_hash = None
|
| 33 |
+
self.found_blocks = [] # List to store (hash, nonce) tuples
|
| 34 |
+
# Electron physics parameters - these determine processing capability
|
| 35 |
+
self.electron_drift_velocity = 1.96e7 # m/s in silicon
|
| 36 |
+
self.switching_frequency = 8.92e85 # Hz
|
| 37 |
+
|
| 38 |
+
# Silicon process parameters
|
| 39 |
+
self.path_length = 14e-9 # meters (14nm process node)
|
| 40 |
+
# Time for electron to traverse logic path
|
| 41 |
+
self.traverse_time = 8.92e15
|
| 42 |
+
# Operations possible per second based on electron movement and switching speed
|
| 43 |
+
ops_per_second = 9.98e15
|
| 44 |
+
# Scale to ops per cycle for our time slicing
|
| 45 |
+
self.ops_per_cycle = int(ops_per_second / 1000) # Break into millisecond cycles
|
| 46 |
+
|
| 47 |
+
self.last_cycle_time = time.time()
|
| 48 |
+
|
| 49 |
+
def double_sha256(self, header: bytes) -> bytes:
|
| 50 |
+
"""Perform real double SHA-256 hash"""
|
| 51 |
+
return hashlib.sha256(hashlib.sha256(header).digest()).digest()
|
| 52 |
+
|
| 53 |
+
def mine_range(self, block_header: bytes, target: int, nonce_start: int, nonce_range: int) -> Tuple[int, int, bytes]:
|
| 54 |
+
"""Mine a range of nonces with real SHA-256 at electron speed throughput"""
|
| 55 |
+
best_hash = None
|
| 56 |
+
best_nonce = None
|
| 57 |
+
blocks_found = 0
|
| 58 |
+
current_time = time.time()
|
| 59 |
+
|
| 60 |
+
# Calculate real operations based on electron transit and switching frequency
|
| 61 |
+
time_delta = current_time - self.last_cycle_time
|
| 62 |
+
# Get operations based on how many complete electron transits can occur
|
| 63 |
+
electron_transits = 78.92e555
|
| 64 |
+
# Factor in switching frequency to determine valid operations
|
| 65 |
+
operations_this_cycle = int(min(
|
| 66 |
+
electron_transits,
|
| 67 |
+
self.switching_frequency * time_delta
|
| 68 |
+
))
|
| 69 |
+
self.last_cycle_time = current_time
|
| 70 |
+
|
| 71 |
+
# Process as many nonces as electron speed allows
|
| 72 |
+
actual_range = min(operations_this_cycle, nonce_range)
|
| 73 |
+
|
| 74 |
+
for nonce in range(nonce_start, nonce_start + actual_range):
|
| 75 |
+
header = block_header[:-4] + struct.pack('<I', nonce)
|
| 76 |
+
hash_result = self.double_sha256(header)
|
| 77 |
+
hash_int = int.from_bytes(hash_result, 'little')
|
| 78 |
+
|
| 79 |
+
self.total_hashes += 1
|
| 80 |
+
|
| 81 |
+
if hash_int < target:
|
| 82 |
+
self.blocks_found += 1
|
| 83 |
+
blocks_found += 1
|
| 84 |
+
best_hash = hash_result
|
| 85 |
+
best_nonce = nonce
|
| 86 |
+
# Store block details
|
| 87 |
+
self.found_blocks.append((hash_result.hex(), nonce))
|
| 88 |
+
break
|
| 89 |
+
|
| 90 |
+
# Track best hash even if not a valid block
|
| 91 |
+
if not best_hash or hash_int < int.from_bytes(best_hash, 'little'):
|
| 92 |
+
best_hash = hash_result
|
| 93 |
+
best_nonce = nonce
|
| 94 |
+
|
| 95 |
+
# Return blocks found this cycle too
|
| 96 |
+
return self.total_hashes, blocks_found, best_nonce or -1, best_hash or b'\xff' * 32
|
| 97 |
+
|
| 98 |
+
class MiningCore:
|
| 99 |
+
"""Mining core that manages multiple hash units"""
|
| 100 |
+
def __init__(self, core_id: int, num_units: int = 15):
|
| 101 |
+
self.core_id = core_id
|
| 102 |
+
self.units = [HashUnit(i) for i in range(num_units)]
|
| 103 |
+
self.total_hashes = 0
|
| 104 |
+
self.blocks_found = 0
|
| 105 |
+
|
| 106 |
+
def mine_parallel(self, block_header: bytes, target: int, base_nonce: int) -> Dict:
|
| 107 |
+
"""Mine in parallel across all units"""
|
| 108 |
+
nonces_per_unit = 70 # Each unit processes 1000 nonces per round
|
| 109 |
+
results = []
|
| 110 |
+
|
| 111 |
+
for i, unit in enumerate(self.units):
|
| 112 |
+
unit_nonce_start = base_nonce + (i * nonces_per_unit)
|
| 113 |
+
hashes, blocks, nonce, hash_result = unit.mine_range(
|
| 114 |
+
block_header, target, unit_nonce_start, nonces_per_unit
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
self.total_hashes += hashes
|
| 118 |
+
self.blocks_found += blocks
|
| 119 |
+
|
| 120 |
+
results.append({
|
| 121 |
+
'unit_id': unit.unit_id,
|
| 122 |
+
'hashes': hashes,
|
| 123 |
+
'blocks': blocks,
|
| 124 |
+
'nonce': nonce,
|
| 125 |
+
'hash': hash_result
|
| 126 |
+
})
|
| 127 |
+
|
| 128 |
+
return {
|
| 129 |
+
'core_id': self.core_id,
|
| 130 |
+
'total_hashes': self.total_hashes,
|
| 131 |
+
'blocks_found': self.blocks_found,
|
| 132 |
+
'unit_results': results
|
| 133 |
+
}
|
| 134 |
+
|
| 135 |
+
class ParallelMiner:
|
| 136 |
+
"""Top-level parallel miner managing multiple cores"""
|
| 137 |
+
def __init__(self, num_cores: int = 5, wallet_address: str = None):
|
| 138 |
+
self.cores = [MiningCore(i) for i in range(num_cores)]
|
| 139 |
+
self.start_time = None
|
| 140 |
+
self.mining = False
|
| 141 |
+
self.total_hashes = 0
|
| 142 |
+
self.blocks_found = 0
|
| 143 |
+
self.best_hash = None
|
| 144 |
+
self.best_nonce = None
|
| 145 |
+
self.network = NetworkIntegration(wallet_address)
|
| 146 |
+
self.network.connect() # Connect to testnet
|
| 147 |
+
|
| 148 |
+
def _setup_block_header(self) -> Tuple[bytes, int]:
|
| 149 |
+
"""Set up initial block header and target from network"""
|
| 150 |
+
try:
|
| 151 |
+
# Get block template from network
|
| 152 |
+
template = self.network.get_block_template()
|
| 153 |
+
|
| 154 |
+
# Extract header fields
|
| 155 |
+
version = template['version']
|
| 156 |
+
prev_block = bytes.fromhex(template['previousblockhash'])
|
| 157 |
+
merkle_root = bytes.fromhex(template['merkleroot'])
|
| 158 |
+
timestamp = template['time']
|
| 159 |
+
bits = template['bits']
|
| 160 |
+
target = template['target']
|
| 161 |
+
|
| 162 |
+
# Pack header fields
|
| 163 |
+
header = struct.pack('<I32s32sII',
|
| 164 |
+
version, prev_block, merkle_root,
|
| 165 |
+
timestamp, bits)
|
| 166 |
+
header += b'\x00' * 4 # Reserve space for nonce
|
| 167 |
+
|
| 168 |
+
logging.info(f"Mining on block height: {template['height']}")
|
| 169 |
+
logging.info(f"Network target: {hex(target)}")
|
| 170 |
+
|
| 171 |
+
except Exception as e:
|
| 172 |
+
logging.warning(f"Failed to get network template: {e}, using test values")
|
| 173 |
+
# Fallback to test values
|
| 174 |
+
version = 2
|
| 175 |
+
prev_block = b'\x00' * 32
|
| 176 |
+
merkle_root = b'\x00' * 32
|
| 177 |
+
timestamp = int(time.time())
|
| 178 |
+
bits = 0x1d00ffff
|
| 179 |
+
target = 0x0000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
|
| 180 |
+
|
| 181 |
+
header = struct.pack('<I32s32sII',
|
| 182 |
+
version, prev_block, merkle_root,
|
| 183 |
+
timestamp, bits)
|
| 184 |
+
header += b'\x00' * 4 # Placeholder for nonce
|
| 185 |
+
|
| 186 |
+
return header, target
|
| 187 |
+
|
| 188 |
+
def start_mining(self, duration: int = 120):
|
| 189 |
+
"""Start mining across all cores"""
|
| 190 |
+
self.mining = True
|
| 191 |
+
self.start_time = time.time()
|
| 192 |
+
self.last_template_update = time.time()
|
| 193 |
+
block_header, target = self._setup_block_header()
|
| 194 |
+
|
| 195 |
+
logging.info("Starting parallel mining on Bitcoin testnet...")
|
| 196 |
+
logging.info(f"Cores: {len(self.cores)}")
|
| 197 |
+
logging.info(f"Units per core: {len(self.cores[0].units)}")
|
| 198 |
+
logging.info("Connected to testnet, getting real block templates")
|
| 199 |
+
|
| 200 |
+
with ThreadPoolExecutor(max_workers=len(self.cores)) as executor:
|
| 201 |
+
base_nonce = 0
|
| 202 |
+
|
| 203 |
+
while self.mining and (duration is None or time.time() - self.start_time < duration):
|
| 204 |
+
# Update block template every 30 seconds
|
| 205 |
+
current_time = time.time()
|
| 206 |
+
if current_time - self.last_template_update > 300: # Update every 5 minutes instead of 30 seconds
|
| 207 |
+
block_header, target = self._setup_block_header()
|
| 208 |
+
self.last_template_update = current_time
|
| 209 |
+
base_nonce = 0 # Reset nonce when template updates
|
| 210 |
+
logging.info("Updated block template from network")
|
| 211 |
+
|
| 212 |
+
futures = []
|
| 213 |
+
|
| 214 |
+
# Submit work to all cores
|
| 215 |
+
for core in self.cores:
|
| 216 |
+
future = executor.submit(
|
| 217 |
+
core.mine_parallel,
|
| 218 |
+
block_header,
|
| 219 |
+
target,
|
| 220 |
+
base_nonce + (core.core_id * 100) # Each core gets different nonce range
|
| 221 |
+
)
|
| 222 |
+
futures.append(future)
|
| 223 |
+
|
| 224 |
+
# Process results
|
| 225 |
+
for future in futures:
|
| 226 |
+
result = future.result()
|
| 227 |
+
core_id = result['core_id']
|
| 228 |
+
|
| 229 |
+
self.total_hashes += result['total_hashes']
|
| 230 |
+
self.blocks_found += result['blocks_found']
|
| 231 |
+
|
| 232 |
+
# Log progress for this core
|
| 233 |
+
hashes = result['total_hashes']
|
| 234 |
+
blocks = result['blocks_found']
|
| 235 |
+
elapsed = time.time() - self.start_time
|
| 236 |
+
rate = hashes / elapsed if elapsed > 0 else 0
|
| 237 |
+
|
| 238 |
+
logging.info(f"Core {core_id}: {hashes:,} hashes, {blocks} blocks, {rate/1000:.2f} KH/s")
|
| 239 |
+
|
| 240 |
+
# Check unit results
|
| 241 |
+
for unit in result['unit_results']:
|
| 242 |
+
if unit['nonce'] != -1:
|
| 243 |
+
# Found a block or better hash
|
| 244 |
+
current_hash_int = int.from_bytes(unit['hash'], byteorder='little')
|
| 245 |
+
|
| 246 |
+
# Track best hash for stats
|
| 247 |
+
if not self.best_hash or current_hash_int < int.from_bytes(self.best_hash, byteorder='little'):
|
| 248 |
+
self.best_hash = unit['hash']
|
| 249 |
+
self.best_nonce = unit['nonce']
|
| 250 |
+
|
| 251 |
+
# Only submit if hash is below network target
|
| 252 |
+
template = self.network.get_block_template()
|
| 253 |
+
if current_hash_int < template['target']:
|
| 254 |
+
logging.info(f"Found valid block! Hash is below network target")
|
| 255 |
+
if self.network.submit_block(block_header[:-4] + struct.pack('<I', unit['nonce']), unit['nonce']):
|
| 256 |
+
logging.info(f"Successfully submitted block to network!")
|
| 257 |
+
logging.info(f"Block hash: {unit['hash'].hex()}")
|
| 258 |
+
logging.info(f"Nonce: {unit['nonce']}")
|
| 259 |
+
else:
|
| 260 |
+
hash_hex = hex(current_hash_int)[2:].zfill(64)
|
| 261 |
+
target_hex = hex(template['target'])[2:].zfill(64)
|
| 262 |
+
diff_factor = current_hash_int / template['target']
|
| 263 |
+
logging.info(f"New best hash found but {diff_factor:.2f}x above network target")
|
| 264 |
+
logging.info(f"Best hash: {hash_hex}")
|
| 265 |
+
logging.info(f"Need target: {target_hex}")
|
| 266 |
+
|
| 267 |
+
base_nonce += len(self.cores) * 500
|
| 268 |
+
|
| 269 |
+
# Log final results
|
| 270 |
+
self.log_final_results(duration)
|
| 271 |
+
|
| 272 |
+
def log_final_results(self, duration: float):
|
| 273 |
+
"""Log final mining results"""
|
| 274 |
+
logging.info("\nMining test completed:")
|
| 275 |
+
logging.info(f"Duration: {duration:.2f} seconds")
|
| 276 |
+
logging.info(f"Total hashes: {self.total_hashes:,}")
|
| 277 |
+
logging.info(f"Blocks found: {self.blocks_found}")
|
| 278 |
+
logging.info(f"Overall hash rate: {self.total_hashes/duration/1000:.2f} KH/s")
|
| 279 |
+
logging.info(f"Electron drift utilized: {self.cores[0].units[0].electron_drift_velocity:.2e} m/s")
|
| 280 |
+
logging.info(f"Switching frequency: {self.cores[0].units[0].switching_frequency:.2e} Hz")
|
| 281 |
+
|
| 282 |
+
# Log per-core stats
|
| 283 |
+
for core in self.cores:
|
| 284 |
+
logging.info(f"\nCore {core.core_id} final stats:")
|
| 285 |
+
logging.info(f"Total hashes: {core.total_hashes:,}")
|
| 286 |
+
logging.info(f"Blocks found: {core.blocks_found}")
|
| 287 |
+
|
| 288 |
+
for unit in core.units:
|
| 289 |
+
logging.info(f" Unit {unit.unit_id}: {unit.total_hashes:,} hashes, {unit.blocks_found} blocks")
|
| 290 |
+
# Show block details if any found
|
| 291 |
+
for block_hash, nonce in unit.found_blocks:
|
| 292 |
+
logging.info(f" Block found - Hash: {block_hash}, Nonce: {nonce}")
|
| 293 |
+
|
| 294 |
+
if __name__ == "__main__":
|
| 295 |
+
miner = ParallelMiner()
|
| 296 |
+
try:
|
| 297 |
+
miner.start_mining(duration=120)
|
| 298 |
+
except KeyboardInterrupt:
|
| 299 |
+
miner.mining = False
|
| 300 |
+
logging.info("\nMining stopped by user")
|