diff --git a/relayer-cli/src/devnetRelayExample.ts b/relayer-cli/src/devnetRelayExample.ts index b952ff47..555cc126 100644 --- a/relayer-cli/src/devnetRelayExample.ts +++ b/relayer-cli/src/devnetRelayExample.ts @@ -5,7 +5,7 @@ export async function start(shutdownManager: ShutdownManager = new ShutdownManag const chainId = parseInt(process.env.VEAOUTBOX_CHAIN_ID); const epochPeriod = 1800; // 30 min const network = "devnet"; - await setupExitHandlers(chainId, shutdownManager); + await setupExitHandlers(chainId, shutdownManager, network); while (!shutdownManager.getIsShuttingDown()) { let nonce = await initialize(chainId, network); // This is libghtbulb switch address in arbitrum sepolia diff --git a/relayer-cli/src/testnetRelayer.ts b/relayer-cli/src/testnetRelayer.ts index 3ac6fc1e..247345f9 100644 --- a/relayer-cli/src/testnetRelayer.ts +++ b/relayer-cli/src/testnetRelayer.ts @@ -9,7 +9,7 @@ export async function start(shutdownManager: ShutdownManager = new ShutdownManag const epochPeriod = getEpochPeriod(chainId); const batchSize = 10; // 10 messages per batch - await setupExitHandlers(chainId, shutdownManager); + await setupExitHandlers(chainId, shutdownManager, network); while (!shutdownManager.getIsShuttingDown()) { let nonce = await initialize(chainId, network); diff --git a/relayer-cli/src/utils/relay.ts b/relayer-cli/src/utils/relay.ts index 38a229a5..eb5271c9 100644 --- a/relayer-cli/src/utils/relay.ts +++ b/relayer-cli/src/utils/relay.ts @@ -25,27 +25,27 @@ const getCount = async (veaOutbox: VeaOutboxArbToEth | VeaOutboxArbToGnosis, cha return Number(result["snapshotSaveds"][0].count); }; -const relay = async (chainid: number, nonce: number) => { - const routeParams = getBridgeConfig(chainid); - const veaOutbox = getVeaOutbox(routeParams.veaOutboxAddress, process.env.PRIVATE_KEY, routeParams.rpcOutbox, chainid); - const count = await getCount(veaOutbox, chainid); +const relay = async (chainId: number, nonce: number) => { + const routeParams = getBridgeConfig(chainId); + const veaOutbox = getVeaOutbox(routeParams.veaOutboxAddress, process.env.PRIVATE_KEY, routeParams.rpcOutbox, chainId); + const count = await getCount(veaOutbox, chainId); const [proof, [to, data]] = await Promise.all([ - getProofAtCount(chainid, nonce, count), - getMessageDataToRelay(chainid, nonce), + getProofAtCount(chainId, nonce, count), + getMessageDataToRelay(chainId, nonce), ]); const txn = await veaOutbox.sendMessage(proof, nonce, to, data); await txn.wait(); }; -const relayBatch = async (chainid: number, nonce: number, maxBatchSize: number) => { - const routeParams = getBridgeConfig(chainid); +const relayBatch = async (chainId: number, nonce: number, maxBatchSize: number) => { + const routeParams = getBridgeConfig(chainId); const web3 = new Web3(routeParams.rpcOutbox); const batchedSend = initializeBatchedSend(web3, routeParams.batcher, process.env.PRIVATE_KEY, 0); const veaOutboxInstance = new web3.eth.Contract(routeParams.veaOutboxContract.abi, routeParams.veaOutboxAddress); - const veaOutbox = getVeaOutbox(routeParams.veaOutboxAddress, process.env.PRIVATE_KEY, routeParams.rpcOutbox, chainid); - const count = await getCount(veaOutbox, chainid); + const veaOutbox = getVeaOutbox(routeParams.veaOutboxAddress, process.env.PRIVATE_KEY, routeParams.rpcOutbox, chainId); + const count = await getCount(veaOutbox, chainId); while (nonce < count) { let batchMessages = 0; @@ -57,8 +57,8 @@ const relayBatch = async (chainid: number, nonce: number, maxBatchSize: number) continue; } const [proof, [to, data]] = await Promise.all([ - getProofAtCount(chainid, nonce, count), - getMessageDataToRelay(chainid, nonce), + getProofAtCount(chainId, nonce, count), + getMessageDataToRelay(chainId, nonce), ]); txns.push({ args: [proof, nonce, to, data], @@ -75,8 +75,8 @@ const relayBatch = async (chainid: number, nonce: number, maxBatchSize: number) return nonce; }; -const relayAllFrom = async (chainid: number, nonce: number, msgSender: string): Promise => { - const routeParams = getBridgeConfig(chainid); +const relayAllFrom = async (chainId: number, nonce: number, msgSender: string): Promise => { + const routeParams = getBridgeConfig(chainId); const web3 = new Web3(routeParams.rpcOutbox); const batchedSend = initializeBatchedSend( @@ -88,19 +88,19 @@ const relayAllFrom = async (chainid: number, nonce: number, msgSender: string): ); const contract = new web3.eth.Contract(routeParams.veaOutboxContract.abi, routeParams.veaOutboxAddress); - const veaOutbox = getVeaOutbox(routeParams.veaOutboxAddress, process.env.PRIVATE_KEY, routeParams.rpcOutbox, chainid); - const count = await getCount(veaOutbox, chainid); + const veaOutbox = getVeaOutbox(routeParams.veaOutboxAddress, process.env.PRIVATE_KEY, routeParams.rpcOutbox, chainId); + const count = await getCount(veaOutbox, chainId); if (!count) return null; let txns = []; - const nonces = await getNonceFrom(chainid, nonce, msgSender); + const nonces = await getNonceFrom(chainId, nonce, msgSender); for (const x of nonces) { const [proof, [to, data]] = await Promise.all([ - getProofAtCount(chainid, x, count), - getMessageDataToRelay(chainid, x), + getProofAtCount(chainId, x, count), + getMessageDataToRelay(chainId, x), ]); txns.push({ args: [proof, x, to, data], @@ -114,9 +114,9 @@ const relayAllFrom = async (chainid: number, nonce: number, msgSender: string): return nonces[nonces.length - 1]; }; -const getNonceFrom = async (chainid: number, nonce: number, msgSender: string) => { +const getNonceFrom = async (chainId: number, nonce: number, msgSender: string) => { try { - const subgraph = getInboxSubgraph(chainid); + const subgraph = getInboxSubgraph(chainId); const result = await request( `https://api.studio.thegraph.com/query/${subgraph}`, diff --git a/relayer-cli/src/utils/relayerHelpers.ts b/relayer-cli/src/utils/relayerHelpers.ts index 7946e40b..7499c1a2 100644 --- a/relayer-cli/src/utils/relayerHelpers.ts +++ b/relayer-cli/src/utils/relayerHelpers.ts @@ -60,15 +60,15 @@ async function updateStateFile(chainId: number, createdTimestamp: number, nonceF } } -async function setupExitHandlers(chainId: number, shutdownManager: ShutdownManager) { +async function setupExitHandlers(chainId: number, shutdownManager: ShutdownManager, network: string) { const cleanup = async () => { console.log("exit"); - const lockFileName = `./state/${chainId}.pid`; + const lockFileName = "./state/" + network + "_" + chainId + ".pid"; if (fs.existsSync(lockFileName)) { await fs.promises.unlink(lockFileName); } }; - const handleExit = async () => { + const handleExit = async (exitCode: number = 0) => { shutdownManager.triggerShutdown(); await cleanup(); process.exit(0); @@ -76,7 +76,7 @@ async function setupExitHandlers(chainId: number, shutdownManager: ShutdownManag ["SIGINT", "SIGTERM", "SIGQUIT"].forEach((signal) => process.on(signal, async () => { - await handleExit(); + await handleExit(0); process.exit(0); }) ); @@ -84,6 +84,16 @@ async function setupExitHandlers(chainId: number, shutdownManager: ShutdownManag process.on("exit", async () => { await handleExit(); }); + + process.on("uncaughtException", async (err) => { + console.error("Uncaught exception:", err); + await handleExit(1); + }); + + process.on("unhandledRejection", async (reason, promise) => { + console.error("Unhandled promise rejection:", reason, "at", promise); + await handleExit(1); + }); } function delay(ms: number) { diff --git a/validator-cli/src/utils/arbMsgExecutor.ts b/validator-cli/src/utils/arbMsgExecutor.ts index f333b876..e2d8c8bf 100644 --- a/validator-cli/src/utils/arbMsgExecutor.ts +++ b/validator-cli/src/utils/arbMsgExecutor.ts @@ -16,8 +16,7 @@ async function messageExecutor(trnxHash: string, childRpc: string, parentRpc: st const childProvider = new ArbitrumProvider(childJsonRpc); const parentProvider = new JsonRpcProvider(parentRpc); - let childReceipt: TransactionReceipt | null; - childReceipt = await childProvider.getTransactionReceipt(trnxHash); + const childReceipt: TransactionReceipt = await childProvider.getTransactionReceipt(trnxHash); if (!childReceipt) { throw new Error(`Transaction receipt not found for hash: ${trnxHash}`); } @@ -25,9 +24,8 @@ async function messageExecutor(trnxHash: string, childRpc: string, parentRpc: st const messageReceipt = new ChildTransactionReceipt(childReceipt); const parentSigner: Signer = new Wallet(PRIVATE_KEY, parentProvider); - let childToParentMessage: ChildToParentMessageWriter; const messages = await messageReceipt.getChildToParentMessages(parentSigner); - childToParentMessage = messages[0]; + const childToParentMessage: ChildToParentMessageWriter = messages[0]; if (!childToParentMessage) { throw new Error("No child-to-parent messages found"); }