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

improve(relayer): Pass SpokePool addresses to listener processes #1902

Merged
merged 16 commits into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/clients/SpokePoolClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ export class IndexedSpokePoolClient extends clients.SpokePoolClient {
*/
protected startWorker(): void {
const {
eventSearchConfig: { fromBlock, maxBlockLookBack: blockRange },
eventSearchConfig: { fromBlock, maxBlockLookBack: blockrange },
james-a-morris marked this conversation as resolved.
Show resolved Hide resolved
spokePool: { address: spokepool },
} = this;
const opts = { blockRange, lookback: `@${fromBlock}` };
const opts = { spokepool, blockrange, lookback: `@${fromBlock}` };

const args = Object.entries(opts)
.map(([k, v]) => [`--${k}`, `${v}`])
Expand Down
20 changes: 16 additions & 4 deletions src/libexec/RelayerSpokePoolIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getOriginFromURL,
getProvider,
getRedisCache,
getSpokePool,
getWSProviders,
Logger,
winston,
Expand Down Expand Up @@ -117,18 +118,24 @@ async function listen(
*/
async function run(argv: string[]): Promise<void> {
const minimistOpts = {
string: ["lookback", "relayer"],
string: ["lookback", "relayer", "spokepool"],
};
const args = minimist(argv, minimistOpts);

const { chainId, lookback, relayer = null, maxBlockRange = 10_000 } = args;
const { chainId, lookback, relayer = null, blockrange: maxBlockRange = 10_000 } = args;
assert(Number.isInteger(chainId), "chainId must be numeric ");
assert(Number.isInteger(maxBlockRange), "maxBlockRange must be numeric");
assert(!isDefined(relayer) || ethersUtils.isAddress(relayer), `relayer address is invalid (${relayer})`);

const { quorum = getChainQuorum(chainId) } = args;
assert(Number.isInteger(quorum), "quorum must be numeric ");

let { spokepool: spokePoolAddr } = args;
assert(
!isDefined(spokePoolAddr) || ethersUtils.isAddress(spokePoolAddr),
`Invalid SpokePool address (${spokePoolAddr})`
);

chain = getNetworkName(chainId);

const quorumProvider = await getProvider(chainId);
Expand All @@ -152,16 +159,21 @@ async function run(argv: string[]): Promise<void> {
logger.debug({ at: "RelayerSpokePoolIndexer::run", message: `Skipping lookback on ${chain}.` });
}

const spokePool = getSpokePool(chainId, spokePoolAddr);
if (!isDefined(spokePoolAddr)) {
({ address: spokePoolAddr } = spokePool);
}

const opts = {
quorum,
spokePool: spokePoolAddr,
deploymentBlock,
lookback: latestBlock.number - startBlock,
maxBlockRange,
filterArgs: getEventFilterArgs(relayer),
quorum,
};

logger.debug({ at: "RelayerSpokePoolIndexer::run", message: `Starting ${chain} SpokePool Indexer.`, opts });
const spokePool = await utils.getSpokePoolContract(chainId);

process.on("SIGHUP", () => {
logger.debug({ at: "Relayer#run", message: `Received SIGHUP in ${chain} listener, stopping...` });
Expand Down
20 changes: 16 additions & 4 deletions src/libexec/SpokePoolListenerExperimental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getNodeUrlList,
getOriginFromURL,
getProvider,
getSpokePool,
getRedisCache,
Logger,
winston,
Expand Down Expand Up @@ -161,19 +162,25 @@ async function listen(eventMgr: EventManager, spokePool: Contract, eventNames: s
*/
async function run(argv: string[]): Promise<void> {
const minimistOpts = {
string: ["lookback", "relayer"],
string: ["lookback", "relayer", "spokepool"],
};
const args = minimist(argv, minimistOpts);

({ chainId } = args);
const { lookback, relayer = null, maxBlockRange = 10_000 } = args;
const { lookback, relayer = null, blockrange: maxBlockRange = 10_000 } = args;
assert(Number.isInteger(chainId), "chainId must be numeric ");
assert(Number.isInteger(maxBlockRange), "maxBlockRange must be numeric");
assert(!isDefined(relayer) || ethersUtils.isAddress(relayer), `relayer address is invalid (${relayer})`);

const { quorum = getChainQuorum(chainId) } = args;
assert(Number.isInteger(quorum), "quorum must be numeric ");

let { spokepool: spokePoolAddr } = args;
assert(
!isDefined(spokePoolAddr) || ethersUtils.isAddress(spokePoolAddr),
`Invalid SpokePool address (${spokePoolAddr})`
);

chain = getNetworkName(chainId);

const quorumProvider = await getProvider(chainId);
Expand All @@ -197,16 +204,21 @@ async function run(argv: string[]): Promise<void> {
logger.debug({ at: "RelayerSpokePoolListener::run", message: `Skipping lookback on ${chain}.` });
}

const spokePool = getSpokePool(chainId, spokePoolAddr);
if (!isDefined(spokePoolAddr)) {
({ address: spokePoolAddr } = spokePool);
}

const opts = {
quorum,
spokePool: spokePoolAddr,
deploymentBlock,
lookback: latestBlock.number - startBlock,
maxBlockRange,
filterArgs: getEventFilterArgs(relayer),
quorum,
};

logger.debug({ at: "RelayerSpokePoolListener::run", message: `Starting ${chain} SpokePool Indexer.`, opts });
const spokePool = await utils.getSpokePoolContract(chainId);

process.on("SIGHUP", () => {
logger.debug({ at: "Relayer#run", message: `Received SIGHUP in ${chain} listener, stopping...` });
Expand Down
35 changes: 23 additions & 12 deletions src/utils/ContractUtils.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
import { getNetworkName, Contract, Signer, getDeployedAddress, getDeployedBlockNumber } from ".";

import * as typechain from "@across-protocol/contracts"; // TODO: refactor once we've fixed export from contract repo
import { CHAIN_IDs, getNetworkName, Contract, Signer, getDeployedAddress, getDeployedBlockNumber } from ".";

// Return an ethers contract instance for a deployed contract, imported from the Across-protocol contracts repo.
export function getDeployedContract(contractName: string, networkId: number, signer?: Signer): Contract {
try {
const address = getDeployedAddress(contractName, networkId);
// If the contractName is SpokePool then we need to modify it to find the correct contract factory artifact.
const factoryName = contractName === "SpokePool" ? castSpokePoolName(networkId) : contractName;
const artifact = typechain[`${[factoryName.replace("_", "")]}__factory`];
const factoryName = `${contractName === "SpokePool" ? castSpokePoolName(networkId) : contractName}__factory`;
const artifact = typechain[factoryName];
return new Contract(address, artifact.abi, signer);
} catch (error) {
throw new Error(`Could not find address for contract ${contractName} on ${networkId}`);
throw new Error(`Could not find address for contract ${contractName} on ${networkId} (${error})`);
}
}

// If the name of the contract is SpokePool then we need to apply a transformation on the name to get the correct
// contract factory name. For example, if the network is "mainnet" then the contract is called Ethereum_SpokePool.
export function castSpokePoolName(networkId: number): string {
let networkName = getNetworkName(networkId);
if (networkName == "Mainnet" || networkName == "Rinkeby" || networkName == "Kovan" || networkName == "Goerli") {
return "Ethereum_SpokePool";
let networkName: string;
switch (networkId) {
case CHAIN_IDs.MAINNET:
case CHAIN_IDs.SEPOLIA:
return "Ethereum_SpokePool";
case CHAIN_IDs.ARBITRUM:
return "Arbitrum_SpokePool";
case CHAIN_IDs.ZK_SYNC:
return "ZkSync_SpokePool";
default:
networkName = getNetworkName(networkId);
}

if (networkName.includes("-")) {
networkName = networkName.substring(0, networkName.indexOf("-"));
}
return `${networkName}_SpokePool`;
return `${networkName.replace(" ", "")}_SpokePool`;
}

// For a chain ID and optional SpokePool address, return a Contract instance with the corresponding ABI.
export function getSpokePool(chainId: number, address?: string): Contract {
const factoryName = castSpokePoolName(chainId);
const artifact = typechain[`${factoryName}__factory`];
return new Contract(address ?? getDeployedAddress("SpokePool", chainId), artifact.abi);
james-a-morris marked this conversation as resolved.
Show resolved Hide resolved
}

export function getParamType(contractName: string, functionName: string, paramName: string): string {
Expand Down