-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
549 lines (474 loc) · 15.1 KB
/
index.js
File metadata and controls
549 lines (474 loc) · 15.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
import Fastify from 'fastify';
import cors from '@fastify/cors';
import rateLimit from '@fastify/rate-limit';
import helmet from '@fastify/helmet';
import fastifyStatic from '@fastify/static';
import { readFile } from 'node:fs/promises';
import { basename, resolve, dirname, join } from 'node:path';
import { fileURLToPath as toFilePath } from 'node:url';
import pkg from './package.json' with { type: 'json' };
import { loadData, initializeDataOnStartup, getCachedData, searchChains, getChainById, getAllChains, getAllRelations, getRelationsById, getEndpointsById, getAllEndpoints, getAllKeywords, validateChainData, traverseRelations, countChainsByTag, getRpcMonitoringResults, getRpcMonitoringStatus, startRpcHealthCheck } from './dataService.js';
import {
PORT, HOST, BODY_LIMIT, MAX_PARAM_LENGTH,
RATE_LIMIT_MAX, RATE_LIMIT_WINDOW_MS,
RELOAD_RATE_LIMIT_MAX, SEARCH_RATE_LIMIT_MAX,
MAX_SEARCH_QUERY_LENGTH, CORS_ORIGIN,
DATA_SOURCE_THE_GRAPH, DATA_SOURCE_CHAINLIST,
DATA_SOURCE_CHAINS, DATA_SOURCE_SLIP44,
DATA_CACHE_ENABLED, DATA_CACHE_FILE
} from './config.js';
/**
* Build and configure the Fastify application
* @param {Object} options - Options for the Fastify instance
* @param {boolean} options.logger - Enable logging (default: true)
* @param {number} options.bodyLimit - Request body size limit
* @param {number} options.maxParamLength - Max parameter length
* @param {boolean} options.loadDataOnStartup - Load data on startup (default: true)
* @returns {Promise<FastifyInstance>} Configured Fastify instance
*/
export async function buildApp(options = {}) {
const {
logger = true,
bodyLimit = BODY_LIMIT,
maxParamLength = MAX_PARAM_LENGTH,
loadDataOnStartup = true
} = options;
const fastify = Fastify({
logger,
bodyLimit,
maxParamLength
});
// Security: CORS
await fastify.register(cors, {
origin: CORS_ORIGIN === '*' ? true : CORS_ORIGIN.split(',').map(s => s.trim()),
credentials: false
});
// Security: Helmet (security headers)
await fastify.register(helmet, {
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: ["'self'"],
styleSrc: ["'self'"],
fontSrc: ["'self'"],
connectSrc: ["'self'"],
imgSrc: ["'self'", "data:"]
}
}
});
// Serve public/ directory for the 3D visualization UI
const __dir = dirname(toFilePath(import.meta.url));
await fastify.register(fastifyStatic, {
root: join(__dir, 'public'),
prefix: '/ui/',
decorateReply: false
});
// Security: Rate limiting
await fastify.register(rateLimit, {
max: RATE_LIMIT_MAX,
timeWindow: RATE_LIMIT_WINDOW_MS
});
// Load data on startup
if (loadDataOnStartup) {
await initializeDataOnStartup({
onBackgroundRefreshSuccess: () => {
startRpcHealthCheck();
}
});
startRpcHealthCheck();
}
/**
* Health check endpoint
*/
fastify.get('/health', async () => {
const cachedData = getCachedData();
return {
status: 'ok',
dataLoaded: cachedData.indexed !== null,
lastUpdated: cachedData.lastUpdated,
totalChains: cachedData.indexed ? cachedData.indexed.all.length : 0
};
});
/**
* Get all chains
*/
fastify.get('/chains', async (request, reply) => {
const { tag } = request.query;
let chains = getAllChains();
// Filter by tag if provided (validate against known tags)
if (tag) {
const validTags = ['Testnet', 'L2', 'Beacon'];
if (!validTags.includes(tag)) {
return sendError(reply, 400, `Invalid tag. Allowed: ${validTags.join(', ')}`);
}
chains = chains.filter(chain => chain.tags?.includes(tag));
}
return {
count: chains.length,
chains
};
});
/**
* Get chain by ID
*/
fastify.get('/chains/:id', async (request, reply) => {
const chainId = parseIntParam(request.params.id);
if (chainId === null) {
return sendError(reply, 400, 'Invalid chain ID');
}
const chain = getChainById(chainId);
if (!chain) {
return sendError(reply, 404, 'Chain not found');
}
return chain;
});
/**
* Search chains (tighter rate limit)
*/
fastify.get('/search', {
config: {
rateLimit: {
max: SEARCH_RATE_LIMIT_MAX,
timeWindow: RATE_LIMIT_WINDOW_MS
}
}
}, async (request, reply) => {
const { q } = request.query;
if (!q) {
return sendError(reply, 400, 'Query parameter "q" is required');
}
if (q.length > MAX_SEARCH_QUERY_LENGTH) {
return sendError(reply, 400, `Query too long. Max length: ${MAX_SEARCH_QUERY_LENGTH}`);
}
const results = searchChains(q);
return {
query: q,
count: results.length,
results
};
});
/**
* Get all chain relations
*/
fastify.get('/relations', async () => {
const relations = getAllRelations();
return relations;
});
/**
* Get relations for a specific chain by ID
*/
fastify.get('/relations/:id', async (request, reply) => {
const chainId = parseIntParam(request.params.id);
if (chainId === null) {
return sendError(reply, 400, 'Invalid chain ID');
}
const result = getRelationsById(chainId);
if (!result) {
return sendError(reply, 404, 'Chain not found');
}
return result;
});
/**
* BFS graph traversal of chain relations
*/
fastify.get('/relations/:id/graph', async (request, reply) => {
const chainId = parseIntParam(request.params.id);
if (chainId === null) {
return sendError(reply, 400, 'Invalid chain ID');
}
const depth = request.query.depth === undefined ? 2 : parseIntParam(request.query.depth);
if (depth === null || depth < 1 || depth > 5) {
return sendError(reply, 400, 'Invalid depth. Must be between 1 and 5');
}
const result = traverseRelations(chainId, depth);
if (!result) {
return sendError(reply, 404, 'Chain not found');
}
return result;
});
/**
* Get all endpoints
*/
fastify.get('/endpoints', async () => {
const endpoints = getAllEndpoints();
return {
count: endpoints.length,
endpoints
};
});
/**
* Get endpoints for a specific chain by ID
*/
fastify.get('/endpoints/:id', async (request, reply) => {
const chainId = parseIntParam(request.params.id);
if (chainId === null) {
return sendError(reply, 400, 'Invalid chain ID');
}
const result = getEndpointsById(chainId);
if (!result) {
return sendError(reply, 404, 'Chain not found');
}
return result;
});
/**
* Get raw data sources
*/
fastify.get('/sources', async () => {
const cachedData = getCachedData();
return {
lastUpdated: cachedData.lastUpdated,
sources: {
theGraph: cachedData.theGraph ? 'loaded' : 'not loaded',
chainlist: cachedData.chainlist ? 'loaded' : 'not loaded',
chains: cachedData.chains ? 'loaded' : 'not loaded',
slip44: cachedData.slip44 ? 'loaded' : 'not loaded'
}
};
});
/**
* Export cached snapshot file
*/
fastify.get('/export', async (_request, reply) => {
if (!DATA_CACHE_ENABLED) {
return sendError(reply, 503, 'Data cache export is disabled');
}
const filePath = resolve(DATA_CACHE_FILE);
try {
const raw = await readFile(filePath, 'utf8');
const exportData = JSON.parse(raw);
reply.header('Content-Type', 'application/json; charset=utf-8');
reply.header('Content-Disposition', `attachment; filename="${basename(filePath)}"`);
return exportData;
} catch (error) {
if (error?.code === 'ENOENT') {
return sendError(reply, 404, 'Export file not found');
}
if (error instanceof SyntaxError) {
return sendError(reply, 500, 'Export file is not valid JSON');
}
fastify.log.error(error, 'Failed to export cache file');
return sendError(reply, 500, 'Failed to export cache file');
}
});
/**
* Get SLIP-0044 coin types as JSON
*/
fastify.get('/slip44', async (_request, reply) => {
const cachedData = getCachedData();
if (!cachedData.slip44) {
return sendError(reply, 503, 'SLIP-0044 data not loaded');
}
return {
count: Object.keys(cachedData.slip44).length,
coinTypes: cachedData.slip44
};
});
/**
* Get specific SLIP-0044 coin type by ID
*/
fastify.get('/slip44/:coinType', async (request, reply) => {
const coinType = parseIntParam(request.params.coinType);
if (coinType === null) {
return sendError(reply, 400, 'Invalid coin type');
}
const cachedData = getCachedData();
if (!cachedData.slip44?.[coinType]) {
return sendError(reply, 404, 'Coin type not found');
}
return cachedData.slip44[coinType];
});
/**
* Reload data from sources (tighter rate limit)
*/
fastify.post('/reload', {
config: {
rateLimit: {
max: RELOAD_RATE_LIMIT_MAX,
timeWindow: RATE_LIMIT_WINDOW_MS
}
}
}, async (request, reply) => {
try {
await loadData();
startRpcHealthCheck();
const cachedData = getCachedData();
return {
status: 'success',
lastUpdated: cachedData.lastUpdated,
totalChains: cachedData.indexed ? cachedData.indexed.all.length : 0
};
} catch (error) {
fastify.log.error(error, 'Failed to reload data');
return sendError(reply, 500, 'Failed to reload data');
}
});
/**
* Validate chain data for potential human errors
*/
fastify.get('/validate', async (_request, reply) => {
const validationResults = validateChainData();
if (validationResults.error) {
return sendError(reply, 503, validationResults.error);
}
return validationResults;
});
/**
* Get extracted keywords from indexed chain and RPC monitor data
*/
fastify.get('/keywords', async () => {
const keywordResults = getAllKeywords();
const cachedData = getCachedData();
return {
lastUpdated: cachedData.lastUpdated,
...keywordResults
};
});
/**
* Get RPC monitoring results
*/
fastify.get('/rpc-monitor', async () => {
const results = getRpcMonitoringResults();
const status = getRpcMonitoringStatus();
return {
...status,
...results
};
});
/**
* Get RPC monitoring results for a specific chain
*/
fastify.get('/rpc-monitor/:id', async (request, reply) => {
const chainId = parseIntParam(request.params.id);
if (chainId === null) {
return sendError(reply, 400, 'Invalid chain ID');
}
const results = getRpcMonitoringResults();
const chainResults = results.results.filter(r => r.chainId === chainId);
if (chainResults.length === 0) {
return sendError(reply, 404, 'No monitoring results found for this chain');
}
const workingCount = chainResults.filter(r => r.status === 'working').length;
const failedCount = chainResults.filter(r => r.status === 'failed').length;
return {
chainId,
chainName: chainResults[0].chainName,
totalEndpoints: chainResults.length,
workingEndpoints: workingCount,
failedEndpoints: failedCount,
lastUpdated: results.lastUpdated,
endpoints: chainResults
};
});
/**
* Get aggregate stats
*/
fastify.get('/stats', async () => {
const chains = getAllChains();
const monitorResults = getRpcMonitoringResults();
const { totalChains, totalMainnets, totalTestnets, totalL2s, totalBeacons } = countChainsByTag(chains);
const rpcWorking = monitorResults.workingEndpoints;
const rpcFailed = monitorResults.failedEndpoints || 0;
const rpcTested = monitorResults.testedEndpoints;
const rpcHealthPercent = rpcTested > 0 ? Math.round((rpcWorking / rpcTested) * 10000) / 100 : null;
return {
totalChains,
totalMainnets,
totalTestnets,
totalL2s,
totalBeacons,
rpc: {
totalEndpoints: monitorResults.totalEndpoints,
tested: rpcTested,
working: rpcWorking,
failed: rpcFailed,
healthPercent: rpcHealthPercent
},
lastUpdated: monitorResults.lastUpdated
};
});
/**
* Root endpoint with API information
*/
fastify.get('/', async (request, reply) => {
return {
name: 'Chains API',
version: pkg.version,
description: 'API query service for blockchain chain data from multiple sources',
endpoints: {
'/health': 'Health check and data status',
'/chains': 'Get all chains (optional ?tag=Testnet|L2|Beacon)',
'/chains/:id': 'Get chain by ID',
'/search?q={query}': 'Search chains by name or ID',
'/relations': 'Get all chain relations data',
'/relations/:id': 'Get relations for a specific chain by ID',
'/endpoints': 'Get all chain endpoints (RPC, firehose, substreams)',
'/endpoints/:id': 'Get endpoints for a specific chain by ID',
'/sources': 'Get data sources status',
'/export': 'Export cached snapshot file',
'/slip44': 'Get all SLIP-0044 coin types as JSON',
'/slip44/:coinType': 'Get specific SLIP-0044 coin type by ID',
'/reload': 'Reload data from sources (POST)',
'/validate': 'Validate chain data for potential human errors',
'/keywords': 'Get extracted keywords (blockchain names, network names, client names, etc.)',
'/rpc-monitor': 'Get RPC endpoint monitoring results',
'/rpc-monitor/:id': 'Get RPC monitoring results for a specific chain by ID',
'/stats': 'Get aggregate stats (chain counts, RPC health percentage)',
'/relations/:id/graph?depth=N': 'BFS graph traversal of chain relations (default depth: 2)'
},
dataSources: [
DATA_SOURCE_THE_GRAPH,
DATA_SOURCE_CHAINLIST,
DATA_SOURCE_CHAINS,
DATA_SOURCE_SLIP44
]
};
});
return fastify;
}
// Helper functions for reducing duplication
/**
* Parse and validate an integer parameter
* @param {string} param - Parameter value to parse
* @returns {number|null} Parsed integer or null if invalid
*/
function parseIntParam(param) {
if (typeof param === 'number') {
return Number.isInteger(param) ? param : null;
}
if (typeof param !== 'string') {
return null;
}
const normalized = param.trim();
if (!/^-?\d+$/.test(normalized)) {
return null;
}
const parsed = Number.parseInt(normalized, 10);
return Number.isNaN(parsed) ? null : parsed;
}
/**
* Send a standardized error response
* @param {FastifyReply} reply - Fastify reply object
* @param {number} code - HTTP status code
* @param {string} message - Error message
*/
function sendError(reply, code, message) {
return reply.code(code).send({ error: message });
}
// Only run the server if this file is executed directly (CLI mode)
// This allows the file to be imported for testing without starting the server
const __filename = toFilePath(import.meta.url);
// Check if this file is being run directly
const isMainModule = process.argv[1] === __filename;
if (isMainModule) {
const start = async () => {
try {
const app = await buildApp();
await app.listen({ port: PORT, host: HOST });
app.log.info(`Server is running at http://${HOST}:${PORT}`);
} catch (err) {
console.error(err);
process.exit(1);
}
};
start();
}