Skip to content

Commit 49c66b6

Browse files
committed
refactor: Re-use the compiler to model and opcodes logic from EDR
1 parent 5739893 commit 49c66b6

File tree

8 files changed

+58
-1616
lines changed

8 files changed

+58
-1616
lines changed

packages/hardhat-core/src/internal/hardhat-network/stack-traces/compiler-to-model.ts

+1-697
Large diffs are not rendered by default.

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
MessageTrace,
1111
PrecompileMessageTrace,
1212
} from "./message-trace";
13-
import { JumpType } from "./model";
14-
import { isJump, isPush, Opcode } from "./opcodes";
13+
import { JumpType, jumpTypeToString } from "./model";
14+
import { isJump, isPush, opcodeToString } from "./opcodes";
1515
import {
1616
SolidityStackTrace,
1717
SourceReference,
@@ -147,23 +147,23 @@ function traceSteps(
147147
if (isJump(inst.opcode)) {
148148
const jump =
149149
inst.jumpType !== JumpType.NOT_JUMP
150-
? chalk.bold(`(${JumpType[inst.jumpType]})`)
150+
? chalk.bold(`(${jumpTypeToString(inst.jumpType)})`)
151151
: "";
152152

153153
console.log(
154-
`${margin} ${pc} ${Opcode[inst.opcode]} ${jump}`.padEnd(50),
154+
`${margin} ${pc} ${opcodeToString(inst.opcode)} ${jump}`.padEnd(50),
155155
location
156156
);
157157
} else if (isPush(inst.opcode)) {
158158
console.log(
159-
`${margin} ${pc} ${Opcode[inst.opcode]} ${bufferToHex(
159+
`${margin} ${pc} ${opcodeToString(inst.opcode)} ${bufferToHex(
160160
inst.pushData!
161161
)}`.padEnd(50),
162162
location
163163
);
164164
} else {
165165
console.log(
166-
`${margin} ${pc} ${Opcode[inst.opcode]}`.padEnd(50),
166+
`${margin} ${pc} ${opcodeToString(inst.opcode)}`.padEnd(50),
167167
location
168168
);
169169
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -641,9 +641,13 @@ export class ErrorInferrer {
641641
);
642642

643643
if (calledFunction !== undefined) {
644-
const isValidCalldata = calledFunction.isValidCalldata(
645-
trace.calldata.slice(4)
646-
);
644+
const isValidCalldata =
645+
// if we don't know the param types, we just assume that the call is valid
646+
calledFunction.paramTypes === undefined ||
647+
AbiHelpers.isValidCalldata(
648+
calledFunction.paramTypes,
649+
trace.calldata.slice(4)
650+
);
647651

648652
if (!isValidCalldata) {
649653
return [

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

+5-48
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,10 @@
1-
import { CompilerOutputBytecode } from "../../../types";
2-
1+
import {
2+
getLibraryAddressPositions,
3+
linkHexStringBytecode,
4+
} from "@nomicfoundation/edr";
35
import { Opcode } from "./opcodes";
46

5-
export function getLibraryAddressPositions(
6-
bytecodeOutput: CompilerOutputBytecode
7-
): number[] {
8-
const positions = [];
9-
for (const libs of Object.values(bytecodeOutput.linkReferences)) {
10-
for (const references of Object.values(libs)) {
11-
for (const ref of references) {
12-
positions.push(ref.start);
13-
}
14-
}
15-
}
16-
17-
return positions;
18-
}
19-
20-
export function normalizeCompilerOutputBytecode(
21-
compilerOutputBytecodeObject: string,
22-
addressesPositions: number[]
23-
): Buffer {
24-
const ZERO_ADDRESS = "0000000000000000000000000000000000000000";
25-
for (const position of addressesPositions) {
26-
compilerOutputBytecodeObject = linkHexStringBytecode(
27-
compilerOutputBytecodeObject,
28-
ZERO_ADDRESS,
29-
position
30-
);
31-
}
32-
33-
return Buffer.from(compilerOutputBytecodeObject, "hex");
34-
}
35-
36-
export function linkHexStringBytecode(
37-
code: string,
38-
address: string,
39-
position: number
40-
) {
41-
if (address.startsWith("0x")) {
42-
address = address.substring(2);
43-
}
44-
45-
return (
46-
code.substring(0, position * 2) +
47-
address +
48-
code.slice(position * 2 + address.length)
49-
);
50-
}
7+
export { getLibraryAddressPositions, linkHexStringBytecode };
518

529
export function zeroOutAddresses(
5310
code: Uint8Array,

0 commit comments

Comments
 (0)