diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5235c9dd16..7f491f8f11 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -126,10 +126,15 @@ jobs: fail-fast: false matrix: test: + # Core Commands - core-apply + - core-check - core-deploy + - core-init - core-read + # Other commands - relay + # Warp Commands - warp-init - warp-read - warp-apply diff --git a/typescript/cli/src/commands/core.ts b/typescript/cli/src/commands/core.ts index a75f6ae61e..51801d9db0 100644 --- a/typescript/cli/src/commands/core.ts +++ b/typescript/cli/src/commands/core.ts @@ -35,7 +35,6 @@ import { fromAddressCommandOption, inputFileCommandOption, outputFileCommandOption, - skipConfirmationOption, } from './options.js'; /** @@ -117,7 +116,6 @@ export const deploy: CommandModuleWithWriteContext<{ ), 'dry-run': dryRunCommandOption, 'from-address': fromAddressCommandOption, - 'skip-confirmation': skipConfirmationOption, }, handler: async ({ context, chain, config: configFilePath, dryRun }) => { logCommandHeader(`Hyperlane Core deployment${dryRun ? ' dry-run' : ''}`); diff --git a/typescript/cli/src/tests/commands/core.ts b/typescript/cli/src/tests/commands/core.ts index 5feabbe2cb..e1cbe0cb59 100644 --- a/typescript/cli/src/tests/commands/core.ts +++ b/typescript/cli/src/tests/commands/core.ts @@ -1,11 +1,45 @@ -import { $ } from 'zx'; +import { $, ProcessPromise } from 'zx'; import { DerivedCoreConfig } from '@hyperlane-xyz/sdk'; +import { Address } from '@hyperlane-xyz/utils'; import { readYamlOrJson } from '../../utils/files.js'; import { ANVIL_KEY, REGISTRY_PATH } from './helpers.js'; +/** + * Deploys the Hyperlane core contracts to the specified chain using the provided config. + */ +export function hyperlaneCoreDeployRaw( + coreInputPath: string, + privateKey?: string, + skipConfirmationPrompts?: boolean, + hypKey?: string, +): ProcessPromise { + if (hypKey) { + return $`HYP_KEY=${hypKey} yarn workspace @hyperlane-xyz/cli run hyperlane core deploy \ + --registry ${REGISTRY_PATH} \ + --config ${coreInputPath} \ + --verbosity debug \ + ${skipConfirmationPrompts ? '--yes' : ''}`; + } + + if (privateKey) { + return $`yarn workspace @hyperlane-xyz/cli run hyperlane core deploy \ + --registry ${REGISTRY_PATH} \ + --config ${coreInputPath} \ + --key ${privateKey} \ + --verbosity debug \ + ${skipConfirmationPrompts ? '--yes' : ''}`; + } + + return $`yarn workspace @hyperlane-xyz/cli run hyperlane core deploy \ + --registry ${REGISTRY_PATH} \ + --config ${coreInputPath} \ + --verbosity debug \ + ${skipConfirmationPrompts ? '--yes' : ''}`; +} + /** * Deploys the Hyperlane core contracts to the specified chain using the provided config. */ @@ -34,6 +68,68 @@ export async function hyperlaneCoreRead(chain: string, coreOutputPath: string) { --yes`; } +/** + * Verifies that a Hyperlane core deployment matches the provided config on the specified chain. + */ +export function hyperlaneCoreCheck( + chain: string, + coreOutputPath: string, + mailbox?: Address, +): ProcessPromise { + if (mailbox) { + return $`yarn workspace @hyperlane-xyz/cli run hyperlane core check \ + --registry ${REGISTRY_PATH} \ + --config ${coreOutputPath} \ + --chain ${chain} \ + --mailbox ${mailbox} \ + --verbosity debug \ + --yes`; + } + + return $`yarn workspace @hyperlane-xyz/cli run hyperlane core check \ + --registry ${REGISTRY_PATH} \ + --config ${coreOutputPath} \ + --chain ${chain} \ + --verbosity debug \ + --yes`; +} + +/** + * Creates a Hyperlane core deployment config + */ +export function hyperlaneCoreInit( + coreOutputPath: string, + privateKey?: string, + hyp_key?: string, +): ProcessPromise { + if (hyp_key) { + return $`${ + hyp_key ? `HYP_KEY=${hyp_key}` : '' + } yarn workspace @hyperlane-xyz/cli run hyperlane core init \ + --registry ${REGISTRY_PATH} \ + --config ${coreOutputPath} \ + --verbosity debug \ + --yes`; + } + + if (privateKey) { + return $`${ + hyp_key ? 'HYP_KEY=${hyp_key}' : '' + } yarn workspace @hyperlane-xyz/cli run hyperlane core init \ + --registry ${REGISTRY_PATH} \ + --config ${coreOutputPath} \ + --verbosity debug \ + --key ${privateKey} \ + --yes`; + } + + return $`yarn workspace @hyperlane-xyz/cli run hyperlane core init \ + --registry ${REGISTRY_PATH} \ + --config ${coreOutputPath} \ + --verbosity debug \ + --yes`; +} + /** * Updates a Hyperlane core deployment on the specified chain using the provided config. */ diff --git a/typescript/cli/src/tests/commands/helpers.ts b/typescript/cli/src/tests/commands/helpers.ts index 66f8f2e7f4..7fdb407324 100644 --- a/typescript/cli/src/tests/commands/helpers.ts +++ b/typescript/cli/src/tests/commands/helpers.ts @@ -1,5 +1,5 @@ import { ethers } from 'ethers'; -import { $, ProcessPromise } from 'zx'; +import { $, ProcessOutput, ProcessPromise } from 'zx'; import { ERC20Test__factory, ERC4626Test__factory } from '@hyperlane-xyz/core'; import { ChainAddresses } from '@hyperlane-xyz/registry'; @@ -34,6 +34,7 @@ export const CHAIN_NAME_3 = 'anvil3'; export const EXAMPLES_PATH = './examples'; export const CORE_CONFIG_PATH = `${EXAMPLES_PATH}/core-config.yaml`; +export const CORE_CONFIG_PATH_2 = `${TEMP_PATH}/${CHAIN_NAME_2}/core-config.yaml`; export const CORE_READ_CONFIG_PATH_2 = `${TEMP_PATH}/${CHAIN_NAME_2}/core-config-read.yaml`; export const CHAIN_2_METADATA_PATH = `${REGISTRY_PATH}/chains/${CHAIN_NAME_2}/metadata.yaml`; export const CHAIN_3_METADATA_PATH = `${REGISTRY_PATH}/chains/${CHAIN_NAME_3}/metadata.yaml`; @@ -60,23 +61,89 @@ export async function asyncStreamInputWrite( await sleep(500); } -export async function selectAnvil2AndAnvil3( - stream: ProcessPromise, -): Promise { - // Scroll down through the mainnet chains list and select anvil2 - await asyncStreamInputWrite( - stream.stdin, - `${KeyBoardKeys.ARROW_DOWN.repeat(3)}${KeyBoardKeys.TAB}`, - ); - // Scroll down through the mainnet chains list again and select anvil3 - await asyncStreamInputWrite( - stream.stdin, - `${KeyBoardKeys.ARROW_DOWN.repeat(2)}${KeyBoardKeys.TAB}${ - KeyBoardKeys.ENTER - }`, - ); +export type TestPromptAction = { + check: (currentOutput: string) => boolean; + input: string; +}; + +/** + * Takes a {@link ProcessPromise} and a list of inputs that will be supplied + * in the provided order when the check in the {@link TestPromptAction} matches the output + * of the {@link ProcessPromise}. + */ +export async function handlePrompts( + processPromise: Readonly, + actions: TestPromptAction[], +): Promise { + let expectedStep = 0; + for await (const out of processPromise.stdout) { + const currentLine: string = out.toString(); + + const currentAction = actions[expectedStep]; + if (currentAction && currentAction.check(currentLine)) { + // Select mainnet chains + await asyncStreamInputWrite(processPromise.stdin, currentAction.input); + expectedStep++; + } + } + + return processPromise; } +export const SELECT_ANVIL_2_FROM_MULTICHAIN_PICKER = `${KeyBoardKeys.ARROW_DOWN.repeat( + 3, +)}${KeyBoardKeys.TAB}`; + +export const SELECT_ANVIL_3_AFTER_ANVIL_2_FROM_MULTICHAIN_PICKER = `${KeyBoardKeys.ARROW_DOWN.repeat( + 2, +)}${KeyBoardKeys.TAB}`; + +export const SELECT_MAINNET_CHAIN_TYPE_STEP: TestPromptAction = { + check: (currentOutput: string) => + currentOutput.includes('Select network type'), + // Select mainnet chains + input: KeyBoardKeys.ENTER, +}; + +export const SELECT_ANVIL_2_AND_ANVIL_3_STEPS: ReadonlyArray = + [ + { + check: (currentOutput: string) => + currentOutput.includes('--Mainnet Chains--'), + input: `${SELECT_ANVIL_2_FROM_MULTICHAIN_PICKER}`, + }, + { + check: (currentOutput: string) => + currentOutput.includes('--Mainnet Chains--'), + input: `${SELECT_ANVIL_3_AFTER_ANVIL_2_FROM_MULTICHAIN_PICKER}${KeyBoardKeys.ENTER}`, + }, + ]; + +export const CONFIRM_DETECTED_OWNER_STEP: Readonly = { + check: (currentOutput: string) => + currentOutput.includes('Detected owner address as'), + input: KeyBoardKeys.ENTER, +}; + +export const SETUP_CHAIN_SIGNERS_MANUALLY_STEPS: ReadonlyArray = + [ + { + check: (currentOutput) => + currentOutput.includes('Please enter the private key for chain'), + input: `${ANVIL_KEY}${KeyBoardKeys.ENTER}`, + }, + { + check: (currentOutput) => + currentOutput.includes('Please enter the private key for chain'), + input: `${ANVIL_KEY}${KeyBoardKeys.ENTER}`, + }, + { + check: (currentOutput) => + currentOutput.includes('Please enter the private key for chain'), + input: `${ANVIL_KEY}${KeyBoardKeys.ENTER}`, + }, + ]; + /** * Retrieves the deployed Warp address from the Warp core config. */ diff --git a/typescript/cli/src/tests/core-deploy.e2e-test.ts b/typescript/cli/src/tests/core-deploy.e2e-test.ts deleted file mode 100644 index 192f0121b4..0000000000 --- a/typescript/cli/src/tests/core-deploy.e2e-test.ts +++ /dev/null @@ -1,108 +0,0 @@ -import { expect } from 'chai'; -import { Signer, Wallet, ethers } from 'ethers'; - -import { - ChainMetadata, - CoreConfig, - ProtocolFeeHookConfig, - randomAddress, -} from '@hyperlane-xyz/sdk'; -import { Address } from '@hyperlane-xyz/utils'; - -import { readYamlOrJson, writeYamlOrJson } from '../utils/files.js'; - -import { hyperlaneCoreDeploy, readCoreConfig } from './commands/core.js'; -import { - ANVIL_KEY, - CHAIN_2_METADATA_PATH, - CHAIN_NAME_2, - CORE_CONFIG_PATH, - CORE_READ_CONFIG_PATH_2, - DEFAULT_E2E_TEST_TIMEOUT, -} from './commands/helpers.js'; - -describe('hyperlane core deploy e2e tests', async function () { - this.timeout(DEFAULT_E2E_TEST_TIMEOUT); - - let signer: Signer; - let initialOwnerAddress: Address; - - before(async () => { - const chainMetadata: ChainMetadata = readYamlOrJson(CHAIN_2_METADATA_PATH); - - const provider = new ethers.providers.JsonRpcProvider( - chainMetadata.rpcUrls[0].http, - ); - - const wallet = new Wallet(ANVIL_KEY); - signer = wallet.connect(provider); - - initialOwnerAddress = await signer.getAddress(); - }); - - it('should create a core deployment with the signer as the mailbox owner', async () => { - await hyperlaneCoreDeploy(CHAIN_NAME_2, CORE_CONFIG_PATH); - - const coreConfig: CoreConfig = await readCoreConfig( - CHAIN_NAME_2, - CORE_READ_CONFIG_PATH_2, - ); - - expect(coreConfig.owner).to.equal(initialOwnerAddress); - expect(coreConfig.proxyAdmin?.owner).to.equal(initialOwnerAddress); - // Assuming that the ProtocolFeeHook is used for deployment - expect((coreConfig.requiredHook as ProtocolFeeHookConfig).owner).to.equal( - initialOwnerAddress, - ); - }); - - it('should create a core deployment with the mailbox owner set to the address in the config', async () => { - const coreConfig: CoreConfig = await readYamlOrJson(CORE_CONFIG_PATH); - - const newOwner = randomAddress().toLowerCase(); - - coreConfig.owner = newOwner; - writeYamlOrJson(CORE_READ_CONFIG_PATH_2, coreConfig); - - // Deploy the core contracts with the updated mailbox owner - await hyperlaneCoreDeploy(CHAIN_NAME_2, CORE_READ_CONFIG_PATH_2); - - // Verify that the owner has been set correctly without modifying any other owner values - const updatedConfig: CoreConfig = await readCoreConfig( - CHAIN_NAME_2, - CORE_READ_CONFIG_PATH_2, - ); - - expect(updatedConfig.owner.toLowerCase()).to.equal(newOwner); - expect(updatedConfig.proxyAdmin?.owner).to.equal(initialOwnerAddress); - // Assuming that the ProtocolFeeHook is used for deployment - expect( - (updatedConfig.requiredHook as ProtocolFeeHookConfig).owner, - ).to.equal(initialOwnerAddress); - }); - - it('should create a core deployment with ProxyAdmin owner of the mailbox set to the address in the config', async () => { - const coreConfig: CoreConfig = await readYamlOrJson(CORE_CONFIG_PATH); - - const newOwner = randomAddress().toLowerCase(); - - coreConfig.proxyAdmin = { owner: newOwner }; - writeYamlOrJson(CORE_READ_CONFIG_PATH_2, coreConfig); - - // Deploy the core contracts with the updated mailbox owner - await hyperlaneCoreDeploy(CHAIN_NAME_2, CORE_READ_CONFIG_PATH_2); - - // Verify that the owner has been set correctly without modifying any other owner values - const updatedConfig: CoreConfig = await readCoreConfig( - CHAIN_NAME_2, - CORE_READ_CONFIG_PATH_2, - ); - - expect(updatedConfig.owner).to.equal(initialOwnerAddress); - expect(updatedConfig.proxyAdmin?.owner.toLowerCase()).to.equal(newOwner); - // Assuming that the ProtocolFeeHook is used for deployment - expect( - (updatedConfig.requiredHook as ProtocolFeeHookConfig).owner, - ).to.equal(initialOwnerAddress); - }); -}); diff --git a/typescript/cli/src/tests/core-apply.e2e-test.ts b/typescript/cli/src/tests/core/core-apply.e2e-test.ts similarity index 98% rename from typescript/cli/src/tests/core-apply.e2e-test.ts rename to typescript/cli/src/tests/core/core-apply.e2e-test.ts index 27667fd719..c062d5b43b 100644 --- a/typescript/cli/src/tests/core-apply.e2e-test.ts +++ b/typescript/cli/src/tests/core/core-apply.e2e-test.ts @@ -11,13 +11,12 @@ import { } from '@hyperlane-xyz/sdk'; import { Address, Domain, addressToBytes32 } from '@hyperlane-xyz/utils'; -import { readYamlOrJson, writeYamlOrJson } from '../utils/files.js'; - +import { readYamlOrJson, writeYamlOrJson } from '../../utils/files.js'; import { hyperlaneCoreApply, hyperlaneCoreDeploy, readCoreConfig, -} from './commands/core.js'; +} from '../commands/core.js'; import { ANVIL_KEY, CHAIN_2_METADATA_PATH, @@ -28,7 +27,7 @@ import { CORE_READ_CONFIG_PATH_2, DEFAULT_E2E_TEST_TIMEOUT, TEMP_PATH, -} from './commands/helpers.js'; +} from '../commands/helpers.js'; const CORE_READ_CHAIN_2_CONFIG_PATH = `${TEMP_PATH}/${CHAIN_NAME_2}/core-config-read.yaml`; const CORE_READ_CHAIN_3_CONFIG_PATH = `${TEMP_PATH}/${CHAIN_NAME_3}/core-config-read.yaml`; diff --git a/typescript/cli/src/tests/core/core-check.e2e-test.ts b/typescript/cli/src/tests/core/core-check.e2e-test.ts new file mode 100644 index 0000000000..f1dd609914 --- /dev/null +++ b/typescript/cli/src/tests/core/core-check.e2e-test.ts @@ -0,0 +1,93 @@ +import { expect } from 'chai'; +import { $ } from 'zx'; + +import { randomAddress } from '@hyperlane-xyz/sdk'; + +import { writeYamlOrJson } from '../../utils/files.js'; +import { + hyperlaneCoreCheck, + hyperlaneCoreDeploy, + readCoreConfig, +} from '../commands/core.js'; +import { + ANVIL_KEY, + CHAIN_NAME_2, + CORE_CONFIG_PATH, + CORE_READ_CONFIG_PATH_2, + DEFAULT_E2E_TEST_TIMEOUT, + REGISTRY_PATH, + deployOrUseExistingCore, +} from '../commands/helpers.js'; + +describe('hyperlane core check e2e tests', async function () { + this.timeout(2 * DEFAULT_E2E_TEST_TIMEOUT); + + before(async () => { + await deployOrUseExistingCore(CHAIN_NAME_2, CORE_CONFIG_PATH, ANVIL_KEY); + }); + + it('should throw an error if the --chain param is not provided', async () => { + const wrongCommand = + $`yarn workspace @hyperlane-xyz/cli run hyperlane core check \ + --registry ${REGISTRY_PATH} \ + --config ${CORE_CONFIG_PATH} \ + --verbosity debug \ + --yes`.nothrow(); + + const output = await wrongCommand; + + expect(output.exitCode).to.equal(1); + expect(output.text().includes('Missing required argument: chain')).to.be + .true; + }); + + it('should successfully run the core check command', async () => { + await readCoreConfig(CHAIN_NAME_2, CORE_READ_CONFIG_PATH_2); + + const output = await hyperlaneCoreCheck( + CHAIN_NAME_2, + CORE_READ_CONFIG_PATH_2, + ); + + expect(output.exitCode).to.equal(0); + expect(output.text().includes('No violations found')).to.be.true; + }); + + it('should find differences between the local and onchain config', async () => { + const coreConfig = await readCoreConfig( + CHAIN_NAME_2, + CORE_READ_CONFIG_PATH_2, + ); + coreConfig.owner = randomAddress(); + writeYamlOrJson(CORE_READ_CONFIG_PATH_2, coreConfig); + const expectedDiffText = `EXPECTED: "${coreConfig.owner}"\n`; + const expectedActualText = `ACTUAL: "0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266"\n`; + + const output = await hyperlaneCoreCheck( + CHAIN_NAME_2, + CORE_READ_CONFIG_PATH_2, + ).nothrow(); + + expect(output.exitCode).to.equal(1); + expect(output.text().includes(expectedDiffText)).to.be.true; + expect(output.text().includes(expectedActualText)).to.be.true; + }); + + it('should successfully check the config when provided with a custom mailbox', async () => { + await hyperlaneCoreDeploy(CHAIN_NAME_2, CORE_CONFIG_PATH); + const coreConfig = await readCoreConfig( + CHAIN_NAME_2, + CORE_READ_CONFIG_PATH_2, + ); + expect(coreConfig.interchainAccountRouter?.mailbox).not.to.be.undefined; + + const output = await hyperlaneCoreCheck( + CHAIN_NAME_2, + CORE_READ_CONFIG_PATH_2, + coreConfig.interchainAccountRouter!.mailbox, + ); + + expect(output.exitCode).to.equal(0); + expect(output.text().includes('No violations found')).to.be.true; + }); +}); diff --git a/typescript/cli/src/tests/core/core-deploy.e2e-test.ts b/typescript/cli/src/tests/core/core-deploy.e2e-test.ts new file mode 100644 index 0000000000..f7544fc8f7 --- /dev/null +++ b/typescript/cli/src/tests/core/core-deploy.e2e-test.ts @@ -0,0 +1,286 @@ +import { expect } from 'chai'; +import { Signer, Wallet, ethers } from 'ethers'; + +import { + ChainMetadata, + CoreConfig, + HookType, + ProtocolFeeHookConfig, + randomAddress, +} from '@hyperlane-xyz/sdk'; +import { Address } from '@hyperlane-xyz/utils'; + +import { readYamlOrJson, writeYamlOrJson } from '../../utils/files.js'; +import { + hyperlaneCoreDeploy, + hyperlaneCoreDeployRaw, + readCoreConfig, +} from '../commands/core.js'; +import { + ANVIL_KEY, + CHAIN_2_METADATA_PATH, + CHAIN_NAME_2, + CORE_CONFIG_PATH, + CORE_READ_CONFIG_PATH_2, + DEFAULT_E2E_TEST_TIMEOUT, + KeyBoardKeys, + SELECT_MAINNET_CHAIN_TYPE_STEP, + SETUP_CHAIN_SIGNERS_MANUALLY_STEPS, + TestPromptAction, + handlePrompts, +} from '../commands/helpers.js'; + +describe('hyperlane core deploy e2e tests', async function () { + this.timeout(DEFAULT_E2E_TEST_TIMEOUT); + + let signer: Signer; + let initialOwnerAddress: Address; + + before(async () => { + const chainMetadata: ChainMetadata = readYamlOrJson(CHAIN_2_METADATA_PATH); + + const provider = new ethers.providers.JsonRpcProvider( + chainMetadata.rpcUrls[0].http, + ); + + const wallet = new Wallet(ANVIL_KEY); + signer = wallet.connect(provider); + + initialOwnerAddress = await signer.getAddress(); + }); + + describe('hyperlane core deploy', () => { + it('should create a core deployment with the signer as the mailbox owner', async () => { + const steps: TestPromptAction[] = [ + ...SETUP_CHAIN_SIGNERS_MANUALLY_STEPS, + SELECT_MAINNET_CHAIN_TYPE_STEP, + { + check: (currentOutput: string) => + currentOutput.includes('--Mainnet Chains--'), + // Scroll down through the mainnet chains list and select anvil2 + input: `${KeyBoardKeys.ARROW_DOWN.repeat(2)}}${KeyBoardKeys.ENTER}`, + }, + { + // When running locally the e2e tests, the chains folder might already have the chain contracts + check: (currentOutput) => + currentOutput.includes('Mailbox already exists at') || + currentOutput.includes('Is this deployment plan correct?'), + input: `yes${KeyBoardKeys.ENTER}`, + }, + { + check: (currentOutput) => + currentOutput.includes('Is this deployment plan correct?'), + input: KeyBoardKeys.ENTER, + }, + ]; + + const output = hyperlaneCoreDeployRaw(CORE_CONFIG_PATH).stdio('pipe'); + + const finalOutput = await handlePrompts(output, steps); + + expect(finalOutput.exitCode).to.equal(0); + + const coreConfig: CoreConfig = await readCoreConfig( + CHAIN_NAME_2, + CORE_READ_CONFIG_PATH_2, + ); + expect(coreConfig.owner).to.equal(initialOwnerAddress); + expect(coreConfig.proxyAdmin?.owner).to.equal(initialOwnerAddress); + // Assuming that the ProtocolFeeHook is used for deployment + const requiredHookConfig = coreConfig.requiredHook as Exclude< + CoreConfig['requiredHook'], + string + >; + expect(requiredHookConfig.type).to.equal(HookType.PROTOCOL_FEE); + expect((requiredHookConfig as ProtocolFeeHookConfig).owner).to.equal( + initialOwnerAddress, + ); + }); + }); + + describe('hyperlane core deploy --yes', () => { + it('should fail if the --chain flag is not provided but the --yes flag is', async () => { + const steps: TestPromptAction[] = [...SETUP_CHAIN_SIGNERS_MANUALLY_STEPS]; + + const output = hyperlaneCoreDeployRaw(CORE_CONFIG_PATH, undefined, true) + .nothrow() + .stdio('pipe'); + + const finalOutput = await handlePrompts(output, steps); + + expect(finalOutput.exitCode).to.equal(1); + expect(finalOutput.text().includes('No chain provided')).to.be.true; + }); + }); + + describe('hyperlane core deploy --key ...', () => { + it('should create a core deployment with the signer as the mailbox owner', async () => { + const steps: TestPromptAction[] = [ + SELECT_MAINNET_CHAIN_TYPE_STEP, + { + check: (currentOutput: string) => + currentOutput.includes('--Mainnet Chains--'), + // Scroll down through the mainnet chains list and select anvil2 + input: `${KeyBoardKeys.ARROW_DOWN.repeat(2)}}${KeyBoardKeys.ENTER}`, + }, + { + // When running locally the e2e tests, the chains folder might already have the chain contracts + check: (currentOutput) => + currentOutput.includes('Mailbox already exists at') || + currentOutput.includes('Is this deployment plan correct?'), + input: `yes${KeyBoardKeys.ENTER}`, + }, + { + check: (currentOutput) => + currentOutput.includes('Is this deployment plan correct?'), + input: KeyBoardKeys.ENTER, + }, + ]; + + const output = hyperlaneCoreDeployRaw(CORE_CONFIG_PATH, ANVIL_KEY).stdio( + 'pipe', + ); + + const finalOutput = await handlePrompts(output, steps); + + expect(finalOutput.exitCode).to.equal(0); + + const coreConfig: CoreConfig = await readCoreConfig( + CHAIN_NAME_2, + CORE_READ_CONFIG_PATH_2, + ); + expect(coreConfig.owner).to.equal(initialOwnerAddress); + expect(coreConfig.proxyAdmin?.owner).to.equal(initialOwnerAddress); + // Assuming that the ProtocolFeeHook is used for deployment + const requiredHookConfig = coreConfig.requiredHook as Exclude< + CoreConfig['requiredHook'], + string + >; + expect(requiredHookConfig.type).to.equal(HookType.PROTOCOL_FEE); + expect((requiredHookConfig as ProtocolFeeHookConfig).owner).to.equal( + initialOwnerAddress, + ); + }); + }); + + describe('HYP_KEY= ... hyperlane core deploy', () => { + it('should create a core deployment with the signer as the mailbox owner', async () => { + const steps: TestPromptAction[] = [ + SELECT_MAINNET_CHAIN_TYPE_STEP, + { + check: (currentOutput: string) => + currentOutput.includes('--Mainnet Chains--'), + // Scroll down through the mainnet chains list and select anvil2 + input: `${KeyBoardKeys.ARROW_DOWN.repeat(2)}}${KeyBoardKeys.ENTER}`, + }, + { + // When running locally the e2e tests, the chains folder might already have the chain contracts + check: (currentOutput) => + currentOutput.includes('Mailbox already exists at') || + currentOutput.includes('Is this deployment plan correct?'), + input: `yes${KeyBoardKeys.ENTER}`, + }, + { + check: (currentOutput) => + currentOutput.includes('Is this deployment plan correct?'), + input: KeyBoardKeys.ENTER, + }, + ]; + + const output = hyperlaneCoreDeployRaw( + CORE_CONFIG_PATH, + undefined, + undefined, + ANVIL_KEY, + ).stdio('pipe'); + + const finalOutput = await handlePrompts(output, steps); + + expect(finalOutput.exitCode).to.equal(0); + + const coreConfig: CoreConfig = await readCoreConfig( + CHAIN_NAME_2, + CORE_READ_CONFIG_PATH_2, + ); + expect(coreConfig.owner).to.equal(initialOwnerAddress); + expect(coreConfig.proxyAdmin?.owner).to.equal(initialOwnerAddress); + // Assuming that the ProtocolFeeHook is used for deployment + const requiredHookConfig = coreConfig.requiredHook as Exclude< + CoreConfig['requiredHook'], + string + >; + expect(requiredHookConfig.type).to.equal(HookType.PROTOCOL_FEE); + expect((requiredHookConfig as ProtocolFeeHookConfig).owner).to.equal( + initialOwnerAddress, + ); + }); + }); + + describe('hyperlane core deploy --yes --key ...', () => { + it('should create a core deployment with the signer as the mailbox owner', async () => { + await hyperlaneCoreDeploy(CHAIN_NAME_2, CORE_CONFIG_PATH); + + const coreConfig: CoreConfig = await readCoreConfig( + CHAIN_NAME_2, + CORE_READ_CONFIG_PATH_2, + ); + + expect(coreConfig.owner).to.equal(initialOwnerAddress); + expect(coreConfig.proxyAdmin?.owner).to.equal(initialOwnerAddress); + // Assuming that the ProtocolFeeHook is used for deployment + expect((coreConfig.requiredHook as ProtocolFeeHookConfig).owner).to.equal( + initialOwnerAddress, + ); + }); + + it('should create a core deployment with the mailbox owner set to the address in the config', async () => { + const coreConfig: CoreConfig = await readYamlOrJson(CORE_CONFIG_PATH); + + const newOwner = randomAddress().toLowerCase(); + + coreConfig.owner = newOwner; + writeYamlOrJson(CORE_READ_CONFIG_PATH_2, coreConfig); + + // Deploy the core contracts with the updated mailbox owner + await hyperlaneCoreDeploy(CHAIN_NAME_2, CORE_READ_CONFIG_PATH_2); + + // Verify that the owner has been set correctly without modifying any other owner values + const updatedConfig: CoreConfig = await readCoreConfig( + CHAIN_NAME_2, + CORE_READ_CONFIG_PATH_2, + ); + + expect(updatedConfig.owner.toLowerCase()).to.equal(newOwner); + expect(updatedConfig.proxyAdmin?.owner).to.equal(initialOwnerAddress); + // Assuming that the ProtocolFeeHook is used for deployment + expect( + (updatedConfig.requiredHook as ProtocolFeeHookConfig).owner, + ).to.equal(initialOwnerAddress); + }); + + it('should create a core deployment with ProxyAdmin owner of the mailbox set to the address in the config', async () => { + const coreConfig: CoreConfig = await readYamlOrJson(CORE_CONFIG_PATH); + + const newOwner = randomAddress().toLowerCase(); + + coreConfig.proxyAdmin = { owner: newOwner }; + writeYamlOrJson(CORE_READ_CONFIG_PATH_2, coreConfig); + + // Deploy the core contracts with the updated mailbox owner + await hyperlaneCoreDeploy(CHAIN_NAME_2, CORE_READ_CONFIG_PATH_2); + + // Verify that the owner has been set correctly without modifying any other owner values + const updatedConfig: CoreConfig = await readCoreConfig( + CHAIN_NAME_2, + CORE_READ_CONFIG_PATH_2, + ); + + expect(updatedConfig.owner).to.equal(initialOwnerAddress); + expect(updatedConfig.proxyAdmin?.owner.toLowerCase()).to.equal(newOwner); + // Assuming that the ProtocolFeeHook is used for deployment + expect( + (updatedConfig.requiredHook as ProtocolFeeHookConfig).owner, + ).to.equal(initialOwnerAddress); + }); + }); +}); diff --git a/typescript/cli/src/tests/core/core-init.e2e-test.ts b/typescript/cli/src/tests/core/core-init.e2e-test.ts new file mode 100644 index 0000000000..5c308df4a5 --- /dev/null +++ b/typescript/cli/src/tests/core/core-init.e2e-test.ts @@ -0,0 +1,216 @@ +import { expect } from 'chai'; +import { Wallet } from 'ethers'; + +import { + CoreConfig, + HookType, + MerkleTreeHookConfig, + ProtocolFeeHookConfig, + randomAddress, +} from '@hyperlane-xyz/sdk'; +import { Address, normalizeAddress } from '@hyperlane-xyz/utils'; + +import { readYamlOrJson } from '../../utils/files.js'; +import { hyperlaneCoreInit } from '../commands/core.js'; +import { + ANVIL_KEY, + CONFIRM_DETECTED_OWNER_STEP, + CORE_CONFIG_PATH_2, + DEFAULT_E2E_TEST_TIMEOUT, + KeyBoardKeys, + TestPromptAction, + handlePrompts, +} from '../commands/helpers.js'; + +describe('hyperlane core init e2e tests', async function () { + this.timeout(2 * DEFAULT_E2E_TEST_TIMEOUT); + + function assertCoreInitConfig( + coreConfig: CoreConfig, + owner: Address, + feeHookOwner: Address = owner, + feeHookBeneficiary: Address = feeHookOwner, + ): void { + expect(coreConfig.owner).to.equal(owner); + expect(coreConfig.proxyAdmin?.owner).to.equal(owner); + + const defaultHookConfig = coreConfig.defaultHook as MerkleTreeHookConfig; + expect(defaultHookConfig.type).to.equal(HookType.MERKLE_TREE); + + const requiredHookConfig = coreConfig.requiredHook as ProtocolFeeHookConfig; + expect(requiredHookConfig.type).to.equal(HookType.PROTOCOL_FEE); + expect(normalizeAddress(requiredHookConfig.owner)).to.equal(feeHookOwner); + expect(normalizeAddress(requiredHookConfig.beneficiary)).to.equal( + feeHookBeneficiary, + ); + } + + describe('hyperlane core init', () => { + it('should successfully generate the core contract deployment config', async () => { + const output = hyperlaneCoreInit(CORE_CONFIG_PATH_2).stdio('pipe'); + + const owner = normalizeAddress(randomAddress()); + const feeHookOwner = normalizeAddress(randomAddress()); + const steps: TestPromptAction[] = [ + { + check: (currentOutput) => + currentOutput.includes('Enter the desired owner address:'), + input: `${owner}${KeyBoardKeys.ENTER}`, + }, + { + check: (currentOutput) => + currentOutput.includes( + 'For trusted relayer ISM, enter relayer address:', + ), + input: `${owner}${KeyBoardKeys.ENTER}`, + }, + { + check: (currentOutput) => + currentOutput.includes( + 'For Protocol Fee Hook, enter owner address:', + ), + input: `${feeHookOwner}${KeyBoardKeys.ENTER}`, + }, + { + check: (currentOutput) => + !!currentOutput.match(/Use this same address \((.*?)\) for/), + input: KeyBoardKeys.ENTER, + }, + ]; + + const finalOutput = await handlePrompts(output, steps); + + expect(finalOutput.exitCode).to.equal(0); + + const deploymentCoreConfig: CoreConfig = + readYamlOrJson(CORE_CONFIG_PATH_2); + assertCoreInitConfig(deploymentCoreConfig, owner, feeHookOwner); + }); + }); + + describe('HYP_KEY=... hyperlane core init', () => { + it('should successfully generate the core contract deployment config when confirming owner prompts', async () => { + const owner = new Wallet(ANVIL_KEY).address; + const steps: TestPromptAction[] = [ + CONFIRM_DETECTED_OWNER_STEP, + { + check: (currentOutput) => + !!currentOutput.match(/Use this same address \((.*?)\) for/), + input: KeyBoardKeys.ENTER, + }, + ]; + + const output = hyperlaneCoreInit( + CORE_CONFIG_PATH_2, + undefined, + ANVIL_KEY, + ).stdio('pipe'); + + const finalOutput = await handlePrompts(output, steps); + + expect(finalOutput.exitCode).to.equal(0); + + const deploymentCoreConfig: CoreConfig = + readYamlOrJson(CORE_CONFIG_PATH_2); + assertCoreInitConfig(deploymentCoreConfig, owner); + }); + + it('should successfully generate the core contract deployment config when not confirming owner prompts', async () => { + const owner = new Wallet(ANVIL_KEY).address; + const feeHookOwner = normalizeAddress(randomAddress()); + const steps: TestPromptAction[] = [ + CONFIRM_DETECTED_OWNER_STEP, + { + check: (currentOutput) => + !!currentOutput.match(/Use this same address \((.*?)\) for/), + input: `no${KeyBoardKeys.ENTER}`, + }, + { + check: (currentOutput) => + currentOutput.includes('Enter beneficiary address for'), + input: `${feeHookOwner}${KeyBoardKeys.ENTER}`, + }, + ]; + + const output = hyperlaneCoreInit( + CORE_CONFIG_PATH_2, + undefined, + ANVIL_KEY, + ).stdio('pipe'); + + const finalOutput = await handlePrompts(output, steps); + + expect(finalOutput.exitCode).to.equal(0); + + const deploymentCoreConfig: CoreConfig = + readYamlOrJson(CORE_CONFIG_PATH_2); + assertCoreInitConfig( + deploymentCoreConfig, + owner, + undefined, + feeHookOwner, + ); + }); + }); + + describe('hyperlane core init --key ...', () => { + it('should successfully generate the core contract deployment config when confirming owner prompts', async () => { + const owner = new Wallet(ANVIL_KEY).address; + const steps: TestPromptAction[] = [ + CONFIRM_DETECTED_OWNER_STEP, + { + check: (currentOutput) => + !!currentOutput.match(/Use this same address \((.*?)\) for/), + input: KeyBoardKeys.ENTER, + }, + ]; + + const output = hyperlaneCoreInit(CORE_CONFIG_PATH_2, ANVIL_KEY).stdio( + 'pipe', + ); + + const finalOutput = await handlePrompts(output, steps); + + expect(finalOutput.exitCode).to.equal(0); + + const deploymentCoreConfig: CoreConfig = + readYamlOrJson(CORE_CONFIG_PATH_2); + assertCoreInitConfig(deploymentCoreConfig, owner); + }); + + it('should successfully generate the core contract deployment config when not confirming owner prompts', async () => { + const owner = new Wallet(ANVIL_KEY).address; + const feeHookOwner = normalizeAddress(randomAddress()); + const steps: TestPromptAction[] = [ + CONFIRM_DETECTED_OWNER_STEP, + { + check: (currentOutput) => + !!currentOutput.match(/Use this same address \((.*?)\) for/), + input: `no${KeyBoardKeys.ENTER}`, + }, + { + check: (currentOutput) => + currentOutput.includes('Enter beneficiary address for'), + input: `${feeHookOwner}${KeyBoardKeys.ENTER}`, + }, + ]; + + const output = hyperlaneCoreInit(CORE_CONFIG_PATH_2, ANVIL_KEY).stdio( + 'pipe', + ); + + const finalOutput = await handlePrompts(output, steps); + + expect(finalOutput.exitCode).to.equal(0); + + const deploymentCoreConfig: CoreConfig = + readYamlOrJson(CORE_CONFIG_PATH_2); + assertCoreInitConfig( + deploymentCoreConfig, + owner, + undefined, + feeHookOwner, + ); + }); + }); +}); diff --git a/typescript/cli/src/tests/core-read.e2e-test.ts b/typescript/cli/src/tests/core/core-read.e2e-test.ts similarity index 90% rename from typescript/cli/src/tests/core-read.e2e-test.ts rename to typescript/cli/src/tests/core/core-read.e2e-test.ts index 7390e8dca4..9ed0a3ca01 100644 --- a/typescript/cli/src/tests/core-read.e2e-test.ts +++ b/typescript/cli/src/tests/core/core-read.e2e-test.ts @@ -8,9 +8,8 @@ import { } from '@hyperlane-xyz/sdk'; import { Address } from '@hyperlane-xyz/utils'; -import { readYamlOrJson } from '../utils/files.js'; - -import { hyperlaneCoreDeploy, readCoreConfig } from './commands/core.js'; +import { readYamlOrJson } from '../../utils/files.js'; +import { hyperlaneCoreDeploy, readCoreConfig } from '../commands/core.js'; import { ANVIL_KEY, CHAIN_2_METADATA_PATH, @@ -18,7 +17,7 @@ import { CORE_CONFIG_PATH, CORE_READ_CONFIG_PATH_2, DEFAULT_E2E_TEST_TIMEOUT, -} from './commands/helpers.js'; +} from '../commands/helpers.js'; describe('hyperlane core read e2e tests', async function () { this.timeout(DEFAULT_E2E_TEST_TIMEOUT); diff --git a/typescript/cli/src/tests/warp-init.e2e-test.ts b/typescript/cli/src/tests/warp-init.e2e-test.ts index b3314a30a9..66cfa3bfc7 100644 --- a/typescript/cli/src/tests/warp-init.e2e-test.ts +++ b/typescript/cli/src/tests/warp-init.e2e-test.ts @@ -16,14 +16,18 @@ import { ANVIL_KEY, CHAIN_NAME_2, CHAIN_NAME_3, + CONFIRM_DETECTED_OWNER_STEP, CORE_CONFIG_PATH, DEFAULT_E2E_TEST_TIMEOUT, KeyBoardKeys, + SELECT_ANVIL_2_AND_ANVIL_3_STEPS, + SELECT_ANVIL_2_FROM_MULTICHAIN_PICKER, + SELECT_MAINNET_CHAIN_TYPE_STEP, + TestPromptAction, WARP_CONFIG_PATH_2, - asyncStreamInputWrite, deployOrUseExistingCore, deployToken, - selectAnvil2AndAnvil3, + handlePrompts, } from './commands/helpers.js'; import { hyperlaneWarpInit } from './commands/warp.js'; @@ -67,37 +71,26 @@ describe('hyperlane warp init e2e tests', async function () { } it('it should generate a warp deploy config with a single chain', async function () { - const output = hyperlaneWarpInit(WARP_CONFIG_PATH_2).stdio('pipe'); - - for await (const out of output.stdout) { - const currentLine: string = out.toString(); - - if ( - currentLine.includes('Creating a new warp route deployment config...') - ) { - // Select mainnet chains - await asyncStreamInputWrite(output.stdin, KeyBoardKeys.ENTER); - } else if (currentLine.includes('--Mainnet Chains--')) { + const steps: TestPromptAction[] = [ + SELECT_MAINNET_CHAIN_TYPE_STEP, + { + check: (currentOutput: string) => + currentOutput.includes('--Mainnet Chains--'), // Scroll down through the mainnet chains list and select anvil2 - await asyncStreamInputWrite( - output.stdin, - `${KeyBoardKeys.ARROW_DOWN.repeat(3)}${KeyBoardKeys.TAB}${ - KeyBoardKeys.ENTER - }`, - ); - } else if (currentLine.includes('token type')) { + input: `${SELECT_ANVIL_2_FROM_MULTICHAIN_PICKER}${KeyBoardKeys.ENTER}`, + }, + CONFIRM_DETECTED_OWNER_STEP, + { + check: (currentOutput: string) => + !!currentOutput.match(/Select .+?'s token type/), // Scroll up through the token type list and select native - await asyncStreamInputWrite( - output.stdin, - `${KeyBoardKeys.ARROW_UP.repeat(2)}${KeyBoardKeys.ENTER}`, - ); - } else if (currentLine.includes('Detected owner address as')) { - // Confirm owner prompts - await asyncStreamInputWrite(output.stdin, KeyBoardKeys.ENTER); - } - } - - await output; + input: `${KeyBoardKeys.ARROW_UP.repeat(2)}${KeyBoardKeys.ENTER}`, + }, + ]; + + const output = hyperlaneWarpInit(WARP_CONFIG_PATH_2).stdio('pipe'); + + await handlePrompts(output, steps); const warpConfig: WarpRouteDeployConfig = readYamlOrJson(WARP_CONFIG_PATH_2); @@ -106,31 +99,26 @@ describe('hyperlane warp init e2e tests', async function () { }); it('it should generate a warp deploy config with a 2 chains warp route (native->native)', async function () { + const steps: TestPromptAction[] = [ + SELECT_MAINNET_CHAIN_TYPE_STEP, + ...SELECT_ANVIL_2_AND_ANVIL_3_STEPS, + CONFIRM_DETECTED_OWNER_STEP, + { + check: (currentOutput: string) => + !!currentOutput.match(/Select .+?'s token type/), + input: `${KeyBoardKeys.ARROW_UP.repeat(2)}${KeyBoardKeys.ENTER}`, + }, + CONFIRM_DETECTED_OWNER_STEP, + { + check: (currentOutput: string) => + !!currentOutput.match(/Select .+?'s token type/), + input: `${KeyBoardKeys.ARROW_UP.repeat(2)}${KeyBoardKeys.ENTER}`, + }, + ]; + const output = hyperlaneWarpInit(WARP_CONFIG_PATH_2).stdio('pipe'); - for await (const out of output.stdout) { - const currentLine: string = out.toString(); - - if ( - currentLine.includes('Creating a new warp route deployment config...') - ) { - // Select mainnet chains - await asyncStreamInputWrite(output.stdin, KeyBoardKeys.ENTER); - } else if (currentLine.includes('--Mainnet Chains--')) { - await selectAnvil2AndAnvil3(output); - } else if (currentLine.match(/Select .+?'s token type/)) { - // Scroll up through the token type list and select native - await asyncStreamInputWrite( - output.stdin, - `${KeyBoardKeys.ARROW_UP.repeat(2)}${KeyBoardKeys.ENTER}`, - ); - } else if (currentLine.includes('Detected owner address as')) { - // Confirm owner prompts - await asyncStreamInputWrite(output.stdin, KeyBoardKeys.ENTER); - } - } - - await output; + await handlePrompts(output, steps); const warpConfig: WarpRouteDeployConfig = readYamlOrJson(WARP_CONFIG_PATH_2); @@ -142,46 +130,35 @@ describe('hyperlane warp init e2e tests', async function () { it('it should generate a warp deploy config with a 2 chains warp route (collateral->synthetic)', async function () { const erc20Token = await deployToken(ANVIL_KEY, CHAIN_NAME_2, 6); + const steps: TestPromptAction[] = [ + SELECT_MAINNET_CHAIN_TYPE_STEP, + ...SELECT_ANVIL_2_AND_ANVIL_3_STEPS, + // First chain token config + CONFIRM_DETECTED_OWNER_STEP, + { + check: (currentOutput: string) => + !!currentOutput.match(/Select .+?'s token type/), + // Scroll down through the token type list and select collateral + input: `${KeyBoardKeys.ARROW_DOWN.repeat(4)}${KeyBoardKeys.ENTER}`, + }, + { + check: (currentOutput: string) => + currentOutput.includes('Enter the existing token address on chain'), + input: `${erc20Token.address}${KeyBoardKeys.ENTER}`, + }, + // Other chain token config + CONFIRM_DETECTED_OWNER_STEP, + { + check: (currentOutput: string) => + !!currentOutput.match(/Select .+?'s token type/), + // Select the synthetic token type + input: `${KeyBoardKeys.ENTER}`, + }, + ]; const output = hyperlaneWarpInit(WARP_CONFIG_PATH_2).stdio('pipe'); - let tokenStep = 0; - for await (const out of output.stdout) { - const currentLine: string = out.toString(); - - if ( - currentLine.includes('Creating a new warp route deployment config...') - ) { - // Select mainnet chains - await asyncStreamInputWrite(output.stdin, KeyBoardKeys.ENTER); - } else if (currentLine.includes('--Mainnet Chains--')) { - await selectAnvil2AndAnvil3(output); - } else if ( - currentLine.includes('Enter the existing token address on chain') - ) { - await asyncStreamInputWrite( - output.stdin, - `${erc20Token.address}${KeyBoardKeys.ENTER}`, - ); - } else if (currentLine.match(/Select .+?'s token type/)) { - if (tokenStep === 0) { - // Scroll down through the token type list and select collateral - await asyncStreamInputWrite( - output.stdin, - `${KeyBoardKeys.ARROW_DOWN.repeat(4)}${KeyBoardKeys.ENTER}`, - ); - } else if (tokenStep === 1) { - // Select the synthetic token type - await asyncStreamInputWrite(output.stdin, KeyBoardKeys.ENTER); - } - tokenStep++; - } else if (currentLine.includes('Detected owner address as')) { - // Confirm owner prompts - await asyncStreamInputWrite(output.stdin, KeyBoardKeys.ENTER); - } - } - - await output; + await handlePrompts(output, steps); const warpConfig: WarpRouteDeployConfig = readYamlOrJson(WARP_CONFIG_PATH_2);