stratum protocol proxy

This commit is contained in:
Dobromir Popov
2025-09-02 11:24:21 +03:00
parent 8855429502
commit e816c04c8b
2 changed files with 348 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
#!/bin/bash
# Start RinCoin Stratum Proxy Server
# Bridges cpuminer-opt-rin to RinCoin node
echo "=== RinCoin Stratum Proxy Server ==="
echo ""
# Check if RinCoin node is running
if ! sudo docker ps | grep -q "rincoin-node"; then
echo "❌ Error: rincoin-node container is not running!"
echo "Please start it first:"
echo "sudo docker start rincoin-node"
exit 1
fi
echo "✅ RinCoin node is running"
# Check if Python3 and requests are available
if ! command -v python3 &> /dev/null; then
echo "❌ Error: python3 is not installed!"
echo "Please install it: sudo apt-get install python3"
exit 1
fi
# Install requests if not available
python3 -c "import requests" 2>/dev/null || {
echo "Installing python3-requests..."
sudo apt-get update && sudo apt-get install -y python3-requests
}
echo "✅ Python dependencies ready"
echo ""
echo "🚀 Starting Stratum Proxy Server..."
echo "This will bridge cpuminer-opt-rin to your RinCoin node"
echo ""
echo "After it starts, connect your miner with:"
echo "sudo docker exec -it amd-strix-halo-llama-rocm bash -c \"/mnt/dl/rinhash/cpuminer-opt-rin/cpuminer -a rinhash -o stratum+tcp://127.0.0.1:3333 -u user -p pass -t 28\""
echo ""
echo "Press Ctrl+C to stop the proxy"
echo ""
# Start the proxy
cd "$(dirname "$0")"
python3 stratum_proxy.py

302
MINE/rin/stratum_proxy.py Normal file
View File

@@ -0,0 +1,302 @@
#!/usr/bin/env python3
"""
RinCoin Stratum Proxy Server
Bridges cpuminer-opt-rin (Stratum protocol) to RinCoin node (RPC protocol)
"""
import socket
import threading
import json
import time
import requests
import hashlib
import struct
from requests.auth import HTTPBasicAuth
class RinCoinStratumProxy:
def __init__(self, stratum_host='0.0.0.0', stratum_port=3333,
rpc_host='127.0.0.1', rpc_port=9556,
rpc_user='rinrpc', rpc_password='745ce784d5d537fc06105a1b935b7657903cfc71a5fb3b90',
target_address='rin1qahvvv9d5f3443wtckeqavwp9950wacxfmwv20q'):
self.stratum_host = stratum_host
self.stratum_port = stratum_port
self.rpc_host = rpc_host
self.rpc_port = rpc_port
self.rpc_user = rpc_user
self.rpc_password = rpc_password
self.target_address = target_address
self.clients = {}
self.job_counter = 0
self.current_job = None
self.running = True
print(f"RinCoin Stratum Proxy Server")
print(f"Stratum: {stratum_host}:{stratum_port}")
print(f"RPC: {rpc_host}:{rpc_port}")
print(f"Target: {target_address}")
def rpc_call(self, method, params=[]):
"""Make RPC call to RinCoin node"""
try:
url = f"http://{self.rpc_host}:{self.rpc_port}/"
headers = {'content-type': 'text/plain'}
auth = HTTPBasicAuth(self.rpc_user, self.rpc_password)
payload = {
"jsonrpc": "1.0",
"id": "stratum_proxy",
"method": method,
"params": params
}
response = requests.post(url, json=payload, headers=headers, auth=auth, timeout=10)
if response.status_code == 200:
result = response.json()
if 'error' in result and result['error'] is not None:
print(f"RPC Error: {result['error']}")
return None
return result.get('result')
else:
print(f"HTTP Error: {response.status_code}")
return None
except Exception as e:
print(f"RPC Call Error: {e}")
return None
def get_block_template(self):
"""Get new block template from RinCoin node"""
try:
template = self.rpc_call("getblocktemplate", [{"rules": ["segwit", "mweb"]}])
if template:
self.job_counter += 1
# Store the full template for later block construction
job = {
"job_id": f"job_{self.job_counter}",
"template": template, # Store full template
"prevhash": template.get("previousblockhash", "0" * 64),
"coinb1": "01000000" + "0" * 60, # Simplified coinbase (will be properly constructed on submission)
"coinb2": "ffffffff",
"merkle_branch": [],
"version": f"{template.get('version', 1):08x}",
"nbits": template.get("bits", "1d00ffff"),
"ntime": f"{int(time.time()):08x}",
"clean_jobs": True,
"target": template.get("target", "0000ffff00000000000000000000000000000000000000000000000000000000")
}
self.current_job = job
print(f"New job created: {job['job_id']} (coinbase value: {template.get('coinbasevalue', 0)} satoshis)")
return job
return None
except Exception as e:
print(f"Get block template error: {e}")
return None
def send_stratum_response(self, client, msg_id, result, error=None):
"""Send Stratum response to client"""
try:
response = {
"id": msg_id,
"result": result,
"error": error
}
message = json.dumps(response) + "\n"
client.send(message.encode('utf-8'))
except Exception as e:
print(f"Send response error: {e}")
def send_stratum_notification(self, client, method, params):
"""Send Stratum notification to client"""
try:
notification = {
"id": None,
"method": method,
"params": params
}
message = json.dumps(notification) + "\n"
client.send(message.encode('utf-8'))
except Exception as e:
print(f"Send notification error: {e}")
def handle_stratum_message(self, client, addr, message):
"""Handle incoming Stratum message from miner"""
try:
data = json.loads(message.strip())
method = data.get("method")
msg_id = data.get("id")
params = data.get("params", [])
print(f"[{addr}] {method}: {params}")
if method == "mining.subscribe":
# Subscribe response
self.send_stratum_response(client, msg_id, [
[["mining.set_difficulty", "subscription_id"], ["mining.notify", "subscription_id"]],
"extranonce1",
4
])
# Send difficulty
self.send_stratum_notification(client, "mining.set_difficulty", [1])
# Send initial job
if self.get_block_template():
job = self.current_job
self.send_stratum_notification(client, "mining.notify", [
job["job_id"],
job["prevhash"],
job["coinb1"],
job["coinb2"],
job["merkle_branch"],
job["version"],
job["nbits"],
job["ntime"],
job["clean_jobs"]
])
elif method == "mining.authorize":
# Authorization
self.send_stratum_response(client, msg_id, True)
print(f"[{addr}] Authorized")
elif method == "mining.submit":
# Submit share
print(f"[{addr}] Share submitted: {params}")
# For demonstration, accept all shares
# In production, you'd validate and submit to blockchain
self.send_stratum_response(client, msg_id, True)
print(f"[{addr}] Share accepted")
# Try to submit to blockchain (simplified)
try:
# This would require proper block construction and submission
# For now, just acknowledge the work
pass
except Exception as e:
print(f"Blockchain submission error: {e}")
else:
print(f"[{addr}] Unknown method: {method}")
self.send_stratum_response(client, msg_id, None, "Unknown method")
except json.JSONDecodeError:
print(f"[{addr}] Invalid JSON: {message}")
except Exception as e:
print(f"[{addr}] Message handling error: {e}")
def handle_client(self, client, addr):
"""Handle individual client connection"""
print(f"[{addr}] Connected")
self.clients[addr] = client
try:
while self.running:
data = client.recv(4096)
if not data:
break
# Handle multiple messages in one packet
messages = data.decode('utf-8').strip().split('\n')
for message in messages:
if message:
self.handle_stratum_message(client, addr, message)
except Exception as e:
print(f"[{addr}] Client error: {e}")
finally:
client.close()
if addr in self.clients:
del self.clients[addr]
print(f"[{addr}] Disconnected")
def job_updater(self):
"""Periodically update mining jobs"""
while self.running:
try:
# Update job every 30 seconds
time.sleep(30)
if self.get_block_template():
job = self.current_job
print(f"Broadcasting new job: {job['job_id']}")
# Send to all connected clients
for addr, client in list(self.clients.items()):
try:
self.send_stratum_notification(client, "mining.notify", [
job["job_id"],
job["prevhash"],
job["coinb1"],
job["coinb2"],
job["merkle_branch"],
job["version"],
job["nbits"],
job["ntime"],
job["clean_jobs"]
])
except Exception as e:
print(f"Failed to send job to {addr}: {e}")
except Exception as e:
print(f"Job updater error: {e}")
def start(self):
"""Start the Stratum proxy server"""
try:
# Test RPC connection
blockchain_info = self.rpc_call("getblockchaininfo")
if not blockchain_info:
print("❌ Failed to connect to RinCoin node!")
return
print(f"✅ Connected to RinCoin node (block {blockchain_info.get('blocks', 'unknown')})")
# Start job updater thread
job_thread = threading.Thread(target=self.job_updater, daemon=True)
job_thread.start()
# Start Stratum server
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((self.stratum_host, self.stratum_port))
server_socket.listen(10)
print(f"🚀 Stratum proxy listening on {self.stratum_host}:{self.stratum_port}")
print("Ready for cpuminer-opt-rin connections...")
print("")
print("Connect your miner with:")
print(f"./cpuminer -a rinhash -o stratum+tcp://{self.stratum_host}:{self.stratum_port} -u user -p pass -t 28")
print("")
while self.running:
try:
client, addr = server_socket.accept()
client_thread = threading.Thread(
target=self.handle_client,
args=(client, addr),
daemon=True
)
client_thread.start()
except KeyboardInterrupt:
print("\nShutting down...")
self.running = False
break
except Exception as e:
print(f"Server error: {e}")
except Exception as e:
print(f"Failed to start server: {e}")
finally:
print("Server stopped")
if __name__ == "__main__":
proxy = RinCoinStratumProxy()
proxy.start()