forked from frstrtr/c2pool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmining_test_client.py
More file actions
executable file
·352 lines (288 loc) · 12.9 KB
/
mining_test_client.py
File metadata and controls
executable file
·352 lines (288 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/usr/bin/env python3
"""
Simple C2Pool Mining Test Client
Tests share submission and storage functionality
"""
import socket
import json
import time
import requests
import sys
import subprocess
def test_web_interface():
"""Test the web interface and get current stats"""
try:
response = requests.get("http://localhost:8084/", timeout=5)
if response.status_code == 200:
data = response.json()
print("✅ Web interface accessible")
print(f" Current difficulty: {data.get('difficulty', 'N/A')}")
print(f" Pool hashrate: {data.get('poolhashps', 'N/A')} H/s")
print(f" Pool shares: {data.get('poolshares', 'N/A')}")
print(f" Connected miners: {data.get('connected_miners', 'N/A')}")
print(f" Testnet mode: {data.get('testnet', 'N/A')}")
return True
else:
print(f"❌ Web interface returned status {response.status_code}")
return False
except Exception as e:
print(f"❌ Web interface error: {e}")
return False
def test_stratum_connection():
"""Test basic Stratum connection on port 8084"""
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
sock.connect(("localhost", 8084))
# Send subscribe request
subscribe_req = {
"id": 1,
"method": "mining.subscribe",
"params": ["test-client/1.0"]
}
message = json.dumps(subscribe_req) + "\n"
sock.send(message.encode())
# Try to receive response
try:
sock.settimeout(5)
response = sock.recv(1024).decode().strip()
print("✅ Stratum connection successful")
print(f" Response: {response}")
# Try authorization with a test address
auth_req = {
"id": 2,
"method": "mining.authorize",
"params": ["tltc1qea8gdr057k9gyjnurk7v3d9enha8qgds58ajt0", "test"]
}
message = json.dumps(auth_req) + "\n"
sock.send(message.encode())
auth_response = sock.recv(1024).decode().strip()
print(f" Auth response: {auth_response}")
return True
except socket.timeout:
print("⚠️ Stratum port accessible but no response timeout")
return True
except UnicodeDecodeError as e:
print(f"⚠️ Stratum response decode error: {e}")
return True
except ConnectionRefusedError:
print("❌ Stratum port not accessible (port 8084)")
return False
except OSError as e:
print(f"❌ Stratum connection error: {e}")
return False
finally:
try:
sock.close()
except OSError:
pass
def simulate_mining_activity():
"""Monitor mining activity by checking web interface stats"""
print("🔄 Monitoring mining activity...")
# Make several requests to monitor pool stats
for i in range(10):
try:
response = requests.get("http://localhost:8084/", timeout=2)
if response.status_code == 200:
data = response.json()
shares = data.get('poolshares', 0)
hashrate = data.get('poolhashps', 0)
difficulty = data.get('difficulty', 1)
miners = data.get('connected_miners', 0)
print(f" Check {i+1}: MiningShares={shares}, Hashrate={hashrate:.2f} H/s, "
f"Difficulty={difficulty}, Miners={miners}")
if shares > 0:
print(" 🎉 Mining shares detected!")
if hashrate > 0:
print(" ⚡ Pool hashrate active!")
time.sleep(2)
except requests.RequestException as e:
print(f" Check {i+1} failed: {e}")
return True
def monitor_miners():
"""Monitor connected miners and their activity"""
print("👥 Monitoring Physical Miners")
print("=============================")
previous_shares = 0
previous_hashrate = 0
try:
for i in range(30): # Monitor for 60 seconds
try:
response = requests.get("http://localhost:8084/", timeout=5)
if response.status_code == 200:
data = response.json()
shares = data.get('poolshares', 0)
hashrate = data.get('poolhashps', 0)
difficulty = data.get('difficulty', 1)
miners = data.get('connected_miners', 0)
# Calculate deltas
share_delta = shares - previous_shares
hashrate_delta = hashrate - previous_hashrate
status = "🟢" if miners > 0 else "🔴"
activity = "📈" if share_delta > 0 else "📊"
print(f"{status} [{i+1:2d}/30] Miners: {miners:2d} | "
f"MiningShares: {shares:4d} (+{share_delta}) | "
f"Hashrate: {hashrate:8.2f} H/s {activity}")
if share_delta > 0:
print(f" 🎯 New mining shares found! Pool is actively mining.")
if miners > 0 and i == 0:
print(f" ✅ Physical miners detected and connected!")
previous_shares = shares
previous_hashrate = hashrate
else:
print(f" ❌ Web interface error: {response.status_code}")
except requests.RequestException as e:
print(f" ⚠️ Monitoring error: {e}")
time.sleep(2)
except KeyboardInterrupt:
print("\n⏹️ Monitoring stopped by user")
return True
def check_physical_miners():
"""Check for active physical miner connections to port 8084"""
print("🔍 Checking for Physical Miner Connections")
print("==========================================")
try:
# Check network connections to port 8084
result = subprocess.run(['netstat', '-an'], capture_output=True, text=True, timeout=10)
if result.returncode == 0:
lines = result.stdout.split('\n')
connections_8084 = []
for line in lines:
if ':8084' in line and 'ESTABLISHED' in line:
connections_8084.append(line.strip())
if connections_8084:
print(f"✅ Found {len(connections_8084)} active connections to port 8084:")
for i, conn in enumerate(connections_8084, 1):
parts = conn.split()
if len(parts) >= 4:
local_addr = parts[3]
remote_addr = parts[4]
print(f" {i}. {remote_addr} -> {local_addr}")
# Try to get more details from lsof if available
try:
lsof_result = subprocess.run(['lsof', '-i', ':8084'], capture_output=True, text=True, timeout=5)
if lsof_result.returncode == 0:
print("📊 Detailed connection info:")
lsof_lines = lsof_result.stdout.split('\n')[1:] # Skip header
for line in lsof_lines:
if line.strip() and 'ESTABLISHED' in line:
print(f" {line}")
except (subprocess.TimeoutExpired, FileNotFoundError):
pass # lsof might not be available
return True
else:
print("ℹ️ No active connections found to port 8084")
print(" Physical miners may not be connected yet")
return False
else:
print(f"❌ Failed to check network connections: {result.stderr}")
return False
except subprocess.TimeoutExpired:
print("❌ Network check timed out")
return False
except Exception as e:
print(f"❌ Error checking physical miners: {e}")
return False
def test_stratum_protocol():
"""Test full Stratum protocol flow as a physical miner would"""
print("🔌 Testing Full Stratum Protocol Flow")
print("====================================")
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(10)
sock.connect(("localhost", 8084))
print("✅ Connected to Stratum server on port 8084")
# Step 1: Subscribe
subscribe_req = {
"id": 1,
"method": "mining.subscribe",
"params": ["physical-miner-test/1.0", None]
}
message = json.dumps(subscribe_req) + "\n"
sock.send(message.encode())
print("📤 Sent mining.subscribe request")
try:
sock.settimeout(5)
response = sock.recv(4096).decode().strip()
print(f"📥 Subscribe response: {response}")
# Step 2: Authorize (with a test Litecoin testnet address)
auth_req = {
"id": 2,
"method": "mining.authorize",
"params": ["tltc1qea8gdr057k9gyjnurk7v3d9enha8qgds58ajt0", "test-password"]
}
message = json.dumps(auth_req) + "\n"
sock.send(message.encode())
print("📤 Sent mining.authorize request")
auth_response = sock.recv(4096).decode().strip()
print(f"📥 Authorize response: {auth_response}")
# Step 3: Wait for mining.notify (job notifications)
print("⏳ Waiting for mining job notifications...")
sock.settimeout(10)
for attempt in range(3):
try:
notify_data = sock.recv(4096).decode().strip()
if notify_data:
print(f"📥 Job notification {attempt + 1}: {notify_data}")
except socket.timeout:
print(f" Attempt {attempt + 1}: No job notification received (timeout)")
return True
except socket.timeout:
print("⚠️ Protocol response timeout - may indicate HTTP-only mode")
return True
except UnicodeDecodeError as e:
print(f"⚠️ Protocol response decode error: {e}")
return True
except ConnectionRefusedError:
print("❌ Could not connect to Stratum server on port 8084")
return False
except Exception as e:
print(f"❌ Stratum protocol test error: {e}")
return False
finally:
try:
sock.close()
except:
pass
def main():
print("🧪 C2Pool Physical Miner Monitor")
print("================================")
# Test 1: Web Interface
print("\n📡 Test 1: Web Interface")
web_ok = test_web_interface()
# Test 2: Stratum Connection
print("\n🔌 Test 2: Stratum Connection (Port 8084)")
stratum_ok = test_stratum_connection()
# Test 3: Mining Activity Check
print("\n⛏️ Test 3: Mining Activity Check")
mining_ok = simulate_mining_activity()
# Test 4: Real-time Physical Miner Monitoring
print("\n👥 Test 4: Physical Miner Monitoring")
print("Press Ctrl+C to stop monitoring...")
time.sleep(1)
monitor_ok = monitor_miners()
# Test 5: Check Physical Miners
print("\n🔍 Test 5: Check Physical Miners Connections")
check_ok = check_physical_miners()
# Test 6: Stratum Protocol Test
print("\n🔌 Test 6: Stratum Protocol Full Flow")
protocol_ok = test_stratum_protocol()
# Final Report
print("\n📊 Monitoring Summary")
print("====================")
print(f"Web Interface: {'✅ PASS' if web_ok else '❌ FAIL'}")
print(f"Stratum Port: {'✅ PASS' if stratum_ok else '❌ FAIL'}")
print(f"Mining Check: {'✅ PASS' if mining_ok else '❌ FAIL'}")
print(f"Miner Monitor: {'✅ PASS' if monitor_ok else '❌ FAIL'}")
print(f"Physical Miners: {'✅ PASS' if check_ok else '❌ FAIL'}")
print(f"Stratum Protocol: {'✅ PASS' if protocol_ok else '❌ FAIL'}")
if web_ok and stratum_ok:
print("\n🎉 C2Pool Enhanced is operational!")
print("💡 Miner connection: stratum+tcp://localhost:8084")
print("🔗 Web interface: http://localhost:8084")
return 0
else:
print("\n⚠️ Some connectivity issues detected.")
return 1
if __name__ == "__main__":
sys.exit(main())