From 7c6da47a1927eeecfe1ce60337e609a90f0bd784 Mon Sep 17 00:00:00 2001 From: Tuan Phan Anh <38557844+fibonacci998@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:02:52 +0700 Subject: [PATCH] fix: add pagination when crawl tx in block (#463) --- ci/config.json.ci | 3 +- config.json | 3 +- src/services/crawl-tx/crawl_tx.service.ts | 44 ++++++++++++++++------- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/ci/config.json.ci b/ci/config.json.ci index ecd1b8618..40deb686d 100644 --- a/ci/config.json.ci +++ b/ci/config.json.ci @@ -31,7 +31,8 @@ "handleTransaction": { "key": "handleTransaction", "blocksPerCall": 100, - "millisecondCrawl": 5000 + "millisecondCrawl": 5000, + "txsPerCall": 100 }, "crawlSigningInfo": { "millisecondCrawl": null, diff --git a/config.json b/config.json index 495306d3f..8aa825dcd 100644 --- a/config.json +++ b/config.json @@ -31,7 +31,8 @@ "handleTransaction": { "key": "handleTransaction", "blocksPerCall": 100, - "millisecondCrawl": 5000 + "millisecondCrawl": 5000, + "txsPerCall": 100 }, "crawlSigningInfo": { "millisecondCrawl": null, diff --git a/src/services/crawl-tx/crawl_tx.service.ts b/src/services/crawl-tx/crawl_tx.service.ts index 2105c542e..69535210a 100644 --- a/src/services/crawl-tx/crawl_tx.service.ts +++ b/src/services/crawl-tx/crawl_tx.service.ts @@ -27,7 +27,7 @@ import AuraRegistry from './aura.registry'; export default class CrawlTxService extends BullableService { private _httpBatchClient: HttpBatchClient; - private _registry!: AuraRegistry; + public _registry!: AuraRegistry; public constructor(public broker: ServiceBroker) { super(broker); @@ -85,28 +85,48 @@ export default class CrawlTxService extends BullableService { .orderBy('height', 'asc'); this.logger.debug(blocks); const promises: any[] = []; - const mapBlockTime: Map = new Map(); - blocks - .filter((block) => block.txs.length > 0) - .forEach((block) => { - this.logger.info('crawl tx by height: ', block.height); - mapBlockTime[block.height] = block.time; + const filterBlocks = blocks.filter((block) => block.txs.length > 0); + filterBlocks.forEach((block) => { + this.logger.info('crawl tx by height: ', block.height); + + const totalPages = Math.ceil( + block.txs.length / config.handleTransaction.txsPerCall + ); + [...Array(totalPages)].forEach((e, i) => { + const pageIndex = (i + 1).toString(); promises.push( this._httpBatchClient.execute( createJsonRpcRequest('tx_search', { query: `tx.height=${block.height}`, + page: pageIndex, + per_page: config.handleTransaction.txsPerCall.toString(), }) ) ); }); + }); const resultPromises: JsonRpcSuccessResponse[] = await Promise.all( promises ); - const listRawTx: any[] = resultPromises.map((result) => ({ - listTx: result.result, - height: result.result.txs[0].height, - timestamp: mapBlockTime[result.result.txs[0].height], - })); + + const listRawTx: any[] = filterBlocks.map((block) => { + const listTxs: any[] = []; + resultPromises + .filter( + (result) => result.result.txs[0].height === block.height.toString() + ) + .forEach((resultPromise) => { + listTxs.push(...resultPromise.result.txs); + }); + return { + listTx: { + txs: listTxs, + total_count: block.txs.length, + }, + height: block.height, + timestamp: block.time, + }; + }); return listRawTx; }