From 457de152263081cbb14159dbc61b09e962115b03 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Tue, 13 Oct 2020 14:22:32 -0300 Subject: [PATCH 01/24] Add subtasks for creating the provider and the server --- .../hardhat-core/src/builtin-tasks/node.ts | 92 ++++++++++++------- .../src/builtin-tasks/task-names.ts | 2 + 2 files changed, 60 insertions(+), 34 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 04d19b3d62..3cca57f3b5 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -3,7 +3,7 @@ import debug from "debug"; import { BN, bufferToHex, privateToAddress, toBuffer } from "ethereumjs-util"; import { HARDHAT_NETWORK_NAME } from "../internal/constants"; -import { task, types } from "../internal/core/config/config-env"; +import { subtask, task, types } from "../internal/core/config/config-env"; import { HardhatError } from "../internal/core/errors"; import { ERRORS } from "../internal/core/errors-list"; import { createProvider } from "../internal/core/providers/construction"; @@ -13,38 +13,17 @@ import { } from "../internal/hardhat-network/jsonrpc/server"; import { Reporter } from "../internal/sentry/reporter"; import { lazyObject } from "../internal/util/lazy"; -import { - Artifacts, - EthereumProvider, - HardhatConfig, - HardhatNetworkConfig, -} from "../types"; +import { EthereumProvider, HardhatNetworkConfig } from "../types"; -import { TASK_NODE } from "./task-names"; +import { + TASK_NODE, + TASK_NODE_CREATE_SERVER, + TASK_NODE_GET_PROVIDER, +} from "./task-names"; import { watchCompilerOutput } from "./utils/watch"; const log = debug("hardhat:core:tasks:node"); -function _createHardhatNetworkProvider( - config: HardhatConfig, - artifacts: Artifacts -): EthereumProvider { - log("Creating HardhatNetworkProvider"); - - const networkName = HARDHAT_NETWORK_NAME; - const networkConfig = config.networks[HARDHAT_NETWORK_NAME]; - - return lazyObject(() => { - log(`Creating hardhat provider for JSON-RPC sever`); - return createProvider( - networkName, - { loggingEnabled: true, ...networkConfig }, - config.paths, - artifacts - ); - }); -} - function logHardhatNetworkAccounts(networkConfig: HardhatNetworkConfig) { if (networkConfig.accounts === undefined) { return; @@ -67,6 +46,51 @@ Private Key: ${privateKey} } export default function () { + subtask(TASK_NODE_GET_PROVIDER).setAction( + async (_, { config, artifacts }): Promise => { + log("Creating HardhatNetworkProvider"); + + const networkName = HARDHAT_NETWORK_NAME; + const networkConfig = config.networks[HARDHAT_NETWORK_NAME]; + + return lazyObject(() => { + log(`Creating hardhat provider for JSON-RPC sever`); + return createProvider( + networkName, + { loggingEnabled: true, ...networkConfig }, + config.paths, + artifacts + ); + }); + } + ); + + subtask(TASK_NODE_CREATE_SERVER) + .addParam("hostname", undefined, undefined, types.string) + .addParam("port", undefined, undefined, types.int) + .addParam("provider", undefined, undefined, types.any) + .setAction( + async ({ + hostname, + port, + provider, + }: { + hostname: string; + port: number; + provider: EthereumProvider; + }): Promise => { + const serverConfig: JsonRpcServerConfig = { + hostname, + port, + provider, + }; + + const server = new JsonRpcServer(serverConfig); + + return server; + } + ); + task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") .addOptionalParam( "hostname", @@ -83,7 +107,7 @@ export default function () { .setAction( async ( { hostname, port }, - { artifacts, network, hardhatArguments, config } + { config, hardhatArguments, network, run } ) => { if ( network.name !== HARDHAT_NETWORK_NAME && @@ -100,13 +124,13 @@ export default function () { } try { - const serverConfig: JsonRpcServerConfig = { + const provider: EthereumProvider = await run(TASK_NODE_GET_PROVIDER); + + const server: JsonRpcServer = await run(TASK_NODE_CREATE_SERVER, { hostname, port, - provider: _createHardhatNetworkProvider(config, artifacts), - }; - - const server = new JsonRpcServer(serverConfig); + provider, + }); const { port: actualPort, address } = await server.listen(); diff --git a/packages/hardhat-core/src/builtin-tasks/task-names.ts b/packages/hardhat-core/src/builtin-tasks/task-names.ts index 7a95fc56bd..9912ab20d8 100644 --- a/packages/hardhat-core/src/builtin-tasks/task-names.ts +++ b/packages/hardhat-core/src/builtin-tasks/task-names.ts @@ -69,6 +69,8 @@ export const TASK_HELP = "help"; export const TASK_RUN = "run"; export const TASK_NODE = "node"; +export const TASK_NODE_GET_PROVIDER = "node:get-provider"; +export const TASK_NODE_CREATE_SERVER = "node:create-server"; export const TASK_TEST = "test"; From 6056633d1e1a8e8075131d4d737b9cf8877c80ed Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Tue, 13 Oct 2020 14:23:08 -0300 Subject: [PATCH 02/24] Avoij creating a new provider --- packages/hardhat-core/src/builtin-tasks/node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 3cca57f3b5..0539ceeecd 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -143,7 +143,7 @@ export default function () { console.log(); try { - await watchCompilerOutput(server.getProvider(), config.paths); + await watchCompilerOutput(provider, config.paths); } catch (error) { console.warn( chalk.yellow( From 5deaccd5acb0a3374f28f55b1f17ee1f8a8fc39f Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Tue, 13 Oct 2020 16:34:04 -0300 Subject: [PATCH 03/24] Use network.provider when it corresponds to hardhat network --- packages/hardhat-core/src/builtin-tasks/node.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 0539ceeecd..f35c7155fd 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -47,7 +47,11 @@ Private Key: ${privateKey} export default function () { subtask(TASK_NODE_GET_PROVIDER).setAction( - async (_, { config, artifacts }): Promise => { + async (_, { artifacts, config, network }): Promise => { + if (network.name === HARDHAT_NETWORK_NAME) { + return network.provider; + } + log("Creating HardhatNetworkProvider"); const networkName = HARDHAT_NETWORK_NAME; @@ -57,7 +61,7 @@ export default function () { log(`Creating hardhat provider for JSON-RPC sever`); return createProvider( networkName, - { loggingEnabled: true, ...networkConfig }, + { ...networkConfig, loggingEnabled: true }, config.paths, artifacts ); From a7372eb40d4b32adc4c96862ea2fb129fb37a9eb Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Tue, 13 Oct 2020 17:53:43 -0300 Subject: [PATCH 04/24] Add RPC for enabling logging --- .../hardhat-core/src/builtin-tasks/node.ts | 5 ++++ .../hardhat-network/provider/input.ts | 5 ++++ .../provider/modules/hardhat.ts | 23 ++++++++++++++++++- .../hardhat-network/provider/provider.ts | 10 ++++++-- 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index f35c7155fd..105d8d78a6 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -49,6 +49,11 @@ export default function () { subtask(TASK_NODE_GET_PROVIDER).setAction( async (_, { artifacts, config, network }): Promise => { if (network.name === HARDHAT_NETWORK_NAME) { + // enable logging for in-memory hardhat network provider + await network.provider.request({ + method: "hardhat_setLoggingEnabled", + params: [true], + }); return network.provider; } diff --git a/packages/hardhat-core/src/internal/hardhat-network/provider/input.ts b/packages/hardhat-core/src/internal/hardhat-network/provider/input.ts index 5ca84ad644..793353cac9 100644 --- a/packages/hardhat-core/src/internal/hardhat-network/provider/input.ts +++ b/packages/hardhat-core/src/internal/hardhat-network/provider/input.ts @@ -337,6 +337,11 @@ export function validateParams( forkConfig: typeof optionalRpcHardhatNetworkConfig ): [RpcHardhatNetworkConfig | undefined]; +export function validateParams( + params: any[], + loggingEnabled: typeof t.boolean +): [boolean]; + // tslint:disable only-hardhat-error export function validateParams(params: any[], ...types: Array>) { diff --git a/packages/hardhat-core/src/internal/hardhat-network/provider/modules/hardhat.ts b/packages/hardhat-core/src/internal/hardhat-network/provider/modules/hardhat.ts index 5841dbd4cb..4730fc89c1 100644 --- a/packages/hardhat-core/src/internal/hardhat-network/provider/modules/hardhat.ts +++ b/packages/hardhat-core/src/internal/hardhat-network/provider/modules/hardhat.ts @@ -18,7 +18,10 @@ import { ForkConfig } from "../node-types"; export class HardhatModule { constructor( private readonly _node: HardhatNode, - private readonly _resetCallback: (forkConfig?: ForkConfig) => Promise + private readonly _resetCallback: (forkConfig?: ForkConfig) => Promise, + private readonly _setLoggingEnabledCallback: ( + loggingEnabled: boolean + ) => void ) {} public async processRequest( @@ -46,6 +49,11 @@ export class HardhatModule { case "hardhat_reset": return this._resetAction(...this._resetParams(params)); + + case "hardhat_setLoggingEnabled": + return this._setLoggingEnabledAction( + ...this._setLoggingEnabledParams(params) + ); } throw new MethodNotFoundError(`Method ${method} not found`); @@ -118,4 +126,17 @@ export class HardhatModule { await this._resetCallback(networkConfig?.forking); return true; } + + // hardhat_setLoggingEnabled + + private _setLoggingEnabledParams(params: any[]): [boolean] { + return validateParams(params, t.boolean); + } + + private async _setLoggingEnabledAction( + loggingEnabled: boolean + ): Promise { + this._setLoggingEnabledCallback(loggingEnabled); + return true; + } } diff --git a/packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts b/packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts index c38efe6a90..dc6e0bdfc9 100644 --- a/packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts +++ b/packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts @@ -71,7 +71,7 @@ export class HardhatNetworkProvider extends EventEmitter private readonly _throwOnCallFailures: boolean, private readonly _genesisAccounts: GenesisAccount[] = [], private readonly _artifacts?: Artifacts, - private readonly _loggingEnabled = false, + private _loggingEnabled = false, private readonly _allowUnlimitedContractSize = false, private readonly _initialDate?: Date, private readonly _experimentalHardhatNetworkMessageTraceHooks: BoundExperimentalHardhatNetworkMessageTraceHook[] = [], @@ -276,7 +276,13 @@ export class HardhatNetworkProvider extends EventEmitter this._netModule = new NetModule(common); this._web3Module = new Web3Module(); this._evmModule = new EvmModule(node); - this._hardhatModule = new HardhatModule(node, this._reset.bind(this)); + this._hardhatModule = new HardhatModule( + node, + this._reset.bind(this), + (loggingEnabled: boolean) => { + this._loggingEnabled = loggingEnabled; + } + ); this._forwardNodeEvents(node); } From e46b8a315f7e941c923146285fce07d291b17877 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Tue, 13 Oct 2020 18:04:04 -0300 Subject: [PATCH 05/24] Add subtask hooks in the node task --- .../hardhat-core/src/builtin-tasks/node.ts | 31 +++++++++++++++++++ .../src/builtin-tasks/task-names.ts | 2 ++ 2 files changed, 33 insertions(+) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 105d8d78a6..b7b6e69d63 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -19,6 +19,8 @@ import { TASK_NODE, TASK_NODE_CREATE_SERVER, TASK_NODE_GET_PROVIDER, + TASK_NODE_SERVER_CREATED, + TASK_NODE_SERVER_READY, } from "./task-names"; import { watchCompilerOutput } from "./utils/watch"; @@ -100,6 +102,31 @@ export default function () { } ); + /** + * This task will be called when the server was successfully created, but it's + * not ready for receiving requests yet. + */ + subtask(TASK_NODE_SERVER_CREATED) + .addParam("provider", undefined, undefined, types.any) + .addParam("server", undefined, undefined, types.any) + .setAction( + async ({}: { provider: EthereumProvider; server: JsonRpcServer }) => { + // this task is meant to be overriden by plugin writers + } + ); + + /** + * This subtask will be run when the server is ready to accept requests + */ + subtask(TASK_NODE_SERVER_READY) + .addParam("provider", undefined, undefined, types.any) + .addParam("server", undefined, undefined, types.any) + .setAction( + async ({}: { provider: EthereumProvider; server: JsonRpcServer }) => { + // this task is meant to be overriden by plugin writers + } + ); + task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") .addOptionalParam( "hostname", @@ -141,6 +168,8 @@ export default function () { provider, }); + await run(TASK_NODE_SERVER_CREATED, { provider, server }); + const { port: actualPort, address } = await server.listen(); console.log( @@ -171,6 +200,8 @@ export default function () { const networkConfig = config.networks[HARDHAT_NETWORK_NAME]; logHardhatNetworkAccounts(networkConfig); + await run(TASK_NODE_SERVER_READY, { provider, server }); + await server.waitUntilClosed(); } catch (error) { if (HardhatError.isHardhatError(error)) { diff --git a/packages/hardhat-core/src/builtin-tasks/task-names.ts b/packages/hardhat-core/src/builtin-tasks/task-names.ts index 9912ab20d8..d74aa56bb9 100644 --- a/packages/hardhat-core/src/builtin-tasks/task-names.ts +++ b/packages/hardhat-core/src/builtin-tasks/task-names.ts @@ -71,6 +71,8 @@ export const TASK_RUN = "run"; export const TASK_NODE = "node"; export const TASK_NODE_GET_PROVIDER = "node:get-provider"; export const TASK_NODE_CREATE_SERVER = "node:create-server"; +export const TASK_NODE_SERVER_CREATED = "node:server-created"; +export const TASK_NODE_SERVER_READY = "node:server-ready"; export const TASK_TEST = "test"; From c4f6d59e3975fb0e2abaede0bbd23ada2c678c9e Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Tue, 13 Oct 2020 20:07:32 -0300 Subject: [PATCH 06/24] Add fork params to node task --- .../hardhat-core/src/builtin-tasks/node.ts | 97 ++++++++++++++----- .../src/internal/core/errors-list.ts | 8 ++ 2 files changed, 83 insertions(+), 22 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index b93aeaf7b0..77c12e3d94 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -4,7 +4,7 @@ import { BN, bufferToHex, privateToAddress, toBuffer } from "ethereumjs-util"; import { HARDHAT_NETWORK_NAME } from "../internal/constants"; import { subtask, task, types } from "../internal/core/config/config-env"; -import { HardhatError } from "../internal/core/errors"; +import { assertHardhatInvariant, HardhatError } from "../internal/core/errors"; import { ERRORS } from "../internal/core/errors-list"; import { createProvider } from "../internal/core/providers/construction"; import { @@ -48,30 +48,55 @@ Private Key: ${privateKey} } subtask(TASK_NODE_GET_PROVIDER).setAction( - async (_, { artifacts, config, network }): Promise => { - if (network.name === HARDHAT_NETWORK_NAME) { - // enable logging for in-memory hardhat network provider - await network.provider.request({ - method: "hardhat_setLoggingEnabled", - params: [true], - }); - return network.provider; - } + async ( + { + forkBlockNumber: forkBlockNumberParam, + forkUrl: forkUrlParam, + }: { + forkBlockNumber?: number; + forkUrl?: string; + }, + { config, network } + ): Promise => { + // enable logging for in-memory hardhat network provider + await network.provider.request({ + method: "hardhat_setLoggingEnabled", + params: [true], + }); - log("Creating HardhatNetworkProvider"); + assertHardhatInvariant( + network.name === HARDHAT_NETWORK_NAME, + "Hardhat node can only be used with hardhat network" + ); - const networkName = HARDHAT_NETWORK_NAME; const networkConfig = config.networks[HARDHAT_NETWORK_NAME]; - return lazyObject(() => { - log(`Creating hardhat provider for JSON-RPC sever`); - return createProvider( - networkName, - { ...networkConfig, loggingEnabled: true }, - config.paths, - artifacts + const forkUrl = forkUrlParam ?? networkConfig.forking?.url; + const forkBlockNumber = + forkBlockNumberParam ?? networkConfig.forking?.blockNumber; + + // we use the hardhat_reset RPC method to enable the fork + if (forkUrl !== undefined) { + await network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: forkUrl, + blockNumber: forkBlockNumber, + }, + }, + ], + }); + } else if (forkBlockNumber !== undefined) { + // we throw an error if the user specified a forkBlockNumber but not a + // forkUrl + throw new HardhatError( + ERRORS.BUILTIN_TASKS.NODE_FORK_BLOCK_NUMBER_WITHOUT_URL ); - }); + } + + return network.provider; } ); @@ -139,8 +164,33 @@ task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") 8545, types.int ) + .addOptionalParam( + "forkUrl", + "The URL of the JSON-RPC server to fork from", + undefined, + types.string + ) + .addOptionalParam( + "forkBlockNumber", + "The block number to fork from", + undefined, + types.int + ) .setAction( - async ({ hostname, port }, { config, hardhatArguments, network, run }) => { + async ( + { + forkBlockNumber, + forkUrl, + hostname, + port, + }: { + forkBlockNumber?: number; + forkUrl?: string; + hostname: string; + port: number; + }, + { config, hardhatArguments, network, run } + ) => { if ( network.name !== HARDHAT_NETWORK_NAME && // We normally set the default network as hardhatArguments.network, @@ -156,7 +206,10 @@ task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") } try { - const provider: EthereumProvider = await run(TASK_NODE_GET_PROVIDER); + const provider: EthereumProvider = await run(TASK_NODE_GET_PROVIDER, { + forkBlockNumber, + forkUrl, + }); const server: JsonRpcServer = await run(TASK_NODE_CREATE_SERVER, { hostname, diff --git a/packages/hardhat-core/src/internal/core/errors-list.ts b/packages/hardhat-core/src/internal/core/errors-list.ts index 5acbb2bdeb..2ca9e8fdcb 100644 --- a/packages/hardhat-core/src/internal/core/errors-list.ts +++ b/packages/hardhat-core/src/internal/core/errors-list.ts @@ -834,6 +834,14 @@ To start the JSON-RPC server, retry the command without the --network parameter. description: `The project cannot be compiled with the current settings.`, shouldBeReported: false, }, + NODE_FORK_BLOCK_NUMBER_WITHOUT_URL: { + number: 608, + message: `You specified a fork block number but not an URL.`, + title: "Missing fork URL", + description: `You passed a block number to fork from, but not URL. Hardhat cannot fork +if the URL of the JSON-RPC weren't set.`, + shouldBeReported: false, + }, }, ARTIFACTS: { NOT_FOUND: { From 525f67fb3329ae59f52af352e9f630a1943d1dd1 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Wed, 14 Oct 2020 11:10:56 -0300 Subject: [PATCH 07/24] Create provider when network is not hardhat --- .../hardhat-core/src/builtin-tasks/node.ts | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 77c12e3d94..1c34227870 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -4,7 +4,7 @@ import { BN, bufferToHex, privateToAddress, toBuffer } from "ethereumjs-util"; import { HARDHAT_NETWORK_NAME } from "../internal/constants"; import { subtask, task, types } from "../internal/core/config/config-env"; -import { assertHardhatInvariant, HardhatError } from "../internal/core/errors"; +import { HardhatError } from "../internal/core/errors"; import { ERRORS } from "../internal/core/errors-list"; import { createProvider } from "../internal/core/providers/construction"; import { @@ -56,24 +56,33 @@ subtask(TASK_NODE_GET_PROVIDER).setAction( forkBlockNumber?: number; forkUrl?: string; }, - { config, network } + { artifacts, config, network } ): Promise => { + if (network.name !== HARDHAT_NETWORK_NAME) { + const networkConfig = config.networks[HARDHAT_NETWORK_NAME]; + + return lazyObject(() => { + log(`Creating hardhat provider for JSON-RPC sever`); + return createProvider( + network.name, + { ...networkConfig, loggingEnabled: true }, + config.paths, + artifacts + ); + }); + } + // enable logging for in-memory hardhat network provider await network.provider.request({ method: "hardhat_setLoggingEnabled", params: [true], }); - assertHardhatInvariant( - network.name === HARDHAT_NETWORK_NAME, - "Hardhat node can only be used with hardhat network" - ); - - const networkConfig = config.networks[HARDHAT_NETWORK_NAME]; + const hardhatNetworkConfig = config.networks[HARDHAT_NETWORK_NAME]; - const forkUrl = forkUrlParam ?? networkConfig.forking?.url; + const forkUrl = forkUrlParam ?? hardhatNetworkConfig.forking?.url; const forkBlockNumber = - forkBlockNumberParam ?? networkConfig.forking?.blockNumber; + forkBlockNumberParam ?? hardhatNetworkConfig.forking?.blockNumber; // we use the hardhat_reset RPC method to enable the fork if (forkUrl !== undefined) { From a93fcf8f0a83d920051635d62e44c6a508732e5f Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Wed, 14 Oct 2020 11:12:14 -0300 Subject: [PATCH 08/24] Add params to TASK_NODE_GET_PROVIDER --- .../hardhat-core/src/builtin-tasks/node.ts | 115 +++++++++--------- 1 file changed, 59 insertions(+), 56 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 1c34227870..ffcdda8a5f 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -47,67 +47,70 @@ Private Key: ${privateKey} } } -subtask(TASK_NODE_GET_PROVIDER).setAction( - async ( - { - forkBlockNumber: forkBlockNumberParam, - forkUrl: forkUrlParam, - }: { - forkBlockNumber?: number; - forkUrl?: string; - }, - { artifacts, config, network } - ): Promise => { - if (network.name !== HARDHAT_NETWORK_NAME) { - const networkConfig = config.networks[HARDHAT_NETWORK_NAME]; - - return lazyObject(() => { - log(`Creating hardhat provider for JSON-RPC sever`); - return createProvider( - network.name, - { ...networkConfig, loggingEnabled: true }, - config.paths, - artifacts - ); - }); - } - - // enable logging for in-memory hardhat network provider - await network.provider.request({ - method: "hardhat_setLoggingEnabled", - params: [true], - }); - - const hardhatNetworkConfig = config.networks[HARDHAT_NETWORK_NAME]; +subtask(TASK_NODE_GET_PROVIDER) + .addParam("forkUrl", undefined, undefined, types.string) + .addParam("forkBlockNumber", undefined, undefined, types.int) + .setAction( + async ( + { + forkBlockNumber: forkBlockNumberParam, + forkUrl: forkUrlParam, + }: { + forkBlockNumber?: number; + forkUrl?: string; + }, + { artifacts, config, network } + ): Promise => { + if (network.name !== HARDHAT_NETWORK_NAME) { + const networkConfig = config.networks[HARDHAT_NETWORK_NAME]; - const forkUrl = forkUrlParam ?? hardhatNetworkConfig.forking?.url; - const forkBlockNumber = - forkBlockNumberParam ?? hardhatNetworkConfig.forking?.blockNumber; + return lazyObject(() => { + log(`Creating hardhat provider for JSON-RPC sever`); + return createProvider( + network.name, + { ...networkConfig, loggingEnabled: true }, + config.paths, + artifacts + ); + }); + } - // we use the hardhat_reset RPC method to enable the fork - if (forkUrl !== undefined) { + // enable logging for in-memory hardhat network provider await network.provider.request({ - method: "hardhat_reset", - params: [ - { - forking: { - jsonRpcUrl: forkUrl, - blockNumber: forkBlockNumber, - }, - }, - ], + method: "hardhat_setLoggingEnabled", + params: [true], }); - } else if (forkBlockNumber !== undefined) { - // we throw an error if the user specified a forkBlockNumber but not a - // forkUrl - throw new HardhatError( - ERRORS.BUILTIN_TASKS.NODE_FORK_BLOCK_NUMBER_WITHOUT_URL - ); - } - return network.provider; - } -); + const hardhatNetworkConfig = config.networks[HARDHAT_NETWORK_NAME]; + + const forkUrl = forkUrlParam ?? hardhatNetworkConfig.forking?.url; + const forkBlockNumber = + forkBlockNumberParam ?? hardhatNetworkConfig.forking?.blockNumber; + + // we use the hardhat_reset RPC method to enable the fork + if (forkUrl !== undefined) { + await network.provider.request({ + method: "hardhat_reset", + params: [ + { + forking: { + jsonRpcUrl: forkUrl, + blockNumber: forkBlockNumber, + }, + }, + ], + }); + } else if (forkBlockNumber !== undefined) { + // we throw an error if the user specified a forkBlockNumber but not a + // forkUrl + throw new HardhatError( + ERRORS.BUILTIN_TASKS.NODE_FORK_BLOCK_NUMBER_WITHOUT_URL + ); + } + + return network.provider; + } + ); subtask(TASK_NODE_CREATE_SERVER) .addParam("hostname", undefined, undefined, types.string) From 2410d2e9c222c43d69dfef16bdb52d05e19e1773 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Wed, 14 Oct 2020 11:13:18 -0300 Subject: [PATCH 09/24] Enable logging after reset --- packages/hardhat-core/src/builtin-tasks/node.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index ffcdda8a5f..fa2c12f1c2 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -75,12 +75,6 @@ subtask(TASK_NODE_GET_PROVIDER) }); } - // enable logging for in-memory hardhat network provider - await network.provider.request({ - method: "hardhat_setLoggingEnabled", - params: [true], - }); - const hardhatNetworkConfig = config.networks[HARDHAT_NETWORK_NAME]; const forkUrl = forkUrlParam ?? hardhatNetworkConfig.forking?.url; @@ -108,6 +102,12 @@ subtask(TASK_NODE_GET_PROVIDER) ); } + // enable logging for in-memory hardhat network provider + await network.provider.request({ + method: "hardhat_setLoggingEnabled", + params: [true], + }); + return network.provider; } ); From e614962c783704a124d91b8c8ccbba3f1b4d55a2 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Wed, 14 Oct 2020 11:16:40 -0300 Subject: [PATCH 10/24] Simplify condition for throwing JSONRPC_UNSUPPORTED_NETWORK --- packages/hardhat-core/src/builtin-tasks/node.ts | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index fa2c12f1c2..93478f91af 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -203,14 +203,10 @@ task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") }, { config, hardhatArguments, network, run } ) => { + // we throw if the user specified a network argument and it's not hardhat if ( network.name !== HARDHAT_NETWORK_NAME && - // We normally set the default network as hardhatArguments.network, - // so this check isn't enough, and we add the next one. This has the - // effect of `--network ` being a false negative, but - // not a big deal. - hardhatArguments.network !== undefined && - hardhatArguments.network !== config.defaultNetwork + hardhatArguments.network !== undefined ) { throw new HardhatError( ERRORS.BUILTIN_TASKS.JSONRPC_UNSUPPORTED_NETWORK From 375a31a15a208d9178af6e05bc1b4b7e29c34a22 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Wed, 14 Oct 2020 11:22:35 -0300 Subject: [PATCH 11/24] Add interface for JsonRpcServer --- packages/hardhat-core/src/builtin-tasks/node.ts | 10 +++++++--- .../src/internal/hardhat-network/jsonrpc/server.ts | 11 +++++++---- .../hardhat-core/src/types/builtin-tasks/index.ts | 1 + packages/hardhat-core/src/types/builtin-tasks/node.ts | 10 ++++++++++ 4 files changed, 25 insertions(+), 7 deletions(-) create mode 100644 packages/hardhat-core/src/types/builtin-tasks/node.ts diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 93478f91af..b8ad8669cc 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -8,12 +8,16 @@ import { HardhatError } from "../internal/core/errors"; import { ERRORS } from "../internal/core/errors-list"; import { createProvider } from "../internal/core/providers/construction"; import { - JsonRpcServer, + JsonRpcServer as JsonRpcServerImpl, JsonRpcServerConfig, } from "../internal/hardhat-network/jsonrpc/server"; import { Reporter } from "../internal/sentry/reporter"; import { lazyObject } from "../internal/util/lazy"; -import { EthereumProvider, HardhatNetworkConfig } from "../types"; +import { + EthereumProvider, + HardhatNetworkConfig, + JsonRpcServer, +} from "../types"; import { TASK_NODE, @@ -132,7 +136,7 @@ subtask(TASK_NODE_CREATE_SERVER) provider, }; - const server = new JsonRpcServer(serverConfig); + const server = new JsonRpcServerImpl(serverConfig); return server; } diff --git a/packages/hardhat-core/src/internal/hardhat-network/jsonrpc/server.ts b/packages/hardhat-core/src/internal/hardhat-network/jsonrpc/server.ts index 331add5e76..f859d4306c 100644 --- a/packages/hardhat-core/src/internal/hardhat-network/jsonrpc/server.ts +++ b/packages/hardhat-core/src/internal/hardhat-network/jsonrpc/server.ts @@ -3,7 +3,10 @@ import http, { Server } from "http"; import { AddressInfo } from "net"; import { Server as WSServer } from "ws"; -import { EIP1193Provider } from "../../../types"; +import { + EIP1193Provider, + JsonRpcServer as IJsonRpcServer, +} from "../../../types"; import { HttpProvider } from "../../core/providers/http"; import JsonRpcHandler from "./handler"; @@ -17,7 +20,7 @@ export interface JsonRpcServerConfig { provider: EIP1193Provider; } -export class JsonRpcServer { +export class JsonRpcServer implements IJsonRpcServer { private _config: JsonRpcServerConfig; private _httpServer: Server; private _wsServer: WSServer; @@ -62,11 +65,11 @@ export class JsonRpcServer { this._wsServer.once("close", resolve); }); - return Promise.all([httpServerClosed, wsServerClosed]); + await Promise.all([httpServerClosed, wsServerClosed]); }; public close = async () => { - return Promise.all([ + await Promise.all([ new Promise((resolve, reject) => { log("Closing JSON-RPC server"); this._httpServer.close((err) => { diff --git a/packages/hardhat-core/src/types/builtin-tasks/index.ts b/packages/hardhat-core/src/types/builtin-tasks/index.ts index 46adf0f865..5ffca74cc1 100644 --- a/packages/hardhat-core/src/types/builtin-tasks/index.ts +++ b/packages/hardhat-core/src/types/builtin-tasks/index.ts @@ -1 +1,2 @@ export * from "./compile"; +export * from "./node"; diff --git a/packages/hardhat-core/src/types/builtin-tasks/node.ts b/packages/hardhat-core/src/types/builtin-tasks/node.ts new file mode 100644 index 0000000000..bce0df2f68 --- /dev/null +++ b/packages/hardhat-core/src/types/builtin-tasks/node.ts @@ -0,0 +1,10 @@ +import { EIP1193Provider } from "../provider"; + +export interface JsonRpcServer { + getProvider(name: string): EIP1193Provider; + + listen(): Promise<{ address: string; port: number }>; + waitUntilClosed(): Promise; + + close(): Promise; +} From 9b20f94c9072c2059d2f9e7fe15268cfa0ef93cc Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Wed, 14 Oct 2020 12:21:59 -0300 Subject: [PATCH 12/24] Simplify logic when creating the provider --- .../hardhat-core/src/builtin-tasks/node.ts | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index b8ad8669cc..4f7827d21d 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -65,18 +65,18 @@ subtask(TASK_NODE_GET_PROVIDER) }, { artifacts, config, network } ): Promise => { + let provider = network.provider; + if (network.name !== HARDHAT_NETWORK_NAME) { const networkConfig = config.networks[HARDHAT_NETWORK_NAME]; - return lazyObject(() => { - log(`Creating hardhat provider for JSON-RPC sever`); - return createProvider( - network.name, - { ...networkConfig, loggingEnabled: true }, - config.paths, - artifacts - ); - }); + log(`Creating hardhat provider for JSON-RPC server`); + provider = createProvider( + network.name, + networkConfig, + config.paths, + artifacts + ); } const hardhatNetworkConfig = config.networks[HARDHAT_NETWORK_NAME]; @@ -87,7 +87,7 @@ subtask(TASK_NODE_GET_PROVIDER) // we use the hardhat_reset RPC method to enable the fork if (forkUrl !== undefined) { - await network.provider.request({ + await provider.request({ method: "hardhat_reset", params: [ { @@ -106,13 +106,13 @@ subtask(TASK_NODE_GET_PROVIDER) ); } - // enable logging for in-memory hardhat network provider - await network.provider.request({ + // enable logging + await provider.request({ method: "hardhat_setLoggingEnabled", params: [true], }); - return network.provider; + return provider; } ); From e7471036161bb8e34ba7d003a490ebb39703d3f3 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Wed, 14 Oct 2020 12:32:40 -0300 Subject: [PATCH 13/24] Make fork params optional in subtask --- packages/hardhat-core/src/builtin-tasks/node.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 4f7827d21d..a9d023ebd5 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -52,8 +52,8 @@ Private Key: ${privateKey} } subtask(TASK_NODE_GET_PROVIDER) - .addParam("forkUrl", undefined, undefined, types.string) - .addParam("forkBlockNumber", undefined, undefined, types.int) + .addOptionalParam("forkUrl", undefined, undefined, types.string) + .addOptionalParam("forkBlockNumber", undefined, undefined, types.int) .setAction( async ( { From 3a9ece542c5125dc193de92cba6518f710a861f8 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Wed, 14 Oct 2020 12:32:53 -0300 Subject: [PATCH 14/24] Fix wrong network name --- packages/hardhat-core/src/builtin-tasks/node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index a9d023ebd5..24ad532eda 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -72,7 +72,7 @@ subtask(TASK_NODE_GET_PROVIDER) log(`Creating hardhat provider for JSON-RPC server`); provider = createProvider( - network.name, + HARDHAT_NETWORK_NAME, networkConfig, config.paths, artifacts From dc2d57c92b529365dce735e30a1e61a53e12ee28 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Wed, 14 Oct 2020 14:42:51 -0300 Subject: [PATCH 15/24] Use 0.0.0.0 as default hostname when inside docker --- .../hardhat-core/src/builtin-tasks/node.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 24ad532eda..308410d219 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -1,4 +1,5 @@ import chalk from "chalk"; +import fsExtra from "fs-extra"; import debug from "debug"; import { BN, bufferToHex, privateToAddress, toBuffer } from "ethereumjs-util"; @@ -171,7 +172,7 @@ task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") .addOptionalParam( "hostname", "The host to which to bind to for new connections", - "localhost", + undefined, types.string ) .addOptionalParam( @@ -197,12 +198,12 @@ task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") { forkBlockNumber, forkUrl, - hostname, + hostname: hostnameParam, port, }: { forkBlockNumber?: number; forkUrl?: string; - hostname: string; + hostname?: string; port: number; }, { config, hardhatArguments, network, run } @@ -223,6 +224,20 @@ task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") forkUrl, }); + // the default hostname is "localhost" unless we are inside a docker + // container, in that case we use "0.0.0.0" + let hostname: string; + if (hostnameParam !== undefined) { + hostname = hostnameParam; + } else { + const insideDocker = fsExtra.existsSync("/.dockerenv"); + if (insideDocker) { + hostname = "0.0.0.0"; + } else { + hostname = "localhost"; + } + } + const server: JsonRpcServer = await run(TASK_NODE_CREATE_SERVER, { hostname, port, From cdb92eabaf802395b6e02aa65e9731c33b2d018e Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Wed, 14 Oct 2020 18:49:05 -0300 Subject: [PATCH 16/24] Fix linter and error-list test --- packages/hardhat-core/src/builtin-tasks/node.ts | 2 +- packages/hardhat-core/src/internal/core/errors-list.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 308410d219..5328544c14 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -1,7 +1,7 @@ import chalk from "chalk"; -import fsExtra from "fs-extra"; import debug from "debug"; import { BN, bufferToHex, privateToAddress, toBuffer } from "ethereumjs-util"; +import fsExtra from "fs-extra"; import { HARDHAT_NETWORK_NAME } from "../internal/constants"; import { subtask, task, types } from "../internal/core/config/config-env"; diff --git a/packages/hardhat-core/src/internal/core/errors-list.ts b/packages/hardhat-core/src/internal/core/errors-list.ts index 42ecbd0d1d..82408121a1 100644 --- a/packages/hardhat-core/src/internal/core/errors-list.ts +++ b/packages/hardhat-core/src/internal/core/errors-list.ts @@ -786,7 +786,7 @@ To start the JSON-RPC server, retry the command without the --network parameter. shouldBeReported: false, }, NODE_FORK_BLOCK_NUMBER_WITHOUT_URL: { - number: 608, + number: 607, message: `You specified a fork block number but not an URL.`, title: "Missing fork URL", description: `You passed a block number to fork from, but not URL. Hardhat cannot fork From d38b9bb32c0423d5a1da928e8b8f3bfaf9d5a345 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Wed, 14 Oct 2020 21:03:43 -0300 Subject: [PATCH 17/24] Add hardhat_setLoggingEnabled to private rpc methods --- .../src/internal/hardhat-network/provider/provider.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts b/packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts index dc6e0bdfc9..0132843de5 100644 --- a/packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts +++ b/packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts @@ -42,7 +42,10 @@ import { const log = debug("hardhat:core:hardhat-network:provider"); // Set of methods that are never logged -const PRIVATE_RPC_METHODS = new Set(["hardhat_getStackTraceFailuresCount"]); +const PRIVATE_RPC_METHODS = new Set([ + "hardhat_getStackTraceFailuresCount", + "hardhat_setLoggingEnabled", +]); // tslint:disable only-hardhat-error From 69b541e705abdce1ed10fb2846b1681a5dc409df Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Thu, 15 Oct 2020 14:09:51 -0300 Subject: [PATCH 18/24] Update packages/hardhat-core/src/builtin-tasks/node.ts Co-authored-by: Patricio Palladino --- packages/hardhat-core/src/builtin-tasks/node.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index 5328544c14..dfc9e429c6 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -171,7 +171,7 @@ subtask(TASK_NODE_SERVER_READY) task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") .addOptionalParam( "hostname", - "The host to which to bind to for new connections", + "The host to which to bind to for new connections (Defaults to 127.0.0.1 running locally, and 0.0.0.0 in Docker)", undefined, types.string ) From 5669d836fdea35124ac308ea6ee4ca178b3470e3 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Thu, 15 Oct 2020 14:13:15 -0300 Subject: [PATCH 19/24] Move logs to the TASK_NODE_SERVER_READY subtask --- .../hardhat-core/src/builtin-tasks/node.ts | 44 +++++++++++++------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index dfc9e429c6..a7a74a2b18 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -160,11 +160,33 @@ subtask(TASK_NODE_SERVER_CREATED) * This subtask will be run when the server is ready to accept requests */ subtask(TASK_NODE_SERVER_READY) + .addParam("address", undefined, undefined, types.string) + .addParam("port", undefined, undefined, types.int) .addParam("provider", undefined, undefined, types.any) .addParam("server", undefined, undefined, types.any) .setAction( - async ({}: { provider: EthereumProvider; server: JsonRpcServer }) => { - // this task is meant to be overriden by plugin writers + async ( + { + address, + port, + }: { + address: string; + port: number; + provider: EthereumProvider; + server: JsonRpcServer; + }, + { config } + ) => { + console.log( + chalk.green( + `Started HTTP and WebSocket JSON-RPC server at http://${address}:${port}/` + ) + ); + + console.log(); + + const networkConfig = config.networks[HARDHAT_NETWORK_NAME]; + logHardhatNetworkAccounts(networkConfig); } ); @@ -248,14 +270,6 @@ task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") const { port: actualPort, address } = await server.listen(); - console.log( - chalk.green( - `Started HTTP and WebSocket JSON-RPC server at http://${address}:${actualPort}/` - ) - ); - - console.log(); - try { await watchCompilerOutput(provider, config.paths); } catch (error) { @@ -273,10 +287,12 @@ task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") Reporter.reportError(error); } - const networkConfig = config.networks[HARDHAT_NETWORK_NAME]; - logHardhatNetworkAccounts(networkConfig); - - await run(TASK_NODE_SERVER_READY, { provider, server }); + await run(TASK_NODE_SERVER_READY, { + address, + port: actualPort, + provider, + server, + }); await server.waitUntilClosed(); } catch (error) { From 23060264163679f7dedb6ba566ae9c15a3283b33 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Thu, 15 Oct 2020 14:18:41 -0300 Subject: [PATCH 20/24] Remove getProvider from JsonRpcServer interface --- packages/hardhat-core/src/types/builtin-tasks/node.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/hardhat-core/src/types/builtin-tasks/node.ts b/packages/hardhat-core/src/types/builtin-tasks/node.ts index bce0df2f68..9d6294b587 100644 --- a/packages/hardhat-core/src/types/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/types/builtin-tasks/node.ts @@ -1,8 +1,4 @@ -import { EIP1193Provider } from "../provider"; - export interface JsonRpcServer { - getProvider(name: string): EIP1193Provider; - listen(): Promise<{ address: string; port: number }>; waitUntilClosed(): Promise; From 18026e3359e80dc9bfe91f075b0815d230eb21cd Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Thu, 15 Oct 2020 14:28:41 -0300 Subject: [PATCH 21/24] Add hostname and port params to node subtask --- packages/hardhat-core/src/builtin-tasks/node.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index a7a74a2b18..a90473fc12 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -148,10 +148,17 @@ subtask(TASK_NODE_CREATE_SERVER) * not ready for receiving requests yet. */ subtask(TASK_NODE_SERVER_CREATED) + .addParam("hostname", undefined, undefined, types.string) + .addParam("port", undefined, undefined, types.int) .addParam("provider", undefined, undefined, types.any) .addParam("server", undefined, undefined, types.any) .setAction( - async ({}: { provider: EthereumProvider; server: JsonRpcServer }) => { + async ({}: { + hostname: string; + port: number; + provider: EthereumProvider; + server: JsonRpcServer; + }) => { // this task is meant to be overriden by plugin writers } ); @@ -266,7 +273,12 @@ task(TASK_NODE, "Starts a JSON-RPC server on top of Hardhat Network") provider, }); - await run(TASK_NODE_SERVER_CREATED, { provider, server }); + await run(TASK_NODE_SERVER_CREATED, { + hostname, + port, + provider, + server, + }); const { port: actualPort, address } = await server.listen(); From 10381fa3120c524bb05653dd472ff8fac962e098 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Thu, 15 Oct 2020 14:29:05 -0300 Subject: [PATCH 22/24] Remove unnecessary import --- packages/hardhat-core/src/builtin-tasks/node.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/hardhat-core/src/builtin-tasks/node.ts b/packages/hardhat-core/src/builtin-tasks/node.ts index a90473fc12..a88b6d92d7 100644 --- a/packages/hardhat-core/src/builtin-tasks/node.ts +++ b/packages/hardhat-core/src/builtin-tasks/node.ts @@ -13,7 +13,6 @@ import { JsonRpcServerConfig, } from "../internal/hardhat-network/jsonrpc/server"; import { Reporter } from "../internal/sentry/reporter"; -import { lazyObject } from "../internal/util/lazy"; import { EthereumProvider, HardhatNetworkConfig, From 05e32f44cc0fefdcebae93d331057ea964388f6e Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Thu, 15 Oct 2020 14:29:14 -0300 Subject: [PATCH 23/24] Fix typecheck problem --- packages/hardhat-core/src/internal/sentry/transport.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/hardhat-core/src/internal/sentry/transport.ts b/packages/hardhat-core/src/internal/sentry/transport.ts index 8517e84099..fd974475c8 100644 --- a/packages/hardhat-core/src/internal/sentry/transport.ts +++ b/packages/hardhat-core/src/internal/sentry/transport.ts @@ -9,7 +9,9 @@ export function getSubprocessTransport(): any { class SubprocessTransport extends Transports.BaseTransport { public async sendEvent(event: Event): Promise { - const { verbose = false, configPath } = event.extra ?? {}; + const extra: { verbose?: boolean; configPath?: string } = + event.extra ?? {}; + const { verbose = false, configPath } = extra; // don't send user's full config path for privacy reasons delete event.extra?.configPath; From ff66873f6934e9b38e02742b9c5784494b9f4886 Mon Sep 17 00:00:00 2001 From: Franco Victorio Date: Thu, 15 Oct 2020 15:42:06 -0300 Subject: [PATCH 24/24] Fix error code numbers --- packages/hardhat-core/src/internal/core/errors-list.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/hardhat-core/src/internal/core/errors-list.ts b/packages/hardhat-core/src/internal/core/errors-list.ts index 149fafbabf..a71dfe50a7 100644 --- a/packages/hardhat-core/src/internal/core/errors-list.ts +++ b/packages/hardhat-core/src/internal/core/errors-list.ts @@ -171,7 +171,7 @@ Please install Hardhat locally using npm or Yarn, and try again.`, shouldBeReported: false, }, TS_NODE_NOT_INSTALLED: { - number: 14, + number: 13, message: `Your Hardhat project uses typescript, but ts-node is not installed. Please run: npm install --save-dev ts-node`, @@ -182,7 +182,7 @@ Please run this and try again: npm install --save-dev ts-node`, shouldBeReported: false, }, TYPESCRIPT_NOT_INSTALLED: { - number: 15, + number: 14, message: `Your Hardhat project uses typescript, but it's not installed. Please run: npm install --save-dev typescript`,