Skip to content

Commit 80ff5bb

Browse files
committed
Explicitly use the constant rather than state in ConsoleLogger
1 parent 4e26e74 commit 80ff5bb

File tree

3 files changed

+9
-19
lines changed

3 files changed

+9
-19
lines changed

packages/hardhat-core/scripts/console-library-generator.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ library console {
8080
`;
8181

8282
logger +=
83-
"\n// In order to optimize map lookup\n" +
84-
"// we'll store 4byte signature as int\n" +
85-
"export const ConsoleLogs = {\n";
83+
"\n/** Maps from a 4-byte function selector to a signature (argument types) */\n" +
84+
"export const CONSOLE_LOG_SIGNATURES: Record<number, string[]> = {\n";
8685

8786
// Add the empty log() first
8887
const sigInt = bytesToInt(keccak256(Buffer.from("log" + "()")).slice(0, 4));

packages/hardhat-core/src/internal/hardhat-network/stack-traces/consoleLogger.ts

+5-13
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ import {
4242
Bytes8Ty,
4343
Bytes9Ty,
4444
BytesTy,
45-
ConsoleLogs,
4645
Int256Ty,
4746
StringTy,
4847
Uint256Ty,
48+
CONSOLE_LOG_SIGNATURES,
4949
} from "./logger";
5050
import {
5151
EvmMessageTrace,
@@ -67,14 +67,6 @@ export type ConsoleLogEntry = string | ConsoleLogArray;
6767
export type ConsoleLogs = ConsoleLogEntry[];
6868

6969
export class ConsoleLogger {
70-
private readonly _consoleLogs: {
71-
[key: number]: string[];
72-
} = {};
73-
74-
constructor() {
75-
this._consoleLogs = ConsoleLogs;
76-
}
77-
7870
public getLogMessages(maybeDecodedMessageTrace: MessageTrace): string[] {
7971
return this.getExecutionLogs(maybeDecodedMessageTrace).map(
8072
consoleLogToString
@@ -132,15 +124,15 @@ export class ConsoleLogger {
132124
}
133125

134126
private _maybeConsoleLog(calldata: Buffer): ConsoleLogs | undefined {
135-
const sig = bytesToInt(calldata.slice(0, 4));
127+
const selector = bytesToInt(calldata.slice(0, 4));
136128
const parameters = calldata.slice(4);
137129

138-
const types = this._consoleLogs[sig];
139-
if (types === undefined) {
130+
const argTypes = CONSOLE_LOG_SIGNATURES[selector];
131+
if (argTypes === undefined) {
140132
return;
141133
}
142134

143-
const consoleLogs = this._decode(parameters, types);
135+
const consoleLogs = this._decode(parameters, argTypes);
144136

145137
this._replaceNumberFormatSpecifiers(consoleLogs);
146138

packages/hardhat-core/src/internal/hardhat-network/stack-traces/logger.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,8 @@ export const Bytes30Ty = "Bytes30";
4242
export const Bytes31Ty = "Bytes31";
4343
export const Bytes32Ty = "Bytes32";
4444

45-
// In order to optimize map lookup
46-
// we'll store 4byte signature as int
47-
export const ConsoleLogs = {
45+
/** Maps from a 4-byte function selector to a signature (argument types) */
46+
export const CONSOLE_LOG_SIGNATURES: Record<number, string[]> = {
4847
1368866505: [],
4948
760966329: [Int256Ty],
5049
1309416733: [Int256Ty],

0 commit comments

Comments
 (0)