-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
provider.ts
380 lines (322 loc) · 11 KB
/
provider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
import Common from "@ethereumjs/common";
import chalk from "chalk";
import debug from "debug";
import { BN } from "ethereumjs-util";
import { EventEmitter } from "events";
import fsExtra from "fs-extra";
import semver from "semver";
import type {
Artifacts,
BoundExperimentalHardhatNetworkMessageTraceHook,
EIP1193Provider,
EthSubscription,
HardhatNetworkChainsConfig,
RequestArguments,
} from "../../../types";
import {
HARDHAT_NETWORK_RESET_EVENT,
HARDHAT_NETWORK_REVERT_SNAPSHOT_EVENT,
} from "../../constants";
import {
InvalidInputError,
MethodNotFoundError,
MethodNotSupportedError,
ProviderError,
} from "../../core/providers/errors";
import { FIRST_SOLC_VERSION_SUPPORTED } from "../stack-traces/solidityTracer";
import { Mutex } from "../vendor/await-semaphore";
import { MiningTimer } from "./MiningTimer";
import { DebugModule } from "./modules/debug";
import { EthModule } from "./modules/eth";
import { EvmModule } from "./modules/evm";
import { HardhatModule } from "./modules/hardhat";
import { ModulesLogger } from "./modules/logger";
import { PersonalModule } from "./modules/personal";
import { NetModule } from "./modules/net";
import { Web3Module } from "./modules/web3";
import { HardhatNode } from "./node";
import {
ForkConfig,
GenesisAccount,
IntervalMiningConfig,
MempoolOrder,
NodeConfig,
TracingConfig,
} from "./node-types";
const log = debug("hardhat:core:hardhat-network:provider");
// Set of methods that are never logged
const PRIVATE_RPC_METHODS = new Set([
"hardhat_getStackTraceFailuresCount",
"hardhat_setLoggingEnabled",
]);
/* eslint-disable @nomiclabs/hardhat-internal-rules/only-hardhat-error */
export const DEFAULT_COINBASE = "0xc014ba5ec014ba5ec014ba5ec014ba5ec014ba5e";
export class HardhatNetworkProvider
extends EventEmitter
implements EIP1193Provider
{
private _common?: Common;
private _node?: HardhatNode;
private _ethModule?: EthModule;
private _netModule?: NetModule;
private _web3Module?: Web3Module;
private _evmModule?: EvmModule;
private _hardhatModule?: HardhatModule;
private _debugModule?: DebugModule;
private _personalModule?: PersonalModule;
private readonly _mutex = new Mutex();
constructor(
private readonly _hardfork: string,
private readonly _networkName: string,
private readonly _chainId: number,
private readonly _networkId: number,
private readonly _blockGasLimit: number,
private readonly _initialBaseFeePerGas: number | undefined,
private readonly _minGasPrice: BN,
private readonly _throwOnTransactionFailures: boolean,
private readonly _throwOnCallFailures: boolean,
private readonly _automine: boolean,
private readonly _intervalMining: IntervalMiningConfig,
private readonly _mempoolOrder: MempoolOrder,
private readonly _chains: HardhatNetworkChainsConfig,
private readonly _logger: ModulesLogger,
private readonly _genesisAccounts: GenesisAccount[] = [],
private readonly _artifacts?: Artifacts,
private readonly _allowUnlimitedContractSize = false,
private readonly _initialDate?: Date,
private readonly _experimentalHardhatNetworkMessageTraceHooks: BoundExperimentalHardhatNetworkMessageTraceHook[] = [],
private _forkConfig?: ForkConfig,
private readonly _forkCachePath?: string,
private readonly _coinbase = DEFAULT_COINBASE
) {
super();
}
public async request(args: RequestArguments): Promise<unknown> {
const release = await this._mutex.acquire();
if (args.params !== undefined && !Array.isArray(args.params)) {
throw new InvalidInputError(
"Hardhat Network doesn't support JSON-RPC params sent as an object"
);
}
try {
let result;
if (this._logger.isEnabled() && !PRIVATE_RPC_METHODS.has(args.method)) {
result = await this._sendWithLogging(args.method, args.params);
} else {
result = await this._send(args.method, args.params);
}
if (args.method === "hardhat_reset") {
this.emit(HARDHAT_NETWORK_RESET_EVENT);
}
if (args.method === "evm_revert") {
this.emit(HARDHAT_NETWORK_REVERT_SNAPSHOT_EVENT);
}
return result;
} finally {
release();
}
}
private async _sendWithLogging(
method: string,
params: any[] = []
): Promise<any> {
try {
const result = await this._send(method, params);
// We log after running the method, because we want to use different
// colors depending on whether it failed or not
// TODO: If an eth_call, eth_sendTransaction, or eth_sendRawTransaction
// fails without throwing, this will be displayed in green. It's unclear
// if this is correct. See Eth module's TODOs for more info.
if (method !== "hardhat_intervalMine") {
this._logger.printMethod(method);
const printedSomething = this._logger.printLogs();
if (printedSomething) {
this._logger.printEmptyLine();
}
}
return result;
} catch (err) {
if (
err instanceof MethodNotFoundError ||
err instanceof MethodNotSupportedError
) {
this._logger.printMethodNotSupported(method);
throw err;
}
this._logger.printFailedMethod(method);
this._logger.printLogs();
if (err instanceof Error && !this._logger.isLoggedError(err)) {
if (ProviderError.isProviderError(err)) {
this._logger.printEmptyLine();
this._logger.printErrorMessage(err.message);
const isEIP155Error =
err instanceof InvalidInputError && err.message.includes("EIP155");
if (isEIP155Error) {
this._logger.printMetaMaskWarning();
}
} else {
this._logger.printUnknownError(err);
}
}
this._logger.printEmptyLine();
throw err;
}
}
private async _send(method: string, params: any[] = []): Promise<any> {
await this._init();
if (method.startsWith("eth_")) {
return this._ethModule!.processRequest(method, params);
}
if (method.startsWith("net_")) {
return this._netModule!.processRequest(method, params);
}
if (method.startsWith("web3_")) {
return this._web3Module!.processRequest(method, params);
}
if (method.startsWith("evm_")) {
return this._evmModule!.processRequest(method, params);
}
if (method.startsWith("hardhat_")) {
return this._hardhatModule!.processRequest(method, params);
}
if (method.startsWith("debug_")) {
return this._debugModule!.processRequest(method, params);
}
if (method.startsWith("personal_")) {
return this._personalModule!.processRequest(method, params);
}
throw new MethodNotFoundError(`Method ${method} not found`);
}
private async _init() {
if (this._node !== undefined) {
return;
}
const config: NodeConfig = {
automine: this._automine,
blockGasLimit: this._blockGasLimit,
minGasPrice: this._minGasPrice,
genesisAccounts: this._genesisAccounts,
allowUnlimitedContractSize: this._allowUnlimitedContractSize,
tracingConfig: await this._makeTracingConfig(),
initialBaseFeePerGas: this._initialBaseFeePerGas,
mempoolOrder: this._mempoolOrder,
hardfork: this._hardfork,
networkName: this._networkName,
chainId: this._chainId,
networkId: this._networkId,
initialDate: this._initialDate,
forkConfig: this._forkConfig,
forkCachePath:
this._forkConfig !== undefined ? this._forkCachePath : undefined,
coinbase: this._coinbase,
chains: this._chains,
};
const [common, node] = await HardhatNode.create(config);
this._common = common;
this._node = node;
this._ethModule = new EthModule(
common,
node,
this._throwOnTransactionFailures,
this._throwOnCallFailures,
this._logger,
this._experimentalHardhatNetworkMessageTraceHooks
);
const miningTimer = this._makeMiningTimer();
this._netModule = new NetModule(common);
this._web3Module = new Web3Module();
this._evmModule = new EvmModule(
node,
miningTimer,
this._logger,
this._experimentalHardhatNetworkMessageTraceHooks
);
this._hardhatModule = new HardhatModule(
node,
(forkConfig?: ForkConfig) => this._reset(miningTimer, forkConfig),
(loggingEnabled: boolean) => {
this._logger.setEnabled(loggingEnabled);
},
this._logger,
this._experimentalHardhatNetworkMessageTraceHooks
);
this._debugModule = new DebugModule(node);
this._personalModule = new PersonalModule(node);
this._forwardNodeEvents(node);
}
private async _makeTracingConfig(): Promise<TracingConfig | undefined> {
if (this._artifacts !== undefined) {
const buildInfos = [];
const buildInfoFiles = await this._artifacts.getBuildInfoPaths();
try {
for (const buildInfoFile of buildInfoFiles) {
const buildInfo = await fsExtra.readJson(buildInfoFile);
if (semver.gte(buildInfo.solcVersion, FIRST_SOLC_VERSION_SUPPORTED)) {
buildInfos.push(buildInfo);
}
}
return {
buildInfos,
};
} catch (error) {
console.warn(
chalk.yellow(
"Stack traces engine could not be initialized. Run Hardhat with --verbose to learn more."
)
);
log(
"Solidity stack traces disabled: Failed to read solc's input and output files. Please report this to help us improve Hardhat.\n",
error
);
}
}
}
private _makeMiningTimer(): MiningTimer {
const miningTimer = new MiningTimer(this._intervalMining, async () => {
try {
await this.request({ method: "hardhat_intervalMine" });
} catch (e) {
console.error("Unexpected error calling hardhat_intervalMine:", e);
}
});
miningTimer.start();
return miningTimer;
}
private async _reset(miningTimer: MiningTimer, forkConfig?: ForkConfig) {
this._forkConfig = forkConfig;
if (this._node !== undefined) {
this._stopForwardingNodeEvents(this._node);
}
this._node = undefined;
miningTimer.stop();
await this._init();
}
private _forwardNodeEvents(node: HardhatNode) {
node.addListener("ethEvent", this._ethEventListener);
}
private _stopForwardingNodeEvents(node: HardhatNode) {
node.removeListener("ethEvent", this._ethEventListener);
}
private _ethEventListener = (payload: { filterId: BN; result: any }) => {
const subscription = `0x${payload.filterId.toString(16)}`;
const result = payload.result;
this._emitLegacySubscriptionEvent(subscription, result);
this._emitEip1193SubscriptionEvent(subscription, result);
};
private _emitLegacySubscriptionEvent(subscription: string, result: any) {
this.emit("notification", {
subscription,
result,
});
}
private _emitEip1193SubscriptionEvent(subscription: string, result: unknown) {
const message: EthSubscription = {
type: "eth_subscription",
data: {
subscription,
result,
},
};
this.emit("message", message);
}
}