Skip to content

Commit

Permalink
feat: adopt EDR multichain
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Sep 19, 2024
1 parent 3e6ed32 commit a2e715e
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,18 @@ export const DEFAULT_COINBASE = "0xc014ba5ec014ba5ec014ba5ec014ba5ec014ba5e";
let _globalEdrContext: EdrContext | undefined;

// Lazy initialize the global EDR context.
export function getGlobalEdrContext(): EdrContext {
const { EdrContext } = requireNapiRsModule(
export async function getGlobalEdrContext(): Promise<EdrContext> {
const { EdrContext, L1_CHAIN_TYPE, l1ProviderFactory } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");

if (_globalEdrContext === undefined) {
// Only one is allowed to exist
_globalEdrContext = new EdrContext();
await _globalEdrContext.registerProviderFactory(
L1_CHAIN_TYPE,
l1ProviderFactory()
);
}

return _globalEdrContext;
Expand Down Expand Up @@ -192,7 +196,7 @@ export class EdrProviderWrapper
loggerConfig: LoggerConfig,
tracingConfig?: TracingConfig
): Promise<EdrProviderWrapper> {
const { Provider } = requireNapiRsModule(
const { L1_CHAIN_TYPE } = requireNapiRsModule(
"@nomicfoundation/edr"
) as typeof import("@nomicfoundation/edr");

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

const hardforkName = getHardforkName(config.hardfork);

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

Expand Down Expand Up @@ -479,18 +486,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
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import type {
SpecId,
MineOrdering,
IntervalRange,
DebugTraceResult,
TracingMessage,
TracingMessageResult,
TracingStep,
import {
type SpecId,
type MineOrdering,
type IntervalRange,
type DebugTraceResult,
type TracingMessage,
type TracingMessageResult,
type TracingStep,
FRONTIER,

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

View workflow job for this annotation

GitHub Actions / Lint

Module '"@nomicfoundation/edr"' has no exported member 'FRONTIER'.
HOMESTEAD,

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

View workflow job for this annotation

GitHub Actions / Lint

Module '"@nomicfoundation/edr"' has no exported member 'HOMESTEAD'.
DAO_FORK,

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

View workflow job for this annotation

GitHub Actions / Lint

Module '"@nomicfoundation/edr"' has no exported member 'DAO_FORK'.
TANGERINE,

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

View workflow job for this annotation

GitHub Actions / Lint

Module '"@nomicfoundation/edr"' has no exported member 'TANGERINE'.
SPURIOUS_DRAGON,

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

View workflow job for this annotation

GitHub Actions / Lint

Module '"@nomicfoundation/edr"' has no exported member 'SPURIOUS_DRAGON'.
BYZANTIUM,

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

View workflow job for this annotation

GitHub Actions / Lint

Module '"@nomicfoundation/edr"' has no exported member 'BYZANTIUM'.
CONSTANTINOPLE,

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

View workflow job for this annotation

GitHub Actions / Lint

Module '"@nomicfoundation/edr"' has no exported member 'CONSTANTINOPLE'.
PETERSBURG,

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

View workflow job for this annotation

GitHub Actions / Lint

Module '"@nomicfoundation/edr"' has no exported member 'PETERSBURG'.
ISTANBUL,

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

View workflow job for this annotation

GitHub Actions / Lint

Module '"@nomicfoundation/edr"' has no exported member 'ISTANBUL'.
MUIR_GLACIER,

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

View workflow job for this annotation

GitHub Actions / Lint

Module '"@nomicfoundation/edr"' has no exported member 'MUIR_GLACIER'.
BERLIN,
LONDON,
ARROW_GLACIER,
GRAY_GLACIER,
MERGE,
SHANGHAI,
CANCUN,
} from "@nomicfoundation/edr";
import { Address } from "@nomicfoundation/ethereumjs-util";

Expand All @@ -21,46 +38,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 Down

0 comments on commit a2e715e

Please sign in to comment.