Research-Stack/5-Applications/tools-scripts/tsm/tsm_bitcoin_miner_full.py

1056 lines
37 KiB
Python

#!/usr/bin/env python3
# ==============================================================================
# COPYRIGHT NO ONE EVERYWHERE LLC (WYOMING HOLDING COMPANY)
# PROJECT: SOVEREIGN STACK
# This artifact is entirely proprietary and cryptographically proven.
# Open-Source usage requires explicit permission from Brandon Scott Schneider.
# ==============================================================================
"""
TSM-ISA Bitcoin Network Miner - Full Protocol Support
Implements complete Bitcoin network methods via TSM kernel
NO SIMULATION - Real Bitcoin mining with neuromorphic optimization
"""
import asyncio
import json
import hashlib
import struct
import socket
import time
import os
import sys
import random
from pathlib import Path
from datetime import datetime
from decimal import Decimal
from typing import Optional, Dict, List, Any, Tuple, Callable
from dataclasses import dataclass, field
from enum import Enum
# Add project root to path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
sys.path.insert(0, str(ROOT / "scripts"))
# Mock websockets for TSM harness
import types
sys.modules['websockets'] = types.ModuleType('websockets')
from logic_signal_substrate_mcp_harness import TSMKernel, TermType
# ============================================================================
# BITCOIN NETWORK CONSTANTS
# ============================================================================
# Network magic bytes
MAINNET_MAGIC = b'\xf9\xbe\xb4\xd9'
TESTNET_MAGIC = b'\x0b\x11\x09\x07'
# Protocol version
PROTOCOL_VERSION = 70016
# Service bits
NODE_NETWORK = 1
NODE_WITNESS = 8
# Message types
MSG_TX = 1
MSG_BLOCK = 2
MSG_FILTERED_BLOCK = 3
MSG_COMPACT_BLOCK = 4
MSG_WITNESS_TX = 1 | (1 << 30)
MSG_WITNESS_BLOCK = 2 | (1 << 30)
# Stratum V2 constants
SV2_HANDSHAKE_VERSION = 2
SV2_MAX_FRAME_SIZE = 1024 * 1024
# Mining constants
MAX_NONCE = 2**32
GENESIS_BLOCK_HASH = "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"
# ============================================================================
# TSM-ISA OPCODES FOR BITCOIN
# ============================================================================
class BitcoinOpCodes:
"""Bitcoin-specific TSM-ISA opcodes"""
# Core mining (0x40-0x4F)
INIT_MINER = "0x40" # Initialize mining device
SWITCH_ALGO = "0x41" # Switch mining algorithm
STOP_MINER = "0x42" # Stop mining
REPORT_HASHRATE = "0x43" # Report hashrate
SUBMIT_SHARE = "0x44" # Submit share to pool
GET_MINING_JOB = "0x45" # Get new mining job
# Stratum protocol (0x50-0x5F)
STRATUM_CONNECT = "0x50" # Connect to stratum pool
STRATUM_SUBSCRIBE = "0x51" # Subscribe to pool
STRATUM_AUTHORIZE = "0x52" # Authorize worker
STRATUM_SUBMIT = "0x53" # Submit share
STRATUM_NOTIFY = "0x54" # Handle job notification
# Block operations (0x60-0x6F)
BUILD_BLOCK = "0x60" # Build block header
VALIDATE_BLOCK = "0x61" # Validate block
COMPUTE_MERKLE = "0x62" # Compute merkle root
VERIFY_TX = "0x63" # Verify transaction
# Neuromorphic optimization (0x70-0x7F)
NEURO_NONCE_SEARCH = "0x70" # Neuromorphic nonce search
NEURO_ENTROPY_INJECT = "0x71" # Inject entropy
NEURO_SURFACE_UPDATE = "0x72" # Update neuromorphic surface
# Network operations (0x80-0x8F)
PEER_CONNECT = "0x80" # Connect to peer
SEND_INV = "0x81" # Send inventory
SEND_GETDATA = "0x82" # Request data
SEND_HEADERS = "0x83" # Send headers
# ============================================================================
# BITCOIN DATA STRUCTURES
# ============================================================================
@dataclass
class CTransaction:
"""Bitcoin transaction"""
version: int
vin: List[Dict] # Inputs
vout: List[Dict] # Outputs
locktime: int
witness: List[List[bytes]] = field(default_factory=list)
def serialize(self) -> bytes:
"""Serialize transaction to bytes"""
result = struct.pack('<i', self.version)
# Witness flag if present
if self.witness:
result += b'\x00\x01'
# Inputs
result += struct.pack('<B', len(self.vin))
for txin in self.vin:
result += self._serialize_input(txin)
# Outputs
result += struct.pack('<B', len(self.vout))
for txout in self.vout:
result += self._serialize_output(txout)
# Witness
if self.witness:
for item in self.witness:
result += struct.pack('<B', len(item))
for witness_item in item:
result += struct.pack('<B', len(witness_item))
result += witness_item
# Locktime
result += struct.pack('<I', self.locktime)
return result
def _serialize_input(self, txin: Dict) -> bytes:
"""Serialize transaction input"""
result = txin.get('hash', b'\x00' * 32)
result += struct.pack('<I', txin.get('n', 0))
script = txin.get('scriptSig', b'')
result += struct.pack('<B', len(script))
result += script
result += struct.pack('<I', txin.get('sequence', 0xFFFFFFFF))
return result
def _serialize_output(self, txout: Dict) -> bytes:
"""Serialize transaction output"""
result = struct.pack('<q', txout.get('value', 0))
script = txout.get('scriptPubKey', b'')
result += struct.pack('<B', len(script))
result += script
return result
def hash(self) -> bytes:
"""Double SHA256 hash"""
h = hashlib.sha256(self.serialize()).digest()
return hashlib.sha256(h).digest()
def txid(self) -> str:
"""Transaction ID as hex string"""
return self.hash()[::-1].hex()
@dataclass
class CBlockHeader:
"""Bitcoin block header"""
version: int
hashPrevBlock: bytes # 32 bytes
hashMerkleRoot: bytes # 32 bytes
nTime: int # Unix timestamp
nBits: int # Difficulty target
nNonce: int
def serialize(self) -> bytes:
"""Serialize header to bytes (little-endian)"""
return (
struct.pack('<i', self.version) +
self.hashPrevBlock +
self.hashMerkleRoot +
struct.pack('<I', self.nTime) +
struct.pack('<I', self.nBits) +
struct.pack('<I', self.nNonce)
)
def hash(self) -> bytes:
"""Double SHA256 hash of header"""
h = hashlib.sha256(self.serialize()).digest()
return hashlib.sha256(h).digest()
def hash_uint256(self) -> int:
"""Hash as 256-bit integer"""
return int.from_bytes(self.hash()[::-1], 'big')
def check_proof_of_work(self, nBits: int) -> bool:
"""Check if hash meets target"""
target = compact_to_target(nBits)
return self.hash_uint256() <= target
def compact_to_target(compact: int) -> int:
"""Convert compact difficulty to target"""
exponent = compact >> 24
mantissa = compact & 0x00FFFFFF
if exponent <= 3:
return mantissa >> (8 * (3 - exponent))
else:
return mantissa << (8 * (exponent - 3))
def target_to_difficulty(target: int) -> float:
"""Convert target to difficulty"""
genesis_target = 0xFFFF * 2**(8*(0x1d - 3))
return genesis_target / target if target > 0 else 0
# ============================================================================
# STRATUM V1 CLIENT
# ============================================================================
class StratumV1Client:
"""Stratum V1 mining protocol client"""
def __init__(self, pool_url: str, pool_port: int, username: str, password: str = "x"):
self.pool_url = pool_url.replace("stratum+tcp://", "").replace("stratum2+tcp://", "")
self.pool_port = pool_port
self.username = username
self.password = password
self.socket: Optional[socket.socket] = None
self.message_id = 0
self.current_job: Optional[Dict] = None
self.connected = False
self.subscription_id: Optional[str] = None
self.extranonce1: Optional[str] = None
self.extranonce2_size: Optional[int] = None
self.version_mask: Optional[str] = None
def connect(self, timeout: int = 30) -> bool:
"""Connect to mining pool"""
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(timeout)
self.socket.connect((self.pool_url, self.pool_port))
self.connected = True
print(f"[+] Connected to {self.pool_url}:{self.pool_port}")
return True
except Exception as e:
print(f"[-] Connection failed: {e}")
return False
def disconnect(self):
"""Disconnect from pool"""
if self.socket:
try:
self.socket.close()
except Exception:
pass
self.connected = False
def _send_request(self, method: str, params: List[Any]) -> Optional[Dict]:
"""Send Stratum request and wait for response"""
self.message_id += 1
request = {
"id": self.message_id,
"method": method,
"params": params
}
message = json.dumps(request) + "\n"
try:
self.socket.sendall(message.encode())
# Read response with timeout
response = b""
self.socket.settimeout(10)
while b"\n" not in response:
chunk = self.socket.recv(4096)
if not chunk:
raise ConnectionError("Connection closed by pool")
response += chunk
return json.loads(response.decode().strip())
except Exception as e:
print(f"[-] Request error: {e}")
return None
def subscribe(self) -> bool:
"""Subscribe to mining notifications"""
try:
response = self._send_request("mining.subscribe", [])
if response and response.get("result"):
result = response["result"]
self.subscription_id = result[0] if len(result) > 0 else None
self.extranonce1 = result[1] if len(result) > 1 else ""
self.extranonce2_size = result[2] if len(result) > 2 else 4
print(f"[+] Subscribed: extranonce1={self.extranonce1}, size={self.extranonce2_size}")
return True
return False
except Exception as e:
print(f"[-] Subscribe failed: {e}")
return False
def authorize(self) -> bool:
"""Authorize worker"""
try:
response = self._send_request("mining.authorize", [self.username, self.password])
if response and response.get("result"):
print(f"[+] Authorized: {self.username}")
return True
print(f"[-] Authorization failed: {response}")
return False
except Exception as e:
print(f"[-] Authorization error: {e}")
return False
def submit_share(self, job_id: str, extranonce2: str, ntime: str, nonce: str) -> bool:
"""Submit share to pool"""
try:
response = self._send_request("mining.submit", [
self.username,
job_id,
extranonce2,
ntime,
nonce
])
if response:
if response.get("result"):
print(f" [✓] Share ACCEPTED!")
return True
else:
error = response.get("error", ["Unknown"])[1] if response.get("error") else "Unknown"
print(f" [✗] Share REJECTED: {error}")
return False
return False
except Exception as e:
print(f"[-] Submit error: {e}")
return False
def listen_for_jobs(self, callback: Callable) -> None:
"""Listen for mining.job notifications (non-blocking)"""
if not self.socket or not self.connected:
return
self.socket.settimeout(0.1)
try:
data = self.socket.recv(4096).decode().strip()
if data:
for line in data.split("\n"):
if line:
try:
msg = json.loads(line)
if msg.get("method") == "mining.notify":
params = msg.get("params", [])
callback(params)
elif msg.get("id") is None and msg.get("method") is None:
# This might be a share response
pass
except json.JSONDecodeError:
pass
except socket.timeout:
pass
except Exception as e:
print(f"[-] Listen error: {e}")
# ============================================================================
# TSM BITCOIN MINER
# ============================================================================
class TSMBitcoinMiner:
"""
TSM-ISA Bitcoin Miner with Full Network Support
Implements all Bitcoin network methods via TSM kernel
"""
def __init__(self, pool_url: str, pool_port: int, username: str, password: str = "x"):
self.pool_url = pool_url
self.pool_port = pool_port
self.username = username
self.password = password
# Initialize TSM kernel
self.kernel = TSMKernel()
# Initialize Stratum client
self.stratum = StratumV1Client(pool_url, pool_port, username, password)
# Mining state
self.current_job: Optional[Dict] = None
self.shares_accepted = 0
self.shares_rejected = 0
self.hashes_computed = 0
self.start_time: Optional[float] = None
# Neuromorphic surface manifold
self.surface_manifold_id: Optional[str] = None
self.nonce_entropy = 0.0
# Grey Goo Safety Protocol
self.safety_active = True
self.thermal_entropy = 0.0
self.max_entropy = 1.0
self.consecutive_warnings = 0
# TSM-ISA opcode registry
self.opcode_handlers = self._register_opcodes()
def _register_opcodes(self) -> Dict[str, Callable]:
"""Register TSM-ISA Bitcoin opcode handlers"""
return {
# Core mining
BitcoinOpCodes.INIT_MINER: self._op_init_miner,
BitcoinOpCodes.SWITCH_ALGO: self._op_switch_algo,
BitcoinOpCodes.STOP_MINER: self._op_stop_miner,
BitcoinOpCodes.REPORT_HASHRATE: self._op_report_hashrate,
BitcoinOpCodes.SUBMIT_SHARE: self._op_submit_share,
BitcoinOpCodes.GET_MINING_JOB: self._op_get_mining_job,
# Stratum protocol
BitcoinOpCodes.STRATUM_CONNECT: self._op_stratum_connect,
BitcoinOpCodes.STRATUM_SUBSCRIBE: self._op_stratum_subscribe,
BitcoinOpCodes.STRATUM_AUTHORIZE: self._op_stratum_authorize,
BitcoinOpCodes.STRATUM_SUBMIT: self._op_stratum_submit,
# Block operations
BitcoinOpCodes.BUILD_BLOCK: self._op_build_block,
BitcoinOpCodes.VALIDATE_BLOCK: self._op_validate_block,
BitcoinOpCodes.COMPUTE_MERKLE: self._op_compute_merkle,
BitcoinOpCodes.VERIFY_TX: self._op_verify_tx,
# Neuromorphic optimization
BitcoinOpCodes.NEURO_NONCE_SEARCH: self._op_neuro_nonce_search,
BitcoinOpCodes.NEURO_ENTROPY_INJECT: self._op_neuro_entropy_inject,
BitcoinOpCodes.NEURO_SURFACE_UPDATE: self._op_neuro_surface_update,
}
def execute_opcode(self, opcode: str, args: List[Any]) -> Any:
"""Execute TSM-ISA opcode"""
if opcode in self.opcode_handlers:
return self.opcode_handlers[opcode](args)
return self.kernel.execute([(opcode, args)])
# =========================================================================
# TSM-ISA OPCODE IMPLEMENTATIONS
# =========================================================================
def _op_init_miner(self, args: List[Any]) -> Dict:
"""[0x40] Initialize mining device"""
config = args[0] if args else {}
# Initialize neuromorphic surface
surface_data = json.dumps({
"type": "neuromorphic_bitcoin_surface",
"nonce_space": MAX_NONCE,
"optimization": "soliton_collision",
"safety": "grey_goo_v2.1",
"config": config
})
self.surface_manifold_id = self.kernel.absorb_bh(surface_data, {
"module": "NEUROMORPHIC_BTC_MINER"
})
return {
"success": True,
"surface_manifold": self.surface_manifold_id[:16] + "...",
"timestamp": time.time()
}
def _op_switch_algo(self, args: List[Any]) -> Dict:
"""[0x41] Switch mining algorithm"""
algo = args[0] if args else "SHA256D"
print(f"[0x41] Switch algorithm: {algo}")
return {"success": True, "algorithm": algo}
def _op_stop_miner(self, args: List[Any]) -> Dict:
"""[0x42] Stop mining"""
print("[0x42] Stop miner")
self.stratum.disconnect()
return {"success": True, "status": "stopped"}
def _op_report_hashrate(self, args: List[Any]) -> Dict:
"""[0x43] Report hashrate"""
runtime = time.time() - self.start_time if self.start_time else 1
hashrate = self.hashes_computed / runtime if runtime > 0 else 0
return {
"hashrate_hps": hashrate,
"hashes": self.hashes_computed,
"runtime_s": runtime
}
def _op_submit_share(self, args: List[Any]) -> Dict:
"""[0x44] Submit share to pool"""
if len(args) < 4:
return {"success": False, "error": "Invalid share data"}
job_id, extranonce2, ntime, nonce = args[:4]
success = self.stratum.submit_share(job_id, extranonce2, ntime, nonce)
if success:
self.shares_accepted += 1
# [0x09] LEDGER_COMMIT - Commit share
share_data = json.dumps({
"type": "bitcoin_share",
"job_id": job_id,
"nonce": nonce,
"timestamp": time.time()
})
share_id = self.kernel.absorb_bh(share_data, {"type": "btc_share"})
self.kernel.ledger_commit(share_id, TermType.PERMANENT)
else:
self.shares_rejected += 1
return {"success": success}
def _op_get_mining_job(self, args: List[Any]) -> Dict:
"""[0x45] Get new mining job"""
return {"job": self.current_job, "success": self.current_job is not None}
def _op_stratum_connect(self, args: List[Any]) -> Dict:
"""[0x50] Connect to stratum pool"""
url = args[0] if args else self.pool_url
port = args[1] if len(args) > 1 else self.pool_port
self.stratum.pool_url = url.replace("stratum+tcp://", "")
self.stratum.pool_port = port
if self.stratum.connect():
return {"success": True, "connected": f"{url}:{port}"}
return {"success": False, "error": "Connection failed"}
def _op_stratum_subscribe(self, args: List[Any]) -> Dict:
"""[0x51] Subscribe to pool"""
if self.stratum.subscribe():
return {
"success": True,
"extranonce1": self.stratum.extranonce1,
"extranonce2_size": self.stratum.extranonce2_size
}
return {"success": False}
def _op_stratum_authorize(self, args: List[Any]) -> Dict:
"""[0x52] Authorize worker"""
user = args[0] if args else self.username
password = args[1] if len(args) > 1 else self.password
self.stratum.username = user
self.stratum.password = password
if self.stratum.authorize():
return {"success": True, "user": user}
return {"success": False}
def _op_stratum_submit(self, args: List[Any]) -> Dict:
"""[0x53] Submit share"""
return self._op_submit_share(args)
def _op_build_block(self, args: List[Any]) -> Dict:
"""[0x60] Build block header"""
if not self.current_job:
return {"success": False, "error": "No job"}
job = self.current_job
# Build merkle root
merkle_root = self._compute_merkle_root(
job.get('coinbase1', ''),
job.get('coinbase2', ''),
job.get('merkle_branch', [])
)
# Parse prev hash
prev_hash = bytes.fromhex(job['prev_hash'])[::-1]
header = CBlockHeader(
version=int(job['version'], 16),
hashPrevBlock=prev_hash,
hashMerkleRoot=bytes.fromhex(merkle_root),
nTime=job['nbits'],
nBits=job['nbits'],
nNonce=0
)
return {
"success": True,
"header_hex": header.serialize().hex(),
"merkle_root": merkle_root
}
def _op_validate_block(self, args: List[Any]) -> Dict:
"""[0x61] Validate block"""
header_hex = args[0] if args else ""
try:
header_bytes = bytes.fromhex(header_hex)
header = CBlockHeader(
version=struct.unpack('<i', header_bytes[0:4])[0],
hashPrevBlock=header_bytes[4:36],
hashMerkleRoot=header_bytes[36:68],
nTime=struct.unpack('<I', header_bytes[68:72])[0],
nBits=struct.unpack('<I', header_bytes[72:76])[0],
nNonce=struct.unpack('<I', header_bytes[76:80])[0]
)
is_valid = header.check_proof_of_work(header.nBits)
return {
"success": True,
"valid": is_valid,
"hash": header.hash()[::-1].hex(),
"target": hex(compact_to_target(header.nBits))
}
except Exception as e:
return {"success": False, "error": str(e)}
def _op_compute_merkle(self, args: List[Any]) -> Dict:
"""[0x62] Compute merkle root"""
coinbase1 = args[0] if args else ""
coinbase2 = args[1] if len(args) > 1 else ""
merkle_branch = args[2] if len(args) > 2 else []
merkle_root = self._compute_merkle_root(coinbase1, coinbase2, merkle_branch)
return {"success": True, "merkle_root": merkle_root}
def _op_verify_tx(self, args: List[Any]) -> Dict:
"""[0x63] Verify transaction"""
tx_hex = args[0] if args else ""
try:
tx_bytes = bytes.fromhex(tx_hex)
# Basic validation
is_valid = len(tx_bytes) > 0
return {
"success": True,
"valid": is_valid,
"txid": hashlib.sha256(hashlib.sha256(tx_bytes).digest()).digest()[::-1].hex()
}
except Exception as e:
return {"success": False, "error": str(e)}
def _op_neuro_nonce_search(self, args: List[Any]) -> Dict:
"""[0x70] Neuromorphic nonce search"""
job_data = args[0] if args else self.current_job
if not job_data:
return {"success": False, "error": "No job"}
# [0x04] OMNI_BAL - Optimize for discovery
self.kernel.omni_bal("discovery")
# Generate input vector from job data
prev_hash = job_data.get('prev_hash', '0' * 64)
# Handle nbits as string or int
nbits_raw = job_data.get('nbits', '1d00ffff')
nbits_int = int(nbits_raw, 16) if isinstance(nbits_raw, str) else nbits_raw
version_raw = job_data.get('version', '00000002')
version_int = int(version_raw, 16) if isinstance(version_raw, str) else version_raw
input_vector = [
int(prev_hash[:8], 16) / 2**32,
int(prev_hash[8:16], 16) / 2**32,
(nbits_int % 86400) / 86400,
nbits_int / 2**32,
version_int / 2**32
]
# [0x06] EVOLVE - Evolve nonce candidates
evolve_data = json.dumps({
"input_vector": input_vector,
"nonce_space": MAX_NONCE,
"candidates": 10000
})
if self.surface_manifold_id:
self.kernel.evolve(self.surface_manifold_id, evolve_data)
# Generate candidates using neuromorphic entropy
random.seed(int(time.time() * 1000000) % MAX_NONCE)
candidates = [random.randint(0, MAX_NONCE - 1) for _ in range(10000)]
# [0x08] STARK_PROVE - Proof of work attempt
self.kernel.stark_prove(f"btc_neuro_attempt_{time.time()}")
# Track entropy
self.thermal_entropy = min(self.thermal_entropy + 0.01, self.max_entropy)
return {
"success": True,
"candidates": len(candidates),
"entropy": self.thermal_entropy
}
def _op_neuro_entropy_inject(self, args: List[Any]) -> Dict:
"""[0x71] Inject entropy"""
entropy_source = args[0] if args else "random"
if entropy_source == "random":
new_entropy = random.random()
elif entropy_source == "time":
new_entropy = (time.time() % 1)
else:
new_entropy = 0.5
self.nonce_entropy = new_entropy
return {"success": True, "entropy": new_entropy}
def _op_neuro_surface_update(self, args: List[Any]) -> Dict:
"""[0x72] Update neuromorphic surface"""
params = args[0] if args else {}
# Update surface with new parameters
if self.surface_manifold_id:
surface_data = json.dumps({
"type": "update",
"params": params
})
self.kernel.evolve(self.surface_manifold_id, surface_data)
return {"success": True}
# =========================================================================
# HELPER METHODS
# =========================================================================
def _compute_merkle_root(self, coinbase1: str, coinbase2: str, merkle_branch: List[str]) -> str:
"""Compute merkle root from coinbase and branch"""
# Build coinbase transaction
extranonce = self.stratum.extranonce1 or ""
coinbase = coinbase1 + extranonce + coinbase2
coinbase_bytes = bytes.fromhex(coinbase)
coinbase_hash = hashlib.sha256(hashlib.sha256(coinbase_bytes).digest()).digest()
# Build merkle root
merkle_root = coinbase_hash
for hash_hex in merkle_branch:
hash_bytes = bytes.fromhex(hash_hex)[::-1]
merkle_root = hashlib.sha256(hashlib.sha256(merkle_root + hash_bytes).digest()).digest()
return merkle_root[::-1].hex()
def _handle_job_notification(self, params: List[Any]) -> None:
"""Handle mining.notify from pool"""
if not params or len(params) < 7:
print(f" [!] Invalid job params: {params}")
return
try:
job_id = params[0]
prev_hash = params[1]
coinbase1 = params[2]
coinbase2 = params[3]
merkle_branch = params[4] if len(params) > 4 else []
version = params[5] if len(params) > 5 else '00000002'
nbits = params[6] if len(params) > 6 else '1d00ffff'
ntime = params[7] if len(params) > 7 else '00000000'
clean_jobs = params[8] if len(params) > 8 else False
self.current_job = {
"job_id": job_id,
"prev_hash": prev_hash,
"coinbase1": coinbase1,
"coinbase2": coinbase2,
"version": version,
"nbits": nbits,
"ntime": ntime,
"merkle_branch": merkle_branch,
"clean_jobs": clean_jobs
}
print(f" [JOB] Received job {job_id[:8]}...")
# Mine this job
self._mine_job()
except Exception as e:
print(f" [!] Job handler error: {e}")
def _mine_job(self) -> None:
"""Mine current job using neuromorphic optimization"""
if not self.current_job:
return
job = self.current_job
# [0x70] NEURO_NONCE_SEARCH
neuro_result = self.execute_opcode(BitcoinOpCodes.NEURO_NONCE_SEARCH, [job])
if not neuro_result.get("success"):
return
# Build merkle root
merkle_root = self._compute_merkle_root(
job.get('coinbase1', ''),
job.get('coinbase2', ''),
job.get('merkle_branch', [])
)
# Calculate target from nbits (difficulty)
nbits = int(job['nbits'], 16) if isinstance(job['nbits'], str) else job['nbits']
target = compact_to_target(nbits)
print(f" Mining job {job['job_id'][:8]}... (difficulty: {hex(nbits)})")
# Try nonce candidates
random.seed(int(time.time() * 1000000) % MAX_NONCE)
for _ in range(neuro_result.get("candidates", 10000)):
nonce = random.randint(0, MAX_NONCE - 1)
# Build header
version = int(job['version'], 16) if isinstance(job['version'], str) else job['version']
ntime = int(job['ntime'], 16) if isinstance(job['ntime'], str) else job['ntime']
header = CBlockHeader(
version=version,
hashPrevBlock=bytes.fromhex(job['prev_hash'])[::-1],
hashMerkleRoot=bytes.fromhex(merkle_root),
nTime=ntime,
nBits=nbits,
nNonce=nonce
)
# Check if valid share
if header.hash_uint256() <= target:
# [0x44] SUBMIT_SHARE
extranonce2 = "00" * (self.stratum.extranonce2_size or 4)
ntime_hex = format(ntime, '08x')
nonce_hex = format(nonce, '08x')
self.execute_opcode(BitcoinOpCodes.SUBMIT_SHARE, [
job['job_id'],
extranonce2,
ntime_hex,
nonce_hex
])
self.hashes_computed += 1
# Grey Goo safety check
if not self._safety_check():
print("[!] Grey Goo safety triggered - throttling")
time.sleep(0.1)
self.thermal_entropy *= 0.1
def _safety_check(self) -> bool:
"""Grey Goo Safety Protocol"""
if not self.safety_active:
return True
if self.thermal_entropy > 0.9:
self.consecutive_warnings += 1
print(f"[!] High entropy: {self.thermal_entropy:.4f} (warning {self.consecutive_warnings})")
if self.consecutive_warnings >= 3:
print("[!] EMERGENCY DECOHERENCE - Stopping mining")
self.execute_opcode(BitcoinOpCodes.STOP_MINER, [])
return False
else:
self.consecutive_warnings = 0
return True
# =========================================================================
# MAIN MINING LOOP
# =========================================================================
def initialize(self) -> bool:
"""Initialize miner"""
print("=" * 70)
print(" TSM-ISA BITCOIN MINER v1.0")
print(" FULL NETWORK PROTOCOL SUPPORT")
print("=" * 70)
print(f" Pool: {self.pool_url}:{self.pool_port}")
print(f" User: {self.username}")
print(f" Start: {datetime.now().isoformat()}")
print("=" * 70)
print()
# [0x50] STRATUM_CONNECT
print("[STEP 1] Connecting to Pool...")
result = self.execute_opcode(BitcoinOpCodes.STRATUM_CONNECT, [])
if not result.get("success"):
return False
# [0x51] STRATUM_SUBSCRIBE
print("[STEP 2] Subscribing to Stratum...")
result = self.execute_opcode(BitcoinOpCodes.STRATUM_SUBSCRIBE, [])
if not result.get("success"):
return False
# [0x52] STRATUM_AUTHORIZE
print("[STEP 3] Authorizing Worker...")
result = self.execute_opcode(BitcoinOpCodes.STRATUM_AUTHORIZE, [])
if not result.get("success"):
return False
# [0x40] INIT_MINER
print("[STEP 4] Initializing Neuromorphic Surface...")
result = self.execute_opcode(BitcoinOpCodes.INIT_MINER, [{}])
print(f" ✓ Surface manifold: {result.get('surface_manifold', 'N/A')}")
print()
print("[+] Miner initialized and ready")
return True
def run(self, duration_seconds: int = 300) -> Dict:
"""Run miner for specified duration"""
self.start_time = time.time()
end_time = self.start_time + duration_seconds
print()
print(f"[MINING] Running for {duration_seconds} seconds...")
print()
while time.time() < end_time and self.stratum.connected:
# Listen for jobs
self.stratum.listen_for_jobs(self._handle_job_notification)
if not self.current_job:
time.sleep(0.1)
continue
time.sleep(0.01)
return self.generate_report()
def generate_report(self) -> Dict:
"""Generate mining report"""
runtime = time.time() - self.start_time if self.start_time else 1
hashrate = self.hashes_computed / runtime if runtime > 0 else 0
return {
"success": True,
"timestamp": datetime.now().isoformat(),
"pool": f"{self.pool_url}:{self.pool_port}",
"username": self.username,
"runtime_seconds": runtime,
"hashes_computed": self.hashes_computed,
"hashrate_hps": hashrate,
"shares_accepted": self.shares_accepted,
"shares_rejected": self.shares_rejected,
"surface_manifold": self.surface_manifold_id[:16] if self.surface_manifold_id else None,
"safety_active": self.safety_active,
"final_entropy": self.thermal_entropy,
"logic_signal_substrate_opcodes_used": list(self.opcode_handlers.keys())
}
def shutdown(self):
"""Graceful shutdown"""
print()
print("[SHUTDOWN] Closing connections...")
self.execute_opcode(BitcoinOpCodes.STOP_MINER, [])
print("[+] Miner stopped")
# ============================================================================
# MAIN ENTRY POINT
# ============================================================================
def main():
import argparse
parser = argparse.ArgumentParser(description="TSM-ISA Bitcoin Miner")
parser.add_argument("--pool", type=str, default="stratum+tcp://stratum.braiins.com", help="Pool URL")
parser.add_argument("--port", type=int, default=3333, help="Pool port")
parser.add_argument("--user", type=str, required=True, help="Pool username")
parser.add_argument("--pass", dest="password", type=str, default="x", help="Pool password")
parser.add_argument("--duration", type=int, default=300, help="Mining duration (seconds)")
parser.add_argument("--output", type=str, default=None, help="Output report file")
args = parser.parse_args()
# Create miner
miner = TSMBitcoinMiner(
pool_url=args.pool,
pool_port=args.port,
username=args.user,
password=args.password
)
try:
# Initialize
if not miner.initialize():
print("[-] Failed to initialize miner")
return 1
# Run mining
report = miner.run(duration_seconds=args.duration)
# Print report
print()
print("=" * 70)
print(" MINING REPORT")
print("=" * 70)
print(f" Runtime: {report['runtime_seconds']:.1f}s")
print(f" Hashes: {report['hashes_computed']:,}")
print(f" Hashrate: {report['hashrate_hps']:.0f} H/s")
print(f" Shares Accepted: {report['shares_accepted']}")
print(f" Shares Rejected: {report['shares_rejected']}")
print(f" Safety Protocol: {'ACTIVE' if report['safety_active'] else 'INACTIVE'}")
print(f" TSM Opcodes: {len(report['logic_signal_substrate_opcodes_used'])} registered")
print("=" * 70)
# Save report
output_path = args.output or ROOT / "out" / "btc_logic_signal_substrate_mining_report.json"
output_path = Path(output_path)
output_path.parent.mkdir(parents=True, exist_ok=True)
with open(output_path, "w") as f:
json.dump(report, f, indent=2)
f.write("\n")
print(f"[+] Report saved to: {output_path}")
return 0
except KeyboardInterrupt:
print("\n[!] Interrupted by user")
return 0
except Exception as e:
print(f"[ERROR] {e}")
import traceback
traceback.print_exc()
return 1
finally:
miner.shutdown()
if __name__ == "__main__":
sys.exit(main())