forked from infosave2007/phpblockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselective_sync_manager.php
More file actions
276 lines (233 loc) · 9.44 KB
/
Copy pathselective_sync_manager.php
File metadata and controls
276 lines (233 loc) · 9.44 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
#!/usr/bin/env php
<?php
declare(strict_types=1);
/**
* Selective Blockchain Sync Manager CLI
* Синхронизирует только критичные блокчейн-данные
*/
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/core/Storage/BlockchainBinaryStorage.php';
require_once __DIR__ . '/core/Storage/SelectiveBlockchainSyncManager.php';
echo "=== Selective Blockchain Sync Manager ===\n\n";
if ($argc < 2) {
showHelp();
exit(1);
}
$command = $argv[1];
try {
// Load configuration
$config = loadConfig();
// Initialize database using DatabaseManager
require_once 'core/Database/DatabaseManager.php';
$pdo = \Blockchain\Core\Database\DatabaseManager::getConnection();
// Initialize binary storage
$binaryStorage = new \Blockchain\Core\Storage\BlockchainBinaryStorage(
$config['blockchain']['data_dir'] ?? 'storage/blockchain',
$config['blockchain'] ?? []
);
// Initialize selective sync manager
$syncManager = new \Blockchain\Core\Storage\SelectiveBlockchainSyncManager($pdo, $binaryStorage, $config);
switch ($command) {
case '--export-blockchain':
$outputFile = $argv[2] ?? 'blockchain_export_' . date('Y-m-d_H-i-s') . '.dat';
echo "Exporting blockchain data to: $outputFile\n";
$result = $syncManager->exportBlockchainToFile($outputFile);
displayResult('Export', $result);
break;
case '--import-blockchain':
if (!isset($argv[2])) {
echo "Error: Import file path required\n";
exit(1);
}
$inputFile = $argv[2];
echo "Importing blockchain data from: $inputFile\n";
$result = $syncManager->importBlockchainFromFile($inputFile);
displayResult('Import', $result);
break;
case '--sync':
$direction = $argv[2] ?? 'both';
echo "Syncing blockchain data (direction: $direction)\n";
$result = $syncManager->syncBlockchainWithBinary($direction);
displayResult('Sync', $result);
break;
case '--validate':
echo "Validating blockchain data integrity...\n";
$result = $syncManager->validateBlockchainIntegrity();
displayValidationResult($result);
break;
case '--status':
echo "Checking synchronization status...\n";
$result = $syncManager->getSyncStatus();
displayStatusResult($result);
break;
case '--analyze-tables':
echo "Analyzing table synchronization strategy...\n";
analyzeTableStrategy();
break;
case '--help':
showHelp();
break;
default:
echo "Unknown command: $command\n";
showHelp();
exit(1);
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . "\n";
exit(1);
}
function showHelp(): void
{
echo "Selective Blockchain Sync Manager\n";
echo "Синхронизирует только критичные блокчейн-данные\n\n";
echo "Usage: php selective_sync_manager.php [command] [options]\n\n";
echo "Commands:\n";
echo " --export-blockchain [file] Export blockchain tables to file\n";
echo " --import-blockchain <file> Import blockchain tables from file\n";
echo " --sync [direction] Sync blockchain data (both|db-to-binary|binary-to-db)\n";
echo " --validate Validate blockchain data integrity\n";
echo " --status Show synchronization status\n";
echo " --analyze-tables Analyze which tables should be synced\n";
echo " --help Show this help\n\n";
echo "Table Classification:\n";
echo " SYNCED (Global blockchain state):\n";
echo " • blocks - blockchain blocks\n";
echo " • transactions - transaction records\n";
echo " • wallets - account balances\n";
echo " • staking - PoS staking data\n";
echo " • validators - validator info\n";
echo " • smart_contracts - contract data\n\n";
echo " NOT SYNCED (Local node data):\n";
echo " • config - node configuration\n";
echo " • nodes - peer node info\n";
echo " • mempool - pending transactions\n";
echo " • logs - system logs\n";
echo " • users - local user accounts\n\n";
echo "Examples:\n";
echo " php selective_sync_manager.php --export-blockchain backup.dat\n";
echo " php selective_sync_manager.php --sync both\n";
echo " php selective_sync_manager.php --validate\n";
}
function loadConfig(): array
{
$configFile = __DIR__ . '/config/config.php';
if (file_exists($configFile)) {
return require $configFile;
}
// Fallback configuration
return [
'database' => [
'host' => 'localhost',
'port' => 3306,
'username' => 'blockchain',
'password' => 'password',
'database' => 'blockchain'
],
'blockchain' => [
'data_dir' => 'storage/blockchain'
]
];
}
function displayResult(string $operation, array $result): void
{
echo "\n$operation Results:\n";
echo "================\n";
foreach ($result as $key => $value) {
if (is_array($value)) {
echo "$key:\n";
foreach ($value as $item) {
echo " - $item\n";
}
} else {
echo "$key: $value\n";
}
}
echo "\n";
}
function displayValidationResult(array $result): void
{
echo "\nValidation Results:\n";
echo "==================\n";
if ($result['valid']) {
echo "✅ Blockchain data integrity: VALID\n";
} else {
echo "❌ Blockchain data integrity: ISSUES FOUND\n";
}
echo "Tables checked: {$result['tables_checked']}\n";
if (!empty($result['issues'])) {
echo "\nIssues found:\n";
foreach ($result['issues'] as $issue) {
echo " ❌ $issue\n";
}
}
echo "\nLocal tables (not validated):\n";
foreach ($result['local_tables_skipped'] as $table) {
echo " ℹ️ $table - local only, not part of blockchain\n";
}
echo "\n";
}
function displayStatusResult(array $result): void
{
echo "\nSynchronization Status:\n";
echo "======================\n";
echo "\n📦 BLOCKCHAIN TABLES (Synced):\n";
foreach ($result['blockchain_tables'] as $table => $info) {
$syncStatus = $info['synced_to_binary'] ? '✅' : '❌';
echo " $syncStatus $table: {$info['records']} records";
if ($info['last_update']) {
echo " (last update: {$info['last_update']})";
}
echo "\n";
}
echo "\n🏠 LOCAL TABLES (Not synced):\n";
foreach ($result['local_tables'] as $table => $info) {
if (isset($info['error'])) {
echo " ⚠️ $table: {$info['error']}\n";
} else {
echo " ℹ️ $table: {$info['records']} records - {$info['note']}\n";
}
}
echo "\n";
}
function analyzeTableStrategy(): void
{
echo "\n📊 Table Synchronization Analysis:\n";
echo "=================================\n";
echo "\n✅ TABLES FOR BINARY SYNC (Global Blockchain State):\n";
$blockchainTables = [
'blocks' => 'Core blockchain blocks - must be identical across all nodes',
'transactions' => 'Transaction records - part of blockchain consensus',
'wallets' => 'Account balances - global state that affects validation',
'staking' => 'PoS staking data - critical for consensus mechanism',
'validators' => 'Validator information - needed for block validation',
'smart_contracts' => 'Contract code and state - part of blockchain state'
];
foreach ($blockchainTables as $table => $reason) {
echo " 📦 $table\n";
echo " → $reason\n";
}
echo "\n❌ TABLES NOT FOR BINARY SYNC (Local Node Data):\n";
$localTables = [
'config' => 'Node-specific configuration - each node has its own settings',
'nodes' => 'Peer network info - different for each node depending on connections',
'mempool' => 'Pending transactions - temporary data, not part of blockchain yet',
'logs' => 'System logs - local debugging/monitoring information',
'users' => 'User accounts - local authentication, not part of blockchain state'
];
foreach ($localTables as $table => $reason) {
echo " 🏠 $table\n";
echo " → $reason\n";
}
echo "\n💡 RATIONALE:\n";
echo " • Blockchain tables contain CONSENSUS-CRITICAL data\n";
echo " • Local tables contain NODE-SPECIFIC data\n";
echo " • Syncing local tables would cause conflicts between nodes\n";
echo " • Each node needs its own configuration and logs\n";
echo " • Mempool is temporary and varies between nodes\n";
echo "\n📋 IMPLEMENTATION:\n";
echo " 1. SelectiveBlockchainSyncManager syncs only blockchain tables\n";
echo " 2. Local tables remain in database only\n";
echo " 3. Binary file contains pure blockchain state\n";
echo " 4. Each node can have different local data\n";
echo " 5. Network consensus only affects blockchain tables\n";
}