Skip to content

Commit

Permalink
fix: review fixes(#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mani Brar authored and Mani Brar committed Nov 27, 2024
1 parent ab41a47 commit 732c5a1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
2 changes: 1 addition & 1 deletion relayer-cli/src/devnetRelayExample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion relayer-cli/src/testnetRelayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
42 changes: 21 additions & 21 deletions relayer-cli/src/utils/relay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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],
Expand All @@ -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<number> => {
const routeParams = getBridgeConfig(chainid);
const relayAllFrom = async (chainId: number, nonce: number, msgSender: string): Promise<number> => {
const routeParams = getBridgeConfig(chainId);

const web3 = new Web3(routeParams.rpcOutbox);
const batchedSend = initializeBatchedSend(
Expand All @@ -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],
Expand All @@ -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}`,
Expand Down
18 changes: 14 additions & 4 deletions relayer-cli/src/utils/relayerHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,30 +60,40 @@ 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);
};

["SIGINT", "SIGTERM", "SIGQUIT"].forEach((signal) =>
process.on(signal, async () => {
await handleExit();
await handleExit(0);
process.exit(0);
})
);

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) {
Expand Down
6 changes: 2 additions & 4 deletions validator-cli/src/utils/arbMsgExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@ 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}`);
}

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");
}
Expand Down

0 comments on commit 732c5a1

Please sign in to comment.