forked from infosave2007/phpblockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_interface.php
More file actions
351 lines (304 loc) · 14.1 KB
/
Copy pathsync_interface.php
File metadata and controls
351 lines (304 loc) · 14.1 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
<?php
/**
* Web Interface for Network Synchronization
* Simple PHP interface for blockchain synchronization with progress tracking
*/
require_once 'network_sync.php';
// Set output buffering off for real-time progress
if (ob_get_level()) {
ob_end_clean();
}
// Handle AJAX requests
if (isset($_GET['action'])) {
header('Content-Type: application/json');
try {
$syncManager = new NetworkSyncManager(true);
switch ($_GET['action']) {
case 'start_sync':
// Start synchronization with real-time progress
header('Content-Type: text/plain');
echo "Starting blockchain synchronization...\n";
flush();
$result = $syncManager->syncAll();
echo "\nSYNC_COMPLETE:" . json_encode($result) . "\n";
break;
case 'get_status':
$status = $syncManager->getStatus();
echo json_encode(['status' => 'success', 'data' => $status]);
break;
case 'check_progress':
// Check log file for latest progress
$logFile = 'logs/network_sync.log';
if (file_exists($logFile)) {
$lines = file($logFile);
$lastLines = array_slice($lines, -10);
echo json_encode(['status' => 'success', 'logs' => $lastLines]);
} else {
echo json_encode(['status' => 'error', 'message' => 'Log file not found']);
}
break;
default:
echo json_encode(['status' => 'error', 'message' => 'Unknown action']);
break;
}
exit;
} catch (Exception $e) {
echo json_encode([
'status' => 'error',
'message' => $e->getMessage()
]);
exit;
}
}
// Main page
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Blockchain Synchronization</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; background-color: #1a1a1a; color: #fff; }
.container { max-width: 800px; margin: 0 auto; }
.header { text-align: center; margin-bottom: 30px; }
.status-box { background: #2d2d2d; padding: 20px; border-radius: 8px; margin: 20px 0; }
.progress-bar { width: 100%; height: 20px; background: #333; border-radius: 10px; overflow: hidden; margin: 10px 0; }
.progress-fill { height: 100%; background: linear-gradient(90deg, #4caf50, #45a049); width: 0%; transition: width 0.3s; }
.log-container { background: #000; padding: 15px; border-radius: 5px; height: 300px; overflow-y: auto; font-family: monospace; font-size: 12px; }
.btn { background: #4caf50; color: white; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; margin: 5px; }
.btn:hover { background: #45a049; }
.btn:disabled { background: #666; cursor: not-allowed; }
.stats-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 15px; margin: 20px 0; }
.stat-item { background: #333; padding: 15px; border-radius: 5px; text-align: center; }
.stat-number { font-size: 24px; font-weight: bold; color: #4caf50; }
.error { color: #f44336; }
.success { color: #4caf50; }
.warning { color: #ff9800; }
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🔗 Blockchain Network Synchronization</h1>
<p>Synchronize blockchain data from network nodes</p>
</div>
<div class="status-box">
<h3>Current Status</h3>
<div id="currentStatus">Loading...</div>
<div class="progress-bar">
<div class="progress-fill" id="progressBar"></div>
</div>
<div id="progressText">Ready to sync</div>
<div style="text-align: center; margin-top: 20px;">
<button class="btn" onclick="startSync()" id="syncBtn">Start Synchronization</button>
<button class="btn" onclick="refreshStatus()" id="statusBtn">Refresh Status</button>
<button class="btn" onclick="clearLogs()" id="clearBtn">Clear Logs</button>
</div>
</div>
<div class="status-box">
<h3>Database Statistics</h3>
<div class="stats-grid" id="statsGrid">
<div class="stat-item">
<div class="stat-number" id="blocks-count">-</div>
<div>Blocks</div>
</div>
<div class="stat-item">
<div class="stat-number" id="transactions-count">-</div>
<div>Transactions</div>
</div>
<div class="stat-item">
<div class="stat-number" id="nodes-count">-</div>
<div>Nodes</div>
</div>
<div class="stat-item">
<div class="stat-number" id="validators-count">-</div>
<div>Validators</div>
</div>
<div class="stat-item">
<div class="stat-number" id="smart_contracts-count">-</div>
<div>Smart Contracts</div>
</div>
<div class="stat-item">
<div class="stat-number" id="staking-count">-</div>
<div>Staking Records</div>
</div>
</div>
</div>
<div class="status-box">
<h3>Synchronization Log</h3>
<div class="log-container" id="logContainer"></div>
</div>
</div>
<script>
let syncInProgress = false;
let logUpdateInterval;
// Load initial status
document.addEventListener('DOMContentLoaded', function() {
refreshStatus();
});
function startSync() {
if (syncInProgress) return;
syncInProgress = true;
document.getElementById('syncBtn').disabled = true;
document.getElementById('progressText').textContent = 'Initializing synchronization...';
// Clear previous logs
document.getElementById('logContainer').innerHTML = '';
// Start progress monitoring
startProgressMonitoring();
// Start sync via server-sent events simulation
fetch('?action=start_sync')
.then(response => {
if (!response.body) {
throw new Error('ReadableStream not supported');
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
function readStream() {
return reader.read().then(({ done, value }) => {
if (done) {
syncCompleted();
return;
}
const text = decoder.decode(value);
const lines = text.split('\n');
lines.forEach(line => {
line = line.trim();
if (!line) return;
if (line.startsWith('SYNC_COMPLETE:')) {
const result = JSON.parse(line.substring(14));
handleSyncComplete(result);
} else if (line.startsWith('{')) {
try {
const progress = JSON.parse(line);
updateProgress(progress);
} catch (e) {
addLogEntry(line);
}
} else {
addLogEntry(line);
}
});
return readStream();
});
}
return readStream();
})
.catch(error => {
console.error('Sync error:', error);
addLogEntry('ERROR: ' + error.message, 'error');
syncCompleted();
});
}
function startProgressMonitoring() {
logUpdateInterval = setInterval(() => {
if (!syncInProgress) {
clearInterval(logUpdateInterval);
return;
}
fetch('?action=check_progress')
.then(response => response.json())
.then(data => {
if (data.status === 'success' && data.logs) {
// Update logs without replacing existing ones
const logContainer = document.getElementById('logContainer');
data.logs.forEach(log => {
if (logContainer.innerHTML.indexOf(log.trim()) === -1) {
addLogEntry(log.trim());
}
});
}
})
.catch(error => console.error('Progress check error:', error));
}, 2000);
}
function updateProgress(progress) {
if (progress.percent !== undefined) {
document.getElementById('progressBar').style.width = progress.percent + '%';
}
if (progress.message) {
document.getElementById('progressText').textContent = progress.message;
addLogEntry(`[${progress.current}/${progress.total}] ${progress.message}`);
}
}
function handleSyncComplete(result) {
document.getElementById('progressBar').style.width = '100%';
document.getElementById('progressText').textContent = 'Synchronization completed!';
addLogEntry('=== SYNCHRONIZATION COMPLETED ===', 'success');
addLogEntry(`Status: ${result.status}`, 'success');
addLogEntry(`Node: ${result.node}`, 'success');
addLogEntry(`Blocks synced: ${result.blocks_synced}`, 'success');
addLogEntry(`Transactions synced: ${result.transactions_synced}`, 'success');
addLogEntry(`Completed at: ${result.completion_time}`, 'success');
syncCompleted();
refreshStatus();
}
function syncCompleted() {
syncInProgress = false;
document.getElementById('syncBtn').disabled = false;
if (logUpdateInterval) {
clearInterval(logUpdateInterval);
}
}
function refreshStatus() {
fetch('?action=get_status')
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
updateStatus(data.data);
} else {
document.getElementById('currentStatus').innerHTML =
'<span class="error">Error loading status: ' + data.message + '</span>';
}
})
.catch(error => {
document.getElementById('currentStatus').innerHTML =
'<span class="error">Error: ' + error.message + '</span>';
});
}
function updateStatus(status) {
// Update main status
const latestBlock = status.latest_block || 0;
const latestTime = status.latest_timestamp || 'Unknown';
document.getElementById('currentStatus').innerHTML = `
<div><strong>Latest Block:</strong> #${latestBlock}</div>
<div><strong>Latest Timestamp:</strong> ${latestTime}</div>
<div><strong>Last Check:</strong> ${status.sync_time}</div>
`;
// Update statistics
Object.keys(status.tables).forEach(table => {
const element = document.getElementById(table + '-count');
if (element) {
element.textContent = status.tables[table].toLocaleString();
}
});
}
function addLogEntry(message, type = 'info') {
const logContainer = document.getElementById('logContainer');
const timestamp = new Date().toLocaleTimeString();
const className = type === 'error' ? 'error' : type === 'success' ? 'success' : type === 'warning' ? 'warning' : '';
const logEntry = document.createElement('div');
logEntry.className = className;
logEntry.textContent = `[${timestamp}] ${message}`;
logContainer.appendChild(logEntry);
logContainer.scrollTop = logContainer.scrollHeight;
}
function clearLogs() {
document.getElementById('logContainer').innerHTML = '';
document.getElementById('progressBar').style.width = '0%';
document.getElementById('progressText').textContent = 'Ready to sync';
}
</script>
</body>
</html>
<?php
// Command line usage information
if (php_sapi_name() === 'cli') {
echo "Web interface for blockchain synchronization\n";
echo "Usage: Access via web browser or use network_sync.php directly\n";
echo "Web URL: http://your-domain/sync_interface.php\n";
echo "\nFor CLI usage:\n";
echo "php network_sync.php sync - Start synchronization\n";
echo "php network_sync.php status - Check status\n";
}
?>