From f1b725d591b7f5d860fd5490e7acff1b3c4667b5 Mon Sep 17 00:00:00 2001 From: jeremy-then Date: Mon, 11 Dec 2023 23:40:44 -0400 Subject: [PATCH] Refactors. Correct defaults in docs. --- lib/2wp-utils-legacy.js | 2 +- lib/2wp-utils.js | 2 +- lib/rsk-utils.js | 22 ++++++----- lib/utils.js | 37 +++++++++++-------- ...03_54-post-papyrus_coinbase_information.js | 8 +++- 5 files changed, 41 insertions(+), 30 deletions(-) diff --git a/lib/2wp-utils-legacy.js b/lib/2wp-utils-legacy.js index 1e01c4d1..1743ff66 100644 --- a/lib/2wp-utils-legacy.js +++ b/lib/2wp-utils-legacy.js @@ -153,7 +153,7 @@ const ensurePeginIsRegistered = async (rskClient, peginBtcTxHash, expectedUxtosC return utxoIsRegistered; }; - const utxoIsRegisteredInTheBridge = await retryWithCheck(method, check, MAX_ATTEMPTS, CHECK_EVERY_MILLISECONDS); + const { result: utxoIsRegisteredInTheBridge } = await retryWithCheck(method, check, MAX_ATTEMPTS, CHECK_EVERY_MILLISECONDS); if(utxoIsRegisteredInTheBridge) { console.debug(`Found pegin ${peginBtcTxHash} registered in the bridge.`); diff --git a/lib/2wp-utils.js b/lib/2wp-utils.js index e380f03e..6561e3fa 100644 --- a/lib/2wp-utils.js +++ b/lib/2wp-utils.js @@ -173,7 +173,7 @@ const ensurePeginIsRegistered = async (rskTxHelper, peginBtcTxHash, expectedUtxo return utxoIsRegistered; }; - const utxoIsRegisteredInTheBridge = await retryWithCheck(method, check, MAX_ATTEMPTS, CHECK_EVERY_MILLISECONDS); + const { result: utxoIsRegisteredInTheBridge } = await retryWithCheck(method, check, MAX_ATTEMPTS, CHECK_EVERY_MILLISECONDS); if(utxoIsRegisteredInTheBridge) { console.debug(`Found pegin ${peginBtcTxHash} registered in the bridge.`); diff --git a/lib/rsk-utils.js b/lib/rsk-utils.js index 7f32a70c..ebddbd40 100644 --- a/lib/rsk-utils.js +++ b/lib/rsk-utils.js @@ -157,7 +157,7 @@ const getRskMempoolTransactionHashes = async (rskTxHelper) => { * * @param {RskTransactionHelper} rskTxHelper * @param {string} txHash - * @param {number} maxAttempts Defaults to 20 + * @param {number} maxAttempts Defaults to 5 * @param {number} checkEveryMilliseconds Defaults to 1000 milliseconds * @returns {Promise} whether the tx is in the mempool or not */ @@ -181,16 +181,18 @@ const waitForRskTxToBeInTheMempool = async (rskTxHelper, txHash, maxAttempts = 5 return txIsInTheMempool; }; - const txIsInTheMempool = await retryWithCheck(method, check, maxAttempts, checkEveryMilliseconds); + const { result: isTxInTheMempool, attempts } = await retryWithCheck(method, check, maxAttempts, checkEveryMilliseconds); - console.debug(`Tx ${txHash} was found in the mempool or mined: ${txIsInTheMempool}`); + console.debug(`Tx ${txHash} was found in the rsk mempool or mined: ${isTxInTheMempool}, after ${attempts} attempts.`); + + return isTxInTheMempool; }; /** * * @param {RskTransactionHelper} rskTxHelper - * @param {number} maxAttempts Defaults to 20 + * @param {number} maxAttempts Defaults to 5 * @param {number} checkEveryMilliseconds Defaults to 1000 milliseconds * @returns {Promise} whether the mempool has new txs or not */ @@ -201,7 +203,7 @@ const waitForRskMempoolToGetNewTxs = async (rskTxHelper, maxAttempts = 5, checkE console.debug(`[waitForRskMempoolToGetNewTxs] initial rsk mempool size: ${initialRskMempoolTxHashes.length}`); console.debug(`Will wait and attempt to check if the rsk mempool has received any new transactions ${maxAttempts} times.`); - const method = async () => { + const areThereNewTxsInTheMempool = async () => { const mempoolTxHashes = await getRskMempoolTransactionHashes(rskTxHelper); if(mempoolTxHashes.length > initialRskMempoolTxHashes.length) { console.debug(`The mempool got ${mempoolTxHashes.length - initialRskMempoolTxHashes.length} new transactions`); @@ -214,13 +216,13 @@ const waitForRskMempoolToGetNewTxs = async (rskTxHelper, maxAttempts = 5, checkE return mempoolHasTxs; }; - const retryResult = await retryWithCheck(method, check, maxAttempts, checkEveryMilliseconds); + const { result: newTxsWhereFoundInTheRskMempool, attempts } = await retryWithCheck(areThereNewTxsInTheMempool, check, maxAttempts, checkEveryMilliseconds); const finalRskMempoolTxHashes = await getRskMempoolTransactionHashes(rskTxHelper); - console.debug(`[waitForRskMempoolToGetNewTxs] final rsk mempool size: ${finalRskMempoolTxHashes.length}. Difference with initial mempool size: ${finalRskMempoolTxHashes.length - initialRskMempoolTxHashes.length}`); + console.debug(`[waitForRskMempoolToGetNewTxs] final rsk mempool size: ${finalRskMempoolTxHashes.length}, after ${attempts} attempts. Difference with initial mempool size: ${finalRskMempoolTxHashes.length - initialRskMempoolTxHashes.length}`); - return retryResult; + return newTxsWhereFoundInTheRskMempool; }; @@ -277,9 +279,9 @@ const triggerRelease = async (rskTransactionHelpers, btcClient, callbacks = {}) return false; // Returning false to make the retryWithCheck loop continue until this check returns true or it reaches the max attempts }; - const wasPegoutBroadcasted = await retryWithCheck(method, pegoutIsBroadcasted => pegoutIsBroadcasted, MAX_ATTEMPTS, CHECK_EVERY_MILLISECONDS); + const { result: wasPegoutBroadcasted, attempts } = await retryWithCheck(method, pegoutIsBroadcasted => pegoutIsBroadcasted, MAX_ATTEMPTS, CHECK_EVERY_MILLISECONDS); - console.debug(`Pegout broadcasted: ${wasPegoutBroadcasted}`); + console.debug(`Pegout broadcasted: ${wasPegoutBroadcasted}, after ${attempts} attempts.`); // Last add_signature and release_btc events emitted here at the block that just broadcasted the pegout to the btc network. if(callbacks.releaseBtcCallback) { diff --git a/lib/utils.js b/lib/utils.js index 5a9f65a4..8e85ccf6 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -199,7 +199,10 @@ const retryWithCheck = async (method, check, maxAttempts = 5, delayInMillisecond try { result = await method(); if(!check || (await check(result, currentAttempts))) { - return result; + return { + result, + attempts: currentAttempts + }; } await wait(delayInMilliseconds); currentAttempts++; @@ -212,7 +215,10 @@ const retryWithCheck = async (method, check, maxAttempts = 5, delayInMillisecond } } } - return result; + return { + result, + attempts: currentAttempts + }; }; /** @@ -220,9 +226,8 @@ const retryWithCheck = async (method, check, maxAttempts = 5, delayInMillisecond * @param {BtcTransactionHelper} btcTxHelper * @returns {Promise>} the mempool tx ids */ -const getBitcoinMempool = async (btcTxHelper) => { - const mempool = await btcTxHelper.nodeClient.execute('getrawmempool', []); - return mempool; +const getBitcoinTransactionsInMempool = async (btcTxHelper) => { + return await btcTxHelper.nodeClient.execute('getrawmempool', []); }; /** @@ -233,9 +238,9 @@ const getBitcoinMempool = async (btcTxHelper) => { const waitForBitcoinTxToBeInMempool = async (btcTxHelper, btcTxHash) => { const bitcoinMempoolHasTx = async () => { - const bitcoinMempool = await getBitcoinMempool(btcTxHelper); - const txIsInMempool = bitcoinMempool.includes(btcTxHash); - if(!txIsInMempool) { + const bitcoinMempool = await getBitcoinTransactionsInMempool(btcTxHelper); + const isTxInMempool = bitcoinMempool.includes(btcTxHash); + if(!isTxInMempool) { console.debug(`Attempting to check if the btc tx (${btcTxHash}) was already mined since it's not in the mempool yet.`); const tx = await btcTransactionHelper.getTransaction(btcTxHash); if(tx) { @@ -275,26 +280,26 @@ const waitForBitcoinTxToBeInMempool = async (btcTxHelper, btcTxHash) => { */ const waitForBitcoinMempoolToGetTxs = async (btcTxHelper, maxAttempts = 5, checkEveryMilliseconds = 1000) => { - const initialBitcoinMempoolSize = (await getBitcoinMempool(btcTxHelper)).length; + const initialBitcoinMempoolSize = (await getBitcoinTransactionsInMempool(btcTxHelper)).length; console.debug(`[waitForBitcoinMempoolToGetTxs] The initial bitcoin mempool size is ${initialBitcoinMempoolSize}.`); console.debug(`Will wait and attempt to check if the bitcoin mempool has received any new transactions ${maxAttempts} times.`); - const getBitcoinMempoolSize = async () => { - const bitcoinMempool = await getBitcoinMempool(btcTxHelper); + const getCountOfTransactionsInMempool = async () => { + const bitcoinMempool = await getBitcoinTransactionsInMempool(btcTxHelper); const bitcoinMempoolSize = bitcoinMempool.length; return bitcoinMempoolSize; }; - const checkBtcMempoolIsNotEmpty = async (bitcoinMempoolSize, currentAttempts) => { + const checkBtcMempoolIsNotEmpty = async (bitcoinMempoolSize) => { return bitcoinMempoolSize > 0; }; - const bitcoinMempoolHasTx = await retryWithCheck(getBitcoinMempoolSize, checkBtcMempoolIsNotEmpty, maxAttempts, checkEveryMilliseconds); + const { result: bitcoinMempoolHasTx, attempts } = await retryWithCheck(getCountOfTransactionsInMempool, checkBtcMempoolIsNotEmpty, maxAttempts, checkEveryMilliseconds); - const finalBitcoinMempoolSize = (await getBitcoinMempool(btcTxHelper)).length; + const finalBitcoinMempoolSize = (await getBitcoinTransactionsInMempool(btcTxHelper)).length; - console.debug(`[waitForBitcoinMempoolToGetTxs] The final bitcoin mempool size is ${finalBitcoinMempoolSize}. Difference with initial mempool size: ${finalBitcoinMempoolSize - initialBitcoinMempoolSize}.`); + console.debug(`[waitForBitcoinMempoolToGetTxs] The final bitcoin mempool size is ${finalBitcoinMempoolSize}, after ${attempts} attempts. Difference with initial mempool size: ${finalBitcoinMempoolSize - initialBitcoinMempoolSize}.`); return bitcoinMempoolHasTx; @@ -321,7 +326,7 @@ module.exports = { }, removePrefix0x, retryWithCheck, - getBitcoinMempool, + getBitcoinTransactionsInMempool, waitForBitcoinTxToBeInMempool, waitForBitcoinMempoolToGetTxs, } diff --git a/tests/01_03_54-post-papyrus_coinbase_information.js b/tests/01_03_54-post-papyrus_coinbase_information.js index 90401da2..992a7ecb 100644 --- a/tests/01_03_54-post-papyrus_coinbase_information.js +++ b/tests/01_03_54-post-papyrus_coinbase_information.js @@ -64,10 +64,14 @@ describe('Calling coinbase information methods after papyrus', () => { const hash = ensure0x(blockHash[0]); - const hasBtcBlockCoinbaseInformation = await retryWithCheck(bridge.methods.hasBtcBlockCoinbaseTransactionInformation(hash).call, (resultSoFar, currentAttempts) => { + const hasBtcBlockCoinbaseTransactionInformationMethod = bridge.methods.hasBtcBlockCoinbaseTransactionInformation(hash).call; + + const check = (resultSoFar, currentAttempts) => { console.log(`Attempting to get the btc block coinbase information in the bridge for hash: ${hash}, attempt: ${currentAttempts}.`); return resultSoFar; - }); + }; + + const { result: hasBtcBlockCoinbaseInformation } = await retryWithCheck(hasBtcBlockCoinbaseTransactionInformationMethod, check); expect(hasBtcBlockCoinbaseInformation).to.be.true;