forked from infosave2007/phpblockchain
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_sync.php
More file actions
Latest commit
executable file
·222 lines (180 loc) · 6.85 KB
/
Copy pathquick_sync.php
File metadata and controls
executable file
·222 lines (180 loc) · 6.85 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
#!/usr/bin/env php
<?php
/**
* Quick Sync Script
* Simple script for emergency blockchain synchronization
*/
require_once__DIR__ . '/network_sync.php';
functionshowUsage() {
echo"Blockchain Quick Sync Tool\n";
echo"==========================\n\n";
echo"Usage: php quick_sync.php [command] [options]\n\n";
echo"Commands:\n";
echo" sync - Start full synchronization\n";
echo" status - Show current database status\n";
echo" check - Quick network connectivity check\n";
echo" repair - Repair and re-sync missing data\n";
echo" help - Show this help message\n\n";
echo"Options:\n";
echo" --verbose - Show detailed output\n";
echo" --quiet - Minimal output\n";
echo" --force - Force sync even if up-to-date\n\n";
echo"Examples:\n";
echo" php quick_sync.php sync --verbose\n";
echo" php quick_sync.php status\n";
echo" php quick_sync.php repair --force\n\n";
}
functionformatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
for ($i = 0; $bytes > 1024; $i++) {
$bytes /= 1024;
}
returnround($bytes, $precision) . '' . $units[$i];
}
functionshowStatus($syncManager, $verbose = false) {
echo"📊 Blockchain Database Status\n";
echo"============================\n\n";
$status = $syncManager->getStatus();
// Show table statistics
$totalRecords = 0;
foreach ($status['tables'] as$table => $count) {
$totalRecords += $count;
printf("%-20s: %s records\n",
ucfirst(str_replace('_', '', $table)),
number_format($count)
);
}
echo"\n";
printf("Total records: %s\n", number_format($totalRecords));
printf("Latest block: #%s\n", $status['latest_block']);
printf("Latest timestamp: %s\n", $status['latest_timestamp'] ?? 'Unknown');
if ($verbose) {
echo"\n📈 Additional Information\n";
echo"========================\n";
// Memory usage
printf("Memory usage: %s\n", formatBytes(memory_get_usage(true)));
printf("Peak memory: %s\n", formatBytes(memory_get_peak_usage(true)));
// Log file size
$logFile = 'logs/network_sync.log';
if (file_exists($logFile)) {
printf("Log file size: %s\n", formatBytes(filesize($logFile)));
}
}
}
functioncheckConnectivity($syncManager) {
echo"🌐 Network Connectivity Check\n";
echo"=============================\n\n";
try {
// This is a simplified check - we'll use the selectBestNode method
$reflection = newReflectionClass($syncManager);
$method = $reflection->getMethod('selectBestNode');
$method->setAccessible(true);
$bestNode = $method->invoke($syncManager);
echo"✅ Successfully connected to: $bestNode\n";
echo"🔗 Network is accessible and responsive\n";
} catch (Exception$e) {
echo"❌ Network check failed: " . $e->getMessage() . "\n";
echo"🔧 Try checking your internet connection or network configuration\n";
returnfalse;
}
returntrue;
}
functionperformSync($syncManager, $options = []) {
$verbose = in_array('--verbose', $options);
$quiet = in_array('--quiet', $options);
$force = in_array('--force', $options);
if (!$quiet) {
echo"🚀 Starting Blockchain Synchronization\n";
echo"======================================\n\n";
if ($force) {
echo"⚠️ Force mode enabled - will re-sync all data\n\n";
}
}
try {
$startTime = microtime(true);
$result = $syncManager->syncAll();
$duration = microtime(true) - $startTime;
if (!$quiet) {
echo"\n✅ Synchronization Completed Successfully!\n";
echo"==========================================\n\n";
printf("📊 Blocks synced: %s\n", number_format($result['blocks_synced']));
printf("💰 Transactions synced: %s\n", number_format($result['transactions_synced']));
printf("🌐 Source node: %s\n", $result['node']);
printf("⏱️ Duration: %.2f seconds\n", $duration);
printf("📅 Completed at: %s\n", $result['completion_time']);
}
returntrue;
} catch (Exception$e) {
echo"\n❌ Synchronization Failed!\n";
echo"==========================\n";
echo"Error: " . $e->getMessage() . "\n";
if ($verbose) {
echo"\nStack trace:\n";
echo$e->getTraceAsString() . "\n";
}
echo"\n🔧 Troubleshooting tips:\n";
echo"- Check your internet connection\n";
echo"- Verify database credentials\n";
echo"- Check if network nodes are accessible\n";
echo"- Try running: php quick_sync.php check\n";
returnfalse;
}
}
functionperformRepair($syncManager, $options = []) {
echo"🔧 Blockchain Data Repair\n";
echo"=========================\n\n";
$verbose = in_array('--verbose', $options);
echo"🔍 Checking for missing or corrupted data...\n";
try {
// Force a complete re-sync
echo"🔄 Performing complete data re-synchronization...\n";
$result = performSync($syncManager, array_merge($options, ['--force']));
if ($result) {
echo"\n✅ Repair completed successfully!\n";
} else {
echo"\n❌ Repair failed - manual intervention may be required\n";
}
} catch (Exception$e) {
echo"\n❌ Repair failed: " . $e->getMessage() . "\n";
}
}
// Main execution
if (php_sapi_name() !== 'cli') {
echo"This script must be run from command line\n";
exit(1);
}
$command = $argv[1] ?? 'help';
$options = array_slice($argv, 2);
try {
$syncManager = newNetworkSyncManager(false);
switch ($command) {
case'sync':
performSync($syncManager, $options);
break;
case'status':
$verbose = in_array('--verbose', $options);
showStatus($syncManager, $verbose);
break;
case'check':
checkConnectivity($syncManager);
break;
case'repair':
performRepair($syncManager, $options);
break;
case'help':
case'--help':
case'-h':
default:
showUsage();
break;
}
} catch (Exception$e) {
echo"💥 Fatal Error: " . $e->getMessage() . "\n";
echo"\nThis usually indicates a configuration or database issue.\n";
echo"Please check:\n";
echo"- Database connection settings\n";
echo"- File permissions\n";
echo"- PHP requirements\n";
exit(1);
}
?>