Skip to content

Commit

Permalink
🧹 APPS-203: Adding stage and network parameters to validate RPCS task (…
Browse files Browse the repository at this point in the history
  • Loading branch information
ravinagill15 authored Nov 21, 2024
1 parent 5d03563 commit 5b563f0
Show file tree
Hide file tree
Showing 8 changed files with 259 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/fair-snails-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@layerzerolabs/devtools-evm-hardhat-test": patch
"@layerzerolabs/devtools-evm-hardhat": patch
---

Added optional stage and networks arguments to lz:healthcheck:validate:rpcs task
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { ActionType } from 'hardhat/types'
import { task, types } from 'hardhat/config'
import { TASK_LZ_VALIDATE_RPCS } from '@/constants'
import { createLogger, printBoolean } from '@layerzerolabs/io-devtools'
import { assertDefinedNetworks } from '@/internal/assertions'
import { printLogo } from '@layerzerolabs/io-devtools/swag'
import { getEidsByNetworkName } from '@/runtime'
import { BaseProvider, JsonRpcProvider, WebSocketProvider } from '@ethersproject/providers'
import { types as cliTypes } from '@/cli'
import { EndpointId, Stage, endpointIdToStage } from '@layerzerolabs/lz-definitions'
import { pickNetworkConfigs } from '@/simulation'

const RPC_URL_KEY = 'url'

Expand All @@ -22,6 +26,8 @@ const logger = createLogger()

interface TaskArguments {
timeout: number
networks?: string[]
stage?: Stage
}

const getProvider = async (rpcUrl: string, networkName: string): Promise<BaseProvider> => {
Expand All @@ -40,10 +46,36 @@ const getProvider = async (rpcUrl: string, networkName: string): Promise<BasePro
const action: ActionType<TaskArguments> = async (taskArgs, hre) => {
printLogo()

const networks = hre.userConfig.networks || {}
// --stage cannot be used in conjunction with --networks
if (taskArgs.networks != null && taskArgs.stage != null) {
logger.error(
`--stage ${taskArgs.stage} cannot be used in conjunction with --networks ${taskArgs.networks.join(',')}`
)

process.exit(1)
}

// And we create a filtering predicate for the stage argument
const isOnStage =
taskArgs.stage == null ? () => true : (eid: EndpointId) => endpointIdToStage(eid) === taskArgs.stage

// Let's grab the networks that will be validated
const networks = taskArgs.networks
? // Here we need to check whether the networks have been defined in hardhat config
pickNetworkConfigs(assertDefinedNetworks(taskArgs.networks))(hre.config.networks)
: taskArgs.stage // But here we are taking them from hardhat config so no assertion is necessary
? pickNetworkConfigs(
Object.entries(getEidsByNetworkName()).flatMap(([networkName, eid]) =>
eid != null && isOnStage(eid) ? [networkName] : []
)
)(hre.config.networks)
: hre.config.networks

const eidByNetworkName = getEidsByNetworkName(hre)

logger.info(`========== Validating RPC URLs for networks: ${Object.keys(eidByNetworkName)}`)
logger.info(
`========== Validating RPC URLs for networks: ${taskArgs.networks?.join(', ') || Object.keys(eidByNetworkName).join(', ')}`
)

const networksWithInvalidRPCs: string[] = []

Expand Down Expand Up @@ -92,10 +124,13 @@ task(
TASK_LZ_VALIDATE_RPCS,
'Validate RPC URLs in hardhat.config.ts. RPCs are only considered valid if they use the https or wss protocol and respond within the specified timeout.',
action
).addParam(
'timeout',
`Maximum amount of time (in milliseconds) that the RPC URLs have to respond. If unspecified, default timeout of ${TIMEOUT}ms will be used.`,
TIMEOUT,
types.int,
true
)
.addParam(
'timeout',
`Maximum amount of time (in milliseconds) that the RPC URLs have to respond. If unspecified, default timeout of ${TIMEOUT}ms will be used.`,
TIMEOUT,
types.int,
true
)
.addParam('networks', 'Comma-separated list of networks to simulate', undefined, cliTypes.csv, true)
.addParam('stage', 'Chain stage. One of: mainnet, testnet, sandbox', undefined, cliTypes.stage, true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'hardhat-deploy'
import { EndpointId } from '@layerzerolabs/lz-definitions'
import '@layerzerolabs/toolbox-hardhat'
import type { HardhatUserConfig } from 'hardhat/types'

import { default as baseConfig } from './hardhat.config'

const MNEMONIC = process.env.MNEMONIC ?? ''

/**
* This is a dummy hardhat config that enables us to test
* hardhat functionality without mocking too much
*/
const config: HardhatUserConfig = {
...baseConfig,
networks: {
bsc: {
eid: EndpointId.BSC_V2_MAINNET,
url: 'https://404-beepboop.drpc.org',
accounts: {
mnemonic: MNEMONIC,
initialIndex: 0,
},
},
arbSepolia: {
eid: EndpointId.ARBSEP_V2_TESTNET,
url: 'https://arbitrum-sepolia.blockpi.network/v1/rpc/public',
accounts: {
mnemonic: MNEMONIC,
initialIndex: 0,
},
},
},
}

export default config
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Mon Feb 12 16:57:18 2024
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script. It
# necessarily has to guess about certain things. Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts. If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character. This
# pacifies every program I know of. The -c flag makes the script do
# this in the first place. The -C flag allows you to define a
# character to toggle this mode off and on.

set force_conservative 0
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}

set timeout 60

match_max 100000

spawn npx hardhat --config hardhat.config.with-valid-https-rpc.ts lz:healthcheck:validate:rpcs

expect "========== All RPC URLs are valid!"

expect eof
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Mon Feb 12 16:57:18 2024
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script. It
# necessarily has to guess about certain things. Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts. If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character. This
# pacifies every program I know of. The -c flag makes the script do
# this in the first place. The -C flag allows you to define a
# character to toggle this mode off and on.

set force_conservative 0
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}

set timeout 60

match_max 100000

spawn npx hardhat --config hardhat.config.with-valid-and-invalid-rpcs.ts lz:healthcheck:validate:rpcs --networks arbSepolia

expect "========== All RPC URLs are valid!"

expect eof
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Mon Feb 12 16:57:18 2024
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script. It
# necessarily has to guess about certain things. Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts. If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character. This
# pacifies every program I know of. The -c flag makes the script do
# this in the first place. The -C flag allows you to define a
# character to toggle this mode off and on.

set force_conservative 0
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}

set timeout 60

match_max 100000

spawn npx hardhat --config hardhat.config.with-valid-and-invalid-rpcs.ts lz:healthcheck:validate:rpcs --stage testnet

expect "========== All RPC URLs are valid!"

expect eof
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Mon Feb 12 16:57:18 2024
# Expect and autoexpect were both written by Don Libes, NIST.
#
# Note that autoexpect does not guarantee a working script. It
# necessarily has to guess about certain things. Two reasons a script
# might fail are:
#
# 1) timing - A surprising number of programs (rn, ksh, zsh, telnet,
# etc.) and devices discard or ignore keystrokes that arrive "too
# quickly" after prompts. If you find your new script hanging up at
# one spot, try adding a short sleep just before the previous send.
# Setting "force_conservative" to 1 (see below) makes Expect do this
# automatically - pausing briefly before sending each character. This
# pacifies every program I know of. The -c flag makes the script do
# this in the first place. The -C flag allows you to define a
# character to toggle this mode off and on.

set force_conservative 0
if {$force_conservative} {
set send_slow {1 .1}
proc send {ignore arg} {
sleep .1
exp_send -s -- $arg
}
}

set timeout 60

match_max 100000

spawn npx hardhat --config hardhat.config.with-valid-and-invalid-rpcs.ts lz:healthcheck:validate:rpcs --stage testnet --networks arbSepolia

expect "--stage testnet cannot be used in conjunction with --networks arbSepolia"

expect eof
27 changes: 26 additions & 1 deletion tests/devtools-evm-hardhat-test/test/task/validate.rpcs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,39 @@ describe(`task ${TASK_LZ_VALIDATE_RPCS}`, () => {
stdio: 'inherit',
})

it('should not validate RPC URLs for network without eid', async () => {
it('should validate all networks when no arguments are provided', async () => {
const result = runExpect('validate-all-networks')

expect(result.status).toBe(0)
})

it('should validate RPC URLs for specific networks', async () => {
const result = runExpect('validate-for-specific-networks')

expect(result.status).toBe(0)
})

it('should validate RPC URLs for a specific stage', async () => {
const result = runExpect('validate-for-specific-stage')

expect(result.status).toBe(0)
})

it('should not allow both --networks and --stage arguments together', async () => {
const result = runExpect('validate-with-networks-and-stage')

expect(result.status).not.toBe(0)
})

it('should validate RPC URLs for a network without an EID', async () => {
const result = runExpect('validate-rpc-for-network-without-eid')

expect(result.status).toBe(0)
})

it('should validate incorrect https RPC URL', async () => {
const result = runExpect('validate-incorrect-https-rpc')

expect(result.status).toBe(0)
})

Expand Down

0 comments on commit 5b563f0

Please sign in to comment.