Skip to content

Commit

Permalink
🗞️ Add --assert mode (LayerZero-Labs#627)
Browse files Browse the repository at this point in the history
  • Loading branch information
janjakubnanista authored Jun 11, 2024
1 parent d306a7c commit 87ed6f5
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/poor-terms-leave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@layerzerolabs/ua-devtools-evm-hardhat-test": patch
"@layerzerolabs/ua-devtools-evm-hardhat": patch
"@layerzerolabs/toolbox-hardhat": patch
---

Add --assert flag to lz:oapp:wire task
24 changes: 24 additions & 0 deletions packages/ua-devtools-evm-hardhat/src/tasks/oapp/wire/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ interface TaskArgs {
logLevel?: string
ci?: boolean
dryRun?: boolean
assert?: boolean
safe?: boolean
signer?: SignerDefinition
/**
Expand Down Expand Up @@ -56,6 +57,7 @@ const action: ActionType<TaskArgs> = async (
logLevel = 'info',
ci = false,
dryRun = false,
assert = false,
safe = false,
signer,
loadConfigSubtask = SUBTASK_LZ_OAPP_CONFIG_LOAD,
Expand All @@ -72,6 +74,12 @@ const action: ActionType<TaskArgs> = async (
// And we'll create a logger for ourselves
const logger = createLogger()

if (assert) {
logger.info(`Running in assertion mode`)
} else if (dryRun) {
logger.info(`Running in dry run mode`)
}

// Now we can load and validate the config
logger.debug(`Using ${loadConfigSubtask} subtask to load the config`)
const graph: OAppOmniGraph = await hre.run(loadConfigSubtask, {
Expand Down Expand Up @@ -99,6 +107,18 @@ const action: ActionType<TaskArgs> = async (
logger.info(`The OApp is wired, no action is necessary`)

return [[], [], []]
} else if (assert) {
// If we are in assertion mode, we'll print out the transactions and exit with code 1
// if there is anything left to configure
logger.error(`The OApp is not fully wired, following transactions are necessary:`)

// Print the outstanding transactions
printRecords(transactions.map(formatOmniTransaction))

// And set the exit code to failure
process.exitCode = process.exitCode || 1

return [[], [], transactions]
}

// If we are in dry run mode, we'll just print the transactions and exit
Expand Down Expand Up @@ -166,5 +186,9 @@ task(TASK_LZ_OAPP_WIRE, 'Wire LayerZero OApp', action)
)
.addFlag('ci', 'Continuous integration (non-interactive) mode. Will not ask for any input from the user')
.addFlag('dryRun', 'Will not execute any transactions')
.addFlag(
'assert',
'Will not execute any transactions and fail if there are any transactions required to configure the OApp'
)
.addFlag('safe', 'Use gnosis safe to sign transactions')
.addParam('signer', 'Index or address of signer', undefined, types.signer, true)
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
createConnectedContractFactory,
createSignerFactory,
createGnosisSignerFactory,
formatOmniTransaction,
} from '@layerzerolabs/devtools-evm-hardhat'
import { printLogo } from '@layerzerolabs/io-devtools/swag'
import { printLogo, printRecords } from '@layerzerolabs/io-devtools/swag'
import { type SignAndSendResult } from '@layerzerolabs/devtools'
import type { SignAndSendTaskArgs } from '@layerzerolabs/devtools-evm-hardhat/tasks'
import { OwnableOmniGraphHardhatSchema } from '@/ownable'
Expand All @@ -23,12 +24,14 @@ interface TaskArgs {
oappConfig: string
logLevel?: string
ci?: boolean
dryRun?: boolean
assert?: boolean
safe?: boolean
signer?: SignerDefinition
}

const action: ActionType<TaskArgs> = async (
{ oappConfig: oappConfigPath, logLevel = 'info', ci = false, safe = false, signer },
{ oappConfig: oappConfigPath, logLevel = 'info', ci = false, dryRun = false, assert = false, safe = false, signer },
hre
): Promise<SignAndSendResult> => {
printLogo()
Expand All @@ -39,6 +42,12 @@ const action: ActionType<TaskArgs> = async (
// And we'll create a logger for ourselves
const logger = createLogger()

if (assert) {
logger.info(`Running in assertion mode`)
} else if (dryRun) {
logger.info(`Running in dry run mode`)
}

// Now we load the graph
const graph: OwnableOmniGraph = await hre.run(SUBTASK_LZ_OAPP_CONFIG_LOAD, {
configPath: oappConfigPath,
Expand All @@ -61,6 +70,25 @@ const action: ActionType<TaskArgs> = async (
logger.info(`The ownership is correct, no action is necessary`)

return [[], [], []]
} else if (assert) {
// If we are in assertion mode, we'll print out the transactions and exit with code 1
// if there is anything left to configure
logger.error(`The ownership is not fully transferred, following transactions are necessary:`)

// Print the outstanding transactions
printRecords(transactions.map(formatOmniTransaction))

// And set the exit code to failure
process.exitCode = process.exitCode || 1

return [[], [], transactions]
}

// If we are in dry run mode, we'll just print the transactions and exit
if (dryRun) {
printRecords(transactions.map(formatOmniTransaction))

return [[], [], transactions]
}

// Tell the user about the transactions
Expand Down Expand Up @@ -96,4 +124,9 @@ task(TASK_LZ_OWNABLE_TRANSFER_OWNERSHIP, 'Transfer ownable contract ownership',
.addParam('logLevel', 'Logging level. One of: error, warn, info, verbose, debug, silly', 'info', types.logLevel)
.addFlag('ci', 'Continuous integration (non-interactive) mode. Will not ask for any input from the user')
.addFlag('safe', 'Use gnosis safe to sign transactions')
.addFlag('dryRun', 'Will not execute any transactions')
.addFlag(
'assert',
'Will not execute any transactions and fail if there are any transactions required to transfer the ownership'
)
.addParam('signer', 'Index or address of signer', undefined, types.signer, true)
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/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

# Deploy everything first
spawn npx hardhat lz:deploy --tags CustomOApp --ci

# Wait for the deployment to finish
expect "Deploying..."
expect "Your contracts are now deployed"
expect eof

# Now run the custom configuration
spawn npx hardhat lz:oapp:wire \
--oapp-config layerzero.config.with-custom-configuration.ts \
--assert

# We check that we got an error message
expect "The OApp is not fully wired, following transactions are necessary:"

# We'll grab the exit code from wait
#
# wait returns a list of four integers when the spawned process terminates
lassign [wait] pid spawn_id os_error wire_exit_code

# And we check that the exit code we got is 1
if {$wire_exit_code != 1} {
puts "Wiring script exited with code $wire_exit_code, expected exit code 1"

exit 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,36 @@ describe(`task ${TASK_LZ_OWNABLE_TRANSFER_OWNERSHIP}`, () => {
expect(promptToContinueMock).toHaveBeenCalledTimes(2)
})

it('should return a list of pending transactions if running in dry run mode', async () => {
const oappConfig = configPathFixture('valid.config.with-owners.js')

const [successful, errors, pending] = await hre.run(TASK_LZ_OWNABLE_TRANSFER_OWNERSHIP, {
oappConfig,
dryRun: true,
})

expect(successful).toEqual([])
expect(errors).toEqual([])
expect(pending).toHaveLength(2)
expect(promptToContinueMock).not.toHaveBeenCalled()
})

it('should set a failure exit code and return a list of pending transactions if running in assert mode', async () => {
const oappConfig = configPathFixture('valid.config.with-owners.js')

const [successful, errors, pending] = await hre.run(TASK_LZ_OWNABLE_TRANSFER_OWNERSHIP, {
oappConfig,
assert: true,
})

expect(successful).toEqual([])
expect(errors).toEqual([])
expect(pending).toHaveLength(2)
expect(promptToContinueMock).not.toHaveBeenCalled()

expect(process.exitCode).toBe(1)
})

it('should return a list of transactions if the user decides to continue', async () => {
const oappConfig = configPathFixture('valid.config.with-owners.js')

Expand Down
19 changes: 19 additions & 0 deletions tests/ua-devtools-evm-hardhat-test/test/task/oapp/wire.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ describe(`task ${TASK_LZ_OAPP_WIRE}`, () => {
expect(promptToContinueMock).not.toHaveBeenCalled()
})

it('should set a failure exit code and return a list of pending transactions if running in assert mode', async () => {
const oappConfig = configPathFixture('valid.config.connected.js')

const [successful, errors, pending] = await hre.run(TASK_LZ_OAPP_WIRE, { oappConfig, assert: true })

expect(successful).toEqual([])
expect(errors).toEqual([])
expect(pending).toHaveLength(2)
expect(promptToContinueMock).not.toHaveBeenCalled()

expect(process.exitCode).toBe(1)
})

it('should return a list of transactions if the user decides to continue', async () => {
const oappConfig = configPathFixture('valid.config.connected.js')

Expand Down Expand Up @@ -547,5 +560,11 @@ describe(`task ${TASK_LZ_OAPP_WIRE}`, () => {

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

it('should exit with code 1 if in assert mode', async () => {
const result = runExpect('assert')

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

0 comments on commit 87ed6f5

Please sign in to comment.