-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmine_blocks.php
More file actions
317 lines (277 loc) · 11.5 KB
/
mine_blocks.php
File metadata and controls
317 lines (277 loc) · 11.5 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
<?php
/**
* Mine Blocks Web Endpoint
* URL: https://yournode.com/mine_blocks.php?cmd=mine&token=YOUR_TOKEN
*/
use Blockchain\Core\Blockchain\Block;
use Blockchain\Core\Transaction\MempoolManager;
use Blockchain\Core\Storage\BlockStorage;
use Blockchain\Core\Consensus\ValidatorManager;
use Blockchain\Core\Consensus\ProofOfStake;
use Blockchain\Core\Transaction\Transaction;
// Determine if running via web or CLI
$isWeb = isset($_SERVER['REQUEST_METHOD']);
if ($isWeb) {
// Set JSON response headers
header('Content-Type: application/json');
// CORS headers if needed
if (isset($_SERVER['HTTP_ORIGIN'])) {
header('Access-Control-Allow-Origin: ' . $_SERVER['HTTP_ORIGIN']);
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
}
// Handle OPTIONS request
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
exit(0);
}
}
try {
// Load environment and configuration
require_once __DIR__ . '/vendor/autoload.php';
// Determine project base directory
$baseDir = __DIR__;
// Load environment variables
require_once $baseDir . '/core/Environment/EnvironmentLoader.php';
\Blockchain\Core\Environment\EnvironmentLoader::load($baseDir);
// Load config
$configFile = $baseDir . '/config/config.php';
$config = [];
if (file_exists($configFile)) {
$config = require $configFile;
}
// Build database config with priority: config.php -> .env -> defaults
$dbConfig = $config['database'] ?? [];
// If empty, fallback to environment variables
if (empty($dbConfig) || !isset($dbConfig['host'])) {
$dbConfig = [
'host' => \Blockchain\Core\Environment\EnvironmentLoader::get('DB_HOST', 'localhost'),
'port' => (int)\Blockchain\Core\Environment\EnvironmentLoader::get('DB_PORT', 3306),
'database' => \Blockchain\Core\Environment\EnvironmentLoader::get('DB_DATABASE', 'blockchain'),
'username' => \Blockchain\Core\Environment\EnvironmentLoader::get('DB_USERNAME', 'root'),
'password' => \Blockchain\Core\Environment\EnvironmentLoader::get('DB_PASSWORD', ''),
'charset' => 'utf8mb4',
];
}
// Connect to database
$host = $dbConfig['host'] ?? 'localhost';
$dbname = $dbConfig['database'] ?? $dbConfig['name'] ?? 'blockchain';
$dsn = sprintf('mysql:host=%s;dbname=%s;charset=utf8mb4', $host, $dbname);
$pdo = new PDO($dsn, $dbConfig['username'] ?? 'root', $dbConfig['password'] ?? '', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
// Authentication check for web requests
if ($isWeb) {
$token = $_GET['token'] ?? null;
if (!$token && isset($_SERVER['HTTP_AUTHORIZATION'])) {
if (preg_match('/Bearer\\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'], $m)) {
$token = trim($m[1]);
}
}
if (!$token) {
http_response_code(401);
echo json_encode(['error' => 'Missing authentication token']);
exit(1);
}
// Verify token against database
$stmt = $pdo->prepare("SELECT COUNT(*) FROM users WHERE api_key = ? AND role = 'admin'");
$stmt->execute([$token]);
if ($stmt->fetchColumn() == 0) {
http_response_code(403);
echo json_encode(['error' => 'Invalid authentication token']);
exit(1);
}
}
// Parse command and options
$command = 'mine'; // default
$maxTransactions = 100;
$verbose = false;
$dryRun = false;
if ($isWeb) {
$command = $_GET['cmd'] ?? $_GET['command'] ?? 'mine';
$maxTransactions = (int)($_GET['max'] ?? $_GET['max_tx'] ?? 100);
$verbose = isset($_GET['verbose']) || isset($_GET['v']);
$dryRun = isset($_GET['dry']) || isset($_GET['dry_run']);
} else {
// CLI mode
foreach ($argv as $arg) {
if (preg_match('/^--max=(\d+)$/', $arg, $m)) $maxTransactions = (int)$m[1];
elseif ($arg === '--dry') $dryRun = true;
elseif ($arg === '--verbose' || $arg === '-v') $verbose = true;
elseif ($arg === 'status') $command = 'status';
elseif ($arg === 'help') $command = 'help';
}
}
function log_message($msg, $force = false) {
global $verbose, $isWeb;
if ($verbose || $force) {
if ($isWeb) {
error_log($msg);
} else {
echo $msg . "\n";
}
}
}
// Execute command
switch ($command) {
case 'mine':
// Initialize components
$mempool = new MempoolManager($pdo, ['min_fee' => 0.001]);
// Determine previous block index/hash from DB blocks table
$prevHash = 'GENESIS';
$height = 0;
$stmt = $pdo->query("SELECT hash, height FROM blocks ORDER BY height DESC LIMIT 1");
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$prevHash = $row['hash'];
$height = (int)$row['height'] + 1;
}
$txs = $mempool->getTransactionsForBlock($maxTransactions);
if (empty($txs)) {
$result = [
'success' => true,
'message' => 'No transactions in mempool to mine',
'block_height' => $height,
'transactions_processed' => 0
];
break;
}
log_message('Preparing block height ' . $height . ' with ' . count($txs) . ' tx(s)', true);
// Get original hashes from mempool for cleanup
$stmt = $pdo->prepare("
SELECT tx_hash FROM mempool
ORDER BY priority_score DESC, created_at ASC
LIMIT :max_count
");
$stmt->bindParam(':max_count', $maxTransactions, PDO::PARAM_INT);
$stmt->execute();
$originalHashes = [];
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$originalHashes[] = $row['tx_hash'];
}
if ($dryRun) {
$result = [
'success' => true,
'message' => '[DRY RUN] Block prepared but not mined',
'block_height' => $height,
'transactions_count' => count($txs),
'original_hashes' => $originalHashes
];
break;
}
// Initialize PoS + validator manager for signature
$validatorManager = new ValidatorManager($pdo, $config);
// Use NullLogger for PSR-3 compatibility
require_once __DIR__ . '/core/Logging/NullLogger.php';
$logger = new \Blockchain\Core\Logging\NullLogger();
$pos = new ProofOfStake($logger);
$pos->setValidatorManager($validatorManager);
$block = new Block($height, $txs, $prevHash, [], []);
// Sign block via ValidatorManager
try {
$pos->signBlock($block);
} catch (Throwable $e) {
log_message('Block signing failed: ' . $e->getMessage(), true);
}
$storage = new BlockStorage(__DIR__ . '/storage/blockchain_runtime.json', $pdo, $validatorManager);
$ok = $storage->saveBlock($block);
if (!$ok) {
$result = [
'success' => false,
'message' => 'Failed to persist block',
'block_height' => $height
];
break;
}
// Remove mined tx from mempool using original hashes
$removed = 0;
$del = $pdo->prepare('DELETE FROM mempool WHERE tx_hash = ? OR tx_hash = ?');
foreach ($originalHashes as $originalHash) {
$dh = strtolower(trim((string)$originalHash));
$dh0 = str_starts_with($dh,'0x') ? $dh : ('0x'.$dh);
$dh1 = str_starts_with($dh,'0x') ? substr($dh,2) : $dh;
$del->execute([$dh0, $dh1]);
$removed += $del->rowCount();
}
$result = [
'success' => true,
'message' => 'Block mined successfully',
'block_height' => $height,
'block_hash' => $block->getHash(),
'transactions_processed' => count($txs),
'transactions_removed_from_mempool' => $removed,
'timestamp' => time()
];
log_message('Block mined: height=' . $height . ' hash=' . $block->getHash() . ' txs=' . count($txs) . ' removed=' . $removed, true);
break;
case 'status':
// Get mempool status
$stmt = $pdo->query("SELECT COUNT(*) as pending_tx FROM mempool");
$mempoolCount = $stmt->fetchColumn();
// Get latest block
$stmt = $pdo->query("SELECT height, hash, timestamp FROM blocks ORDER BY height DESC LIMIT 1");
$latestBlock = $stmt->fetch(PDO::FETCH_ASSOC);
$result = [
'success' => true,
'mempool_transactions' => (int)$mempoolCount,
'latest_block' => $latestBlock ? [
'height' => (int)$latestBlock['height'],
'hash' => $latestBlock['hash'],
'timestamp' => (int)$latestBlock['timestamp']
] : null,
'ready_to_mine' => $mempoolCount > 0
];
break;
case 'help':
default:
$result = [
'success' => true,
'message' => 'Mining endpoint help',
'commands' => [
'mine' => 'Mine new block from mempool transactions',
'status' => 'Get mining and mempool status'
],
'parameters' => [
'cmd' => 'Command to execute (mine|status)',
'token' => 'Authentication token (required)',
'max' => 'Maximum transactions per block (default: 100)',
'verbose' => 'Enable verbose logging',
'dry' => 'Dry run mode (prepare but don\'t mine)'
],
'examples' => [
'mine_blocks.php?cmd=mine&token=YOUR_TOKEN',
'mine_blocks.php?cmd=status&token=YOUR_TOKEN',
'mine_blocks.php?cmd=mine&token=YOUR_TOKEN&max=50&verbose=1'
]
];
break;
}
// Output result
if ($isWeb) {
echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} else {
if ($result['success']) {
echo $result['message'] . "\n";
if (isset($result['block_hash'])) {
echo "Block Hash: " . $result['block_hash'] . "\n";
}
} else {
echo "Error: " . $result['message'] . "\n";
exit(1);
}
}
} catch (Throwable $e) {
$error = [
'success' => false,
'message' => 'Mining error: ' . $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine()
];
if ($isWeb) {
http_response_code(500);
echo json_encode($error);
} else {
echo "Error: " . $error['message'] . "\n";
echo "File: " . $error['file'] . ":" . $error['line'] . "\n";
exit(1);
}
}