Skip to content

Commit

Permalink
Adds waiting for pegout tx not yet mined
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-then committed Mar 1, 2024
1 parent 8a6e8af commit e7453a8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
31 changes: 31 additions & 0 deletions tool/pegout-tracker/pegout-tracker.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

const Web3 = require('web3');
const Bridge = require('@rsksmart/rsk-precompiled-abis').bridge;
const BridgeTransactionParser = require('../../index');
const EventEmitter = require('node:events');
const LiveMonitor = require('../live-monitor/live-monitor');
const { MONITOR_EVENTS } = require('../live-monitor/live-monitor-utils');
const { wait } = require('../../utils');

const {
isPegoutRequestRejectedTx,
Expand Down Expand Up @@ -46,6 +48,28 @@ const validateRequiredConfirmationsForCustomNetwork = (network, options) => {
}
};

const isPegoutRequestTransaction = (tx) => {
return tx && tx.to === Bridge.address && (tx.input === '0x' || tx.input === '0x80af2871');
};

const isTransactionWaitingToBeMined = (tx) => {
return tx && tx.blockNumber === null;
};

const waitForTransactionToBeMined = async (web3, txHash, checkEveryMilliseconds = 1_000, maxAttempts = 60) => {
let tx = await web3.eth.getTransaction(txHash);
let attempt = 1;
while(isTransactionWaitingToBeMined(tx)) {
await wait(checkEveryMilliseconds);
tx = await web3.eth.getTransaction(txHash);
if(attempt > maxAttempts) {
throw new Error(`Transaction ${txHash} is taking too long to be mined. Tried ${attempt} times checking every ${checkEveryMilliseconds / 1000} seconds.`);
}
attempt++;
}
return tx;
};

class PegoutTracker extends EventEmitter {

constructor() {
Expand Down Expand Up @@ -81,6 +105,13 @@ class PegoutTracker extends EventEmitter {
const rskClient = new Web3(networkUrl);

const bridgeTransactionParser = new BridgeTransactionParser(rskClient);

const tx = await rskClient.eth.getTransaction(pegoutTxHash);

if(isTransactionWaitingToBeMined(tx) && isPegoutRequestTransaction(tx)) {
console.warn(`The pegout request transaction with hash ${pegoutTxHash} has not been mined. Waiting for it to be mined...`);
await waitForTransactionToBeMined(rskClient, pegoutTxHash);
}

const rskTx = await bridgeTransactionParser.getBridgeTransactionByTxHash(pegoutTxHash);

Expand Down
3 changes: 3 additions & 0 deletions utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const RLP = require('rlp');
const Bridge = require('@rsksmart/rsk-precompiled-abis').bridge;

const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms));

const verifyHashOrBlockNumber = (blockHashOrBlockNumber) => {
if (typeof blockHashOrBlockNumber === 'string' &&
blockHashOrBlockNumber.indexOf('0x') === 0 &&
Expand Down Expand Up @@ -109,4 +111,5 @@ module.exports = {
isReleaseBtcTx,
bridgeStateKeysToStorageIndexMap,
getBridgeStorageAtBlock,
wait,
};

0 comments on commit e7453a8

Please sign in to comment.