|
18 | 18 | * more meaningful errors.
|
19 | 19 | */
|
20 | 20 |
|
21 |
| -import semver from "semver"; |
22 |
| - |
23 |
| -import { |
24 |
| - DecodedEvmMessageTrace, |
25 |
| - isDecodedCallTrace, |
26 |
| - isDecodedCreateTrace, |
27 |
| - isEvmStep, |
28 |
| -} from "./message-trace"; |
29 |
| -import { Opcode } from "./opcodes"; |
30 |
| -import { |
31 |
| - SolidityStackTrace, |
32 |
| - StackTraceEntryType, |
33 |
| -} from "./solidity-stack-trace"; |
34 |
| - |
35 |
| -const FIRST_SOLC_VERSION_WITH_MAPPED_SMALL_INTERNAL_FUNCTIONS = "0.6.9"; |
36 |
| - |
37 |
| -export function stackTraceMayRequireAdjustments( |
38 |
| - stackTrace: SolidityStackTrace, |
39 |
| - decodedTrace: DecodedEvmMessageTrace |
40 |
| -): boolean { |
41 |
| - if (stackTrace.length === 0) { |
42 |
| - return false; |
43 |
| - } |
44 |
| - |
45 |
| - const lastFrame = stackTrace[stackTrace.length - 1]; |
46 |
| - |
47 |
| - return ( |
48 |
| - lastFrame.type === StackTraceEntryType.REVERT_ERROR && |
49 |
| - !lastFrame.isInvalidOpcodeError && |
50 |
| - lastFrame.message.isEmpty() && |
51 |
| - semver.gte( |
52 |
| - decodedTrace.bytecode.compilerVersion, |
53 |
| - FIRST_SOLC_VERSION_WITH_MAPPED_SMALL_INTERNAL_FUNCTIONS |
54 |
| - ) |
55 |
| - ); |
56 |
| -} |
57 |
| - |
58 |
| -export function adjustStackTrace( |
59 |
| - stackTrace: SolidityStackTrace, |
60 |
| - decodedTrace: DecodedEvmMessageTrace |
61 |
| -): SolidityStackTrace { |
62 |
| - const start = stackTrace.slice(0, -1); |
63 |
| - const [revert] = stackTrace.slice(-1); |
64 |
| - |
65 |
| - if (isNonContractAccountCalledError(decodedTrace)) { |
66 |
| - return [ |
67 |
| - ...start, |
68 |
| - { |
69 |
| - type: StackTraceEntryType.NONCONTRACT_ACCOUNT_CALLED_ERROR, |
70 |
| - sourceReference: revert.sourceReference!, |
71 |
| - }, |
72 |
| - ]; |
73 |
| - } |
74 |
| - |
75 |
| - if (isConstructorInvalidParamsError(decodedTrace)) { |
76 |
| - return [ |
77 |
| - ...start, |
78 |
| - { |
79 |
| - type: StackTraceEntryType.INVALID_PARAMS_ERROR, |
80 |
| - sourceReference: revert.sourceReference!, |
81 |
| - }, |
82 |
| - ]; |
83 |
| - } |
84 |
| - |
85 |
| - if (isCallInvalidParamsError(decodedTrace)) { |
86 |
| - return [ |
87 |
| - ...start, |
88 |
| - { |
89 |
| - type: StackTraceEntryType.INVALID_PARAMS_ERROR, |
90 |
| - sourceReference: revert.sourceReference!, |
91 |
| - }, |
92 |
| - ]; |
93 |
| - } |
94 |
| - |
95 |
| - return stackTrace; |
96 |
| -} |
97 |
| - |
98 |
| -function isNonContractAccountCalledError( |
99 |
| - decodedTrace: DecodedEvmMessageTrace |
100 |
| -): boolean { |
101 |
| - return matchOpcodes(decodedTrace, -9, [ |
102 |
| - Opcode.EXTCODESIZE, |
103 |
| - Opcode.ISZERO, |
104 |
| - Opcode.DUP1, |
105 |
| - Opcode.ISZERO, |
106 |
| - ]); |
107 |
| -} |
108 |
| - |
109 |
| -function isConstructorInvalidParamsError(decodedTrace: DecodedEvmMessageTrace) { |
110 |
| - if (!isDecodedCreateTrace(decodedTrace)) { |
111 |
| - return false; |
112 |
| - } |
113 |
| - |
114 |
| - return ( |
115 |
| - matchOpcodes(decodedTrace, -20, [Opcode.CODESIZE]) && |
116 |
| - matchOpcodes(decodedTrace, -15, [Opcode.CODECOPY]) && |
117 |
| - matchOpcodes(decodedTrace, -7, [Opcode.LT, Opcode.ISZERO]) |
118 |
| - ); |
119 |
| -} |
120 |
| - |
121 |
| -function isCallInvalidParamsError(decodedTrace: DecodedEvmMessageTrace) { |
122 |
| - if (!isDecodedCallTrace(decodedTrace)) { |
123 |
| - return false; |
124 |
| - } |
125 |
| - |
126 |
| - return ( |
127 |
| - matchOpcodes(decodedTrace, -11, [Opcode.CALLDATASIZE]) && |
128 |
| - matchOpcodes(decodedTrace, -7, [Opcode.LT, Opcode.ISZERO]) |
129 |
| - ); |
130 |
| -} |
131 |
| - |
132 |
| -function matchOpcode( |
133 |
| - decodedTrace: DecodedEvmMessageTrace, |
134 |
| - stepIndex: number, |
135 |
| - opcode: Opcode |
136 |
| -): boolean { |
137 |
| - const [step] = decodedTrace.steps.slice(stepIndex, stepIndex + 1); |
138 |
| - |
139 |
| - if (step === undefined || !isEvmStep(step)) { |
140 |
| - return false; |
141 |
| - } |
142 |
| - |
143 |
| - const instruction = decodedTrace.bytecode.getInstruction(step.pc); |
144 |
| - |
145 |
| - return instruction.opcode === opcode; |
146 |
| -} |
147 |
| - |
148 |
| -function matchOpcodes( |
149 |
| - decodedTrace: DecodedEvmMessageTrace, |
150 |
| - firstStepIndex: number, |
151 |
| - opcodes: Opcode[] |
152 |
| -): boolean { |
153 |
| - let index = firstStepIndex; |
154 |
| - for (const opcode of opcodes) { |
155 |
| - if (!matchOpcode(decodedTrace, index, opcode)) { |
156 |
| - return false; |
157 |
| - } |
158 |
| - |
159 |
| - index += 1; |
160 |
| - } |
161 |
| - |
162 |
| - return true; |
163 |
| -} |
| 21 | +export { |
| 22 | + stackTraceMayRequireAdjustments, |
| 23 | + adjustStackTrace, |
| 24 | +} from "@nomicfoundation/edr"; |
0 commit comments