Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x 40x | // cSpell:ignore cardano utxos import { Cardano, CardanoNode, Seconds, createSlotEpochInfoCalc } from '@cardano-sdk/core'; import { GenesisData } from '..'; import { Logger } from 'ts-log'; import { NetworkInfoResponses, WSMessage, WsProvider, isTxRelevant } from '@cardano-sdk/cardano-services-client'; import { Notification, Pool } from 'pg'; import { Server, createServer } from 'http'; import { WebSocket, WebSocketServer } from 'ws'; import { getLovelaceSupply, getProtocolParameters, getStake, transactionsByAddresses } from './requests'; import { initDB } from './db'; import { toGenesisParams } from '../NetworkInfo/DbSyncNetworkInfoProvider/mappers'; import { toSerializableObject } from '@cardano-sdk/util'; import { v4 } from 'uuid'; export { WebSocket } from 'ws'; declare module 'ws' { interface WebSocket { addresses: Cardano.PaymentAddress[]; clientId: string; heartbeat: number; logError: (error: Error, msg: string) => void; logInfo: (msg: object | string) => void; logDebug: (msg: object | string) => void; sendMessage: (message: WSMessage) => void; } interface WebSocketServer { clients: Set<WebSocket>; } } export interface WsServerConfiguration { /** The cache time to live in seconds. */ dbCacheTtl: number; /** The heartbeat check interval in seconds. */ heartbeatInterval?: number; /** The heartbeat timeout in seconds. */ heartbeatTimeout?: number; /** The port to listen. */ port: number; } export interface WsServerDependencies { /** The `CardanoNode` object. */ cardanoNode: CardanoNode; /** The PostgreSQL Pool. */ db: Pool; /** The `GenesisData` object */ genesisData: GenesisData; /** The logger. */ logger: Logger; } // eslint-disable-next-line @typescript-eslint/no-empty-function const noop = () => {}; interface NotificationBody { message: WSMessage; transactions: Cardano.HydratedTx[]; } interface NotificationEvent { notification: number; transactions: Cardano.HydratedTx[]; } /** * Since some debug log information may require heavy computation to stringify data, * better checking if we are interested in logging some data before actually logging it. */ const debugLog = process.env.LOGGER_MIN_SEVERITY === 'debug'; const toError = (error: unknown) => error instanceof Error ? error : new Error(`Unknown error: ${JSON.stringify(error)}`); export class CardanoWsServer extends WsProvider { private cardanoNode: CardanoNode; private closeNotify = noop; private closing = false; private db: Pool; private heartbeatInterval: NodeJS.Timer | undefined; private lastReceivedNotification = 0; private lastSentNotification = 0; private lastSlot = Number.POSITIVE_INFINITY as Cardano.Slot; private logger: Logger; private networkInfo: NetworkInfoResponses; private notifications = new Map<number, NotificationBody>(); private server: Server; private stakeInterval: NodeJS.Timer | undefined; private syncing = true; private wss: WebSocketServer; constructor(dependencies: WsServerDependencies, cfg: WsServerConfiguration) { super(); this.cardanoNode = dependencies.cardanoNode; this.db = dependencies.db; this.logger = dependencies.logger; this.networkInfo = { genesisParameters: toGenesisParams(dependencies.genesisData) } as NetworkInfoResponses; // Create the HTTP and the WebSocket servers this.server = this.createHttpServer(); this.wss = new WebSocketServer({ path: '/ws', server: this.server }) as WebSocketServer; // Attach the handlers to the servers events this.wss.on('connection', this.createOnConnection()); this.server.on('error', (error) => { this.logger.error(error, 'Async error from HTTP server'); this.close(); }); this.wss.on('error', (error) => { this.logger.error(error, 'Async error from WebSocket server'); this.close(); }); // Init the server this.init( cfg.port, Seconds.toMilliseconds(Seconds(cfg.dbCacheTtl || 120)), Seconds.toMilliseconds(Seconds(cfg.heartbeatInterval || 10)), Seconds.toMilliseconds(Seconds(cfg.heartbeatTimeout || 60)) ).catch((error) => { this.logger.error(error, 'Error in init sequence'); this.close(); }); } /** Closes the server. */ close(callback?: () => void) { this.closing = true; this.closeNotify(); Iif (this.heartbeatInterval) clearInterval(this.heartbeatInterval); Iif (this.stakeInterval) clearInterval(this.stakeInterval); this.wss.close((wsError) => { Iif (wsError && (!(wsError instanceof Error) || wsError.message !== 'The server is not running')) this.logger.error(wsError, 'Error while closing the WebSocket server'); this.server.close((httpError) => { // TODO required TypeScript v5.5.4 to remove casting as any // eslint-disable-next-line @typescript-eslint/no-explicit-any Iif (httpError && (!('code' in httpError) || (httpError as any).code !== 'ERR_SERVER_NOT_RUNNING')) this.logger.error(httpError, 'Error while closing the HTTP server'); Iif (callback) callback(); }); }); for (const client of this.wss.clients) client.close(); } /** Creates a simple HTTP server which just handles the `/health` URL. Mainly used to listen the WS server. */ private createHttpServer() { return createServer(async (req, res) => { const { method, url } = req; Iif (['/health', '/ready'].includes(url!)) { const healthCheck = await this.healthCheck(); Iif (url === '/health' && healthCheck.notRecoverable) res.statusCode = 500; Iif (url === '/ready' && !healthCheck.ok) res.statusCode = 500; return res.end(JSON.stringify(healthCheck)); } this.logger.info(method, url); res.statusCode = 404; res.end('Not found'); }); } private createOnNotification() { // This is the entry point for a new NOTIFY event from the DB; i.e. each time a new record is inserted in the block table // eslint-disable-next-line sonarjs/cognitive-complexity return (msg: Notification) => (async () => { const notification = ++this.lastReceivedNotification; const { payload } = msg; Iif (!payload) throw new Error('Missing payload in NOTIFY'); // The payload of the NOTIFY event contain the tip in the correct format const { blockId, ...ledgerTip } = JSON.parse(payload) as Cardano.Tip & { blockId: string }; this.networkInfo.ledgerTip = ledgerTip; this.logger.debug(`Notification ${notification}: ${JSON.stringify(ledgerTip)}`); const epochRollover = async () => { Iif (ledgerTip.slot <= this.lastSlot) return { ledgerTip }; this.logger.debug(`Epoch rollover for notification ${notification}...`); await this.onEpochRollover(); const { eraSummaries, lovelaceSupply, protocolParameters } = this.networkInfo; Iif (debugLog) this.logger.debug( `Epoch rollover for notification ${notification}: ${JSON.stringify( toSerializableObject({ eraSummaries, lovelaceSupply, protocolParameters }) )}` ); return { eraSummaries, ledgerTip, lovelaceSupply, protocolParameters }; }; const loadTransactions = async () => { const addressesObj: Record<Cardano.PaymentAddress, true> = {}; // Benchmarks // https://jsbenchmark.com/#eyJjYXNlcyI6W3siaWQiOiI5aTVXR2E3bXp0MEE2LU1FZFJDaVQiLCJjb2RlIjoiY29uc3QgeyB3c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RyZXNzZXNbYUlkeCsrXSA9IGFkZHJlc3M7XG4gICAgICAgIHdzLmFkZHJlc3Nlcy5wdXNoKGFkZHJlc3MpO1xuICAgICAgfVxuXG4gICAgICBjbGllbnRzLnB1c2god3MpO1xuICAgIH1cbn1cblxucmV0dXJuIHsgd3NzOiB7IGNsaWVudHM6IG5ldyBTZXQoY2xpZW50cykgfSB9OyJ9fQ for (const ws of this.wss.clients) { const addresses = ws.addresses; // eslint-disable-next-line space-in-parens for (let aLen = addresses.length; aLen--; ) addressesObj[addresses[aLen]] = true; } const addresses = Object.keys(addressesObj) as Cardano.PaymentAddress[]; Iif (debugLog) this.logger.debug(`Transactions for notification ${notification} ${JSON.stringify(addresses)}`); const txs = addresses.length === 0 ? [] : await transactionsByAddresses(addresses, this.db, { blockId }); Iif (debugLog) this.logger.debug(`Transactions for notification ${notification} ${JSON.stringify(txs)}`); return txs; }; const [networkInfo, transactions] = await Promise.all([epochRollover(), loadTransactions()]); this.send({ networkInfo }, { notification, transactions }); })().catch((error) => { this.logger.error(error, 'Error while handling tip notification'); // Since an error while handling tip notification may be source of data inconsistencies, better to shutdown this.emitHealth( error instanceof Error ? error.message || 'Unknown error' : `Unknown error ${JSON.stringify(error)}`, { notRecoverable: true, overwrite: true } ); this.close(); }); } private listenNotify() { const { db, logger } = this; // This is the function which listens for events throw the NOTIFY command. // It recursively calls itself to handle reconnection. const addListener = () => { logger.info('Connecting to DB to listen on sdk_tip NOTIFY...'); // eslint-disable-next-line unicorn/consistent-function-scoping const reAddListener = () => { // eslint-disable-next-line unicorn/consistent-destructuring Iif (!this.closing) setTimeout(addListener, 1000).unref(); }; // Ask for a DB client to the connections pool. db.connect((err, client, done) => { Iif (err) { logger.error(err, 'Error while connecting to DB to listen on sdk_tip NOTIFY'); this.emitHealth(err.message, { overwrite: true }); // In case of error opening the DB client, just retry after 1". return reAddListener(); } logger.info('Connected to DB to listen on sdk_tip NOTIFY'); // Set the function to close the client; used here for error handling and later by server close this.closeNotify = () => { logger.info('Closing DB connection listening on sdk_tip NOTIFY'); this.emitHealth('closing'); // Resets itself, in case it is called more than once... this.closeNotify = noop; done(); }; // Attach the handlers to the DB client events client.on('end', () => { this.emitHealth('closed'); // Being this the client closed event handler, there is no longer need for the function to close it this.closeNotify = noop; // If the connection was closed because the server is being closed as well, that's all // For any other reason the connection was closed, just retry a new connection reAddListener(); }); client.on('error', (e) => { logger.error(e, 'Async error from sdk_tip NOTIFY'); this.emitHealth(e.message, { overwrite: true }); this.closeNotify(); }); // eslint-disable-next-line unicorn/consistent-destructuring client.on('notification', this.createOnNotification()); // Issue the LISTEN command to get the notification event client.query('LISTEN sdk_tip', (e) => { Iif (!e) return; logger.error(e, 'Error while listening on sdk_tip NOTIFY'); this.emitHealth(e.message, { overwrite: true }); this.closeNotify(); }); }); }; addListener(); } private async init(port: number, dbCacheTtl: number, heartbeatInterval: number, heartbeatTimeout: number) { const { cardanoNode, db, logger, networkInfo, server, wss } = this; const refreshStake = async () => { const stake = await getStake(cardanoNode, db); networkInfo.stake = stake; this.send({ networkInfo: { stake } }); }; networkInfo.ledgerTip = await initDB(db, logger); await Promise.all([this.onEpochRollover(), refreshStake()]); this.stakeInterval = setInterval( () => refreshStake().catch((error) => { logger.error(error, 'Error while refreshing stake'); this.close(); }), dbCacheTtl ); // eslint-disable-next-line unicorn/consistent-destructuring this.stakeInterval.unref(); this.heartbeatInterval = setInterval(() => { const threshold = Date.now() - heartbeatTimeout; for (const ws of wss.clients.values()) Iif (ws.heartbeat < threshold) { ws.logInfo('Timed out, closing'); ws.close(); } }, heartbeatInterval); // eslint-disable-next-line unicorn/consistent-destructuring this.heartbeatInterval.unref(); const check = async () => { const { localNode } = await cardanoNode.healthCheck(); Iif (!localNode) throw new Error('Missing node health check response'); Iif (!localNode.ledgerTip) throw new Error('Missing "ledgerTip" in node health check response'); Iif (!localNode.networkSync) throw new Error('Missing "networkSync" in node health check response'); const projectedTip = networkInfo.ledgerTip; const tipDiff = localNode.ledgerTip.blockNo - projectedTip.blockNo; // Two blocks difference tolerance Iif (tipDiff >= 2) return this.emitHealth({ localNode, ok: false, projectedTip }); // Leave untouched the status set by healthCheck in init method Iif (localNode.ledgerTip.blockNo === 0) return; const ok = localNode.networkSync >= 0.999; // eslint-disable-next-line unicorn/consistent-destructuring Iif (ok && this.syncing) this.syncing = false; this.emitHealth({ localNode, ok, projectedTip }); }; const healthCheck = () => { check() .finally(() => setTimeout(healthCheck, 1000).unref()) .catch((error_) => { const error = toError(error_); this.emitHealth(error.message); logger.error(error, 'While checking node health check'); }); }; // Check synchronously to be sure init is complete await check(); // Next schedule asynchronous checks healthCheck(); server.listen(port, () => logger.info('WebSocket server ready and listening')); this.listenNotify(); } private createOnConnection() { // This is the entry point for each new WebSocket connection return (ws: WebSocket) => { const { logger, networkInfo, syncing } = this; const clientId = (ws.clientId = v4()); ws.addresses = []; // Create some wrappers for the logger ws.logError = (error: Error, msg: string) => { logger.error({ clientId }, msg); logger.error(error, msg); }; ws.logInfo = (msg: object | string) => logger.info(...(typeof msg === 'string' ? [{ clientId }, msg] : [{ clientId, ...msg }])); ws.logDebug = (msg: object | string) => logger.debug(...(typeof msg === 'string' ? [{ clientId }, msg] : [{ clientId, ...msg }])); ws.sendMessage = (message: WSMessage) => { const stringMessage = JSON.stringify(toSerializableObject(message)); ws.logDebug(stringMessage); ws.send(stringMessage); }; const onClose = (close?: boolean) => { Iif (close) ws.close(); ws.logInfo('Connection closed'); // eslint-disable-next-line @typescript-eslint/no-empty-function ws.sendMessage = () => {}; }; ws.logInfo('Connected'); // If still syncing, actually do not accept connections Iif (syncing) { ws.sendMessage({ clientId, syncing: true }); setTimeout(() => onClose(true), 1000); return; } // Attach the handlers to the WS connection events ws.on('close', onClose); ws.on('error', (error) => ws.logError(error, 'Async error from WebSocket connection')); // This is the entry point for each new WebSocket message from this connection ws.on('message', (data) => { // First of all, refresh the heartbeat timeout ws.heartbeat = Date.now(); // This is never expected... just in case Iif (!(data instanceof Buffer)) return ws.logError(new Error('Not a Buffer'), `Unexpected data from WebSocket ${JSON.stringify(data)}`); // DoS protection Iif (data.length > 1024 * 100) { ws.logError(new Error('Buffer too long'), 'Unexpected data length from WebSocket: closing'); return ws.close(); } let request: WSMessage; try { request = JSON.parse(data.toString('utf8')); } catch (error) { ws.logError(error as Error, 'Error parsing message: closing'); return ws.close(); } // Heartbeat messages do not expect a response Iif (Object.keys(request).length === 0) return; this.request(ws, request).catch((error) => ws.logError(error, 'Error while processing request')); }); // Actually set the timeout for the first time ws.heartbeat = Date.now(); ws.sendMessage({ clientId, networkInfo }); }; } private async onEpochRollover() { const { cardanoNode, db, networkInfo } = this; // Immediately calculate last slot for this epoch (even if with old era summaries) to avoid the event being called twice Iif (networkInfo.eraSummaries) this.lastSlot = createSlotEpochInfoCalc(networkInfo.eraSummaries)(networkInfo.ledgerTip.slot).lastSlot.slot; [networkInfo.eraSummaries, networkInfo.lovelaceSupply, networkInfo.protocolParameters] = await Promise.all([ cardanoNode.eraSummaries(), getLovelaceSupply(db, networkInfo.genesisParameters.maxLovelaceSupply), getProtocolParameters(db) ]); // Calculate last slot for this epoch with new era summaries this.lastSlot = createSlotEpochInfoCalc(networkInfo.eraSummaries)(networkInfo.ledgerTip.slot).lastSlot.slot; } private async request(ws: WebSocket, request: WSMessage) { const { txsByAddresses, requestId } = request; try { ws.logInfo(request); Iif (txsByAddresses) { const action = (transactions?: Cardano.HydratedTx[], utxos?: Cardano.HydratedTx[]) => { Iif (transactions) ws.logInfo(`Sending ${transactions.length} transactions for request ${requestId}`); Iif (utxos) ws.logInfo(`Sending ${utxos.length} partial transactions for request ${requestId}`); ws.sendMessage({ transactions, utxos }); }; const { addresses, lower } = txsByAddresses; ws.addresses.push(...addresses); await transactionsByAddresses(addresses, this.db, { action, lower }); } Iif (requestId) { ws.logInfo(`Responding to request ${requestId}`); ws.sendMessage({ responseTo: requestId }); } } catch (error_) { const error = toError(error_); ws.logError(error, 'While performing request'); Iif (requestId) ws.sendMessage({ error, responseTo: requestId }); } } private send(message: WSMessage, notificationEvent?: NotificationEvent) { // If the message is not bound to a tip notification, just send it Iif (!notificationEvent) return this.sendString(JSON.stringify(toSerializableObject(message))); const { notification, transactions } = notificationEvent; Iif (debugLog) this.logger.debug(`Scheduling notification: ${JSON.stringify(toSerializableObject(message))}`); // Ensure messages from notifications are propagated in the same order as the notification was received this.notifications.set(notification, { message, transactions }); while (this.notifications.has(this.lastSentNotification + 1)) { this.sendNotification(this.notifications.get(++this.lastSentNotification)!); this.notifications.delete(this.lastSentNotification); } } private sendNotification(notification: NotificationBody) { const { message, transactions } = notification; const stringMessage = JSON.stringify(toSerializableObject(message)); for (const ws of this.wss.clients) { const txs = transactions.filter((tx) => isTxRelevant(tx, ws.addresses)); Iif (debugLog) ws.logDebug( `Sending notification: ${ txs.length === 0 ? stringMessage : JSON.stringify(toSerializableObject({ ...message, transactions: txs })) }` ); txs.length === 0 ? ws.send(stringMessage) : ws.sendMessage({ ...message, transactions: txs }); } } private sendString(message: string) { for (const ws of this.wss.clients.values()) ws.send(message); } } |