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 Oct 22, 2024
1 parent 09ead48 commit fd31950
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 @@ -84,14 +84,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 @@ -193,7 +197,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 @@ -476,18 +483,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,
HOMESTEAD,
DAO_FORK,
TANGERINE,
SPURIOUS_DRAGON,
BYZANTIUM,
CONSTANTINOPLE,
PETERSBURG,
ISTANBUL,
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 fd31950

Please sign in to comment.