Skip to content

Commit

Permalink
Updates test to use new get2wpBalances.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-then committed Oct 1, 2024
1 parent 45db702 commit 8082c93
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 126 deletions.
14 changes: 2 additions & 12 deletions lib/2wp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const {
waitAndUpdateBridge
} = require('./rsk-utils');
const { retryWithCheck, ensure0x } = require('./utils');
const { waitForBitcoinTxToBeInMempool, waitForBitcoinMempoolToGetTxs, getBtcAddressBalanceInSatoshis } = require('./btc-utils');
const { waitForBitcoinTxToBeInMempool, waitForBitcoinMempoolToGetTxs, getBtcAddressBalanceInSatoshis, fundBtcAddressCheckingBalance } = require('./btc-utils');
const { getBridge } = require('./precompiled-abi-forks-util');
const { getBridgeState } = require('@rsksmart/bridge-state-data-parser');
const { getDerivedRSKAddressInformation } = require('@rsksmart/btc-rsk-derivation');
Expand Down Expand Up @@ -248,17 +248,7 @@ const createSenderRecipientInfo = async (rskTxHelper, btcTxHelper, type = 'legac
await rskTxHelper.importAccount(rskRecipientRskAddressInfo.privateKey);
await rskTxHelper.unlockAccount(rskRecipientRskAddressInfo.address);
if(Number(initialAmountToFundInBtc) > 0) {
const initialAddressBalance = await btcTxHelper.getAddressBalance(btcSenderAddressInfo.address);
const fundTxHash = await btcTxHelper.fundAddress(btcSenderAddressInfo.address, initialAmountToFundInBtc);
const finalAddressBalance = await btcTxHelper.getAddressBalance(btcSenderAddressInfo.address);
// If the final address balance is the same as the initial balance, then the tx has not yet reached the mempool or has not been mined. Rare condition that may happen randomly.
if(finalAddressBalance === initialAddressBalance) {
const inMempool = await waitForBitcoinTxToBeInMempool(btcTxHelper, fundTxHash);
if(inMempool) {
await btcTxHelper.mine();
}
}

await fundBtcAddressCheckingBalance(btcTxHelper, btcSenderAddressInfo.address, initialAmountToFundInBtc);
}
return {
btcSenderAddressInfo,
Expand Down
94 changes: 57 additions & 37 deletions lib/btc-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,44 +154,64 @@ const waitForBitcoinTxToBeInMempool = async (btcTxHelper, btcTxHash, maxAttempts
* @returns {Promise<boolean>}
*/
const waitForBitcoinMempoolToGetTxs = async (btcTxHelper, maxAttempts = 3, checkEveryMilliseconds = 500) => {
const initialBitcoinMempoolSize = (await getBitcoinTransactionsInMempool(btcTxHelper)).length;
logger.debug(`[${waitForBitcoinMempoolToGetTxs.name}] The initial bitcoin mempool size is ${initialBitcoinMempoolSize}.`);
logger.debug(`[${waitForBitcoinMempoolToGetTxs.name}] 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 txsInMempool = await getBitcoinTransactionsInMempool(btcTxHelper);
const finalBitcoinMempoolSize = txsInMempool.length;

logger.debug(`[${waitForBitcoinMempoolToGetTxs.name}] The final bitcoin mempool size is ${finalBitcoinMempoolSize}, after ${attempts} attempts. Difference with initial mempool size: ${finalBitcoinMempoolSize - initialBitcoinMempoolSize}.`);

return bitcoinMempoolHasTx;
}
const initialBitcoinMempoolSize = (await getBitcoinTransactionsInMempool(btcTxHelper)).length;
logger.debug(`[${waitForBitcoinMempoolToGetTxs.name}] The initial bitcoin mempool size is ${initialBitcoinMempoolSize}.`);
logger.debug(`[${waitForBitcoinMempoolToGetTxs.name}] Will wait and attempt to check if the bitcoin mempool has received any new transactions ${maxAttempts} times.`);

const getBtcAddressBalanceInSatoshis = async (btcTxHelper, btcAddress) => {
return Number(btcToSatoshis(await btcTxHelper.getAddressBalance(btcAddress)));
const getCountOfTransactionsInMempool = async () => {
const bitcoinMempool = await getBitcoinTransactionsInMempool(btcTxHelper);
const bitcoinMempoolSize = bitcoinMempool.length;
return bitcoinMempoolSize;
};

module.exports = {
publicKeyToCompressed,
fundAddressAndGetData,
getBitcoinTransactionsInMempool,
waitForBitcoinTxToBeInMempool,
waitForBitcoinMempoolToGetTxs,
getBtcAddressBalanceInSatoshis,
const checkBtcMempoolIsNotEmpty = async (bitcoinMempoolSize) => {
return bitcoinMempoolSize > 0;
};

const { result: bitcoinMempoolHasTx, attempts } = await retryWithCheck(
getCountOfTransactionsInMempool,
checkBtcMempoolIsNotEmpty,
maxAttempts,
checkEveryMilliseconds
);

const txsInMempool = await getBitcoinTransactionsInMempool(btcTxHelper);
const finalBitcoinMempoolSize = txsInMempool.length;

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)));
};

/**
* Funds a btc address and checks if the balance has been updated. If the balance has not been updated, then the tx has not yet reached the mempool or has not been mined. Rare condition that may happen randomly.
* In that case, the code waits for the tx to be in the mempool and then mines a block.
* @param {BtcTransactionHelper} btcTxHelper to make transactions to the bitcoin network
* @param {string} btcAddress address to fund
* @param {number} amountInBtc amount to fund in btc
*/
const fundBtcAddressCheckingBalance = async (btcTxHelper, btcAddress, amountInBtc) => {
const initialAddressBalance = await btcTxHelper.getAddressBalance(btcAddress);
const fundTxHash = await btcTxHelper.fundAddress(btcAddress, Number(amountInBtc));
const finalAddressBalance = await btcTxHelper.getAddressBalance(btcAddress);
if(finalAddressBalance === initialAddressBalance) {
const inMempool = await waitForBitcoinTxToBeInMempool(btcTxHelper, fundTxHash, 10);
if(inMempool) {
await btcTxHelper.mine();
}
}
};

module.exports = {
publicKeyToCompressed,
fundAddressAndGetData,
getBitcoinTransactionsInMempool,
waitForBitcoinTxToBeInMempool,
waitForBitcoinMempoolToGetTxs,
getBtcAddressBalanceInSatoshis,
fundBtcAddressCheckingBalance,
};
Loading

0 comments on commit 8082c93

Please sign in to comment.