-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: override run task for zksync hardhat network (#1462)
* feat: add node task * feat: refactor task node * feat: update task node * fix: call subtask for era test node start * fix: set default port at 8545 for both node tasks * feat: wrap deploy script run with era test node for the hardhat network * fix: fix runScripts to be executed with node support and override default tasks with node support * fix: add fund-up as root dependency and set 8011 as default port for node-zksync task * fix: delete example unused files and packages --------- Co-authored-by: nikola-bozin-txfusion <[email protected]> Co-authored-by: Marko Arambasic <[email protected]>
- Loading branch information
1 parent
e7a8397
commit a49c593
Showing
13 changed files
with
417 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<TaskArgumentsT extends TaskArguments>( | ||
name: string, | ||
description?: string, | ||
withNode?: boolean, | ||
action?: ActionType<TaskArgumentsT>, | ||
): 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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Network } from 'hardhat/types'; | ||
|
||
export type ZKSyncTasksWithWrappedNode = typeof global & { | ||
_zkSyncTasksForWrapping: ZKSyncTasksForWrapping; | ||
_zkSyncNodeNetwork?: Network; | ||
}; | ||
|
||
export class ZKSyncTasksForWrapping { | ||
public taskNames: string[] = []; | ||
|
||
constructor() { | ||
this.taskNames = []; | ||
} | ||
|
||
public addTask(taskName: string) { | ||
this.taskNames.push(taskName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/hardhat-zksync-node/src/core/global-interceptor.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { RunSuperFunction, TaskArguments } from 'hardhat/types'; | ||
import { GlobalWithHardhatContext } from 'hardhat/src/internal/context'; | ||
import { HARDHAT_NETWORK_NAME } from 'hardhat/plugins'; | ||
import { configureNetwork, startServer, waitForNodeToBeReady } from '../utils'; | ||
import { ZKSyncTasksWithWrappedNode } from './global-task'; | ||
|
||
export function interceptAndWrapTasksWithNode() { | ||
const zkSyncGlobal = global as ZKSyncTasksWithWrappedNode & GlobalWithHardhatContext; | ||
const taskMap = zkSyncGlobal.__hardhatContext.tasksDSL.getTaskDefinitions(); | ||
|
||
if (!zkSyncGlobal._zkSyncTasksForWrapping) { | ||
return; | ||
} | ||
|
||
zkSyncGlobal._zkSyncTasksForWrapping.taskNames.forEach((taskName) => { | ||
const foundTask = taskMap[taskName]; | ||
|
||
if (!foundTask) { | ||
return; | ||
} | ||
|
||
if (foundTask.isSubtask) { | ||
zkSyncGlobal.__hardhatContext.tasksDSL.subtask(foundTask.name, foundTask.description, wrapTaskWithNode); | ||
} | ||
|
||
zkSyncGlobal.__hardhatContext.tasksDSL.task(foundTask.name, foundTask.description, wrapTaskWithNode); | ||
}); | ||
} | ||
|
||
async function wrapTaskWithNode(taskArgs: TaskArguments, env: any, runSuper: RunSuperFunction<TaskArguments>) { | ||
if (env.network.zksync !== true || env.network.name !== HARDHAT_NETWORK_NAME) { | ||
return await runSuper(taskArgs); | ||
} | ||
const zkSyncGlobal = global as ZKSyncTasksWithWrappedNode; | ||
const { commandArgs, server, port } = await startServer(); | ||
try { | ||
await server.listen(commandArgs, false); | ||
await waitForNodeToBeReady(port); | ||
const oldNetwork = env.network; | ||
await configureNetwork(env.config, env.network, port); | ||
env.injectToGlobal(); | ||
zkSyncGlobal._zkSyncNodeNetwork = env.network; | ||
const result = await runSuper(taskArgs); | ||
env.network = oldNetwork; | ||
delete zkSyncGlobal._zkSyncNodeNetwork; | ||
env.injectToGlobal(); | ||
return result; | ||
} finally { | ||
await server.stop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Network } from 'hardhat/types'; | ||
|
||
export type ZKSyncTasksWithWrappedNode = typeof global & { | ||
_zkSyncTasksForWrapping: ZKSyncTasksForWrapping; | ||
_zkSyncNodeNetwork?: Network; | ||
}; | ||
|
||
export class ZKSyncTasksForWrapping { | ||
public taskNames: string[] = []; | ||
|
||
constructor() { | ||
this.taskNames = []; | ||
} | ||
|
||
public addTask(taskName: string) { | ||
this.taskNames.push(taskName); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { isRunningHardhatCoreTests } from 'hardhat/internal/core/execution-mode'; | ||
import { HardhatArguments } from 'hardhat/types'; | ||
import { getEnvVariablesMap } from 'hardhat/internal/core/params/env-variables'; | ||
import path from 'path'; | ||
import { startServer, waitForNodeToBeReady } from '../utils'; | ||
|
||
export async function runScript( | ||
scriptPath: string, | ||
scriptArgs: string[] = [], | ||
extraNodeArgs: string[] = [], | ||
extraEnvVars: { [name: string]: string } = {}, | ||
): Promise<number> { | ||
const { fork } = await import('child_process'); | ||
const processExecArgv = withFixedInspectArg(process.execArgv); | ||
|
||
const nodeArgs = [ | ||
...processExecArgv, | ||
...getTsNodeArgsIfNeeded(scriptPath, extraEnvVars.HARDHAT_TYPECHECK === 'true'), | ||
...extraNodeArgs, | ||
]; | ||
|
||
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, { | ||
stdio: 'inherit', | ||
execArgv: nodeArgs, | ||
env: envVars, | ||
}); | ||
|
||
childProcess.once('close', async (status) => { | ||
await server.stop(); | ||
resolve(status as number); | ||
}); | ||
|
||
childProcess.once('error', async (error) => { | ||
await server.stop(); | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
|
||
export async function runScriptWithHardhat( | ||
hardhatArguments: HardhatArguments, | ||
scriptPath: string, | ||
scriptArgs: string[] = [], | ||
extraNodeArgs: string[] = [], | ||
extraEnvVars: { [name: string]: string } = {}, | ||
): Promise<number> { | ||
return runScript(scriptPath, scriptArgs, [...extraNodeArgs, '--require', path.join(__dirname, 'register')], { | ||
...getEnvVariablesMap(hardhatArguments), | ||
...extraEnvVars, | ||
}); | ||
} | ||
|
||
function withFixedInspectArg(argv: string[]) { | ||
const fixIfInspectArg = (arg: string) => { | ||
if (arg.toLowerCase().includes('--inspect-brk=')) { | ||
return '--inspect'; | ||
} | ||
return arg; | ||
}; | ||
return argv.map(fixIfInspectArg); | ||
} | ||
|
||
function getTsNodeArgsIfNeeded(scriptPath: string, shouldTypecheck: boolean): string[] { | ||
if (process.execArgv.includes('ts-node/register')) { | ||
return []; | ||
} | ||
|
||
// if we are running the tests we only want to transpile, or these tests | ||
// take forever | ||
if (isRunningHardhatCoreTests()) { | ||
return ['--require', 'ts-node/register/transpile-only']; | ||
} | ||
|
||
// If the script we are going to run is .ts we need ts-node | ||
if (/\.tsx?$/i.test(scriptPath)) { | ||
return ['--require', `ts-node/register${shouldTypecheck ? '' : '/transpile-only'}`]; | ||
} | ||
|
||
return []; | ||
} |
Oops, something went wrong.