From 95d4108d0b8c851ff423a2ee367cbd1dd1e35010 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20C=C3=A1rdenas?= Date: Tue, 12 Oct 2021 10:48:13 -0500 Subject: [PATCH] fix: socket.io incorrect microblock and mempool updates * fix: missing break in microblock updates * fix: also search pruned mempool txs for socket updates * fix: allow empty subscription array on connect --- client/src/socket-io/index.ts | 5 ++++- src/api/routes/socket-io.ts | 8 ++++++-- src/datastore/postgres-store.ts | 1 + 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/client/src/socket-io/index.ts b/client/src/socket-io/index.ts index 950c04c4..9c47fe6c 100644 --- a/client/src/socket-io/index.ts +++ b/client/src/socket-io/index.ts @@ -62,7 +62,10 @@ export class StacksApiSocketClient { subscriptions.delete(topic); } // Update the subscriptions in the socket handshake so topics are persisted on re-connect. - this.socket.io.opts.query!.subscriptions = Array.from(subscriptions).join(','); + if (this.socket.io.opts.query === undefined) { + this.socket.io.opts.query = {}; + } + this.socket.io.opts.query.subscriptions = Array.from(subscriptions).join(','); return { unsubscribe: () => { this.handleSubscription(topic, false); diff --git a/src/api/routes/socket-io.ts b/src/api/routes/socket-io.ts index 96dbf447..4b9e00b5 100644 --- a/src/api/routes/socket-io.ts +++ b/src/api/routes/socket-io.ts @@ -87,8 +87,12 @@ export function createSocketIORouter(db: DataStore, server: http.Server) { } io.on('connection', socket => { + logger.info('[socket.io] new connection'); prometheus?.connect(); - socket.on('disconnect', _ => prometheus?.disconnect()); + socket.on('disconnect', reason => { + logger.info(`[socket.io] disconnected: ${reason}`); + prometheus?.disconnect(); + }); const subscriptions = socket.handshake.query['subscriptions']; if (subscriptions) { // TODO: check if init topics are valid, reject connection with error if not @@ -173,7 +177,7 @@ export function createSocketIORouter(db: DataStore, server: http.Server) { const dbTxQuery = await db.getMempoolTx({ txId: txId, includeUnanchored: true, - includePruned: false, + includePruned: true, }); if (!dbTxQuery.found) { return; diff --git a/src/datastore/postgres-store.ts b/src/datastore/postgres-store.ts index 38581d1c..b8c62aa7 100644 --- a/src/datastore/postgres-store.ts +++ b/src/datastore/postgres-store.ts @@ -608,6 +608,7 @@ export class PgDataStore case 'microblockUpdate': const microblock = notification.payload as PgMicroblockNotificationPayload; this.emit('microblockUpdate', microblock.microblockHash); + break; case 'txUpdate': const tx = notification.payload as PgTxNotificationPayload; this.emit('txUpdate', tx.txId);