diff --git a/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts b/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts index d195b0eeb..f610ed883 100644 --- a/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts +++ b/backend/src/api/bitcoin/bitcoin-api-abstract-factory.ts @@ -3,7 +3,7 @@ import { IEsploraApi } from './esplora-api.interface'; export interface AbstractBitcoinApi { $getRawMempool(): Promise; $getRawTransaction(txId: string, skipConversion?: boolean, addPrevout?: boolean, lazyPrevouts?: boolean): Promise; - $getMempoolTransactions(expectedCount: number); + $getMempoolTransactions(lastTxid: string); $getTransactionHex(txId: string): Promise; $getBlockHeightTip(): Promise; $getBlockHashTip(): Promise; diff --git a/backend/src/api/bitcoin/bitcoin-api.ts b/backend/src/api/bitcoin/bitcoin-api.ts index 237c69834..3ccea01ef 100644 --- a/backend/src/api/bitcoin/bitcoin-api.ts +++ b/backend/src/api/bitcoin/bitcoin-api.ts @@ -59,7 +59,7 @@ class BitcoinApi implements AbstractBitcoinApi { }); } - $getMempoolTransactions(expectedCount: number): Promise { + $getMempoolTransactions(lastTxid: string): Promise { return Promise.resolve([]); } diff --git a/backend/src/api/bitcoin/esplora-api.ts b/backend/src/api/bitcoin/esplora-api.ts index 46b17a4d2..73a44a845 100644 --- a/backend/src/api/bitcoin/esplora-api.ts +++ b/backend/src/api/bitcoin/esplora-api.ts @@ -69,33 +69,8 @@ class ElectrsApi implements AbstractBitcoinApi { return this.$queryWrapper(config.ESPLORA.REST_API_URL + '/tx/' + txId); } - async $getMempoolTransactions(expectedCount: number): Promise { - const transactions: IEsploraApi.Transaction[] = []; - let count = 0; - let done = false; - let last_txid = ''; - while (!done) { - try { - const result = await this.$queryWrapper(config.ESPLORA.REST_API_URL + '/mempool/txs' + (last_txid ? '/' + last_txid : '')); - if (result) { - for (const tx of result) { - transactions.push(tx); - count++; - } - logger.info(`Fetched ${count} of ${expectedCount} mempool transactions from esplora`); - if (result.length > 0) { - last_txid = result[result.length - 1].txid; - } else { - done = true; - } - } else { - done = true; - } - } catch(err) { - logger.err('failed to fetch bulk mempool transactions from esplora'); - } - } - return transactions; + async $getMempoolTransactions(lastSeenTxid?: string): Promise { + return this.$queryWrapper(config.ESPLORA.REST_API_URL + '/mempool/txs' + (lastSeenTxid ? '/' + lastSeenTxid : '')); } $getTransactionHex(txId: string): Promise { diff --git a/backend/src/api/mempool.ts b/backend/src/api/mempool.ts index d0e63ae78..88533365e 100644 --- a/backend/src/api/mempool.ts +++ b/backend/src/api/mempool.ts @@ -9,6 +9,7 @@ import loadingIndicators from './loading-indicators'; import bitcoinClient from './bitcoin/bitcoin-client'; import bitcoinSecondClient from './bitcoin/bitcoin-second-client'; import rbfCache from './rbf-cache'; +import { IEsploraApi } from './bitcoin/esplora-api.interface'; class Mempool { private inSync: boolean = false; @@ -104,11 +105,34 @@ class Mempool { } public async $reloadMempool(expectedCount: number): Promise { - const rawTransactions = await bitcoinApi.$getMempoolTransactions(expectedCount); - logger.info(`Inserting loaded mempool transactions into local cache`); - for (const transaction of rawTransactions) { - const extendedTransaction = transactionUtils.extendMempoolTransaction(transaction); - this.mempoolCache[extendedTransaction.txid] = extendedTransaction; + let count = 0; + let done = false; + let last_txid; + loadingIndicators.setProgress('mempool', count / expectedCount * 100); + while (!done) { + try { + const result = await bitcoinApi.$getMempoolTransactions(last_txid); + if (result) { + for (const tx of result) { + const extendedTransaction = transactionUtils.extendMempoolTransaction(tx); + this.mempoolCache[extendedTransaction.txid] = extendedTransaction; + count++; + } + logger.info(`Fetched ${count} of ${expectedCount} mempool transactions from esplora`); + if (result.length > 0) { + last_txid = result[result.length - 1].txid; + } else { + done = true; + } + if (count < expectedCount) { + loadingIndicators.setProgress('mempool', count / expectedCount * 100); + } + } else { + done = true; + } + } catch(err) { + logger.err('failed to fetch bulk mempool transactions from esplora'); + } } logger.info(`Done inserting loaded mempool transactions into local cache`); }