Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add pagination when crawl tx; add job crawl missing tx in block #463

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ci/config.json.ci
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"handleTransaction": {
"key": "handleTransaction",
"blocksPerCall": 100,
"millisecondCrawl": 5000
"millisecondCrawl": 5000,
"txsPerCall": 100
},
"crawlSigningInfo": {
"millisecondCrawl": null,
Expand Down
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"handleTransaction": {
"key": "handleTransaction",
"blocksPerCall": 100,
"millisecondCrawl": 5000
"millisecondCrawl": 5000,
"txsPerCall": 100
},
"crawlSigningInfo": {
"millisecondCrawl": null,
Expand Down
44 changes: 32 additions & 12 deletions src/services/crawl-tx/crawl_tx.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -85,28 +85,48 @@ export default class CrawlTxService extends BullableService {
.orderBy('height', 'asc');
this.logger.debug(blocks);
const promises: any[] = [];
const mapBlockTime: Map<number, string> = 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;
}

Expand Down