From 87170247bd237c2bc90eaf94e9333cd0e3201118 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Fri, 11 Feb 2022 20:35:53 +0900 Subject: [PATCH 1/2] Revert "Merge pull request #1240 from nymkappa/feature/mempool-sync-threshold" This reverts commit 2f921f4cc73994cc9ea9c317c8897de0d1881340, reversing changes made to 877be47e5be03e31b25cc12fbe2ee45be881e5d8. --- backend/src/api/mempool.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/backend/src/api/mempool.ts b/backend/src/api/mempool.ts index 3a389c059..a8b0b461a 100644 --- a/backend/src/api/mempool.ts +++ b/backend/src/api/mempool.ts @@ -168,8 +168,7 @@ class Mempool { const newTransactionsStripped = newTransactions.map((tx) => Common.stripTransaction(tx)); this.latestTransactions = newTransactionsStripped.concat(this.latestTransactions).slice(0, 6); - const syncedThreshold = 0.99; // If we synced 99% of the mempool tx count, consider we're synced - if (!this.inSync && Object.keys(this.mempoolCache).length >= transactions.length * syncedThreshold) { + if (!this.inSync && transactions.length === Object.keys(this.mempoolCache).length) { this.inSync = true; logger.notice('The mempool is now in sync!'); loadingIndicators.setProgress('mempool', 100); From a88d6d2fca401e94d8e08047d538b94847170f65 Mon Sep 17 00:00:00 2001 From: nymkappa Date: Fri, 11 Feb 2022 21:25:58 +0900 Subject: [PATCH 2/2] Don't wait for 100% mempool sync before starting block indexing --- backend/src/api/blocks.ts | 6 +++--- backend/src/api/mempool.ts | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/backend/src/api/blocks.ts b/backend/src/api/blocks.ts index d2487414f..affbf0049 100644 --- a/backend/src/api/blocks.ts +++ b/backend/src/api/blocks.ts @@ -61,7 +61,7 @@ class Blocks { // optimize here by directly fetching txs in the "outdated" mempool transactions.push(mempool[txIds[i]]); transactionsFound++; - } else if (config.MEMPOOL.BACKEND === 'esplora' || memPool.isInSync() || i === 0) { + } else if (config.MEMPOOL.BACKEND === 'esplora' || !memPool.hasPriority() || i === 0) { // Otherwise we fetch the tx data through backend services (esplora, electrum, core rpc...) if (!quiet && (i % (Math.round((txIds.length) / 10)) === 0 || i + 1 === txIds.length)) { // Avoid log spam logger.debug(`Indexing tx ${i + 1} of ${txIds.length} in block #${blockHeight}`); @@ -175,7 +175,7 @@ class Blocks { public async $generateBlockDatabase() { if (['mainnet', 'testnet', 'signet'].includes(config.MEMPOOL.NETWORK) === false || // Bitcoin only config.MEMPOOL.INDEXING_BLOCKS_AMOUNT === 0 || // Indexing of older blocks must be enabled - !memPool.isInSync() || // We sync the mempool first + memPool.hasPriority() || // We sync the mempool first this.blockIndexingStarted === true || // Indexing must not already be in progress config.DATABASE.ENABLED === false ) { @@ -314,7 +314,7 @@ class Blocks { if (this.newBlockCallbacks.length) { this.newBlockCallbacks.forEach((cb) => cb(blockExtended, txIds, transactions)); } - if (memPool.isInSync()) { + if (!memPool.hasPriority()) { diskCache.$saveCacheToDisk(); } } diff --git a/backend/src/api/mempool.ts b/backend/src/api/mempool.ts index a8b0b461a..64505ba2b 100644 --- a/backend/src/api/mempool.ts +++ b/backend/src/api/mempool.ts @@ -13,6 +13,7 @@ class Mempool { private static WEBSOCKET_REFRESH_RATE_MS = 10000; private static LAZY_DELETE_AFTER_SECONDS = 30; private inSync: boolean = false; + private mempoolCacheDelta: number = -1; private mempoolCache: { [txId: string]: TransactionExtended } = {}; private mempoolInfo: IBitcoinApi.MempoolInfo = { loaded: false, size: 0, bytes: 0, usage: 0, total_fee: 0, maxmempool: 300000000, mempoolminfee: 0.00001000, minrelaytxfee: 0.00001000 }; @@ -32,6 +33,17 @@ class Mempool { setInterval(this.deleteExpiredTransactions.bind(this), 20000); } + /** + * Return true if we should leave resources available for mempool tx caching + */ + public hasPriority(): boolean { + if (this.inSync) { + return false; + } else { + return this.mempoolCacheDelta == -1 || this.mempoolCacheDelta > 25; + } + } + public isInSync(): boolean { return this.inSync; } @@ -100,6 +112,8 @@ class Mempool { const diff = transactions.length - currentMempoolSize; const newTransactions: TransactionExtended[] = []; + this.mempoolCacheDelta = Math.abs(diff); + if (!this.inSync) { loadingIndicators.setProgress('mempool', Object.keys(this.mempoolCache).length / transactions.length * 100); } @@ -174,6 +188,8 @@ class Mempool { loadingIndicators.setProgress('mempool', 100); } + this.mempoolCacheDelta = Math.abs(transactions.length - Object.keys(this.mempoolCache).length); + if (this.mempoolChangedCallback && (hasChange || deletedTransactions.length)) { this.mempoolChangedCallback(this.mempoolCache, newTransactions, deletedTransactions); }