Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/arb-sep-to-sep-testnet #344

Merged
merged 15 commits into from
Nov 28, 2024
Prev Previous commit
Next Next commit
feat(relayer): sep & chiado testnet
Mani Brar authored and Mani Brar committed Oct 21, 2024
commit a4b6a9045272df7f0de667337f325b6fe398236b
3 changes: 2 additions & 1 deletion relayer-cli/package.json
Original file line number Diff line number Diff line change
@@ -10,7 +10,8 @@
"yarn": "4.2.2"
},
"scripts": {
"start-devnet-relayer": "npx ts-node ./src/devnetRelayExample.ts"
"start-devnet-relayer": "npx ts-node ./src/devnetRelayExample.ts",
"start-testnet-sepolia": "npx ts-node ./src/testnet/arbSepToSepRelayer.ts"
},
"dependencies": {
"@kleros/vea-contracts": "workspace:^",
39 changes: 39 additions & 0 deletions relayer-cli/src/testnet/arbSepToChiadoRelayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import * as fs from "fs";
require("dotenv").config();
import { relayBatch } from "utils/relay";
import { initialize, updateStateFile } from "utils/relayerHelpers";
const _contract = require("@kleros/vea-contracts/deployments/chiado/VeaOutboxArbToGnosisTestnet.json");

let chain_id = 10200;
const epochPeriod = 7200; // 3 hrs
const batchSize = 10; // 10 messages per batch
const network = "testnet";

["SIGINT", "SIGTERM", "SIGQUIT", "EXIT", "MODULE_NOT_FOUND"].forEach((signal) =>
process.on(signal, async () => {
console.log("exit");
const lock_file_name = "./src/state/" + chain_id + ".pid";
if (fs.existsSync(lock_file_name)) {
fs.unlinkSync(lock_file_name);
}
process.exit(0);
})
);

(async () => {
while (1) {
let nonce = await initialize(chain_id, network);
console.log("chain_id", chain_id, "nonce", nonce);
nonce = await relayBatch(chain_id, nonce, batchSize, _contract);
if (nonce != null) await updateStateFile(chain_id, Math.floor(Date.now() / 1000), nonce, network);

const currentTS = Math.floor(Date.now() / 1000);
const delayAmount = (epochPeriod - (currentTS % epochPeriod)) * 1000 + 100 * 1000;
console.log("waiting for the next epoch. . .", Math.floor(delayAmount / 1000), "seconds");
await delay(delayAmount);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Add error handling inside the main loop to prevent crashes

Currently, any unhandled exceptions within the loop will cause the relayer to crash and exit. Wrapping the contents of the loop in a try...catch block ensures that the relayer continues running even if an error occurs, enhancing robustness.

Apply this diff to add error handling:

 (async () => {
   while (true) {
+    try {
     let nonce = await initialize(chain_id, network);
     console.log("chain_id", chain_id, "nonce", nonce);
     nonce = await relayBatch(chain_id, nonce, batchSize, _contract);
     if (nonce != null) await updateStateFile(chain_id, Math.floor(Date.now() / 1000), nonce, network);

     const currentTS = Math.floor(Date.now() / 1000);
     const delayAmount = (epochPeriod - (currentTS % epochPeriod)) * 1000 + 100 * 1000;
     console.log("waiting for the next epoch. . .", Math.floor(delayAmount / 1000), "seconds");
     await delay(delayAmount);
+    } catch (error) {
+      console.error("An error occurred in the main loop:", error);
+    }
   }
 })();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
while (1) {
let nonce = await initialize(chain_id, network);
console.log("chain_id", chain_id, "nonce", nonce);
nonce = await relayBatch(chain_id, nonce, batchSize, _contract);
if (nonce != null) await updateStateFile(chain_id, Math.floor(Date.now() / 1000), nonce, network);
const currentTS = Math.floor(Date.now() / 1000);
const delayAmount = (epochPeriod - (currentTS % epochPeriod)) * 1000 + 100 * 1000;
console.log("waiting for the next epoch. . .", Math.floor(delayAmount / 1000), "seconds");
await delay(delayAmount);
}
while (true) {
try {
let nonce = await initialize(chain_id, network);
console.log("chain_id", chain_id, "nonce", nonce);
nonce = await relayBatch(chain_id, nonce, batchSize, _contract);
if (nonce != null) await updateStateFile(chain_id, Math.floor(Date.now() / 1000), nonce, network);
const currentTS = Math.floor(Date.now() / 1000);
const delayAmount = (epochPeriod - (currentTS % epochPeriod)) * 1000 + 100 * 1000;
console.log("waiting for the next epoch. . .", Math.floor(delayAmount / 1000), "seconds");
await delay(delayAmount);
} catch (error) {
console.error("An error occurred in the main loop:", error);
}
}
🧰 Tools
🪛 Biome

[error] 24-24: Unexpected constant condition.

(lint/correctness/noConstantCondition)

})();

function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
38 changes: 38 additions & 0 deletions relayer-cli/src/testnet/arbSepToSepRelayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import * as fs from "fs";
require("dotenv").config();
import { relayBatch } from "utils/relay";
import { initialize, updateStateFile } from "utils/relayerHelpers";

const _contract = require("@kleros/vea-contracts/deployments/sepolia/VeaOutboxArbToEthTestnet.json");
const network = "testnet";

let chain_id = 11155111;
const epochPeriod = 7200; // 3 hrs
const batchSize = 10; // 10 messages per batch

["SIGINT", "SIGTERM", "SIGQUIT", "EXIT", "MODULE_NOT_FOUND"].forEach((signal) =>
process.on(signal, async () => {
console.log("exit");
const lock_file_name = "./src/state/" + chain_id + ".pid";
if (fs.existsSync(lock_file_name)) {
fs.unlinkSync(lock_file_name);
}
process.exit(0);
})
);

(async () => {
while (1) {
let nonce = await initialize(chain_id, network);
nonce = await relayBatch(chain_id, nonce, batchSize, _contract);
if (nonce != null) await updateStateFile(chain_id, Math.floor(Date.now() / 1000), nonce, network);
const currentTS = Math.floor(Date.now() / 1000);
const delayAmount = (epochPeriod - (currentTS % epochPeriod)) * 1000 + 100 * 1000;
console.log("waiting for the next epoch. . .", Math.floor(delayAmount / 1000), "seconds");
await delay(delayAmount);
}
})();

function delay(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}