Skip to content

Commit

Permalink
Update hardhat to 2.19 and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
zemse committed Feb 15, 2024
1 parent 80d7197 commit 8030730
Show file tree
Hide file tree
Showing 16 changed files with 240 additions and 357 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ build/Release
# Dependency directories
node_modules/
jspm_packages/
.yarn/

# TypeScript v1 declaration files
typings/
Expand Down
1 change: 1 addition & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: .yarn/releases/yarn-1.22.1.cjs
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@types/readable-stream": "^2.3.14",
"chai": "^4.3.7",
"dotenv": "^16.0.3",
"hardhat": "^2.18.3",
"hardhat": "^2.19.4",
"hardhat-deploy": "^0.11.25",
"mocha": "^7.1.2",
"prettier": "2.0.5",
Expand All @@ -62,5 +62,6 @@
},
"resolutions": {
"safe-buffer": "^5.2.1"
}
},
"packageManager": "[email protected]"
}
4 changes: 3 additions & 1 deletion src/opcodes/log0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ function parse(step: InterpreterStep, currentAddress?: string): Item<LOG0> {
const dataSize = parseNumber(stack.pop()!);

const data = hexPrefix(
step.memory.slice(dataOffset, dataOffset + dataSize).toString("hex")
Buffer.from(step.memory.slice(dataOffset, dataOffset + dataSize)).toString(
"hex"
)
);

return {
Expand Down
4 changes: 3 additions & 1 deletion src/opcodes/log1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ function parse(step: InterpreterStep, currentAddress?: string): Item<LOG1> {
const topic0 = parseBytes32(stack.pop()!);

const data = hexPrefix(
step.memory.slice(dataOffset, dataOffset + dataSize).toString("hex")
Buffer.from(step.memory.slice(dataOffset, dataOffset + dataSize)).toString(
"hex"
)
);

return {
Expand Down
4 changes: 3 additions & 1 deletion src/opcodes/log2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ function parse(step: InterpreterStep, currentAddress?: string): Item<LOG2> {
const topic1 = parseBytes32(stack.pop()!);

const data = hexPrefix(
step.memory.slice(dataOffset, dataOffset + dataSize).toString("hex")
Buffer.from(step.memory.slice(dataOffset, dataOffset + dataSize)).toString(
"hex"
)
);

return {
Expand Down
4 changes: 3 additions & 1 deletion src/opcodes/log3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ function parse(step: InterpreterStep, currentAddress?: string): Item<LOG3> {
const topic2 = parseBytes32(stack.pop()!);

const data = hexPrefix(
step.memory.slice(dataOffset, dataOffset + dataSize).toString("hex")
Buffer.from(step.memory.slice(dataOffset, dataOffset + dataSize)).toString(
"hex"
)
);

return {
Expand Down
4 changes: 3 additions & 1 deletion src/opcodes/log4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ function parse(step: InterpreterStep, currentAddress?: string): Item<LOG4> {
const topic3 = parseBytes32(stack.pop()!);

const data = hexPrefix(
step.memory.slice(dataOffset, dataOffset + dataSize).toString("hex")
Buffer.from(step.memory.slice(dataOffset, dataOffset + dataSize)).toString(
"hex"
)
);

return {
Expand Down
2 changes: 1 addition & 1 deletion src/opcodes/revert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function parse(step: InterpreterStep): Item<REVERT> {
const offset = Number(step.stack[step.stack.length - 1].toString());
const length = Number(step.stack[step.stack.length - 2].toString());
const data = hexPrefix(
step.memory.slice(offset, offset + length).toString("hex")
Buffer.from(step.memory.slice(offset, offset + length)).toString("hex")
);

return {
Expand Down
4 changes: 3 additions & 1 deletion src/opcodes/sha3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export interface SHA3 {
function parse(step: InterpreterStep): AwaitedItem<SHA3> {
const offset = parseNumber(step.stack[step.stack.length - 1].toString(16));
const size = parseNumber(step.stack[step.stack.length - 2].toString(16));
const data = step.memory.slice(offset, offset + size).toString("hex");
const data = Buffer.from(step.memory.slice(offset, offset + size)).toString(
"hex"
);

const next = 1; // get stack just after this opcode
return {
Expand Down
12 changes: 6 additions & 6 deletions src/tasks/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ async function traceTransctionWithProgress(node: HardhatNode, hash: string) {
// to avoid having to distinguish between empty and non-existing accounts.
// We *could* do it during the non-forked mode, but for simplicity we just
// don't support it at all.
const isPreSpuriousDragon = !vm._common.gteHardfork("spuriousDragon");
const isPreSpuriousDragon = !vm.common.gteHardfork("spuriousDragon");
if (isPreSpuriousDragon) {
throw new Error(
"Tracing is not supported for transactions using hardforks older than Spurious Dragon. "
Expand All @@ -220,7 +220,7 @@ async function traceTransctionWithProgress(node: HardhatNode, hash: string) {
let progressPrinted = Date.now();
for (const tx of block.transactions) {
totalProgress += Number(tx.gasLimit.toString());
if (tx.hash().equals(hashBuffer)) {
if (Buffer.from(tx.hash()).equals(hashBuffer)) {
break;
}
}
Expand All @@ -230,26 +230,26 @@ async function traceTransctionWithProgress(node: HardhatNode, hash: string) {
const sender = tx.getSenderAddress();
if (tx.type === 0) {
txWithCommon = new FakeSenderTransaction(sender, tx, {
common: vm._common,
common: vm.common,
});
} else if (tx.type === 1) {
txWithCommon = new FakeSenderAccessListEIP2930Transaction(sender, tx, {
common: vm._common,
common: vm.common,
});
} else if (tx.type === 2) {
txWithCommon = new FakeSenderEIP1559Transaction(
sender,
{ ...tx, gasPrice: undefined },
{
common: vm._common,
common: vm.common,
}
);
} else {
// throw new Error("Only legacy, EIP2930, and EIP1559 txs are supported");
continue;
}

const txHash = txWithCommon.hash();
const txHash = Buffer.from(txWithCommon.hash());
debug("running tx %s", txHash.toString("hex"));
if (txHash.equals(hashBuffer)) {
await vm.runTx({
Expand Down
18 changes: 9 additions & 9 deletions src/trace-recorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const debug = createDebug("hardhat-tracer:trace-recorder");

interface NewContractEvent {
address: Address;
code: Buffer;
code: Uint8Array;
}

export class TraceRecorder {
Expand Down Expand Up @@ -58,7 +58,7 @@ export class TraceRecorder {
) {
debug("handleBeforeTx");
this.trace = new TransactionTrace();
this.trace.hash = hexPrefix(tx.hash().toString("hex"));
this.trace.hash = hexPrefix(Buffer.from(tx.hash()).toString("hex"));

resolve?.();
}
Expand All @@ -85,7 +85,7 @@ export class TraceRecorder {
params: {
from: hexPrefix(message.caller.toString()),
to: hexPrefix((message._codeAddress ?? message.to).toString()),
inputData: hexPrefix(message.data.toString("hex")),
inputData: hexPrefix(Buffer.from(message.data).toString("hex")),
gasLimit: Number(message.gasLimit.toString()),
},
children: [],
Expand All @@ -97,7 +97,7 @@ export class TraceRecorder {
params: {
from: hexPrefix(message.caller.toString()),
to: hexPrefix(message.to.toString()),
inputData: hexPrefix(message.data.toString("hex")),
inputData: hexPrefix(Buffer.from(message.data).toString("hex")),
gasLimit: Number(message.gasLimit.toString()),
value: hexPrefix(message.value.toString(16)),
},
Expand All @@ -109,7 +109,7 @@ export class TraceRecorder {
opcode: "CREATE",
params: {
from: hexPrefix(message.caller.toString()),
initCode: hexPrefix(message.data.toString("hex")),
initCode: hexPrefix(Buffer.from(message.data).toString("hex")),
gasLimit: Number(message.gasLimit.toString()),
value: hexPrefix(message.value.toString(16)),
},
Expand All @@ -120,10 +120,10 @@ export class TraceRecorder {
opcode: "CREATE2",
params: {
from: hexPrefix(message.caller.toString()),
initCode: hexPrefix(message.data.toString("hex")),
initCode: hexPrefix(Buffer.from(message.data).toString("hex")),
gasLimit: Number(message.gasLimit.toString()),
value: hexPrefix(message.value.toString(16)),
salt: hexPrefix(message.salt.toString("hex")),
salt: hexPrefix(Buffer.from(message.salt).toString("hex")),
},
children: [],
} as Item<CREATE2>;
Expand All @@ -142,7 +142,7 @@ export class TraceRecorder {

public handleNewContract(
contract: NewContractEvent,
resolve: ((result?: any) => void) | undefined
resolve?: ((result?: any) => void) | undefined
) {
debug("handleNewContract %s", contract.address.toString());
if (!this.trace || !this.trace.parent) {
Expand Down Expand Up @@ -262,7 +262,7 @@ export class TraceRecorder {
}

this.trace.returnCurrentCall(
"0x" + evmResult.execResult.returnValue.toString("hex"),
"0x" + Buffer.from(evmResult.execResult.returnValue).toString("hex"),
Number(evmResult?.execResult.executionGasUsed),
evmResult?.execResult.exceptionError
);
Expand Down
4 changes: 2 additions & 2 deletions src/utils/check-opcodes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
getOpcodesForHF,
Opcode,
} from "@nomicfoundation/ethereumjs-evm/dist/opcodes";
} from "@nomicfoundation/ethereumjs-evm/dist/cjs/opcodes";
import { VM } from "@nomicfoundation/ethereumjs-vm";

export function checkIfOpcodesAreValid(opcodes: Map<string, boolean>, vm: VM) {
// fetch the opcodes which work on this VM
const activeOpcodesMap = new Map<string, boolean>();
for (const opcode of getOpcodesForHF(vm._common).opcodes.values()) {
for (const opcode of getOpcodesForHF(vm.common).opcodes.values()) {
activeOpcodesMap.set(opcode.fullName, true);
}

Expand Down
4 changes: 4 additions & 0 deletions src/utils/state-overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export async function applyStateOverrides(
// for balance and nonce
if (overrides.balance !== undefined || overrides.nonce !== undefined) {
const account = await vm.stateManager.getAccount(address);
if (account === undefined) {
throw new Error("account is undefined");
}

if (overrides.nonce !== undefined) {
account.nonce = BigNumber.from(overrides.nonce).toBigInt();
}
Expand Down
2 changes: 1 addition & 1 deletion test/project.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("Hardhat Runtime Environment extension", function () {
});
});

describe.only("Test task", function () {
describe("Test task", function () {
useEnvironment("hardhat-project");

// before(async function () {
Expand Down
Loading

0 comments on commit 8030730

Please sign in to comment.