From b4c467e6d1abecc52ba416cd5ba1b394f2b843a7 Mon Sep 17 00:00:00 2001 From: jeremy-then Date: Tue, 12 Dec 2023 12:52:01 -0400 Subject: [PATCH] Updates maxAttempts and delay. Fixes sonar suggestions. --- lib/rsk-utils.js | 27 +++++++++++++++++++-------- lib/utils.js | 24 ++++++++++++++++-------- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/lib/rsk-utils.js b/lib/rsk-utils.js index ebddbd40..c8d19f85 100644 --- a/lib/rsk-utils.js +++ b/lib/rsk-utils.js @@ -157,23 +157,34 @@ const getRskMempoolTransactionHashes = async (rskTxHelper) => { * * @param {RskTransactionHelper} rskTxHelper * @param {string} txHash - * @param {number} maxAttempts Defaults to 5 - * @param {number} checkEveryMilliseconds Defaults to 1000 milliseconds + * @param {number} maxAttempts Defaults to 3 + * @param {number} checkEveryMilliseconds Defaults to 500 milliseconds * @returns {Promise} whether the tx is in the mempool or not */ -const waitForRskTxToBeInTheMempool = async (rskTxHelper, txHash, maxAttempts = 5, checkEveryMilliseconds = 1000) => { +const waitForRskTxToBeInTheMempool = async (rskTxHelper, txHash, maxAttempts = 3, checkEveryMilliseconds = 500) => { const method = async () => { + const tx = await rskTxHelper.getClient().eth.getTransaction(txHash); - if(tx && !tx.blockNumber) { + + const isTxInTheMempool = tx && !tx.blockNumber; + + if(isTxInTheMempool) { console.debug(`The tx (${txHash}) is in the mempool`); return true; - } else if(tx && tx.blockNumber) { + } + + const isTxAlreadyMined = tx && tx.blockNumber; + + if(isTxAlreadyMined) { console.debug(`The tx (${txHash}) is already mined in a block`); return true; } + console.debug(`The tx (${txHash}) is not in the mempool nor in a block yet. Will keep retrying until it is in the mempool, block, or it reaches the max attempts to find it`); + return false; + }; const check = async (txIsInTheMempool, currentAttempts) => { @@ -192,11 +203,11 @@ const waitForRskTxToBeInTheMempool = async (rskTxHelper, txHash, maxAttempts = 5 /** * * @param {RskTransactionHelper} rskTxHelper - * @param {number} maxAttempts Defaults to 5 - * @param {number} checkEveryMilliseconds Defaults to 1000 milliseconds + * @param {number} maxAttempts Defaults to 3 + * @param {number} checkEveryMilliseconds Defaults to 500 milliseconds * @returns {Promise} whether the mempool has new txs or not */ -const waitForRskMempoolToGetNewTxs = async (rskTxHelper, maxAttempts = 5, checkEveryMilliseconds = 1000) => { +const waitForRskMempoolToGetNewTxs = async (rskTxHelper, maxAttempts = 3, checkEveryMilliseconds = 500) => { const initialRskMempoolTxHashes = await getRskMempoolTransactionHashes(rskTxHelper); diff --git a/lib/utils.js b/lib/utils.js index 8e85ccf6..999be738 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -186,13 +186,13 @@ const removePrefix0x = hash => hash.substr(2); * @param {function} check callback function to check the result of the method. * If this callback returns true, then the method call is considered successful and the result is returned. * Otherwise, the method is executed again. - * @param {number} maxAttempts defaults to 5 - * @param {number} delayInMilliseconds defaults to 2000 milliseconds + * @param {number} maxAttempts defaults to 3 + * @param {number} delayInMilliseconds defaults to 500 milliseconds * @param {function} onError callback function for the caller to check the thrown error. If the callback returns true, then the function will stop executing. * If this callback is not provided, then the error will be thrown. * @returns {Promise} the result of the method call or the last value of `result` after the attempts. */ -const retryWithCheck = async (method, check, maxAttempts = 5, delayInMilliseconds = 2000, onError) => { +const retryWithCheck = async (method, check, maxAttempts = 3, delayInMilliseconds = 500, onError) => { let currentAttempts = 1; let result; while(currentAttempts <= maxAttempts) { @@ -232,10 +232,13 @@ const getBitcoinTransactionsInMempool = async (btcTxHelper) => { /** * - * @param {BtcTransactionHelper} btcTxHelper - * @param {Promise} btcTxHash + * @param {BtcTransactionHelper} btcTxHelper + * @param {string} btcTxHash + * @param {number} maxAttempts defaults to 3 + * @param {number} checkEveryMilliseconds defaults to 500 milliseconds + * @returns {Promise} whether the tx got to the mempool or not after the attempts */ -const waitForBitcoinTxToBeInMempool = async (btcTxHelper, btcTxHash) => { +const waitForBitcoinTxToBeInMempool = async (btcTxHelper, btcTxHash, maxAttempts = 3, checkEveryMilliseconds = 500) => { const bitcoinMempoolHasTx = async () => { const bitcoinMempool = await getBitcoinTransactionsInMempool(btcTxHelper); @@ -270,15 +273,20 @@ const waitForBitcoinTxToBeInMempool = async (btcTxHelper, btcTxHash) => { throw e; }; - await retryWithCheck(bitcoinMempoolHasTx, checkBitcoinMempoolHasTx, 5, 1000, onError); + const { result: btcTxAlreadyFoundInMempool } = retryWithCheck(bitcoinMempoolHasTx, checkBitcoinMempoolHasTx, maxAttempts, checkEveryMilliseconds, onError); + + return btcTxAlreadyFoundInMempool; + }; /** * Waits until the bitcoin mempool has at least one tx. * @param {BtcTransactionHelper} btcTxHelper + * @param {number} maxAttempts defaults to 3 + * @param {number} checkEveryMilliseconds defaults to 500 milliseconds * @returns {Promise} */ -const waitForBitcoinMempoolToGetTxs = async (btcTxHelper, maxAttempts = 5, checkEveryMilliseconds = 1000) => { +const waitForBitcoinMempoolToGetTxs = async (btcTxHelper, maxAttempts = 3, checkEveryMilliseconds = 500) => { const initialBitcoinMempoolSize = (await getBitcoinTransactionsInMempool(btcTxHelper)).length;