From 3ac1e7a88e978b642b4171ef90bb29b22dced0d5 Mon Sep 17 00:00:00 2001 From: perfogic Date: Fri, 4 Oct 2024 01:50:22 +0700 Subject: [PATCH] try_fix: fetch chunk mempool txs instead of fetching all batch txs --- packages/orchestrator/src/constants.ts | 1 - .../src/services/relayer/index.ts | 102 +++++++++--------- packages/orchestrator/src/utils/array.ts | 7 ++ 3 files changed, 61 insertions(+), 49 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..c962a4f 100644 --- a/packages/orchestrator/src/constants.ts +++ b/packages/orchestrator/src/constants.ts @@ -1,6 +1,5 @@ export const RELAY_HEADER_BATCH_SIZE = 250; 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 ITERATION_DELAY = { diff --git a/packages/orchestrator/src/services/relayer/index.ts b/packages/orchestrator/src/services/relayer/index.ts index 07eafea..7ef060e 100644 --- a/packages/orchestrator/src/services/relayer/index.ts +++ b/packages/orchestrator/src/services/relayer/index.ts @@ -37,6 +37,7 @@ import { RELAY_DEPOSIT_BLOCKS_SIZE, RELAY_HEADER_BATCH_SIZE, RETRY_DELAY, + SCAN_MEMPOOL_CHUNK_SIZE, } from "../../constants"; import { calculateOutpointKey, @@ -330,62 +331,67 @@ class RelayerService implements RelayerInterface { async scanTxsFromMempools() { try { let mempoolTxs = await this.btcClient.getrawmempool(); - let detailMempoolTxs: BitcoinTransaction[] = await retry( - async () => { - return ( - await this.btcClient.batch([ - ...mempoolTxs.map((txid: string) => ({ - method: "getrawtransaction", - params: [txid, true], - })), - ]) - ).map((item) => item.result); - }, - 10, - RETRY_DELAY - ); + const txChunks = chunkArray(mempoolTxs, SCAN_MEMPOOL_CHUNK_SIZE); + for (const txChunk of txChunks) { + let detailMempoolTxs: BitcoinTransaction[] = await retry( + async () => { + return ( + await this.btcClient.batch([ + ...txChunk.map((txid: string) => ({ + method: "getrawtransaction", + params: [txid, true], + })), + ]) + ).map((item) => item.result); + }, + 3, + RETRY_DELAY + ); - for (const tx of detailMempoolTxs) { - let txid = tx.txid; - if (this.relayTxids.get(txid)) continue; + for (const tx of detailMempoolTxs) { + let txid = tx.txid; + if (this.relayTxids.get(txid)) continue; - const outputs = tx.vout; + const outputs = tx.vout; - for (let vout = 0; vout < outputs.length; vout++) { - let output = outputs[vout]; - if (output.scriptPubKey.type != ScriptPubkeyType.WitnessScriptHash) { - continue; - } + for (let vout = 0; vout < outputs.length; vout++) { + let output = outputs[vout]; + if ( + output.scriptPubKey.type != ScriptPubkeyType.WitnessScriptHash + ) { + continue; + } - const address = decodeAddress(output, this.network); - if (address !== output.scriptPubKey.address) continue; + const address = decodeAddress(output, this.network); + if (address !== output.scriptPubKey.address) continue; - const script = await this.watchedScriptClient.getScript( - output.scriptPubKey.hex - ); + const script = await this.watchedScriptClient.getScript( + output.scriptPubKey.hex + ); - if (!script) continue; + if (!script) continue; - this.logger.info(`Found pending deposit transaction ${txid}`); - this.relayTxids.set(txid, { - tx, - script, - }); + this.logger.info(`Found pending deposit transaction ${txid}`); + this.relayTxids.set(txid, { + tx, + script, + }); - setNestedMap( - this.depositIndex, - [ - toReceiverAddr(convertSdkDestToWasmDest(script.dest)), - address, - getTxidKey(txid, vout), - ], - { - txid, - vout, - height: undefined, - amount: output.value, - } as Deposit - ); + setNestedMap( + this.depositIndex, + [ + toReceiverAddr(convertSdkDestToWasmDest(script.dest)), + address, + getTxidKey(txid, vout), + ], + { + txid, + vout, + height: undefined, + amount: output.value, + } as Deposit + ); + } } } } catch (err) { diff --git a/packages/orchestrator/src/utils/array.ts b/packages/orchestrator/src/utils/array.ts new file mode 100644 index 0000000..74c4335 --- /dev/null +++ b/packages/orchestrator/src/utils/array.ts @@ -0,0 +1,7 @@ +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; +};