-
Notifications
You must be signed in to change notification settings - Fork 6
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/relayer tests #395
base: dev
Are you sure you want to change the base?
Feat/relayer tests #395
Changes from 6 commits
5834674
7130a0f
3178c67
b9c2063
9a19159
b6c735a
47900e9
4da75bf
1609300
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
import { EventEmitter } from "events"; | ||
import { relayAllFrom } from "./utils/relay"; | ||
import { initialize, ShutdownManager, updateStateFile, setupExitHandlers, delay } from "./utils/relayerHelpers"; | ||
import { BotEvents } from "utils/botEvents"; | ||
|
||
export async function start(shutdownManager: ShutdownManager = new ShutdownManager()) { | ||
export async function start(shutdownManager: ShutdownManager = new ShutdownManager(), emitter: EventEmitter) { | ||
const chainId = parseInt(process.env.VEAOUTBOX_CHAIN_ID); | ||
const epochPeriod = 1800; // 30 min | ||
const network = "devnet"; | ||
await setupExitHandlers(chainId, shutdownManager, network); | ||
await setupExitHandlers(chainId, shutdownManager, network, emitter); | ||
while (!shutdownManager.getIsShuttingDown()) { | ||
let nonce = await initialize(chainId, network); | ||
let nonce = await initialize(chainId, network, emitter); | ||
// This is libghtbulb switch address in arbitrum sepolia | ||
const sender = process.env.DEVNET_SENDER; | ||
nonce = await relayAllFrom(chainId, nonce, sender); | ||
if (nonce != null) await updateStateFile(chainId, Math.floor(Date.now() / 1000), nonce, network); | ||
if (nonce != null) await updateStateFile(chainId, Math.floor(Date.now() / 1000), nonce, network, emitter); | ||
|
||
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"); | ||
emitter.emit(BotEvents.WAITING, delayAmount); | ||
await delay(delayAmount); | ||
} | ||
} | ||
|
||
if (require.main === module) { | ||
const emitter = new EventEmitter(); | ||
const shutdownManager = new ShutdownManager(false); | ||
start(shutdownManager); | ||
start(shutdownManager, emitter); | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,28 +1,64 @@ | ||||||||||||||||||||||||||||||||
require("dotenv").config(); | ||||||||||||||||||||||||||||||||
import { relayBatch } from "utils/relay"; | ||||||||||||||||||||||||||||||||
import { initialize, updateStateFile, delay, setupExitHandlers, ShutdownManager } from "utils/relayerHelpers"; | ||||||||||||||||||||||||||||||||
import { EventEmitter } from "node:events"; | ||||||||||||||||||||||||||||||||
import { relayBatch, RelayBatchDeps } from "utils/relay"; | ||||||||||||||||||||||||||||||||
import { | ||||||||||||||||||||||||||||||||
initialize as initializeNonce, | ||||||||||||||||||||||||||||||||
updateStateFile, | ||||||||||||||||||||||||||||||||
delay, | ||||||||||||||||||||||||||||||||
setupExitHandlers, | ||||||||||||||||||||||||||||||||
ShutdownManager, | ||||||||||||||||||||||||||||||||
} from "utils/relayerHelpers"; | ||||||||||||||||||||||||||||||||
import { getEpochPeriod } from "consts/bridgeRoutes"; | ||||||||||||||||||||||||||||||||
import { initialize as initializeEmitter } from "utils/logger"; | ||||||||||||||||||||||||||||||||
import { BotEvents } from "utils/botEvents"; | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
export async function start(shutdownManager: ShutdownManager = new ShutdownManager()) { | ||||||||||||||||||||||||||||||||
const network = "testnet"; | ||||||||||||||||||||||||||||||||
const chainId = parseInt(process.env.VEAOUTBOX_CHAIN_ID); | ||||||||||||||||||||||||||||||||
interface RelayerConfig { | ||||||||||||||||||||||||||||||||
chainId: number; | ||||||||||||||||||||||||||||||||
network: string; | ||||||||||||||||||||||||||||||||
shutdownManager: ShutdownManager; | ||||||||||||||||||||||||||||||||
emitter: EventEmitter; | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||
* Start the relayer | ||||||||||||||||||||||||||||||||
* @param chainId The chain id of the relayer | ||||||||||||||||||||||||||||||||
* @param network The network of the relayer | ||||||||||||||||||||||||||||||||
* @param shutdownManager The shutdown manager | ||||||||||||||||||||||||||||||||
* @param emitter The event emitter | ||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Update JSDoc comments to reflect the new function signature The Apply this diff to update the JSDoc comments: /**
* Start the relayer
- * @param chainId The chain id of the relayer
- * @param network The network of the relayer
- * @param shutdownManager The shutdown manager
- * @param emitter The event emitter
+ * @param config The relayer configuration
+ * @param config.chainId The chain id of the relayer
+ * @param config.network The network of the relayer
+ * @param config.shutdownManager The shutdown manager
+ * @param config.emitter The event emitter
*/
export async function start({ chainId, network, shutdownManager, emitter }: RelayerConfig) { 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think its needed, the comments and implementation is clear enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mani99brar Understood, I'll retract the suggestion. The TypeScript types and current documentation are indeed clear enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mani99brar the bot is right, the correct way of documenting object properties is with the dot notation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||||||||||||||||||||
export async function start({ chainId, network, shutdownManager, emitter }: RelayerConfig) { | ||||||||||||||||||||||||||||||||
initializeEmitter(emitter); | ||||||||||||||||||||||||||||||||
emitter.emit(BotEvents.STARTED, chainId, network); | ||||||||||||||||||||||||||||||||
const epochPeriod = getEpochPeriod(chainId); | ||||||||||||||||||||||||||||||||
const batchSize = 10; // 10 messages per batch | ||||||||||||||||||||||||||||||||
const maxBatchSize = 10; // 10 messages per batch | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
await setupExitHandlers(chainId, shutdownManager, network); | ||||||||||||||||||||||||||||||||
await setupExitHandlers(chainId, shutdownManager, network, emitter); | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
while (!shutdownManager.getIsShuttingDown()) { | ||||||||||||||||||||||||||||||||
let nonce = await initialize(chainId, network); | ||||||||||||||||||||||||||||||||
nonce = await relayBatch(chainId, nonce, batchSize); | ||||||||||||||||||||||||||||||||
if (nonce != null) await updateStateFile(chainId, Math.floor(Date.now() / 1000), nonce, network); | ||||||||||||||||||||||||||||||||
let nonce = await initializeNonce(chainId, network, emitter); | ||||||||||||||||||||||||||||||||
const relayBatchDeps: RelayBatchDeps = { | ||||||||||||||||||||||||||||||||
chainId, | ||||||||||||||||||||||||||||||||
nonce, | ||||||||||||||||||||||||||||||||
maxBatchSize, | ||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||
nonce = await relayBatch(relayBatchDeps); | ||||||||||||||||||||||||||||||||
if (nonce != null) await updateStateFile(chainId, Math.floor(Date.now() / 1000), nonce, network, emitter); | ||||||||||||||||||||||||||||||||
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"); | ||||||||||||||||||||||||||||||||
emitter.emit(BotEvents.WAITING, delayAmount); | ||||||||||||||||||||||||||||||||
await delay(delayAmount); | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
if (require.main === module) { | ||||||||||||||||||||||||||||||||
const emitter = new EventEmitter(); | ||||||||||||||||||||||||||||||||
const shutdownManager = new ShutdownManager(false); | ||||||||||||||||||||||||||||||||
start(shutdownManager); | ||||||||||||||||||||||||||||||||
const testnetRelayerConfig: RelayerConfig = { | ||||||||||||||||||||||||||||||||
shutdownManager, | ||||||||||||||||||||||||||||||||
emitter, | ||||||||||||||||||||||||||||||||
chainId: Number(process.env.VEAOUTBOX_CHAIN_ID), | ||||||||||||||||||||||||||||||||
network: "testnet", | ||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||
Comment on lines
+59
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Verify that You're using Apply this diff to add a check for the environment variable: + if (!process.env.VEAOUTBOX_CHAIN_ID) {
+ throw new Error('VEAOUTBOX_CHAIN_ID environment variable is not defined');
+ }
const testnetRelayerConfig: RelayerConfig = {
shutdownManager,
emitter,
chainId: Number(process.env.VEAOUTBOX_CHAIN_ID),
network: "testnet",
}; 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's already an issue created for env validation and it will be addressed there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mani99brar Understood, thanks for letting me know that environment variable validation will be handled separately. |
||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||
start(testnetRelayerConfig); | ||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export enum BotEvents { | ||
// Bridger state | ||
STARTED = "started", | ||
WAITING = "waiting", | ||
EXIT = "exit", | ||
|
||
// Bot health | ||
EXCEPTION = "exception", | ||
PROMISE_REJECTION = "promise_rejection", | ||
|
||
// Lock file | ||
LOCK_CLAIMED = "lock_claimed", | ||
LOCK_DIRECTORY = "lock_directory", | ||
LOCK_RELEASED = "lock_released", | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,49 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { EventEmitter } from "node:events"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
import { BotEvents } from "./botEvents"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* Listens to relevant events of an EventEmitter instance and issues log lines | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @param emitter - The event emitter instance that issues the relevant events | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* @example | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* const emitter = new EventEmitter(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
* initialize(emitter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export const initialize = (emitter: EventEmitter) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
return configurableInitialize(emitter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
export const configurableInitialize = (emitter: EventEmitter) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Relayer state logs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
emitter.on(BotEvents.STARTED, (chainId, network) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.log(`Relayer started for ${chainId} on ${network}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
emitter.on(BotEvents.WAITING, (delayAmount) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.log(`Waiting for next epoch: ${delayAmount} ms`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
emitter.on(BotEvents.EXIT, () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.log("Exiting"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Bot health logs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
emitter.on(BotEvents.EXCEPTION, (err) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.error("Uncaught Exception occurred", err); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
emitter.on(BotEvents.PROMISE_REJECTION, (reason, promise) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.error("Unhandled promise rejection:", reason, "at", promise); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
// Lock file logs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
emitter.on(BotEvents.LOCK_CLAIMED, () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.log("Lock claimed"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
emitter.on(BotEvents.LOCK_DIRECTORY, (pwd) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.log(`Lock file directory: ${pwd}`); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
emitter.on(BotEvents.LOCK_RELEASED, () => { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.log("Lock released"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+19
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add error handling to event listeners. The event listeners should include try-catch blocks to prevent crashes if event handling fails. Here's an example for one event handler: - emitter.on(BotEvents.STARTED, (chainId, network) => {
- console.log(`Relayer started for ${chainId} on ${network}`);
- });
+ emitter.on(BotEvents.STARTED, (chainId, network) => {
+ try {
+ console.log(`Relayer started for ${chainId} on ${network}`);
+ } catch (error) {
+ console.error('Failed to handle STARTED event:', error);
+ }
+ }); 📝 Committable suggestion
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont see how this is necessary There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mani99brar You're right. The event listeners are just doing simple console logging, which is very unlikely to throw errors. Adding try-catch blocks would be unnecessary complexity. I'll retract this suggestion. ✏️ Learnings added
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { getProofIndices } from "./proof"; | ||
|
||
describe("proof", () => { | ||
describe("getProofIndices", () => { | ||
it("should return an empty array", () => { | ||
const result = getProofIndices(7, 7); | ||
expect(result).toEqual([]); | ||
}); | ||
it("should return the proof indices", () => { | ||
const expectedProofIndices = ["3", "0,1", "4,6"]; | ||
const result = getProofIndices(2, 7); | ||
expect(result).toEqual(expectedProofIndices); | ||
}); | ||
it("should return the proof indices(for a large count", () => { | ||
const expectedProofIndices = ["6", "4,5", "0,3", "8,14"]; | ||
const result = getProofIndices(7, 15); | ||
expect(result).toEqual(expectedProofIndices); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
import request from "graphql-request"; | ||
import { getInboxSubgraph } from "../consts/bridgeRoutes"; | ||
|
||
const getMessageDataToRelay = async (chainid: number, nonce: number) => { | ||
/** | ||
* Get the message data to relay from the subgraph | ||
* @param chainId The chain id of the veaOutbox chain | ||
* @param nonce The nonce of the message | ||
* @returns The message id and data to relay | ||
*/ | ||
const getMessageDataToRelay = async (chainId: number, nonce: number, requestGraph: typeof request = request) => { | ||
try { | ||
const subgraph = getInboxSubgraph(chainid); | ||
const subgraph = getInboxSubgraph(chainId); | ||
|
||
const result = await request( | ||
const result = await requestGraph( | ||
`https://api.studio.thegraph.com/query/${subgraph}`, | ||
`{ | ||
messageSents(first: 5, where: {nonce: ${nonce}}) { | ||
|
@@ -24,9 +30,22 @@ const getMessageDataToRelay = async (chainid: number, nonce: number) => { | |
} | ||
}; | ||
|
||
const getProofAtCount = async (chainid: number, nonce: number, count: number): Promise<string[]> => { | ||
const proofIndices = getProofIndices(nonce, count); | ||
|
||
/** | ||
* Get the proof of the message at a given count | ||
* @param chainId The chain id of the veaOutbox chain | ||
* @param nonce The nonce of the message | ||
* @param count The current veaOutbox count | ||
* @returns The proof of the message | ||
*/ | ||
const getProofAtCount = async ( | ||
chainId: number, | ||
nonce: number, | ||
count: number, | ||
requestGraph: typeof request = request, | ||
calculateProofIndices: typeof getProofIndices = getProofIndices, | ||
fetchInboxSubgraph: typeof getInboxSubgraph = getInboxSubgraph | ||
): Promise<string[]> => { | ||
const proofIndices = calculateProofIndices(nonce, count); | ||
if (proofIndices.length == 0) return []; | ||
|
||
let query = "{"; | ||
|
@@ -38,23 +57,27 @@ const getProofAtCount = async (chainid: number, nonce: number, count: number): P | |
query += "}"; | ||
|
||
try { | ||
const subgraph = getInboxSubgraph(chainid); | ||
const subgraph = fetchInboxSubgraph(chainId); | ||
|
||
const result = await request(`https://api.studio.thegraph.com/query/${subgraph}`, query); | ||
const result = await requestGraph(`https://api.studio.thegraph.com/query/${subgraph}`, query); | ||
|
||
const proof = []; | ||
|
||
for (let i = 0; i < proofIndices.length; i++) { | ||
proof.push(result[`layer${i}`][0].hash); | ||
} | ||
|
||
return proof; | ||
} catch (e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not a change of the PR, so please don't address it within it... but this try/catch is unexpected for me. If it fails getting the hashes... it returns an empty array? not throw the error? the request failing should be unexpected, not a controlled error There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you are right. It doesn't make any sense to return an empty array to me either. Creating an issue for it. |
||
console.log(e); | ||
return []; | ||
} | ||
}; | ||
|
||
/** | ||
* Get the proof indices of the message | ||
* @param nonce The nonce of the message | ||
* @param count The current veaOutbox count | ||
* @returns The proof indices of the message | ||
*/ | ||
const getProofIndices = (nonce: number, count: number) => { | ||
let proof = []; | ||
if (nonce >= count) return proof; | ||
|
@@ -76,4 +99,4 @@ const getProofIndices = (nonce: number, count: number) => { | |
return proof; | ||
}; | ||
|
||
export { getProofAtCount, getMessageDataToRelay }; | ||
export { getProofAtCount, getMessageDataToRelay, getProofIndices }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verify environment variables are defined before use
You're using
process.env.VEAOUTBOX_CHAIN_ID
to setchainId
. If this environment variable isundefined
,parseInt(undefined)
results inNaN
, which could cause issues. Similarly,sender
is set fromprocess.env.DEVNET_SENDER
. Consider adding checks to ensure these environment variables are defined and handle the case where they are not.Apply this diff to add checks for the environment variables:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's already an issue created for env validation and it will be addressed there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mani99brar Understood, I'll defer to that issue for environment variable validation.
⊙﹏⊙