From dcc05f45e426082b29988b5e6c9af41be9cacdf8 Mon Sep 17 00:00:00 2001 From: Marko Arambasic Date: Tue, 1 Oct 2024 14:14:06 +0200 Subject: [PATCH] fix: fix runScripts to be executed with node support and override default tasks with node support --- .../src/core/config-env.ts | 27 +++++++ .../src/core/global-tasks.ts | 4 +- packages/hardhat-zksync-deploy/src/index.ts | 17 ++-- packages/hardhat-zksync-node/package.json | 4 +- .../src/core/global-task.ts | 4 +- .../hardhat-zksync-node/src/core/register.ts | 77 +++++++++++++++++++ .../src/core/script-runner.ts | 39 +++------- packages/hardhat-zksync-node/src/index.ts | 3 +- packages/hardhat-zksync-node/src/utils.ts | 3 +- pnpm-lock.yaml | 6 ++ 10 files changed, 140 insertions(+), 44 deletions(-) create mode 100644 packages/hardhat-zksync-deploy/src/core/config-env.ts diff --git a/packages/hardhat-zksync-deploy/src/core/config-env.ts b/packages/hardhat-zksync-deploy/src/core/config-env.ts new file mode 100644 index 000000000..549f39202 --- /dev/null +++ b/packages/hardhat-zksync-deploy/src/core/config-env.ts @@ -0,0 +1,27 @@ +import { ActionType, ConfigurableTaskDefinition, TaskArguments } from 'hardhat/types'; +import { HardhatContext } from 'hardhat/internal/context'; +import { ZKSyncTasksForWrapping, ZKSyncTasksWithWrappedNode } from './global-tasks'; + +export function taskWithEraTestNode( + name: string, + description?: string, + withNode?: boolean, + action?: ActionType, +): ConfigurableTaskDefinition { + const ctx = HardhatContext.getHardhatContext(); + const dsl = ctx.tasksDSL; + + if (withNode) { + if ((global as ZKSyncTasksWithWrappedNode)._zkSyncTasksForWrapping) { + (global as ZKSyncTasksWithWrappedNode)._zkSyncTasksForWrapping = new ZKSyncTasksForWrapping(); + } + + (global as ZKSyncTasksWithWrappedNode)._zkSyncTasksForWrapping.addTask(name); + } + + if (description === undefined) { + return dsl.task(name); + } + + return dsl.task(name, description, action); +} diff --git a/packages/hardhat-zksync-deploy/src/core/global-tasks.ts b/packages/hardhat-zksync-deploy/src/core/global-tasks.ts index 17ab1a8e2..9f35a8057 100644 --- a/packages/hardhat-zksync-deploy/src/core/global-tasks.ts +++ b/packages/hardhat-zksync-deploy/src/core/global-tasks.ts @@ -8,7 +8,9 @@ export type ZKSyncTasksWithWrappedNode = typeof global & { export class ZKSyncTasksForWrapping { public taskNames: string[] = []; - constructor() {} + constructor() { + this.taskNames = []; + } public addTask(taskName: string) { this.taskNames.push(taskName); diff --git a/packages/hardhat-zksync-deploy/src/index.ts b/packages/hardhat-zksync-deploy/src/index.ts index 4b3282eb2..a430e9336 100644 --- a/packages/hardhat-zksync-deploy/src/index.ts +++ b/packages/hardhat-zksync-deploy/src/index.ts @@ -1,4 +1,4 @@ -import { extendConfig, extendEnvironment, task, types } from 'hardhat/config'; +import { extendConfig, extendEnvironment, types } from 'hardhat/config'; import chalk from 'chalk'; import { string } from 'hardhat/internal/core/params/argumentTypes'; import { TASK_DEPLOY_ZKSYNC, TASK_DEPLOY_ZKSYNC_LIBRARIES, TASK_DEPLOY_ZKSYNC_CONTRACT } from './task-names'; @@ -6,13 +6,9 @@ import './type-extensions'; import { deployZkSyncContract, zkSyncDeploy, zkSyncLibraryDeploy } from './task-actions'; import { DEFAULT_DEPLOY_SCRIPTS_PATH, defaultAccountDeployerSettings } from './constants'; import { DeployerExtension } from './deployer-extension'; -import { ZKSyncTasksForWrapping, ZKSyncTasksWithWrappedNode } from './core/global-tasks'; +import { taskWithEraTestNode } from './core/config-env'; export * from './deployer'; -if (!(global as ZKSyncTasksWithWrappedNode)._zkSyncTasksForWrapping) { - (global as ZKSyncTasksWithWrappedNode)._zkSyncTasksForWrapping = new ZKSyncTasksForWrapping(); -} - extendConfig((config, userConfig) => { config.paths.deployPaths = userConfig.paths?.deployPaths ?? DEFAULT_DEPLOY_SCRIPTS_PATH; config.deployerAccounts = { ...defaultAccountDeployerSettings, ...userConfig?.deployerAccounts }; @@ -31,12 +27,12 @@ extendEnvironment((hre) => { hre.deployer = new DeployerExtension(hre); }); -task(TASK_DEPLOY_ZKSYNC, 'Runs the deploy scripts for ZKsync network') +taskWithEraTestNode(TASK_DEPLOY_ZKSYNC, 'Runs the deploy scripts for ZKsync network', true) .addParam('script', 'A certain deploy script to be launched', '') .addOptionalParam('tags', 'specify which deploy script to execute via tags, separated by commas', undefined, string) .setAction(zkSyncDeploy); -task(TASK_DEPLOY_ZKSYNC_LIBRARIES, 'Runs the library deploy for ZKsync network') +taskWithEraTestNode(TASK_DEPLOY_ZKSYNC_LIBRARIES, 'Runs the library deploy for ZKsync network', true) .addOptionalParam( 'privateKeyOrIndex', 'Private key or index of the account that will deploy the libraries', @@ -57,7 +53,7 @@ task(TASK_DEPLOY_ZKSYNC_LIBRARIES, 'Runs the library deploy for ZKsync network') .addFlag('compileAllContracts', 'Flag to compile all contracts at the end of the process') .setAction(zkSyncLibraryDeploy); -task(TASK_DEPLOY_ZKSYNC_CONTRACT, 'Runs the deploy scripts for ZKsync network') +taskWithEraTestNode(TASK_DEPLOY_ZKSYNC_CONTRACT, 'Runs the deploy scripts for ZKsync network', true) .addParam('contractName', 'A contract name or a FQN', '') .addOptionalVariadicPositionalParam( 'constructorArgsParams', @@ -75,9 +71,6 @@ task(TASK_DEPLOY_ZKSYNC_CONTRACT, 'Runs the deploy scripts for ZKsync network') .addFlag('noCompile', 'Flag to disable auto compilation') .setAction(deployZkSyncContract); -(global as ZKSyncTasksWithWrappedNode)._zkSyncTasksForWrapping.addTask(TASK_DEPLOY_ZKSYNC); -(global as ZKSyncTasksWithWrappedNode)._zkSyncTasksForWrapping.addTask(TASK_DEPLOY_ZKSYNC_LIBRARIES); -(global as ZKSyncTasksWithWrappedNode)._zkSyncTasksForWrapping.addTask(TASK_DEPLOY_ZKSYNC_CONTRACT); try { require.resolve('zksync2-js'); console.info(chalk.red('The package zksync2-js is deprecated. Please use zksync-ethers.')); diff --git a/packages/hardhat-zksync-node/package.json b/packages/hardhat-zksync-node/package.json index 8edb1bc5b..9a15e182a 100644 --- a/packages/hardhat-zksync-node/package.json +++ b/packages/hardhat-zksync-node/package.json @@ -41,7 +41,9 @@ "chai": "^4.3.4", "undici": "^6.18.2", "sinon-chai": "^3.7.0", - "sinon": "^18.0.0" + "sinon": "^18.0.0", + "source-map-support": "^0.5.21", + "debug": "^4.3.5" }, "devDependencies": { "@types/chai": "^4.3.16", diff --git a/packages/hardhat-zksync-node/src/core/global-task.ts b/packages/hardhat-zksync-node/src/core/global-task.ts index 17ab1a8e2..9f35a8057 100644 --- a/packages/hardhat-zksync-node/src/core/global-task.ts +++ b/packages/hardhat-zksync-node/src/core/global-task.ts @@ -8,7 +8,9 @@ export type ZKSyncTasksWithWrappedNode = typeof global & { export class ZKSyncTasksForWrapping { public taskNames: string[] = []; - constructor() {} + constructor() { + this.taskNames = []; + } public addTask(taskName: string) { this.taskNames.push(taskName); diff --git a/packages/hardhat-zksync-node/src/core/register.ts b/packages/hardhat-zksync-node/src/core/register.ts index e69de29bb..bf2e3b37a 100644 --- a/packages/hardhat-zksync-node/src/core/register.ts +++ b/packages/hardhat-zksync-node/src/core/register.ts @@ -0,0 +1,77 @@ +import debug from 'debug'; + +import { HardhatContext } from 'hardhat/internal/context'; +import { loadConfigAndTasks } from 'hardhat/internal/core/config/config-loading'; +import { getEnvHardhatArguments } from 'hardhat/internal/core/params/env-variables'; +import { HARDHAT_PARAM_DEFINITIONS } from 'hardhat/internal/core/params/hardhat-params'; +import { Environment } from 'hardhat/internal/core/runtime-environment'; +import { loadTsNode, willRunWithTypescript } from 'hardhat/internal/core/typescript-support'; +import { disableReplWriterShowProxy, isNodeCalledWithoutAScript } from 'hardhat/internal/util/console'; +import { LazyInitializationProviderAdapter } from 'hardhat/internal/core/providers/lazy-initialization'; +import { log } from 'console'; +import { createProvider } from 'hardhat/internal/core/providers/construction'; +import { MessageTrace } from 'hardhat/internal/hardhat-network/stack-traces/message-trace'; +import { Artifacts } from 'hardhat/internal/artifacts'; +import { BASE_URL, ZKSYNC_ERA_TEST_NODE_NETWORK_NAME } from '../constants'; +import { getNetworkConfig } from '../utils'; + +if (!HardhatContext.isCreated()) { + require('source-map-support/register'); + + const ctx = HardhatContext.createHardhatContext(); + + if (isNodeCalledWithoutAScript()) { + disableReplWriterShowProxy(); + } + + const hardhatArguments = getEnvHardhatArguments(HARDHAT_PARAM_DEFINITIONS, process.env); + + if (hardhatArguments.verbose) { + debug.enable('hardhat*'); + } + + if (willRunWithTypescript(hardhatArguments.config)) { + loadTsNode(hardhatArguments.tsconfig, hardhatArguments.typecheck); + } + + const { resolvedConfig, userConfig } = loadConfigAndTasks(hardhatArguments); + + const env = new Environment( + resolvedConfig, + hardhatArguments, + ctx.tasksDSL.getTaskDefinitions(), + ctx.tasksDSL.getScopesDefinitions(), + ctx.environmentExtenders, + ctx.experimentalHardhatNetworkMessageTraceHooks, + userConfig, + ctx.providerExtenders, + ); + + const zksyncNodePort = process.env.ZKNodePort; + const url = `${BASE_URL}:${zksyncNodePort}`; + const networkName = ZKSYNC_ERA_TEST_NODE_NETWORK_NAME; + + env.network.name = networkName; + Object.assign(env.network.config, getNetworkConfig(url)); + resolvedConfig.networks[env.network.name] = env.network.config; + + const artifacts = new Artifacts(resolvedConfig.paths.artifacts); + + const provider = new LazyInitializationProviderAdapter(async () => { + log(`Creating provider for network ${networkName}`); + return createProvider( + resolvedConfig, + networkName, + artifacts, + ctx.experimentalHardhatNetworkMessageTraceHooks.map( + (hook) => (trace: MessageTrace, isCallMessageTrace: boolean) => hook(env, trace, isCallMessageTrace), + ), + ctx.providerExtenders, + ); + }); + + env.network.provider = provider; + ctx.setHardhatRuntimeEnvironment(env); + + env.injectToGlobal(); +} diff --git a/packages/hardhat-zksync-node/src/core/script-runner.ts b/packages/hardhat-zksync-node/src/core/script-runner.ts index 42d8eb649..69c96ec01 100644 --- a/packages/hardhat-zksync-node/src/core/script-runner.ts +++ b/packages/hardhat-zksync-node/src/core/script-runner.ts @@ -1,9 +1,8 @@ import { isRunningHardhatCoreTests } from 'hardhat/internal/core/execution-mode'; import { HardhatArguments } from 'hardhat/types'; import { getEnvVariablesMap } from 'hardhat/internal/core/params/env-variables'; -import { HardhatContext } from 'hardhat/internal/context'; -import { request } from 'http'; -import { configureNetwork, startServer, waitForNodeToBeReady } from '../utils'; +import path from 'path'; +import { startServer, waitForNodeToBeReady } from '../utils'; export async function runScript( scriptPath: string, @@ -20,7 +19,11 @@ export async function runScript( ...extraNodeArgs, ]; - const envVars = { ...process.env, ...extraEnvVars }; + const { commandArgs, server, port } = await startServer(); + await server.listen(commandArgs, false); + await waitForNodeToBeReady(port); + + const envVars = { ...process.env, ...extraEnvVars, ZKNodePort: port.toString() }; return new Promise((resolve, reject) => { const childProcess = fork(scriptPath, scriptArgs, { @@ -29,31 +32,13 @@ export async function runScript( env: envVars, }); - let runnedServer: any; - - childProcess.on('spawn', () => { - return new Promise(async (_, rejectChild) => { - try { - request('hardhat/register'); - const ctx = HardhatContext.getHardhatContext(); - const { commandArgs, server, port } = await startServer(); - runnedServer = server; - await server.listen(commandArgs, false); - await waitForNodeToBeReady(port); - await configureNetwork(ctx.environment!.config, ctx.environment!.network, port); - } catch (error) { - rejectChild(error); - } - }); - }); - - childProcess.once('close', (status) => { - runnedServer?.stop(); + childProcess.once('close', async (status) => { + await server.stop(); resolve(status as number); }); - childProcess.once('error', (error) => { - runnedServer?.stop(); + childProcess.once('error', async (error) => { + await server.stop(); reject(error); }); }); @@ -66,7 +51,7 @@ export async function runScriptWithHardhat( extraNodeArgs: string[] = [], extraEnvVars: { [name: string]: string } = {}, ): Promise { - return runScript(scriptPath, scriptArgs, [...extraNodeArgs], { + return runScript(scriptPath, scriptArgs, [...extraNodeArgs, '--require', path.join(__dirname, 'register')], { ...getEnvVariablesMap(hardhatArguments), ...extraEnvVars, }); diff --git a/packages/hardhat-zksync-node/src/index.ts b/packages/hardhat-zksync-node/src/index.ts index 336267a47..c034731e0 100644 --- a/packages/hardhat-zksync-node/src/index.ts +++ b/packages/hardhat-zksync-node/src/index.ts @@ -36,8 +36,9 @@ import { interceptAndWrapTasksWithNode } from './core/global-interceptor'; import { runScriptWithHardhat } from './core/script-runner'; task(TASK_RUN).setAction(async (args, hre, runSuper) => { - if (!hre.network.zksync) { + if (!hre.network.zksync || hre.network.name !== HARDHAT_NETWORK_NAME) { await runSuper(args, hre); + return; } await runScriptWithHardhat(hre.hardhatArguments, path.resolve(args.script)); diff --git a/packages/hardhat-zksync-node/src/utils.ts b/packages/hardhat-zksync-node/src/utils.ts index 43a6aba63..3154169ee 100644 --- a/packages/hardhat-zksync-node/src/utils.ts +++ b/packages/hardhat-zksync-node/src/utils.ts @@ -388,7 +388,7 @@ export function adjustTaskArgsForPort(taskArgs: string[], currentPort: number): return taskArgs; } -function getNetworkConfig(url: string) { +export function getNetworkConfig(url: string) { return { accounts: NETWORK_ACCOUNTS.REMOTE, gas: NETWORK_GAS.AUTO, @@ -398,6 +398,7 @@ function getNetworkConfig(url: string) { timeout: 20000, url, ethNetwork: NETWORK_ETH.LOCALHOST, + chainId: 260, zksync: true, }; } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 329e3168d..1e6d2cc33 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1136,6 +1136,9 @@ importers: chalk: specifier: ^4.1.2 version: 4.1.2 + debug: + specifier: ^4.3.5 + version: 4.3.5 fs-extra: specifier: ^11.2.0 version: 11.2.0 @@ -1148,6 +1151,9 @@ importers: sinon-chai: specifier: ^3.7.0 version: 3.7.0(chai@4.4.1)(sinon@18.0.0) + source-map-support: + specifier: ^0.5.21 + version: 0.5.21 undici: specifier: ^6.18.2 version: 6.18.2