Switch to batch mempool/txs/:txid endpoint

This commit is contained in:
Mononaut 2023-07-23 15:15:16 +09:00
parent 202d4122b4
commit db715a1dba
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E

View File

@ -5,8 +5,6 @@ import { AbstractBitcoinApi } from './bitcoin-api-abstract-factory';
import { IEsploraApi } from './esplora-api.interface'; import { IEsploraApi } from './esplora-api.interface';
import logger from '../../logger'; import logger from '../../logger';
import JsonStream from 'JSONStream';
const axiosConnection = axios.create({ const axiosConnection = axios.create({
httpAgent: new http.Agent({ keepAlive: true, }) httpAgent: new http.Agent({ keepAlive: true, })
}); });
@ -74,22 +72,30 @@ class ElectrsApi implements AbstractBitcoinApi {
async $getMempoolTransactions(expectedCount: number): Promise<IEsploraApi.Transaction[]> { async $getMempoolTransactions(expectedCount: number): Promise<IEsploraApi.Transaction[]> {
const transactions: IEsploraApi.Transaction[] = []; const transactions: IEsploraApi.Transaction[] = [];
let count = 0; let count = 0;
return new Promise((resolve, reject) => { let done = false;
axiosConnection.get(config.ESPLORA.REST_API_URL + '/mempool/txs', { ...this.activeAxiosConfig, timeout: 60000, responseType: 'stream' }).then(response => { let last_txid = '';
response.data.pipe(JsonStream.parse('*')).on('data', transaction => { while (!done) {
count++; try {
if (count % 10000 === 0) { const result = await this.$queryWrapper<IEsploraApi.Transaction[]>(config.ESPLORA.REST_API_URL + '/mempool/txs' + (last_txid ? '/' + last_txid : ''));
logger.info(`Fetched ${count} of ${expectedCount} mempool transactions from esplora`); if (result) {
for (const tx of result) {
transactions.push(tx);
count++;
} }
transactions.push(transaction); logger.info(`Fetched ${count} of ${expectedCount} mempool transactions from esplora`);
}).on('end', () => { if (result.length > 0) {
logger.info(`Fetched all ${count} of ${expectedCount} mempool transactions from esplora`); last_txid = result[result.length - 1].txid;
resolve(transactions); } else {
}).on('error', (err) => { done = true;
reject(err); }
}); } else {
}); done = true;
}); }
} catch(err) {
logger.err('failed to fetch bulk mempool transactions from esplora');
}
}
return transactions;
} }
$getTransactionHex(txId: string): Promise<string> { $getTransactionHex(txId: string): Promise<string> {