From ee05a6852e3343dbd4b45768c4b720e1567b75fb Mon Sep 17 00:00:00 2001 From: softsimon Date: Sun, 7 May 2023 18:47:56 +0400 Subject: [PATCH] Rapid mempool sync --- backend/src/api/mempool.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/backend/src/api/mempool.ts b/backend/src/api/mempool.ts index 127314218..5746ca6d4 100644 --- a/backend/src/api/mempool.ts +++ b/backend/src/api/mempool.ts @@ -11,7 +11,6 @@ import bitcoinSecondClient from './bitcoin/bitcoin-second-client'; import rbfCache from './rbf-cache'; class Mempool { - private static WEBSOCKET_REFRESH_RATE_MS = 10000; private inSync: boolean = false; private mempoolCacheDelta: number = -1; private mempoolCache: { [txId: string]: TransactionExtended } = {}; @@ -34,7 +33,6 @@ class Mempool { private SAMPLE_TIME = 10000; // In ms private timer = new Date().getTime(); private missingTxCount = 0; - private mainLoopTimeout: number = 120000; constructor() { @@ -134,7 +132,7 @@ class Mempool { this.mempoolCacheDelta = Math.abs(diff); if (!this.inSync) { - loadingIndicators.setProgress('mempool', Object.keys(this.mempoolCache).length / transactions.length * 100); + loadingIndicators.setProgress('mempool', currentMempoolSize / transactions.length * 100); } // https://github.com/mempool/mempool/issues/3283 @@ -147,6 +145,7 @@ class Mempool { } }; + let loggerTimer = new Date().getTime() / 1000; for (const txid of transactions) { if (!this.mempoolCache[txid]) { try { @@ -169,9 +168,12 @@ class Mempool { logger.debug(`Error finding transaction '${txid}' in the mempool: ` + (e instanceof Error ? e.message : e)); } } - - if ((new Date().getTime()) - start > Mempool.WEBSOCKET_REFRESH_RATE_MS) { - break; + const elapsedSeconds = Math.round((new Date().getTime() / 1000) - loggerTimer); + if (elapsedSeconds > 4) { + const progress = (currentMempoolSize + newTransactions.length) / transactions.length * 100; + logger.debug(`Mempool is synchronizing. Processed ${newTransactions.length}/${diff} txs (${Math.round(progress)}%)`); + loadingIndicators.setProgress('mempool', progress); + loggerTimer = new Date().getTime() / 1000; } }