Skip to content

Commit

Permalink
Add beforeTx and afterTx VM events
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio authored and alcuadrado committed May 17, 2024
1 parent b7c0a08 commit 7692b79
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ export class EdrProviderWrapper

const needsTraces =
this._node._vm.evm.events.eventNames().length > 0 ||
this._node._vm.events.eventNames().length > 0 ||
this._rawTraceCallbacks.onStep !== undefined ||
this._rawTraceCallbacks.onAfterMessage !== undefined ||
this._rawTraceCallbacks.onBeforeMessage !== undefined;
Expand All @@ -378,7 +379,14 @@ export class EdrProviderWrapper
const rawTraces = responseObject.traces;
for (const rawTrace of rawTraces) {
const trace = rawTrace.trace();

// beforeTx event
if (this._node._vm.events.listenerCount("beforeTx") > 0) {
this._node._vm.events.emit("beforeTx");
}

for (const traceItem of trace) {
// step event
if ("pc" in traceItem) {
if (this._node._vm.evm.events.listenerCount("step") > 0) {
this._node._vm.evm.events.emit(
Expand All @@ -389,7 +397,9 @@ export class EdrProviderWrapper
if (this._rawTraceCallbacks.onStep !== undefined) {
await this._rawTraceCallbacks.onStep(traceItem);
}
} else if ("executionResult" in traceItem) {
}
// afterMessage event
else if ("executionResult" in traceItem) {
if (this._node._vm.evm.events.listenerCount("afterMessage") > 0) {
this._node._vm.evm.events.emit(
"afterMessage",
Expand All @@ -401,7 +411,9 @@ export class EdrProviderWrapper
traceItem.executionResult
);
}
} else {
}
// beforeMessage event
else {
if (this._node._vm.evm.events.listenerCount("beforeMessage") > 0) {
this._node._vm.evm.events.emit(
"beforeMessage",
Expand All @@ -413,6 +425,11 @@ export class EdrProviderWrapper
}
}
}

// afterTx event
if (this._node._vm.events.listenerCount("afterTx") > 0) {
this._node._vm.events.emit("afterTx");
}
}
}

Expand Down Expand Up @@ -478,6 +495,10 @@ export class EdrProviderWrapper
);
}

private _setVerboseTracing(enabled: boolean) {
this._provider.setVerboseTracing(enabled);

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

View workflow job for this annotation

GitHub Actions / Lint

Property 'setVerboseTracing' does not exist on type 'Provider'.
}

private _ethEventListener(event: SubscriptionEvent) {
const subscription = `0x${event.filterId.toString(16)}`;
const results = Array.isArray(event.result) ? event.result : [event.result];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import { AsyncEventEmitter } from "@nomicfoundation/ethereumjs-util";
* interface only has the things used by those plugins.
*/
export interface MinimalEthereumJsVm {
events: AsyncEventEmitter<MinimalEthereumJsVmEvents>;
evm: {
events: AsyncEventEmitter<MinimalEthereumJsVmEvents>;
events: AsyncEventEmitter<MinimalEthereumJsEvmEvents>;
};
stateManager: {
putContractCode: (address: Address, code: Buffer) => Promise<void>;
Expand All @@ -27,10 +28,18 @@ export interface MinimalEthereumJsVm {
};
}

// we need to use a type instead of an interface to satisfy the type constarint
// we need to use a type instead of an interface to satisfy the type constraint
// of the AsyncEventEmitter type param
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
type MinimalEthereumJsVmEvents = {
beforeTx: () => void;
afterTx: () => void;
};

// we need to use a type instead of an interface to satisfy the type constraint
// of the AsyncEventEmitter type param
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
type MinimalEthereumJsEvmEvents = {
beforeMessage: (
data: MinimalMessage,
resolve?: (result?: any) => void
Expand All @@ -46,13 +55,15 @@ type MinimalEthereumJsVmEvents = {
};

export class MinimalEthereumJsVmEventEmitter extends AsyncEventEmitter<MinimalEthereumJsVmEvents> {}
export class MinimalEthereumJsEvmEventEmitter extends AsyncEventEmitter<MinimalEthereumJsEvmEvents> {}

export function getMinimalEthereumJsVm(
provider: EdrProviderT
): MinimalEthereumJsVm {
const minimalEthereumJsVm: MinimalEthereumJsVm = {
events: new MinimalEthereumJsVmEventEmitter(),
evm: {
events: new MinimalEthereumJsVmEventEmitter(),
events: new MinimalEthereumJsEvmEventEmitter(),
},
stateManager: {
putContractCode: async (address: Address, code: Buffer) => {
Expand Down

0 comments on commit 7692b79

Please sign in to comment.