1
1
import chalk from "chalk" ;
2
2
import debug from "debug" ;
3
+ import { VmTraceDecoder , initializeVmTraceDecoder } from "@nomicfoundation/edr" ;
3
4
import { Reporter } from "../../sentry/reporter" ;
4
5
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" ;
20
6
21
7
const log = debug ( "hardhat:core:hardhat-network:node" ) ;
22
8
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 (
93
10
vmTraceDecoder : VmTraceDecoder ,
94
11
tracingConfig : TracingConfig
95
12
) {
96
- if ( tracingConfig . buildInfos === undefined ) {
97
- return ;
98
- }
99
-
100
13
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 ) ;
119
15
} catch ( error ) {
120
16
console . warn ( JSON . stringify ( error , null , 2 ) , ( error as any ) . message ) ;
121
17
console . warn (
@@ -134,3 +30,8 @@ export function initializeVmTraceDecoder(
134
30
}
135
31
}
136
32
}
33
+
34
+ export {
35
+ VmTraceDecoder ,
36
+ initializeVmTraceDecoderWrapper as initializeVmTraceDecoder ,
37
+ } ;
0 commit comments