Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Optimism provider #5840

Closed
wants to merge 4 commits into from
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
2 changes: 1 addition & 1 deletion packages/hardhat-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
"dependencies": {
"@ethersproject/abi": "^5.1.2",
"@metamask/eth-sig-util": "^4.0.0",
"@nomicfoundation/edr": "^0.6.4",
"@ignored/edr-optimism": "^0.6.4",
"@nomicfoundation/ethereumjs-common": "4.0.4",
"@nomicfoundation/ethereumjs-tx": "5.0.4",
"@nomicfoundation/ethereumjs-util": "9.0.4",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type {
RawTrace,
Response,
SubscriptionEvent,
} from "@nomicfoundation/edr";
} from "@ignored/edr-optimism";
import { Common } from "@nomicfoundation/ethereumjs-common";
import chalk from "chalk";
import debug from "debug";
Expand Down Expand Up @@ -84,14 +84,28 @@ export const DEFAULT_COINBASE = "0xc014ba5ec014ba5ec014ba5ec014ba5ec014ba5e";
let _globalEdrContext: EdrContext | undefined;

// Lazy initialize the global EDR context.
export function getGlobalEdrContext(): EdrContext {
const { EdrContext } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
export async function getGlobalEdrContext(): Promise<EdrContext> {
const {
EdrContext,
GENERIC_CHAIN_TYPE,
OPTIMISM_CHAIN_TYPE,
genericChainProviderFactory,
optimismProviderFactory,
} = requireNapiRsModule(
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

if (_globalEdrContext === undefined) {
// Only one is allowed to exist
_globalEdrContext = new EdrContext();
await _globalEdrContext.registerProviderFactory(
GENERIC_CHAIN_TYPE,
genericChainProviderFactory()
);
await _globalEdrContext.registerProviderFactory(
OPTIMISM_CHAIN_TYPE,
optimismProviderFactory()
);
}

return _globalEdrContext;
Expand Down Expand Up @@ -193,9 +207,9 @@ export class EdrProviderWrapper
loggerConfig: LoggerConfig,
tracingConfig?: TracingConfig
): Promise<EdrProviderWrapper> {
const { Provider } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
const { GENERIC_CHAIN_TYPE } = requireNapiRsModule(
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

const coinbase = config.coinbase ?? DEFAULT_COINBASE;

Expand Down Expand Up @@ -226,8 +240,9 @@ export class EdrProviderWrapper

const hardforkName = getHardforkName(config.hardfork);

const provider = await Provider.withConfig(
getGlobalEdrContext(),
const context = await getGlobalEdrContext();
const provider = await context.createProvider(
GENERIC_CHAIN_TYPE,
{
allowBlocksWithSameTimestamp:
config.allowBlocksWithSameTimestamp ?? false,
Expand Down Expand Up @@ -298,8 +313,10 @@ export class EdrProviderWrapper
}
},
},
(event: SubscriptionEvent) => {
eventAdapter.emit("ethEvent", event);
{
subscriptionCallback: (event: SubscriptionEvent) => {
eventAdapter.emit("ethEvent", event);
},
}
);

Expand Down Expand Up @@ -476,18 +493,20 @@ export class EdrProviderWrapper
}

// temporarily added to make smock work with HH+EDR
private _setCallOverrideCallback(callback: CallOverrideCallback) {
private async _setCallOverrideCallback(
callback: CallOverrideCallback
): Promise<void> {
this._callOverrideCallback = callback;

this._provider.setCallOverrideCallback(
await this._provider.setCallOverrideCallback(
async (address: Buffer, data: Buffer) => {
return this._callOverrideCallback?.(address, data);
}
);
}

private _setVerboseTracing(enabled: boolean) {
this._provider.setVerboseTracing(enabled);
private async _setVerboseTracing(enabled: boolean): Promise<void> {
await this._provider.setVerboseTracing(enabled);
}

private _ethEventListener(event: SubscriptionEvent) {
Expand Down Expand Up @@ -604,7 +623,7 @@ export class EdrProviderWrapper
async function clientVersion(edrClientVersion: string): Promise<string> {
const hardhatPackage = await getPackageJson();
const edrVersion = edrClientVersion.split("/")[1];
return `HardhatNetwork/${hardhatPackage.version}/@nomicfoundation/edr/${edrVersion}`;
return `HardhatNetwork/${hardhatPackage.version}/@ignored/edr-optimism/${edrVersion}`;
}

export async function createHardhatNetworkProvider(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { requireNapiRsModule } from "../../../common/napi-rs";

const { ReturnData } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

export { ReturnData };
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,26 @@ import type {
TracingMessage,
TracingMessageResult,
TracingStep,
} from "@nomicfoundation/edr";
} from "@ignored/edr-optimism";
import {
FRONTIER,
HOMESTEAD,
DAO_FORK,
TANGERINE,
SPURIOUS_DRAGON,
BYZANTIUM,
CONSTANTINOPLE,
PETERSBURG,
ISTANBUL,
MUIR_GLACIER,
BERLIN,
LONDON,
ARROW_GLACIER,
GRAY_GLACIER,
MERGE,
SHANGHAI,
CANCUN,
} from "@ignored/edr-optimism";
import { Address } from "@nomicfoundation/ethereumjs-util";

import { requireNapiRsModule } from "../../../../common/napi-rs";
Expand All @@ -21,46 +40,42 @@ import {

/* eslint-disable @nomicfoundation/hardhat-internal-rules/only-hardhat-error */

export function ethereumsjsHardforkToEdrSpecId(hardfork: HardforkName): SpecId {
const { SpecId } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");

export function ethereumsjsHardforkToEdrSpecId(hardfork: HardforkName): string {
switch (hardfork) {
case HardforkName.FRONTIER:
return SpecId.Frontier;
return FRONTIER;
case HardforkName.HOMESTEAD:
return SpecId.Homestead;
return HOMESTEAD;
case HardforkName.DAO:
return SpecId.DaoFork;
return DAO_FORK;
case HardforkName.TANGERINE_WHISTLE:
return SpecId.Tangerine;
return TANGERINE;
case HardforkName.SPURIOUS_DRAGON:
return SpecId.SpuriousDragon;
return SPURIOUS_DRAGON;
case HardforkName.BYZANTIUM:
return SpecId.Byzantium;
return BYZANTIUM;
case HardforkName.CONSTANTINOPLE:
return SpecId.Constantinople;
return CONSTANTINOPLE;
case HardforkName.PETERSBURG:
return SpecId.Petersburg;
return PETERSBURG;
case HardforkName.ISTANBUL:
return SpecId.Istanbul;
return ISTANBUL;
case HardforkName.MUIR_GLACIER:
return SpecId.MuirGlacier;
return MUIR_GLACIER;
case HardforkName.BERLIN:
return SpecId.Berlin;
return BERLIN;
case HardforkName.LONDON:
return SpecId.London;
return LONDON;
case HardforkName.ARROW_GLACIER:
return SpecId.ArrowGlacier;
return ARROW_GLACIER;
case HardforkName.GRAY_GLACIER:
return SpecId.GrayGlacier;
return GRAY_GLACIER;
case HardforkName.MERGE:
return SpecId.Merge;
return MERGE;
case HardforkName.SHANGHAI:
return SpecId.Shanghai;
return SHANGHAI;
case HardforkName.CANCUN:
return SpecId.Cancun;
return CANCUN;
default:
const _exhaustiveCheck: never = hardfork;
throw new Error(
Expand All @@ -71,8 +86,8 @@ export function ethereumsjsHardforkToEdrSpecId(hardfork: HardforkName): SpecId {

export function edrSpecIdToEthereumHardfork(specId: SpecId): HardforkName {
const { SpecId } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

switch (specId) {
case SpecId.Frontier:
Expand Down Expand Up @@ -138,8 +153,8 @@ export function ethereumjsMempoolOrderToEdrMineOrdering(
mempoolOrder: MempoolOrder
): MineOrdering {
const { MineOrdering } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

switch (mempoolOrder) {
case "fifo":
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { requireNapiRsModule } from "../../../../common/napi-rs";

const { ExitCode } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

export { ExitCode };
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Provider as EdrProviderT } from "@nomicfoundation/edr";
import type { Provider as EdrProviderT } from "@ignored/edr-optimism";
import type { Address } from "@nomicfoundation/ethereumjs-util";
import type {
MinimalEVMResult,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ExceptionalHalt, SuccessReason } from "@nomicfoundation/edr";
import type { ExceptionalHalt, SuccessReason } from "@ignored/edr-optimism";
import type { Address } from "@nomicfoundation/ethereumjs-util";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { requireNapiRsModule } from "../../../common/napi-rs";

const { createModelsAndDecodeBytecodes } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

export { createModelsAndDecodeBytecodes };
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { requireNapiRsModule } from "../../../common/napi-rs";

const { printMessageTrace, printStackTrace } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

export { printMessageTrace, printStackTrace };
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { requireNapiRsModule } from "../../../common/napi-rs";

const { linkHexStringBytecode } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

export { linkHexStringBytecode };
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {
PrecompileMessageTrace,
CreateMessageTrace,
CallMessageTrace,
} from "@nomicfoundation/edr";
} from "@ignored/edr-optimism";

export type { PrecompileMessageTrace, CreateMessageTrace, CallMessageTrace };

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type {
ContractTooLargeErrorStackTraceEntry,
InternalFunctionCallStackEntry,
ContractCallRunOutOfGasError,
} from "@nomicfoundation/edr";
} from "@ignored/edr-optimism";

import { requireNapiRsModule } from "../../../common/napi-rs";

Expand All @@ -39,8 +39,8 @@ const {
PRECOMPILE_FUNCTION_NAME,
UNRECOGNIZED_CONTRACT_NAME,
} = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

export {
SourceReference,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { requireNapiRsModule } from "../../../common/napi-rs";

const { SolidityTracer } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

export { SolidityTracer };
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { VmTraceDecoder as VmTraceDecoderT } from "@nomicfoundation/edr";
import type { VmTraceDecoder as VmTraceDecoderT } from "@ignored/edr-optimism";
import chalk from "chalk";
import debug from "debug";
import { Reporter } from "../../sentry/reporter";
Expand All @@ -8,8 +8,8 @@ import { requireNapiRsModule } from "../../../common/napi-rs";
const log = debug("hardhat:core:hardhat-network:node");

const { VmTraceDecoder, initializeVmTraceDecoder } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

function initializeVmTraceDecoderWrapper(
vmTraceDecoder: VmTraceDecoderT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { requireNapiRsModule } from "../../../common/napi-rs";

const { VmTracer } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

export { VmTracer as VMTracer };
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ import {
} from "./execution";

const { stackTraceEntryTypeToString } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");
"@ignored/edr-optimism"
) as typeof import("@ignored/edr-optimism");

interface StackFrameDescription {
type: string;
Expand Down
Loading