Skip to content

Commit

Permalink
validation: validate rpc url and supported eids
Browse files Browse the repository at this point in the history
Signed-off-by: shankar <[email protected]>
  • Loading branch information
shankars99 committed Jan 15, 2025
1 parent c77b9cc commit 2f464d0
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 10 deletions.
90 changes: 90 additions & 0 deletions packages/devtools-move/tasks/evm/utils/validateOmnicontracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { ethers } from 'ethers'

import type { OmniContractMetadataMapping } from './types'

export async function validateOmniContractsOrTerminate(omniContracts: OmniContractMetadataMapping) {
let shouldNotTerminate = true
shouldNotTerminate = shouldNotTerminate && (await validateRpcUrl(omniContracts))
shouldNotTerminate = shouldNotTerminate && (await validateEidSupport(omniContracts))

if (!shouldNotTerminate) {
process.exit(1)
}
return shouldNotTerminate
}

export async function validateRpcUrl(omniContracts: OmniContractMetadataMapping): Promise<boolean> {
let shouldNotTerminate = true
const badRpcUrls: Record<string, string> = {}

for (const [eid, { provider }] of Object.entries(omniContracts)) {
let blockNumber = 0
try {
blockNumber = await getBlockNumber(provider)
} catch (error) {
blockNumber = 0
}

if (blockNumber === 0) {
badRpcUrls[eid] = provider.connection.url
}
}

if (Object.keys(badRpcUrls).length > 0) {
console.error(
`The following EIDs have an invalid RPC URL (block number returned by them is 0):\n${Object.entries(
badRpcUrls
)
.map(([eid, url]) => `${eid}: ${url}`)
.join('\n')}`
)
shouldNotTerminate = false
}

return shouldNotTerminate
}

export async function validateEidSupport(omniContracts: OmniContractMetadataMapping): Promise<boolean> {
let shouldNotTerminate = true
const unsupportedEids: Record<string, string[]> = {}

for (const [eid, { contract, peers }] of Object.entries(omniContracts)) {
const epv2 = contract.epv2

for (const peer of peers) {
const peerEid = peer.eid

let isSupported = false
try {
isSupported = await epv2.isSupportedEid(peerEid)
} catch (error) {
if (!unsupportedEids[eid]) {
unsupportedEids[eid] = []
}
isSupported = false
}

if (!isSupported) {
unsupportedEids[eid].push(peerEid)
}
}
}

if (Object.keys(unsupportedEids).length > 0) {
console.error(
'The following EIDs are not supported by the EPV2 contract (endpointAddress::isSupportedEid(u32))'
)
for (const [eid, badEids] of Object.entries(unsupportedEids)) {
console.error(
`EID: ${eid}\t EndpointV2: ${omniContracts[eid].contract.epv2.address}\t Unsupported EIDs: ${badEids.join(', ')}`
)
}
shouldNotTerminate = false
}

return shouldNotTerminate
}

export async function getBlockNumber(provider: ethers.providers.JsonRpcProvider): Promise<number> {
return await provider.getBlockNumber()
}
21 changes: 11 additions & 10 deletions packages/devtools-move/tasks/evm/wire-evm.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
import fs from 'fs'

import { Contract, ethers } from 'ethers'

import { getDeploymentAddressAndAbi } from '@layerzerolabs/lz-evm-sdk-v2'
import { OAppOmniGraphHardhat } from '@layerzerolabs/toolbox-hardhat'
import { getNetworkForChainId, ChainType } from '@layerzerolabs/lz-definitions'

import { createEidToNetworkMapping, getConfigConnectionsFromChainType, getHHAccountConfig } from '../shared/utils'
import { basexToBytes32 } from '../shared/basexToBytes32'

import AnvilForkNode from './utils/anvilForkNode'
import { createSetDelegateTransactions } from './wire/setDelegate'
import { createSetEnforcedOptionsTransactions } from './wire/setEnforcedOptions'
import { createSetPeerTransactions } from './wire/setPeer'
Expand All @@ -16,13 +12,17 @@ import { createSetReceiveLibraryTransactions } from './wire/setReceiveLibrary'
import { createSetSendConfigTransactions } from './wire/setSendConfig'
import { createSetSendLibraryTransactions } from './wire/setSendLibrary'
import { executeTransactions } from './wire/transactionExecutor'
import { createSetReceiveLibraryTimeoutTransactions } from './wire/setReceiveLibraryTimeout'

import AnvilForkNode from './utils/anvilForkNode'
import { createEidToNetworkMapping, getConfigConnectionsFromChainType, getHHAccountConfig } from '../shared/utils'
import { basexToBytes32 } from '../shared/basexToBytes32'
import type { OmniContractMetadataMapping, TxEidMapping } from './utils/types'
import { validateOmniContractsOrTerminate } from './utils/validateOmnicontracts'

import fs from 'fs'
import path from 'path'
import dotenv from 'dotenv'
import { getNetworkForChainId, ChainType } from '@layerzerolabs/lz-definitions'
import { OAppOmniGraphHardhat } from '@layerzerolabs/toolbox-hardhat'
import { createSetReceiveLibraryTimeoutTransactions } from './wire/setReceiveLibraryTimeout'

/**
* @description Handles wiring of EVM contracts with the Aptos OApp
Expand Down Expand Up @@ -118,6 +118,7 @@ async function wireEvm(args: any) {
const privateKey = readPrivateKey(args)

const omniContracts = await createEvmOmniContracts(args, privateKey, ChainType.EVM, true)
await validateOmniContractsOrTerminate(omniContracts)

// Build a Transaction mapping for each type of transaction. It is further indexed by the eid.
const TxTypeEidMapping: TxEidMapping = {
Expand Down Expand Up @@ -149,7 +150,7 @@ async function wireEvm(args: any) {
let anvilForkNode: AnvilForkNode | null = null
try {
anvilForkNode = new AnvilForkNode(rpcUrlSelfMap, 8545)
console.log(args)

if (args.simulate === 'true') {
const forkRpcMap = await anvilForkNode.startNodes()
await executeTransactions(omniContracts, TxTypeEidMapping, forkRpcMap, 'dry-run', privateKey, args)
Expand Down

0 comments on commit 2f464d0

Please sign in to comment.