Skip to content

Commit

Permalink
fix(relayer): coderabbit review & nonce handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mani Brar authored and Mani Brar committed Nov 4, 2024
1 parent a4b6a90 commit 8a440c5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 43 deletions.
21 changes: 8 additions & 13 deletions relayer-cli/src/testnet/arbSepToChiadoRelayer.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import * as fs from "fs";
require("dotenv").config();
import { relayBatch } from "utils/relay";
import { initialize, updateStateFile } from "utils/relayerHelpers";
import { initialize, updateStateFile, delay } from "utils/relayerHelpers";
const _contract = require("@kleros/vea-contracts/deployments/chiado/VeaOutboxArbToGnosisTestnet.json");

let chain_id = 10200;
const chainId = 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";
const lock_file_name = "./src/state/" + chainId + ".pid";
if (fs.existsSync(lock_file_name)) {
fs.unlinkSync(lock_file_name);
}
Expand All @@ -21,19 +21,14 @@ const network = "testnet";
);

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

while (true) {
let nonce = await initialize(chainId, network);
console.log("chain_id", chainId, "nonce", nonce);
nonce = await relayBatch(chainId, nonce, batchSize, _contract);
if (nonce != null) await updateStateFile(chainId, 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));
}
18 changes: 7 additions & 11 deletions relayer-cli/src/testnet/arbSepToSepRelayer.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import * as fs from "fs";
require("dotenv").config();
import { relayBatch } from "utils/relay";
import { initialize, updateStateFile } from "utils/relayerHelpers";
import { initialize, updateStateFile, delay } from "utils/relayerHelpers";

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

let chain_id = 11155111;
let chainId = 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";
const lock_file_name = "./src/state/" + chainId + ".pid";
if (fs.existsSync(lock_file_name)) {
fs.unlinkSync(lock_file_name);
}
Expand All @@ -22,17 +22,13 @@ const batchSize = 10; // 10 messages per batch
);

(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);
while (true) {
let nonce = await initialize(chainId, network);
nonce = await relayBatch(chainId, nonce, batchSize, _contract);
if (nonce != null) await updateStateFile(chainId, 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));
}
12 changes: 9 additions & 3 deletions relayer-cli/src/utils/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ function getVeaInbox(veaInboxAddress: string, privateKey: string, web3ProviderUR
return VeaInboxArbToEth__factory.connect(veaInboxAddress, getWallet(privateKey, web3ProviderURL));
} else if (chainId == 10200) {
return VeaInboxArbToGnosis__factory.connect(veaInboxAddress, getWallet(privateKey, web3ProviderURL));
} else {
throw new Error(`Unsupported chainId: ${chainId}`);
}
}

Expand All @@ -38,14 +40,18 @@ function getVeaOutbox(veaInboxAddress: string, privateKey: string, web3ProviderU
return VeaOutboxArbToEth__factory.connect(veaInboxAddress, getWallet(privateKey, web3ProviderURL));
} else if (chainId == 10200) {
return VeaOutboxArbToGnosis__factory.connect(veaInboxAddress, getWallet(privateKey, web3ProviderURL));
} else {
throw new Error(`Unsupported chainId: ${chainId}`);
}
}

function getVeaOutboxProvider(veaInboxAddress: string, privateKey: string, rpc: JsonRpcProvider, chainId: number) {
function getVeaOutboxProvider(veaOutboxAddress: string, privateKey: string, rpc: JsonRpcProvider, chainId: number) {
if (chainId == 11155111) {
return VeaOutboxArbToEth__factory.connect(veaInboxAddress, getWalletRPC(privateKey, rpc));
return VeaOutboxArbToEth__factory.connect(veaOutboxAddress, getWalletRPC(privateKey, rpc));
} else if (chainId == 10200) {
return VeaOutboxArbToGnosis__factory.connect(veaInboxAddress, getWalletRPC(privateKey, rpc));
return VeaOutboxArbToGnosis__factory.connect(veaOutboxAddress, getWalletRPC(privateKey, rpc));
} else {
throw new Error(`Unsupported chainId: ${chainId}`);
}
}

Expand Down
30 changes: 17 additions & 13 deletions relayer-cli/src/utils/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,31 @@ const relayBatch = async (chainid: number, nonce: number, maxBatchSize: number,
const veaOutbox = getVeaOutbox(routeParams.veaOutbox, process.env.PRIVATE_KEY, routeParams.rpcOutbox, chainid);
const count = await getCount(veaOutbox, chainid);

while (nonce <= count) {
while (nonce < count) {
let batchMessages = 0;
let txns = [];
for (let i = 0; batchMessages < maxBatchSize && nonce + i < count; i++) {
const isMsgRelayed = await veaOutbox.isMsgRelayed(nonce + i);
if (isMsgRelayed) continue;
const proof = await getProofAtCount(chainid, nonce + i, count);
const [to, data] = await getMessageDataToRelay(chainid, nonce + i);
while (batchMessages < maxBatchSize && nonce < count) {
const isMsgRelayed = await veaOutbox.isMsgRelayed(nonce);
if (isMsgRelayed) {
nonce++;
continue;
}
const proof = await getProofAtCount(chainid, nonce, count);
const [to, data] = await getMessageDataToRelay(chainid, nonce);
txns.push({
args: [proof, nonce + i, to, data],
args: [proof, nonce, to, data],
method: contract.methods.sendMessage,
to: contract.options.address,
});
batchMessages += 1;
nonce++;
}
try {
await batchedSend(txns);
// Updating nonce to the next message to be sent
nonce += batchMessages + 1;
} catch (error) {
console.error(`Unable to execute messgae batch(${batchMessages} msgs) from nonce:${nonce} `, error);
if (batchMessages > 0) {
try {
await batchedSend(txns);
} catch (error) {
console.error(`Unable to execute messgae batch(${batchMessages} msgs) from nonce:${nonce} `, error);
}
}
}
return nonce;
Expand Down
10 changes: 7 additions & 3 deletions relayer-cli/src/utils/relayerHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ async function initialize(chain_id: number, network: string): Promise<number> {

// STATE_DIR is absolute path of the directory where the state files are stored
// STATE_DIR must have trailing slash
const state_file = process.env.STATE_DIR + chain_id + ".json";
const state_file = process.env.STATE_DIR + network + "_" + chain_id + ".json";
if (!fs.existsSync(state_file)) {
// No state file so initialize starting now
const tsnow = Math.floor(Date.now() / 1000);
Expand All @@ -38,10 +38,14 @@ async function updateStateFile(chain_id: number, createdTimestamp: number, nonce
};
fs.writeFileSync(chain_state_file, JSON.stringify(json), { encoding: "utf8" });

const lock_file_name = "./src/state/testnet_" + chain_id + ".pid";
const lock_file_name = "./src/state/" + network + "_" + chain_id + ".pid";
if (fs.existsSync(lock_file_name)) {
fs.unlinkSync(lock_file_name);
}
}

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

export { initialize, updateStateFile, delay };

0 comments on commit 8a440c5

Please sign in to comment.