forked from NomicFoundation/hardhat
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consoleLogger.ts
253 lines (226 loc) · 7.1 KB
/
consoleLogger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import {
bufferToBigInt,
bufferToHex,
bufferToInt,
fromSigned,
} from "@nomicfoundation/ethereumjs-util";
import util from "util";
import {
AddressTy,
BoolTy,
Bytes10Ty,
Bytes11Ty,
Bytes12Ty,
Bytes13Ty,
Bytes14Ty,
Bytes15Ty,
Bytes16Ty,
Bytes17Ty,
Bytes18Ty,
Bytes19Ty,
Bytes1Ty,
Bytes20Ty,
Bytes21Ty,
Bytes22Ty,
Bytes23Ty,
Bytes24Ty,
Bytes25Ty,
Bytes26Ty,
Bytes27Ty,
Bytes28Ty,
Bytes29Ty,
Bytes2Ty,
Bytes30Ty,
Bytes31Ty,
Bytes32Ty,
Bytes3Ty,
Bytes4Ty,
Bytes5Ty,
Bytes6Ty,
Bytes7Ty,
Bytes8Ty,
Bytes9Ty,
BytesTy,
ConsoleLogs,
Int256Ty,
StringTy,
Uint256Ty,
} from "./logger";
import {
CallMessageTrace,
EvmMessageTrace,
isCallTrace,
isEvmStep,
isPrecompileTrace,
MessageTrace,
} from "./message-trace";
const CONSOLE_ADDRESS = "0x000000000000000000636F6e736F6c652e6c6f67"; // toHex("console.log")
const REGISTER_SIZE = 32;
// eslint-disable-next-line @typescript-eslint/no-empty-interface
interface ConsoleLogArray extends Array<ConsoleLogEntry> {}
export type ConsoleLogEntry = string | ConsoleLogArray;
// eslint-disable-next-line @typescript-eslint/no-redeclare
export type ConsoleLogs = ConsoleLogEntry[];
export class ConsoleLogger {
private readonly _consoleLogs: {
[key: number]: string[];
} = {};
constructor() {
this._consoleLogs = ConsoleLogs;
}
public getLogMessages(maybeDecodedMessageTrace: MessageTrace): string[] {
return this.getExecutionLogs(maybeDecodedMessageTrace).map((log) => {
if (log === undefined) {
return "";
}
// special case for console.log()
if (log.length === 0) {
return "";
}
return util.format(log[0], ...log.slice(1));
});
}
public getExecutionLogs(
maybeDecodedMessageTrace: MessageTrace
): ConsoleLogs[] {
if (isPrecompileTrace(maybeDecodedMessageTrace)) {
return [];
}
const logs: ConsoleLogs[] = [];
this._collectExecutionLogs(maybeDecodedMessageTrace, logs);
return logs;
}
private _collectExecutionLogs(trace: EvmMessageTrace, logs: ConsoleLogs) {
for (const messageTrace of trace.steps) {
if (isEvmStep(messageTrace) || isPrecompileTrace(messageTrace)) {
continue;
}
if (
isCallTrace(messageTrace) &&
bufferToHex(messageTrace.address) === CONSOLE_ADDRESS.toLowerCase()
) {
const log = this._maybeConsoleLog(messageTrace);
if (log !== undefined) {
logs.push(log);
}
continue;
}
this._collectExecutionLogs(messageTrace, logs);
}
}
private _maybeConsoleLog(call: CallMessageTrace): ConsoleLogs | undefined {
const sig = bufferToInt(call.calldata.slice(0, 4));
const parameters = call.calldata.slice(4);
const types = this._consoleLogs[sig];
if (types === undefined) {
return;
}
return this._decode(parameters, types);
}
private _decode(data: Buffer, types: string[]): ConsoleLogs {
return types.map((type, i) => {
const position: number = i * 32;
switch (types[i]) {
case Uint256Ty:
return bufferToBigInt(
data.slice(position, position + REGISTER_SIZE)
).toString(10);
case Int256Ty:
return fromSigned(
data.slice(position, position + REGISTER_SIZE)
).toString();
case BoolTy:
if (data[position + 31] !== 0) {
return "true";
}
return "false";
case StringTy:
const sStart = bufferToInt(
data.slice(position, position + REGISTER_SIZE)
);
const sLen = bufferToInt(data.slice(sStart, sStart + REGISTER_SIZE));
return data
.slice(sStart + REGISTER_SIZE, sStart + REGISTER_SIZE + sLen)
.toString();
case AddressTy:
return bufferToHex(
data.slice(position + 12, position + REGISTER_SIZE)
);
case BytesTy:
const bStart = bufferToInt(
data.slice(position, position + REGISTER_SIZE)
);
const bLen = bufferToInt(data.slice(bStart, bStart + REGISTER_SIZE));
return bufferToHex(
data.slice(bStart + REGISTER_SIZE, bStart + REGISTER_SIZE + bLen)
);
case Bytes1Ty:
return bufferToHex(data.slice(position, position + 1));
case Bytes2Ty:
return bufferToHex(data.slice(position, position + 2));
case Bytes3Ty:
return bufferToHex(data.slice(position, position + 3));
case Bytes4Ty:
return bufferToHex(data.slice(position, position + 4));
case Bytes5Ty:
return bufferToHex(data.slice(position, position + 5));
case Bytes6Ty:
return bufferToHex(data.slice(position, position + 6));
case Bytes7Ty:
return bufferToHex(data.slice(position, position + 7));
case Bytes8Ty:
return bufferToHex(data.slice(position, position + 8));
case Bytes9Ty:
return bufferToHex(data.slice(position, position + 9));
case Bytes10Ty:
return bufferToHex(data.slice(position, position + 10));
case Bytes11Ty:
return bufferToHex(data.slice(position, position + 11));
case Bytes12Ty:
return bufferToHex(data.slice(position, position + 12));
case Bytes13Ty:
return bufferToHex(data.slice(position, position + 13));
case Bytes14Ty:
return bufferToHex(data.slice(position, position + 14));
case Bytes15Ty:
return bufferToHex(data.slice(position, position + 15));
case Bytes16Ty:
return bufferToHex(data.slice(position, position + 16));
case Bytes17Ty:
return bufferToHex(data.slice(position, position + 17));
case Bytes18Ty:
return bufferToHex(data.slice(position, position + 18));
case Bytes19Ty:
return bufferToHex(data.slice(position, position + 19));
case Bytes20Ty:
return bufferToHex(data.slice(position, position + 20));
case Bytes21Ty:
return bufferToHex(data.slice(position, position + 21));
case Bytes22Ty:
return bufferToHex(data.slice(position, position + 22));
case Bytes23Ty:
return bufferToHex(data.slice(position, position + 23));
case Bytes24Ty:
return bufferToHex(data.slice(position, position + 24));
case Bytes25Ty:
return bufferToHex(data.slice(position, position + 25));
case Bytes26Ty:
return bufferToHex(data.slice(position, position + 26));
case Bytes27Ty:
return bufferToHex(data.slice(position, position + 27));
case Bytes28Ty:
return bufferToHex(data.slice(position, position + 28));
case Bytes29Ty:
return bufferToHex(data.slice(position, position + 29));
case Bytes30Ty:
return bufferToHex(data.slice(position, position + 30));
case Bytes31Ty:
return bufferToHex(data.slice(position, position + 31));
case Bytes32Ty:
return bufferToHex(data.slice(position, position + 32));
default:
return "";
}
});
}
}