From b589a465194f44b2e92749b91ddb2a6f88c149dc Mon Sep 17 00:00:00 2001 From: perfogic Date: Fri, 4 Oct 2024 01:43:35 +0700 Subject: [PATCH] chore: update script --- packages/orchestrator/src/constants.ts | 1 + packages/orchestrator/src/script.ts | 34 ++++++++++++++---------- packages/orchestrator/src/utils/array.ts | 7 +++++ 3 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 packages/orchestrator/src/utils/array.ts diff --git a/packages/orchestrator/src/constants.ts b/packages/orchestrator/src/constants.ts index 426dade..570ea41 100644 --- a/packages/orchestrator/src/constants.ts +++ b/packages/orchestrator/src/constants.ts @@ -3,6 +3,7 @@ export const SCAN_MEMPOOL_CHUNK_SIZE = 1000; export const SCAN_MEMPOOL_CHUNK_DELAY = 1000; // 1 second export const RELAY_DEPOSIT_BLOCKS_SIZE = 200; // 10 blocks export const RETRY_DELAY = 1000; // 1 second +export const SCAN_RAW_MEMPOOL_CHUNK_SIZE = 3000; export const ITERATION_DELAY = { RELAY_HEADER_BATCH_DELAY: 500, RELAY_HEADER_INTERVAL: 10000, diff --git a/packages/orchestrator/src/script.ts b/packages/orchestrator/src/script.ts index 7a69681..b5c3a78 100644 --- a/packages/orchestrator/src/script.ts +++ b/packages/orchestrator/src/script.ts @@ -1,5 +1,6 @@ import { RPCClient } from "rpc-bitcoin"; import { RETRY_DELAY } from "./constants"; +import { chunkArray } from "./utils/array"; import { retry } from "./utils/catchAsync"; const main = async () => { @@ -12,20 +13,25 @@ const main = async () => { let mempoolTxs = await btcClient.getrawmempool({}); const time = new Date().getTime(); - let detailMempoolTxs = await retry( - async () => { - return ( - await btcClient.batch([ - ...mempoolTxs.map((txid: string) => ({ - method: "getrawtransaction", - params: [txid, true], - })), - ]) - ).map((item) => item.result); - }, - 10, - RETRY_DELAY - ); + const txChunks = chunkArray(mempoolTxs, 3000); + let detailMempoolTxs = []; + for (const txChunk of txChunks) { + let result = await retry( + async () => { + return ( + await btcClient.batch([ + ...txChunk.map((txid: string) => ({ + method: "getrawtransaction", + params: [txid, true], + })), + ]) + ).map((item) => item.result); + }, + 3, + RETRY_DELAY + ); + detailMempoolTxs = [...result]; + } const nextTime = new Date().getTime(); console.log(detailMempoolTxs.length, (nextTime - time) / 1000); diff --git a/packages/orchestrator/src/utils/array.ts b/packages/orchestrator/src/utils/array.ts new file mode 100644 index 0000000..852406f --- /dev/null +++ b/packages/orchestrator/src/utils/array.ts @@ -0,0 +1,7 @@ +export const chunkArray = (array: T[], chunkSize: number): T[][] => { + const chunks = []; + for (let i = 0; i < array.length; i += chunkSize) { + chunks.push(array.slice(i, i + chunkSize)); + } + return chunks; +};