Skip to content

Commit

Permalink
Refactors. Correct defaults in docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-then committed Dec 12, 2023
1 parent 2997818 commit 98af287
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/2wp-utils-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down
2 changes: 1 addition & 1 deletion lib/2wp-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
Expand Down
22 changes: 12 additions & 10 deletions lib/rsk-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>} whether the tx is in the mempool or not
*/
Expand All @@ -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<boolean>} whether the mempool has new txs or not
*/
Expand All @@ -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`);
Expand All @@ -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;

};

Expand Down Expand Up @@ -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) {
Expand Down
37 changes: 21 additions & 16 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -212,17 +215,19 @@ const retryWithCheck = async (method, check, maxAttempts = 5, delayInMillisecond
}
}
}
return result;
return {
result,
attempts: currentAttempts
};
};

/**
*
* @param {BtcTransactionHelper} btcTxHelper
* @returns {Promise<Array<string>>} 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', []);
};

/**
Expand All @@ -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) {
Expand Down Expand Up @@ -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;

Expand All @@ -321,7 +326,7 @@ module.exports = {
},
removePrefix0x,
retryWithCheck,
getBitcoinMempool,
getBitcoinTransactionsInMempool,
waitForBitcoinTxToBeInMempool,
waitForBitcoinMempoolToGetTxs,
}
8 changes: 6 additions & 2 deletions tests/01_03_54-post-papyrus_coinbase_information.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 98af287

Please sign in to comment.