Skip to content

Commit

Permalink
try_fix: fetch chunk mempool txs instead of fetching all batch txs
Browse files Browse the repository at this point in the history
  • Loading branch information
perfogic committed Oct 3, 2024
1 parent d44dbd6 commit 3ac1e7a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 49 deletions.
1 change: 0 additions & 1 deletion packages/orchestrator/src/constants.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
102 changes: 54 additions & 48 deletions packages/orchestrator/src/services/relayer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
RELAY_DEPOSIT_BLOCKS_SIZE,
RELAY_HEADER_BATCH_SIZE,
RETRY_DELAY,
SCAN_MEMPOOL_CHUNK_SIZE,
} from "../../constants";
import {
calculateOutpointKey,
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions packages/orchestrator/src/utils/array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const chunkArray = <T>(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;
};

0 comments on commit 3ac1e7a

Please sign in to comment.