forked from infosave2007/phpblockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_manager.php
More file actions
373 lines (319 loc) · 11.6 KB
/
Copy pathnode_manager.php
File metadata and controls
373 lines (319 loc) · 11.6 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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
<?php
/**
* Simple Node Manager for Budget Hosting
* Basic PHP CLI without complex dependencies
*/
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/config/config.php';
use Blockchain\Core\Application;
use Blockchain\Core\Recovery\BlockchainRecoveryManager;
use Blockchain\Core\Storage\BlockchainBinaryStorage;
use Blockchain\Core\Network\NodeHealthMonitor;
function showHelp(): void
{
echo "Simple Node Manager\n";
echo "==================\n\n";
echo "Usage: php node_manager.php <command>\n\n";
echo "Commands:\n";
echo " status - Show node health status\n";
echo " check - Perform full health check\n";
echo " backup - Create backup\n";
echo " recover - Attempt recovery\n";
echo " install - Install/create database tables\n";
echo " help - Show this help\n\n";
}
function createRequiredDirectories(): void
{
$dirs = [
__DIR__ . '/storage',
__DIR__ . '/storage/blockchain',
__DIR__ . '/storage/backups',
__DIR__ . '/storage/state',
__DIR__ . '/storage/contracts',
__DIR__ . '/logs'
];
foreach ($dirs as $dir) {
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
echo "Created directory: {$dir}\n";
}
}
}
function initializeComponents(): array
{
try {
$config = include __DIR__ . '/config/config.php';
// Database connection using DatabaseManager
require_once __DIR__ . '/core/Database/DatabaseManager.php';
$database = \Blockchain\Core\Database\DatabaseManager::getConnection();
// Initialize components
$binaryStorage = new BlockchainBinaryStorage();
$healthMonitor = new NodeHealthMonitor($binaryStorage, $database, $config);
$recoveryManager = new BlockchainRecoveryManager($binaryStorage);
return [
'database' => $database,
'binary_storage' => $binaryStorage,
'health_monitor' => $healthMonitor,
'recovery_manager' => $recoveryManager,
'config' => $config
];
} catch (Exception $e) {
echo "❌ Failed to initialize: " . $e->getMessage() . "\n";
exit(1);
}
}
function showStatus(array $components): void
{
echo "Node Health Status\n";
echo "==================\n\n";
$healthMonitor = $components['health_monitor'];
$health = $healthMonitor->quickHealthCheck();
echo "Overall Status: " . ($health['healthy'] ? "✅ Healthy" : "❌ Unhealthy") . "\n";
echo "Node ID: {$health['node_id']}\n";
echo "Check Time: {$health['check_time']} ms\n";
echo "Timestamp: " . date('Y-m-d H:i:s', $health['timestamp']) . "\n\n";
echo "Component Checks:\n";
foreach ($health['checks'] as $check => $result) {
$icon = $result ? "✅" : "❌";
echo " {$icon} " . ucfirst(str_replace('_', ' ', $check)) . "\n";
}
if (!empty($health['errors'])) {
echo "\nErrors:\n";
foreach ($health['errors'] as $error) {
echo " • {$error}\n";
}
}
// Network stats
$networkStats = $healthMonitor->getNetworkStats();
if (!empty($networkStats)) {
echo "\nNetwork Statistics:\n";
foreach ($networkStats as $stat) {
echo " {$stat['status']}: {$stat['count']} nodes\n";
}
}
echo "\n";
}
function performFullCheck(array $components): void
{
echo "Performing Full Health Check\n";
echo "============================\n\n";
$healthMonitor = $components['health_monitor'];
$health = $healthMonitor->fullHealthCheck();
echo "Overall Status: " . ($health['healthy'] ? "✅ Healthy" : "❌ Unhealthy") . "\n";
echo "Check Time: {$health['check_time']} ms\n\n";
echo "Detailed Checks:\n";
foreach ($health['checks'] as $check => $result) {
$icon = $result ? "✅" : "❌";
echo " {$icon} " . ucfirst(str_replace('_', ' ', $check)) . "\n";
}
if (isset($health['details'])) {
echo "\nDetailed Information:\n";
foreach ($health['details'] as $key => $value) {
if (is_numeric($value)) {
if ($key === 'binary_file_size' || $key === 'disk_free') {
$value = formatBytes($value);
} elseif ($key === 'memory_usage' || $key === 'memory_peak') {
$value = formatBytes($value);
}
}
echo " " . ucfirst(str_replace('_', ' ', $key)) . ": {$value}\n";
}
}
echo "\n";
}
function createBackup(array $components): void
{
echo "Creating Backup\n";
echo "===============\n\n";
$recoveryManager = $components['recovery_manager'];
$backupPath = __DIR__ . '/storage/backups/backup_' . date('Y-m-d_H-i-s');
echo "Backup location: {$backupPath}\n";
$result = $recoveryManager->createBackup();
if ($result['success']) {
echo "✅ Backup created successfully\n";
echo "Files backed up:\n";
foreach ($result['files'] as $type => $file) {
echo " • {$type}: " . basename($file) . "\n";
}
echo "Total size: " . formatBytes($result['size']) . "\n";
} else {
echo "❌ Backup failed\n";
foreach ($result['errors'] as $error) {
echo " • {$error}\n";
}
}
echo "\n";
}
function attemptRecovery(array $components): void
{
echo "Attempting Recovery\n";
echo "===================\n\n";
$recoveryManager = $components['recovery_manager'];
echo "Running auto-recovery...\n";
$result = $recoveryManager->performAutoRecovery();
if ($result['success']) {
echo "✅ Recovery completed successfully\n";
echo "Actions taken:\n";
foreach ($result['actions_taken'] as $action) {
echo " • {$action}\n";
}
} else {
echo "❌ Recovery failed\n";
foreach ($result['errors'] as $error) {
echo " • {$error}\n";
}
}
echo "\n";
}
function installTables(array $components): void
{
echo "Installing Database Tables\n";
echo "==========================\n\n";
$database = $components['database'];
try {
// Create node_status table
$database->exec("
CREATE TABLE IF NOT EXISTS node_status (
node_id VARCHAR(64) PRIMARY KEY,
node_url VARCHAR(255) NOT NULL,
status ENUM('healthy', 'degraded', 'recovering', 'offline', 'error') NOT NULL,
details JSON,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_status (status),
INDEX idx_updated (updated_at)
)
");
// Create other required tables
$database->exec("
CREATE TABLE IF NOT EXISTS blocks (
id INT AUTO_INCREMENT PRIMARY KEY,
hash VARCHAR(64) UNIQUE NOT NULL,
previous_hash VARCHAR(64),
merkle_root VARCHAR(64),
timestamp INT NOT NULL,
nonce INT,
difficulty INT,
data TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_hash (hash),
INDEX idx_timestamp (timestamp)
)
");
$database->exec("
CREATE TABLE IF NOT EXISTS transactions (
id INT AUTO_INCREMENT PRIMARY KEY,
hash VARCHAR(64) UNIQUE NOT NULL,
from_address VARCHAR(42),
to_address VARCHAR(42) NOT NULL,
amount DECIMAL(20,8) NOT NULL,
fee DECIMAL(20,8) DEFAULT 0,
data TEXT,
signature TEXT,
block_hash VARCHAR(64),
timestamp INT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_hash (hash),
INDEX idx_addresses (from_address, to_address),
INDEX idx_timestamp (timestamp),
FOREIGN KEY (block_hash) REFERENCES blocks(hash)
)
");
$database->exec("
CREATE TABLE IF NOT EXISTS wallets (
id INT AUTO_INCREMENT PRIMARY KEY,
address VARCHAR(42) UNIQUE NOT NULL,
public_key TEXT NOT NULL,
private_key TEXT,
balance DECIMAL(20,8) DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_address (address)
)
");
$database->exec("
CREATE TABLE IF NOT EXISTS mempool (
id INT AUTO_INCREMENT PRIMARY KEY,
transaction_hash VARCHAR(64) UNIQUE NOT NULL,
transaction_data JSON NOT NULL,
priority INT DEFAULT 1,
status ENUM('pending', 'processing', 'confirmed', 'rejected', 'expired') DEFAULT 'pending',
retry_count INT DEFAULT 0,
expires_at TIMESTAMP NULL,
node_id VARCHAR(64),
broadcast_count INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_hash (transaction_hash),
INDEX idx_status (status),
INDEX idx_priority (priority),
INDEX idx_expires (expires_at),
INDEX idx_node (node_id)
)
");
$database->exec("
CREATE TABLE IF NOT EXISTS nodes (
id INT AUTO_INCREMENT PRIMARY KEY,
url VARCHAR(255) UNIQUE NOT NULL,
node_id VARCHAR(64),
active BOOLEAN DEFAULT 1,
last_seen TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_url (url),
INDEX idx_active (active),
INDEX idx_last_seen (last_seen)
)
");
echo "✅ All tables created successfully\n";
} catch (Exception $e) {
echo "❌ Failed to create tables: " . $e->getMessage() . "\n";
}
echo "\n";
}
function formatBytes(int $bytes): string
{
if ($bytes < 1024) {
return $bytes . ' B';
} elseif ($bytes < 1048576) {
return round($bytes / 1024, 2) . ' KB';
} elseif ($bytes < 1073741824) {
return round($bytes / 1048576, 2) . ' MB';
} else {
return round($bytes / 1073741824, 2) . ' GB';
}
}
// Main execution
if (php_sapi_name() !== 'cli') {
echo "This script must be run from command line\n";
exit(1);
}
$command = $argv[1] ?? 'help';
switch ($command) {
case 'status':
createRequiredDirectories();
$components = initializeComponents();
showStatus($components);
break;
case 'check':
createRequiredDirectories();
$components = initializeComponents();
performFullCheck($components);
break;
case 'backup':
createRequiredDirectories();
$components = initializeComponents();
createBackup($components);
break;
case 'recover':
createRequiredDirectories();
$components = initializeComponents();
attemptRecovery($components);
break;
case 'install':
createRequiredDirectories();
$components = initializeComponents();
installTables($components);
break;
case 'help':
default:
showHelp();
break;
}