Skip to content

Commit f225dfd

Browse files
committed
refactor: Port the VmTraceDecoder from Hardhat
1 parent 8c5ff87 commit f225dfd

File tree

1 file changed

+8
-107
lines changed

1 file changed

+8
-107
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,17 @@
11
import chalk from "chalk";
22
import debug from "debug";
3+
import { VmTraceDecoder, initializeVmTraceDecoder } from "@nomicfoundation/edr";
34
import { Reporter } from "../../sentry/reporter";
45
import { TracingConfig } from "../provider/node-types";
5-
import { createModelsAndDecodeBytecodes } from "./compiler-to-model";
6-
import { ContractsIdentifier } from "./contracts-identifier";
7-
import {
8-
isCreateTrace,
9-
isEvmStep,
10-
isPrecompileTrace,
11-
MessageTrace,
12-
} from "./message-trace";
13-
import { Bytecode, ContractFunctionType } from "./model";
14-
import {
15-
FALLBACK_FUNCTION_NAME,
16-
RECEIVE_FUNCTION_NAME,
17-
UNRECOGNIZED_CONTRACT_NAME,
18-
UNRECOGNIZED_FUNCTION_NAME,
19-
} from "./solidity-stack-trace";
206

217
const log = debug("hardhat:core:hardhat-network:node");
228

23-
export class VmTraceDecoder {
24-
constructor(private readonly _contractsIdentifier: ContractsIdentifier) {}
25-
26-
public getContractAndFunctionNamesForCall(
27-
code: Buffer,
28-
calldata?: Buffer
29-
): { contractName: string; functionName?: string } {
30-
const isCreate = calldata === undefined;
31-
const bytecode = this._contractsIdentifier.getBytecodeForCall(
32-
code,
33-
isCreate
34-
);
35-
36-
const contractName = bytecode?.contract.name ?? UNRECOGNIZED_CONTRACT_NAME;
37-
38-
if (isCreate) {
39-
return {
40-
contractName,
41-
};
42-
} else {
43-
if (bytecode === undefined) {
44-
return {
45-
contractName,
46-
functionName: "",
47-
};
48-
} else {
49-
const func = bytecode.contract.getFunctionFromSelector(
50-
calldata.slice(0, 4)
51-
);
52-
53-
const functionName: string =
54-
func === undefined
55-
? UNRECOGNIZED_FUNCTION_NAME
56-
: func.type === ContractFunctionType.FALLBACK
57-
? FALLBACK_FUNCTION_NAME
58-
: func.type === ContractFunctionType.RECEIVE
59-
? RECEIVE_FUNCTION_NAME
60-
: func.name;
61-
62-
return {
63-
contractName,
64-
functionName,
65-
};
66-
}
67-
}
68-
}
69-
70-
public tryToDecodeMessageTrace(messageTrace: MessageTrace): MessageTrace {
71-
if (isPrecompileTrace(messageTrace)) {
72-
return messageTrace;
73-
}
74-
75-
return {
76-
...messageTrace,
77-
bytecode: this._contractsIdentifier.getBytecodeForCall(
78-
messageTrace.code,
79-
isCreateTrace(messageTrace)
80-
),
81-
steps: messageTrace.steps.map((s) =>
82-
isEvmStep(s) ? s : this.tryToDecodeMessageTrace(s)
83-
),
84-
};
85-
}
86-
87-
public addBytecode(bytecode: Bytecode) {
88-
this._contractsIdentifier.addBytecode(bytecode);
89-
}
90-
}
91-
92-
export function initializeVmTraceDecoder(
9+
function initializeVmTraceDecoderWrapper(
9310
vmTraceDecoder: VmTraceDecoder,
9411
tracingConfig: TracingConfig
9512
) {
96-
if (tracingConfig.buildInfos === undefined) {
97-
return;
98-
}
99-
10013
try {
101-
for (const buildInfo of tracingConfig.buildInfos) {
102-
const bytecodes = createModelsAndDecodeBytecodes(
103-
buildInfo.solcVersion,
104-
buildInfo.input,
105-
buildInfo.output
106-
);
107-
108-
for (const bytecode of bytecodes) {
109-
if (
110-
tracingConfig.ignoreContracts === true &&
111-
bytecode.contract.name.startsWith("Ignored")
112-
) {
113-
continue;
114-
}
115-
116-
vmTraceDecoder.addBytecode(bytecode);
117-
}
118-
}
14+
initializeVmTraceDecoder(vmTraceDecoder, tracingConfig);
11915
} catch (error) {
12016
console.warn(JSON.stringify(error, null, 2), (error as any).message);
12117
console.warn(
@@ -134,3 +30,8 @@ export function initializeVmTraceDecoder(
13430
}
13531
}
13632
}
33+
34+
export {
35+
VmTraceDecoder,
36+
initializeVmTraceDecoderWrapper as initializeVmTraceDecoder,
37+
};

0 commit comments

Comments
 (0)