Skip to content

Commit

Permalink
Merge pull request #163 from rsksmart/add-multiple-pegouts-batch-test
Browse files Browse the repository at this point in the history
Adds 'should create multiple pegouts with mixed values same and above…
  • Loading branch information
marcos-iov authored Nov 11, 2024
2 parents b65a362 + bb4a7eb commit f44d0b6
Show file tree
Hide file tree
Showing 4 changed files with 272 additions and 38 deletions.
7 changes: 5 additions & 2 deletions lib/2wp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ const assertRefundUtxosSameAsPeginUtxos = async(rskTxHelper, btcTxHelper, peginT
* @param {BN} amountInWeisBN
* @param {string} rskFromAddress
* @param {boolean} mine If true, mines 1 block after sending the transaction. If false, it will not mine the tx and will return undefined. Defaults to true.
* @returns {web3.eth.TransactionReceipt | undefined}
* @returns {Promise<web3.eth.TransactionReceipt | txPromise>} the rsk tx receipt if `mine` is true, otherwise the tx promise.
*/
const sendTxToBridge = async (rskTxHelper, amountInWeisBN, rskFromAddress, mine = true) => {

const txPromise = rskTxHelper.sendTransaction({
from: rskFromAddress,
to: BRIDGE_ADDRESS,
value: amountInWeisBN,
gasPrice: TO_BRIDGE_GAS_PRICE,
});

if(!mine) {
return;
return txPromise;
}

// Wait for the rsk tx to be in the rsk mempool before mining
Expand All @@ -78,6 +80,7 @@ const sendTxToBridge = async (rskTxHelper, amountInWeisBN, rskFromAddress, mine
await mineAndSync(getRskTransactionHelpers());
const result = await txPromise;
return result;

};

/**
Expand Down
34 changes: 22 additions & 12 deletions lib/btc-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,27 @@ const waitForBitcoinMempoolToGetTxs = async (btcTxHelper, maxAttempts = 3, check
logger.debug(`[${waitForBitcoinMempoolToGetTxs.name}] The final bitcoin mempool size is ${finalBitcoinMempoolSize}, after ${attempts} attempts. Difference with initial mempool size: ${finalBitcoinMempoolSize - initialBitcoinMempoolSize}.`);

return bitcoinMempoolHasTx;
}
}

const getBtcAddressBalanceInSatoshis = async (btcTxHelper, btcAddress) => {
return Number(btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddress)));
};
const getBtcAddressBalanceInSatoshis = async (btcTxHelper, btcAddress) => {
return Number(btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddress)));
};

module.exports = {
publicKeyToCompressed,
fundAddressAndGetData,
getBitcoinTransactionsInMempool,
waitForBitcoinTxToBeInMempool,
waitForBitcoinMempoolToGetTxs,
getBtcAddressBalanceInSatoshis,
};
/**
* converts a base58 bitcoin address to its string hash160 representation
* @param {string} base58Address the bitcoin address in base58 format
* @returns {string} the hash160 representation of the base58 bitcoin address
*/
const base58AddressToHash160 = (base58Address) => {
return bitcoinJs.address.fromBase58Check(base58Address).hash.toString('hex');
};

module.exports = {
publicKeyToCompressed,
fundAddressAndGetData,
getBitcoinTransactionsInMempool,
waitForBitcoinTxToBeInMempool,
waitForBitcoinMempoolToGetTxs,
getBtcAddressBalanceInSatoshis,
base58AddressToHash160,
};
25 changes: 25 additions & 0 deletions lib/rsk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,30 @@ const getNewFundedRskAddress = async (rskTxHelper, fundingAmountInRbtc = DEFAULT
return address;
};

/**
* Waits for the rsk mempool to get `atLeastExpectedCount` txs. Attempting `maxAttempts` times, checking every `waitTimeInMilliseconds`.
* @param {RskTransactionHelper} rskTxHelper
* @param {number} atLeastExpectedCount
* @param {number} waitTimeInMilliseconds defaults to 500 milliseconds
* @param {number} maxAttempts defaults to 20 attempts
* @returns {Promise<void>}
* @throws {Error} if the rsk mempool doesn't get at least `atLeastExpectedCount` txs after `maxAttempts` attempts
*/
const waitForRskMempoolToGetAtLeastThisManyTxs = async (rskTxHelper, atLeastExpectedCount, waitTimeInMilliseconds = 500, maxAttempts = 20) => {
let attempts = 1;
while(attempts <= maxAttempts) {
const rskMempoolTxs = await getRskMempoolTransactionHashes(rskTxHelper);
logger.debug(`[${waitForRskMempoolToGetAtLeastThisManyTxs.name}] rsk mempool txs: ${rskMempoolTxs.length}`);
if(rskMempoolTxs.length >= atLeastExpectedCount) {
logger.debug(`[${waitForRskMempoolToGetAtLeastThisManyTxs.name}] rsk mempool has ${rskMempoolTxs.length} (at least expected was ${atLeastExpectedCount}) txs after ${attempts} attempts`);
return;
}
await wait(waitTimeInMilliseconds);
attempts++;
}
throw new Error(`[${waitForRskMempoolToGetAtLeastThisManyTxs.name}] rsk mempool didn't get at least ${atLeastExpectedCount} txs after ${maxAttempts} attempts`);
};

module.exports = {
mineAndSync,
waitForBlock,
Expand All @@ -558,4 +582,5 @@ module.exports = {
getPegoutEventsInBlockRange,
setFeePerKb,
getNewFundedRskAddress,
waitForRskMempoolToGetAtLeastThisManyTxs,
};
Loading

0 comments on commit f44d0b6

Please sign in to comment.