Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
MempoolOrder,
} from "./node-types";
import {
edrRpcDebugTraceToHardhat,
edrTracingMessageResultToMinimalEVMResult,
edrTracingMessageToMinimalMessage,
edrTracingStepToMinimalInterpreterStep,
Expand Down Expand Up @@ -157,11 +156,14 @@
_vm: MinimalEthereumJsVm;
},
private readonly _subscriptionConfig: SubscriptionConfig,
// Store the initial `genesisAccounts`, `cacheDir`, and `chainOverrides` for `hardhat_reset`
// calls, in case there is switching between local and fork configurations.
// Store the initial `genesisAccounts`, `cacheDir`, `chainOverrides`, and local-network
// genesis values for `hardhat_reset` calls, in case there is switching between local
// and fork configurations.
private readonly _originalGenesisAccounts: GenesisAccount[],
private readonly _originalCacheDir: string | undefined,
private readonly _originalChainOverrides: ChainOverride[] | undefined
private readonly _originalChainOverrides: ChainOverride[] | undefined,
private readonly _originalGenesisBlockGasLimit: bigint,
private readonly _originalGenesisBlockTime: bigint | undefined
) {
super();
}
Expand Down Expand Up @@ -200,9 +202,15 @@

const cacheDir = config.forkCachePath;

let fork;
const genesisBlockGasLimit = BigInt(config.blockGasLimit);
const genesisBlockTime =
config.initialDate !== undefined
? BigInt(Math.floor(config.initialDate.getTime() / 1000))
: undefined;

let network;
if (config.forkConfig !== undefined) {
fork = {
network = {
blockNumber:
config.forkConfig.blockNumber !== undefined
? BigInt(config.forkConfig.blockNumber)
Expand All @@ -212,13 +220,13 @@
httpHeaders: httpHeadersToEdr(config.forkConfig.httpHeaders),
url: config.forkConfig.jsonRpcUrl,
};
} else {
network = {
genesisBlockGasLimit,
genesisBlockTime,
};
}

const initialDate =
config.initialDate !== undefined
? BigInt(Math.floor(config.initialDate.getTime() / 1000))
: undefined;

// To accommodate construction ordering, we need an adapter to forward events
// from the EdrProvider callback to the wrapper's listener
const eventAdapter = new EdrProviderEventAdapter();
Expand All @@ -230,7 +238,7 @@
const edrHardfork = ethereumsjsHardforkToEdrSpecId(hardforkName);

const [genesisState, ownedAccounts] = _genesisStateAndOwnedAccounts(
fork !== undefined,
config.forkConfig !== undefined,
edrHardfork,
config.genesisAccounts
);
Expand All @@ -247,26 +255,26 @@
allowUnlimitedContractSize: config.allowUnlimitedContractSize,
bailOnCallFailure: config.throwOnCallFailures,
bailOnTransactionFailure: config.throwOnTransactionFailures,
blockGasLimit: BigInt(config.blockGasLimit),
chainId: BigInt(config.chainId),
coinbase: Buffer.from(coinbase.slice(2), "hex"),
defaultTransactionGasLimit: BigInt(config.blockGasLimit),
precompileOverrides,
fork,
genesisState,
hardfork: edrHardfork,
initialDate,
initialBaseFeePerGas:
config.initialBaseFeePerGas !== undefined
? BigInt(config.initialBaseFeePerGas!)
: undefined,
minGasPrice: config.minGasPrice,
mining: {
autoMine: config.automine,
blockGasLimit: BigInt(config.blockGasLimit),
interval: ethereumjsIntervalMiningConfigToEdr(config.intervalMining),
memPool: {
order: ethereumjsMempoolOrderToEdrMineOrdering(config.mempoolOrder),
},
},
network,
networkId: BigInt(config.networkId),
observability: {},
ownedAccounts,
Comment on lines 269 to 280
Expand Down Expand Up @@ -314,7 +322,7 @@
const context = await getGlobalEdrContext();
const provider = await context.createProvider(
GENERIC_CHAIN_TYPE,
edrProviderConfig,

Check failure on line 325 in packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument of type '{ allowBlocksWithSameTimestamp: boolean; allowUnlimitedContractSize: boolean; bailOnCallFailure: boolean; bailOnTransactionFailure: boolean; chainId: bigint; coinbase: Buffer<ArrayBuffer>; ... 11 more ...; transactionGasCap: bigint | undefined; }' is not assignable to parameter of type 'ProviderConfig'.
edrLoggerConfig,
edrSubscriptionConfig,
contractDecoder
Expand All @@ -326,13 +334,15 @@

const wrapper = new EdrProviderWrapper(
provider,
edrProviderConfig,

Check failure on line 337 in packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts

View workflow job for this annotation

GitHub Actions / Lint

Argument of type '{ allowBlocksWithSameTimestamp: boolean; allowUnlimitedContractSize: boolean; bailOnCallFailure: boolean; bailOnTransactionFailure: boolean; chainId: bigint; coinbase: Buffer<ArrayBuffer>; ... 11 more ...; transactionGasCap: bigint | undefined; }' is not assignable to parameter of type 'ProviderConfig'.
edrLoggerConfig,
minimalEthereumJsNode,
edrSubscriptionConfig,
config.genesisAccounts,
cacheDir,
chainOverrides
chainOverrides,
genesisBlockGasLimit,
genesisBlockTime
);

// Pass through all events from the provider
Expand Down Expand Up @@ -392,11 +402,8 @@
this._node._vm.events.eventNames().length > 0;

if (needsTraces) {
const rawTraces = responseObject.traces;
for (const rawTrace of rawTraces) {
// For other consumers in JS we need to marshall the entire trace over FFI
const trace = rawTrace.trace;

const rawTraces = responseObject.traces();

Check failure on line 405 in packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts

View workflow job for this annotation

GitHub Actions / Lint

This expression is not callable because it is a 'get' accessor. Did you mean to use it without '()'?
for (const trace of rawTraces) {
// beforeTx event
if (this._node._vm.events.listenerCount("beforeTx") > 0) {
this._node._vm.events.emit("beforeTx");
Expand All @@ -413,7 +420,7 @@
}
}
// afterMessage event
else if ("executionResult" in traceItem) {
else if ("execResult" in traceItem) {
if (this._node._vm.evm.events.listenerCount("afterMessage") > 0) {
this._node._vm.evm.events.emit(
"afterMessage",
Expand Down Expand Up @@ -442,20 +449,37 @@
if (isErrorResponse(response)) {
let error;

let stackTrace: SolidityStackTrace | null = null;
try {
stackTrace = responseObject.stackTrace();
} catch (e) {
log("Failed to get stack trace: %O", e);
}

if (stackTrace !== null) {
error = encodeSolidityStackTrace(response.error.message, stackTrace);
const stackTrace = responseObject.stackTrace();

if (stackTrace?.kind === "StackTrace") {
error = encodeSolidityStackTrace(
response.error.message,
// EDR's `SolidityStackTraceEntry` union includes
// `CheatcodeErrorStackTraceEntry`, which Hardhat's local copy
// doesn't know about. Cheatcodes only fire from solidity-test
// runs, never from the network-provider path; the cast is safe
// at runtime.
stackTrace.entries as SolidityStackTrace
);
// Pass data and transaction hash from the original error
(error as any).data = response.error.data?.data ?? undefined;
(error as any).transactionHash =
response.error.data?.transactionHash ?? undefined;
} else {
if (stackTrace !== null) {
switch (stackTrace.kind) {
case "UnexpectedError":
log(
"Failed to get stack trace due to error: %O",
stackTrace.errorMessage
);
break;
case "HeuristicFailed":
log("Failed to get stack trace due to failing heuristics");
break;
}
}

if (response.error.code === InvalidArgumentsError.CODE) {
error = new InvalidArgumentsError(response.error.message);
} else {
Expand All @@ -479,11 +503,6 @@
// e.g. `HardhatNetwork/2.19.0/@nomicfoundation/edr/0.2.0-dev`
if (args.method === "web3_clientVersion") {
return clientVersion(response.result);
} else if (
args.method === "debug_traceTransaction" ||
args.method === "debug_traceCall"
) {
return edrRpcDebugTraceToHardhat(response.result);
} else {
return response.result;
}
Expand Down Expand Up @@ -519,18 +538,19 @@
this._providerConfig.genesisState = genesisState;
this._providerConfig.ownedAccounts = ownedAccounts;

const currentNetworkConfig = this._providerConfig.network;

Check failure on line 541 in packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'network' does not exist on type 'ProviderConfig'. Did you mean 'networkId'?
const currentlyForked = "url" in currentNetworkConfig;

if (forkConfig !== undefined) {
const cacheDir =
this._providerConfig.fork === undefined
? this._originalCacheDir
: this._providerConfig.fork?.cacheDir;
const cacheDir = currentlyForked
? currentNetworkConfig.cacheDir
: this._originalCacheDir;

const chainOverrides =
this._providerConfig.fork === undefined
? this._originalChainOverrides
: this._providerConfig.fork?.chainOverrides;
const chainOverrides = currentlyForked
? currentNetworkConfig.chainOverrides
: this._originalChainOverrides;

this._providerConfig.fork = {
this._providerConfig.network = {

Check failure on line 553 in packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'network' does not exist on type 'ProviderConfig'. Did you mean 'networkId'?
blockNumber:
forkConfig.blockNumber !== undefined
? BigInt(forkConfig.blockNumber)
Expand All @@ -541,7 +561,18 @@
url: forkConfig.jsonRpcUrl,
};
} else {
this._providerConfig.fork = undefined;
const genesisBlockGasLimit = currentlyForked
? this._originalGenesisBlockGasLimit
: currentNetworkConfig.genesisBlockGasLimit;

const genesisBlockTime = currentlyForked
? this._originalGenesisBlockTime
: currentNetworkConfig.genesisBlockTime;

this._providerConfig.network = {

Check failure on line 572 in packages/hardhat-core/src/internal/hardhat-network/provider/provider.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'network' does not exist on type 'ProviderConfig'. Did you mean 'networkId'?
genesisBlockGasLimit,
genesisBlockTime,
};
}

const context = await getGlobalEdrContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,113 +174,43 @@
}
}

export function edrRpcDebugTraceToHardhat(
rpcDebugTrace: DebugTraceResult
): RpcDebugTraceOutput {
const structLogs = rpcDebugTrace.structLogs.map((log) => {
const result: RpcStructLog = {
depth: Number(log.depth),
gas: Number(log.gas),
gasCost: Number(log.gasCost),
op: log.opName,
pc: Number(log.pc),
};

if (log.memory !== undefined) {
result.memory = log.memory;
}

if (log.stack !== undefined) {
// Remove 0x prefix which is required by EIP-3155, but not expected by Hardhat.
result.stack = log.stack?.map((item) => item.slice(2));
}

if (log.storage !== undefined) {
result.storage = Object.fromEntries(
Object.entries(log.storage).map(([key, value]) => {
return [key.slice(2), value.slice(2)];
})
);
}

if (log.error !== undefined) {
result.error = {
message: log.error,
};
}

return result;
});

// REVM trace adds initial STOP that Hardhat doesn't expect
if (structLogs.length > 0 && structLogs[0].op === "STOP") {
structLogs.shift();
}

let returnValue = rpcDebugTrace.output?.toString() ?? "";
if (returnValue === "0x") {
returnValue = "";
}

return {
failed: !rpcDebugTrace.pass,
gas: Number(rpcDebugTrace.gasUsed),
returnValue,
structLogs,
};
}

export function edrTracingStepToMinimalInterpreterStep(
step: TracingStep
): MinimalInterpreterStep {
const minimalInterpreterStep: MinimalInterpreterStep = {
pc: Number(step.pc),
pc: step.pc,

Check failure on line 181 in packages/hardhat-core/src/internal/hardhat-network/provider/utils/convertToEdr.ts

View workflow job for this annotation

GitHub Actions / Lint

Type 'bigint' is not assignable to type 'number'.
depth: step.depth,
opcode: {
name: step.opcode,
name: step.opcode.name,

Check failure on line 184 in packages/hardhat-core/src/internal/hardhat-network/provider/utils/convertToEdr.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'name' does not exist on type 'string'.
},
stack: step.stack,
memory: step.memory,
};
Comment on lines +184 to 188

if (step.memory !== undefined) {
minimalInterpreterStep.memory = step.memory;
}

return minimalInterpreterStep;
}

export function edrTracingMessageResultToMinimalEVMResult(
tracingMessageResult: TracingMessageResult
): MinimalEVMResult {
const { result, contractAddress } = tracingMessageResult.executionResult;

// only SuccessResult has logs
const success = "logs" in result;
const execResult = tracingMessageResult.execResult;

Check failure on line 196 in packages/hardhat-core/src/internal/hardhat-network/provider/utils/convertToEdr.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'execResult' does not exist on type 'TracingMessageResult'.

const minimalEVMResult: MinimalEVMResult = {
execResult: {
executionGasUsed: result.gasUsed,
success,
success: execResult.success,
executionGasUsed: execResult.executionGasUsed,
contractAddress:
execResult.contractAddress !== undefined
? new Address(execResult.contractAddress)
: undefined,
reason: execResult.reason,
output:
execResult.output !== undefined
? Buffer.from(execResult.output)
: undefined,
},
};

// only success and exceptional halt have reason
if ("reason" in result) {
minimalEVMResult.execResult.reason = result.reason;
}
if ("output" in result) {
const { output } = result;
if (output instanceof Uint8Array) {
minimalEVMResult.execResult.output = Buffer.from(output);
} else {
minimalEVMResult.execResult.output = Buffer.from(output.returnValue);
}
}

if (contractAddress !== undefined) {
minimalEVMResult.execResult.contractAddress = new Address(contractAddress);
}

return minimalEVMResult;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/hardhat-core/test/builtin-tasks/compile.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLatestSupportedSolcVersion } from "@nomicfoundation/edr";
import { latestSupportedSolidityVersion } from "@nomicfoundation/edr";
import { assert, expect } from "chai";
import ci from "ci-info";
import * as fsExtra from "fs-extra";
Expand Down Expand Up @@ -57,7 +57,7 @@ describe("compile task", function () {
// Test to check that the last version of solc is being tested
const userConfigSolcVersion = this.env.userConfig.solidity;

const lastSolcVersion = getLatestSupportedSolcVersion();
const lastSolcVersion = latestSupportedSolidityVersion();

assert.equal(
userConfigSolcVersion,
Expand Down
Loading
Loading