Skip to content

Commit

Permalink
Updates maxAttempts and delay. Fixes sonar suggestions.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-then committed Dec 12, 2023
1 parent 87a2d38 commit b4c467e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
27 changes: 19 additions & 8 deletions lib/rsk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>} 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) => {
Expand All @@ -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<boolean>} 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);

Expand Down
24 changes: 16 additions & 8 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>} 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) {
Expand Down Expand Up @@ -232,10 +232,13 @@ const getBitcoinTransactionsInMempool = async (btcTxHelper) => {

/**
*
* @param {BtcTransactionHelper} btcTxHelper
* @param {Promise<string>} btcTxHash
* @param {BtcTransactionHelper} btcTxHelper
* @param {string} btcTxHash
* @param {number} maxAttempts defaults to 3
* @param {number} checkEveryMilliseconds defaults to 500 milliseconds
* @returns {Promise<boolean>} 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);
Expand Down Expand Up @@ -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<boolean>}
*/
const waitForBitcoinMempoolToGetTxs = async (btcTxHelper, maxAttempts = 5, checkEveryMilliseconds = 1000) => {
const waitForBitcoinMempoolToGetTxs = async (btcTxHelper, maxAttempts = 3, checkEveryMilliseconds = 500) => {

const initialBitcoinMempoolSize = (await getBitcoinTransactionsInMempool(btcTxHelper)).length;

Expand Down

0 comments on commit b4c467e

Please sign in to comment.