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 33e85aaf..6561e3fa 100644 --- a/lib/2wp-utils.js +++ b/lib/2wp-utils.js @@ -1,6 +1,6 @@ const expect = require('chai').expect; -const { sendFromCow, mineAndSync, sendTxWithCheck, getUnlockedAddress } = require('./rsk-utils'); -const { wait, retryWithCheck } = require('./utils'); +const { sendFromCow, mineAndSync, sendTxWithCheck, getUnlockedAddress, waitForRskMempoolToGetNewTxs, waitAndUpdateBridge } = require('./rsk-utils'); +const { retryWithCheck, waitForBitcoinTxToBeInMempool, waitForBitcoinMempoolToGetTxs } = require('./utils'); const { getBridge, getLatestActiveForkName } = require('./precompiled-abi-forks-util'); const { getBridgeState } = require('@rsksmart/bridge-state-data-parser'); const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter'); @@ -62,7 +62,10 @@ const sendTxToBridge = async (rskTxHelper, amountInRbtc, rskFromAddress, mine = if(!mine) { return; } - await wait(1000); + + // Wait for the rsk tx to be in the rsk mempool before mining + await waitForRskMempoolToGetNewTxs(rskTxHelper); + await mineAndSync(getRskTransactionHelpers()); const result = await txPromise; return result; @@ -103,9 +106,13 @@ const isUtxoRegisteredInBridge = async (rskTxHelper, peginTxHash, expectedUxtosC const mineForPeginRegistration = async (rskTxHelper, btcTxHelper) => { // Enough confirmations to register the coinbase but not the pegin. + // Wait for the pegin to be in the bitcoin mempool before mining + await waitForBitcoinMempoolToGetTxs(btcTxHelper); await btcTxHelper.mine(BTC_TO_RSK_MINIMUM_CONFIRMATIONS - 1); await waitAndUpdateBridge(rskTxHelper, 500); // One more confirmation to register the pegin. + // Wait for the pegin to be in the bitcoin mempool before mining + await waitForBitcoinMempoolToGetTxs(btcTxHelper); await btcTxHelper.mine(1); await waitAndUpdateBridge(rskTxHelper, 500); }; @@ -132,6 +139,9 @@ const sendPegin = async (rskTxHelper, btcTxHelper, btcSenderAddressInformation, const peginBtcTxHash = await btcTxHelper.transferBtc(btcSenderAddressInformation, recipientsTransferInformation, data); + // Wait for the pegin to be in the bitcoin mempool before mining + await waitForBitcoinTxToBeInMempool(btcTxHelper, peginBtcTxHash); + await mineForPeginRegistration(rskTxHelper, btcTxHelper); return peginBtcTxHash; @@ -163,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.`); @@ -177,18 +187,6 @@ const ensurePeginIsRegistered = async (rskTxHelper, peginBtcTxHash, expectedUtxo }; -/** - * Waits for the specified time, updates the bridge and mines 1 rsk block - * @param {RskTransactionHelper} rskTxHelper - * @param {number} timeInMilliseconds defaults to 1000 - * @returns {Promise} - */ -const waitAndUpdateBridge = async (rskTxHelper, timeInMilliseconds = 1000) => { - await wait(timeInMilliseconds); - await rskTxHelper.updateBridge(); - await mineAndSync(getRskTransactionHelpers()); -}; - /** * Creates a pegin v1 data for a user to indicate to which rsk address to receive their pegin funds. * @param {string} rskDestinationAddress @@ -273,5 +271,5 @@ module.exports = { createPeginV1TxData, mineForPeginRegistration, MIN_PEGOUT_VALUE_IN_RBTC, - disableWhitelisting + disableWhitelisting, }; diff --git a/lib/rsk-utils.js b/lib/rsk-utils.js index 5507d756..c8d19f85 100644 --- a/lib/rsk-utils.js +++ b/lib/rsk-utils.js @@ -5,7 +5,7 @@ const { getBridge, getLatestActiveForkName } = require('./precompiled-abi-forks- const hopBridgeTxParser = require('bridge-transaction-parser-hop400'); const fingerrootBridgeTxParser = require('bridge-transaction-parser-fingerroot500'); const { getRskTransactionHelpers } = require('../lib/rsk-tx-helper-provider'); -const { removePrefix0x } = require('./utils'); +const { removePrefix0x, waitForBitcoinMempoolToGetTxs } = require('./utils'); const BTC_TO_RSK_MINIMUM_ACCEPTABLE_CONFIRMATIONS = 3; const RSK_TO_BTC_MINIMUM_ACCEPTABLE_CONFIRMATIONS = 3; @@ -127,6 +127,116 @@ const increaseBlockToNextPegoutHeight = async (rskTransactionHelpers) => { } }; +/** + * Waits for the specified time, updates the bridge and mines 1 rsk block + * @param {RskTransactionHelper} rskTxHelper + * @param {number} timeInMilliseconds defaults to 1000 + * @returns {Promise} + */ +const waitAndUpdateBridge = async (rskTxHelper, timeInMilliseconds = 1000) => { + await wait(timeInMilliseconds); + await rskTxHelper.updateBridge(); + + // Wait for the rsk `updateBridge` tx to be in the rsk mempool before mining + await waitForRskMempoolToGetNewTxs(rskTxHelper); + + await mineAndSync(getRskTransactionHelpers()); +}; + +/** + * + * @param {RskTransactionHelper} rskTxHelper + * @returns {Promise} array of tx hashes in the mempool + */ +const getRskMempoolTransactionHashes = async (rskTxHelper) => { + const mempoolBlock = await rskTxHelper.getClient().eth.getBlock('pending'); + return mempoolBlock.transactions; +}; + +/** + * + * @param {RskTransactionHelper} rskTxHelper + * @param {string} txHash + * @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 = 3, checkEveryMilliseconds = 500) => { + + const method = async () => { + + const tx = await rskTxHelper.getClient().eth.getTransaction(txHash); + + const isTxInTheMempool = tx && !tx.blockNumber; + + if(isTxInTheMempool) { + console.debug(`The tx (${txHash}) is in the mempool`); + return true; + } + + 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) => { + console.debug(`Attempting to find the tx ${txHash} in the mempool. Attempt ${currentAttempts} out of ${maxAttempts}`); + return txIsInTheMempool; + }; + + const { result: isTxInTheMempool, attempts } = await retryWithCheck(method, check, maxAttempts, checkEveryMilliseconds); + + 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 3 + * @param {number} checkEveryMilliseconds Defaults to 500 milliseconds + * @returns {Promise} whether the mempool has new txs or not + */ +const waitForRskMempoolToGetNewTxs = async (rskTxHelper, maxAttempts = 3, checkEveryMilliseconds = 500) => { + + const initialRskMempoolTxHashes = await getRskMempoolTransactionHashes(rskTxHelper); + + 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 areThereNewTxsInTheMempool = async () => { + const mempoolTxHashes = await getRskMempoolTransactionHashes(rskTxHelper); + if(mempoolTxHashes.length > initialRskMempoolTxHashes.length) { + console.debug(`The mempool got ${mempoolTxHashes.length - initialRskMempoolTxHashes.length} new transactions`); + return true; + } + return false; + }; + + const check = async (mempoolHasTxs) => { + return mempoolHasTxs; + }; + + const { result: newTxsWhereFoundInTheRskMempool, attempts } = await retryWithCheck(areThereNewTxsInTheMempool, check, maxAttempts, checkEveryMilliseconds); + + const finalRskMempoolTxHashes = await getRskMempoolTransactionHashes(rskTxHelper); + + console.debug(`[waitForRskMempoolToGetNewTxs] final rsk mempool size: ${finalRskMempoolTxHashes.length}, after ${attempts} attempts. Difference with initial mempool size: ${finalRskMempoolTxHashes.length - initialRskMempoolTxHashes.length}`); + + return newTxsWhereFoundInTheRskMempool; + + }; + /** * * @param {Array} rskTransactionHelpers RskTransactionHelper instances each belonging to one federator node to make calls to the rsk network @@ -141,12 +251,9 @@ const triggerRelease = async (rskTransactionHelpers, btcClient, callbacks = {}) await increaseBlockToNextPegoutHeight(rskTransactionHelpers); } - // Sync all nodes - await waitForSync(rskTransactionHelpers); - // Adds the pegout to the pegoutsWaitingForConfirmations structure with this 1 confirmation - await rskTxHelper.updateBridge(); - await mineAndSync(rskTransactionHelpers); // release_request_received and batch_pegout_created triggered here (if appropriate fork, RSKIP185 and RSKIP271, is/are active) + // release_request_received and batch_pegout_created triggered here (if appropriate fork, RSKIP185 and RSKIP271, is/are active) + await waitAndUpdateBridge(rskTxHelper); if(callbacks.pegoutCreatedCallback) { await callbacks.pegoutCreatedCallback(rskTxHelper); @@ -156,8 +263,8 @@ const triggerRelease = async (rskTransactionHelpers, btcClient, callbacks = {}) await mineAndSync(rskTransactionHelpers, BTC_TO_RSK_MINIMUM_ACCEPTABLE_CONFIRMATIONS - 1); // Moves the pegout from pegoutsWaitingForConfirmations to pegoutsWaitingForSignatures - await rskTxHelper.updateBridge(); // Makes an updateCollections call which is the tx that will move the pegout to pegoutsWaitingForSignatures - await mineAndSync(rskTransactionHelpers); // pegout_confirmed event triggered here (if appropriate fork, RSKIP326, is active) + // pegout_confirmed event triggered here (if appropriate fork, RSKIP326, is active) + await waitAndUpdateBridge(rskTxHelper); if(callbacks.pegoutConfirmedCallback) { await callbacks.pegoutConfirmedCallback(rskTxHelper); @@ -183,22 +290,25 @@ 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) { await callbacks.releaseBtcCallback(rskTxHelper); } + // Waiting to make sure that the pegout tx is in the bitcoin mempool before mining the required blocks for confirmation. + await waitForBitcoinMempoolToGetTxs(btcClient); + // From the btc network side, mine `RSK_TO_BTC_MINIMUM_ACCEPTABLE_CONFIRMATIONS + 1` blocks, 1 for the pegout funds to be mined and reflected in the recipient address, // and `RSK_TO_BTC_MINIMUM_ACCEPTABLE_CONFIRMATIONS` more to have enough confirmation for the change balance to be reflected back in the bridge (like a pegin) await btcClient.mine(RSK_TO_BTC_MINIMUM_ACCEPTABLE_CONFIRMATIONS + 1); // Make pegnatories register the change utxo back in the bridge - await rskTxHelper.updateBridge(); - await mineAndSync(rskTransactionHelpers); // At this point the bridge should already have the change uxto registered + // After this point the bridge should already have the change uxto registered + await waitAndUpdateBridge(rskTxHelper); }; @@ -221,7 +331,7 @@ const sendTxWithCheck = async (rskTxHelper, method, from, checkCallback) => { const estimatedGas = await method.estimateGas({ from }); const txReceiptPromise = method.send({ from, value: 0, gasPrice: 0, gas: estimatedGas }); - await wait(1000); + await waitForRskMempoolToGetNewTxs(rskTxHelper); await mineAndSync(getRskTransactionHelpers()); return await txReceiptPromise; @@ -344,5 +454,9 @@ module.exports = { getUnlockedAddress, getFedsPubKeys, activateFork, - getLatestForkName + getLatestForkName, + getRskMempoolTransactionHashes, + waitForRskTxToBeInTheMempool, + waitForRskMempoolToGetNewTxs, + waitAndUpdateBridge, }; diff --git a/lib/tests/2wp.js b/lib/tests/2wp.js index 872ceb6e..25c5b40c 100644 --- a/lib/tests/2wp.js +++ b/lib/tests/2wp.js @@ -9,6 +9,7 @@ const { getRskTransactionHelpers, getRskTransactionHelper } = require('../rsk-tx const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); const btcEthUnitConverter = require('@rsksmart/btc-eth-unit-converter'); const { sendTxToBridge, sendPegin, ensurePeginIsRegistered, donateToBridge } = require('../2wp-utils'); +const { waitAndUpdateBridge } = require('../rsk-utils'); const DONATION_AMOUNT = 250; const REJECTED_REASON = 1; @@ -50,8 +51,7 @@ const execute = (description, getRskHost) => { rskTxHelpers = getRskTransactionHelpers(); // Update the bridge to sync btc blockchains - await rskTxHelper.updateBridge(); - await rskUtils.mineAndSync(rskTxHelpers); + await waitAndUpdateBridge(rskTxHelper); // At the moment there are a lot of pegout tests that depend on the bridge to have enough balance. // Those tests are not doing a pegin if needed, so we need to donate to the bridge to ensure it has enough balance. diff --git a/lib/utils.js b/lib/utils.js index cfc96921..999be738 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -99,6 +99,9 @@ const fundAddressAndGetData = async (btcTxHelper, addressToFund, amountToFundInB const txId = await btcTxHelper.transferBtc(btcSenderAddressInformation, recipientsTransactionInformation); + // Wait for the pegin to be in the bitcoin mempool before mining + await waitForBitcoinTxToBeInMempool(btcTxHelper, txId); + const rawTx = await btcTxHelper.nodeClient.getRawTransaction(txId); await btcTxHelper.importAddress(addressToFund); @@ -183,20 +186,23 @@ 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) { try { result = await method(); if(!check || (await check(result, currentAttempts))) { - return result; + return { + result, + attempts: currentAttempts + }; } await wait(delayInMilliseconds); currentAttempts++; @@ -209,9 +215,104 @@ const retryWithCheck = async (method, check, maxAttempts = 5, delayInMillisecond } } } - return result; + return { + result, + attempts: currentAttempts + }; +}; + +/** + * + * @param {BtcTransactionHelper} btcTxHelper + * @returns {Promise>} the mempool tx ids + */ +const getBitcoinTransactionsInMempool = async (btcTxHelper) => { + return await btcTxHelper.nodeClient.execute('getrawmempool', []); +}; + +/** + * + * @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, maxAttempts = 3, checkEveryMilliseconds = 500) => { + + const bitcoinMempoolHasTx = async () => { + 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) { + console.debug(`The btc tx (${btcTxHash}) was already mined.`); + return true; + } + return false; + } + return true; + }; + + const checkBitcoinMempoolHasTx = async (btcTxAlreadyFound, currentAttempts) => { + if(btcTxAlreadyFound) { + console.debug(`The btc tx ${btcTxHash} was found in the mempool at attempt ${currentAttempts}.`); + } else { + console.log(`Attempting to get the btc tx ${btcTxHash} in the mempool. Attempt: ${currentAttempts}.`); + } + return btcTxAlreadyFound; + }; + + const onError = async (e) => { + if(e.message.includes('No such mempool or blockchain transaction')) { + console.debug(`The btc tx ${btcTxHash} is not in the mempool nor mined yet. Let's allow some more time before retrying to get it.`); + return true; + } + console.error(`Un expected error while trying to get the btc tx ${btcTxHash} in the mempool.`, e); + throw e; + }; + + 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 = 3, checkEveryMilliseconds = 500) => { + + 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 getCountOfTransactionsInMempool = async () => { + const bitcoinMempool = await getBitcoinTransactionsInMempool(btcTxHelper); + const bitcoinMempoolSize = bitcoinMempool.length; + return bitcoinMempoolSize; + }; + + const checkBtcMempoolIsNotEmpty = async (bitcoinMempoolSize) => { + return bitcoinMempoolSize > 0; + }; + + const { result: bitcoinMempoolHasTx, attempts } = await retryWithCheck(getCountOfTransactionsInMempool, checkBtcMempoolIsNotEmpty, maxAttempts, checkEveryMilliseconds); + + const finalBitcoinMempoolSize = (await getBitcoinTransactionsInMempool(btcTxHelper)).length; + + console.debug(`[waitForBitcoinMempoolToGetTxs] The final bitcoin mempool size is ${finalBitcoinMempoolSize}, after ${attempts} attempts. Difference with initial mempool size: ${finalBitcoinMempoolSize - initialBitcoinMempoolSize}.`); + + return bitcoinMempoolHasTx; + +} + module.exports = { sequentialPromise: sequentialPromise, mapPromiseAll: mapPromiseAll, @@ -233,4 +334,7 @@ module.exports = { }, removePrefix0x, retryWithCheck, + getBitcoinTransactionsInMempool, + waitForBitcoinTxToBeInMempool, + waitForBitcoinMempoolToGetTxs, } diff --git a/testRunner.js b/multipleTestExecutionsRunner.js similarity index 72% rename from testRunner.js rename to multipleTestExecutionsRunner.js index 430b5775..38c70649 100644 --- a/testRunner.js +++ b/multipleTestExecutionsRunner.js @@ -6,13 +6,22 @@ const timesArg = Number(process.argv[2]); const RUN_ALL_TESTS_THESE_TIMES = timesArg || Number(process.env.RUN_ALL_TESTS_THESE_TIMES) || 1; -console.info(`Will attempt to run tests ${RUN_ALL_TESTS_THESE_TIMES} times.`) +console.info(`Will attempt to run tests ${RUN_ALL_TESTS_THESE_TIMES} times.`); + +const cleanEnvCommand = "kill $(ps -A | grep -e java -e python -e bitcoind | awk '{print $1}')"; + +const ensureCleanEnv = () => { + console.info('Cleaning environment...'); + shell.exec(cleanEnvCommand); + console.info('Environment clean.'); +}; let fails = 0; let attempts = 1; for(let i = 0; i < RUN_ALL_TESTS_THESE_TIMES; i++) { console.info(`Running tests ${attempts} out of ${RUN_ALL_TESTS_THESE_TIMES} times.`); + ensureCleanEnv(); if (shell.exec('npm run test-fail-fast').code !== 0) { fails++; } diff --git a/package.json b/package.json index 1a9a95a2..fe0e285d 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "test": "mocha --timeout 600000", "test-fail-fast": "mocha -b --timeout 600000", - "run-tests-multiple-times": "node testRunner.js", + "run-tests-multiple-times": "node multipleTestExecutionsRunner.js", "run-single-test-file": "node singleTestFileRunner.js" }, "author": "", diff --git a/tests/01_03_01-lock_whitelist_pre_papyrus.js b/tests/01_03_01-lock_whitelist_pre_papyrus.js index 57ede0a1..6d928799 100644 --- a/tests/01_03_01-lock_whitelist_pre_papyrus.js +++ b/tests/01_03_01-lock_whitelist_pre_papyrus.js @@ -15,6 +15,7 @@ const { sendPegin, ensurePeginIsRegistered } = require('../lib/2wp-utils'); const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation'); const { btcToWeis, btcToSatoshis, satoshisToBtc } = require('@rsksmart/btc-eth-unit-converter'); const { getBridge, getLatestActiveForkName } = require('../lib/precompiled-abi-forks-util'); +const { waitAndUpdateBridge } = require('../lib/rsk-utils'); let rskTxHelpers; let btcTxHelper; @@ -164,8 +165,7 @@ describe('Lock whitelisting', () => { const federationBalanceAfterPegin = await btcTxHelper.getAddressBalance(federationAddress); expect(Number(btcToSatoshis(federationBalanceAfterPegin))).to.equal(Number(btcToSatoshis(initialFederationBalance + AMOUNT_TO_TRY_TO_LOCK)), `Lock BTC federation ${federationAddress} credit`); - await rskTxHelper.updateBridge(); - await rskUtils.mineAndSync(rskTxHelpers); + await waitAndUpdateBridge(rskTxHelper); const initialBlockNumber = await rskTxHelper.getBlockNumber(); await rskUtils.mineAndSync(rskTxHelpers); @@ -231,8 +231,8 @@ describe('Lock whitelisting', () => { const federationBalanceAfterPegin = await btcTxHelper.getAddressBalance(federationAddress); expect(federationBalanceAfterPegin).to.equal(initialFederationBalance + PEGIN_VALUE_IN_BTC, 'The federation address should have its balance increased by the pegin amount'); - await rskTxHelper.updateBridge(); - await rskUtils.mineAndSync(rskTxHelpers); + // Update the bridge to sync + await waitAndUpdateBridge(rskTxHelper); const recipientRskAddressBalance = Number(await rskTxHelper.getBalance(recipientRskAddressInfo.address)); expect(recipientRskAddressBalance).to.equal(0, 'The recipient rsk address should not have any balance'); diff --git a/tests/01_03_54-post-papyrus_coinbase_information.js b/tests/01_03_54-post-papyrus_coinbase_information.js index fe5856af..992a7ecb 100644 --- a/tests/01_03_54-post-papyrus_coinbase_information.js +++ b/tests/01_03_54-post-papyrus_coinbase_information.js @@ -31,6 +31,7 @@ describe('Calling coinbase information methods after papyrus', () => { const blockHash = await btcClient.mine(1); await wait(1000); await rskTxHelper.updateBridge(); + await rskUtils.waitForRskMempoolToGetNewTxs(rskTxHelper); const blockData = await btcClient.nodeClient.getBlock(blockHash[0], false); const block = bitcoinJs.Block.fromHex(blockData); @@ -63,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;