Skip to content

Commit

Permalink
Update colors
Browse files Browse the repository at this point in the history
  • Loading branch information
zemse committed Aug 8, 2023
1 parent abd65cb commit 1696e1b
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 44 deletions.
12 changes: 3 additions & 9 deletions src/opcodes/mload.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { InterpreterStep } from "@nomicfoundation/ethereumjs-evm";

import { AwaitedItem, Item } from "../types";
import {
colorKey,
colorLabel,
colorValue,
parseBytes32,
parseHex,
} from "../utils";
import { colorLabel, colorMload, colorValue, parseBytes32 } from "../utils";

export interface MLOAD {
offset: string;
value: string;
}

function parse(step: InterpreterStep): AwaitedItem<MLOAD> {
const offset = parseHex(step.stack[step.stack.length - 1].toString(16), 4);
const offset = parseBytes32(step.stack[step.stack.length - 1].toString(16));

const next = 1; // get stack just after this opcode
return {
Expand All @@ -37,7 +31,7 @@ function parse(step: InterpreterStep): AwaitedItem<MLOAD> {
}

function format(item: Item<MLOAD>): string {
return `${colorLabel("[MLOAD]")} ${colorKey(
return `${colorLabel("[MLOAD]")} ${colorMload(
item.params.offset
)}${colorValue(item.params.value)}`;
}
Expand Down
13 changes: 3 additions & 10 deletions src/opcodes/msize.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { InterpreterStep } from "@nomicfoundation/ethereumjs-evm";

import { AwaitedItem, Item } from "../types";
import {
colorKey,
colorLabel,
colorValue,
parseBytes32,
parseHex,
} from "../utils";
import { colorLabel, colorValue, parseBytes32 } from "../utils";

export interface MSIZE {
size: string;
Expand All @@ -21,9 +15,8 @@ function parse(): AwaitedItem<MSIZE> {
parse: (stepNext: InterpreterStep) => ({
opcode: "MSIZE",
params: {
size: parseHex(
stepNext.stack[stepNext.stack.length - 1].toString(16),
4
size: parseBytes32(
stepNext.stack[stepNext.stack.length - 1].toString(16)
),
},
format(): string {
Expand Down
7 changes: 3 additions & 4 deletions src/opcodes/mstore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { InterpreterStep } from "@nomicfoundation/ethereumjs-evm";

import { Item } from "../types";
import {
colorKey,
colorLabel,
colorMstore,
colorValue,
parseBytes32,
parseHex,
shallowCopyStack2,
} from "../utils";

Expand All @@ -21,7 +20,7 @@ function parse(step: InterpreterStep): Item<MSTORE> {
throw new Error("[hardhat-tracer]: Faulty MSTORE");
}

const offset = parseHex(stack.pop()!, 4);
const offset = parseBytes32(stack.pop()!);
const value = parseBytes32(stack.pop()!);

return {
Expand All @@ -34,7 +33,7 @@ function parse(step: InterpreterStep): Item<MSTORE> {
}

function format(item: Item<MSTORE>): string {
return `${colorLabel("[MSTORE]")} ${colorKey(
return `${colorLabel("[MSTORE]")} ${colorMstore(
item.params.offset
)}${colorValue(item.params.value)}`;
}
Expand Down
9 changes: 4 additions & 5 deletions src/opcodes/mstore8.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import { InterpreterStep } from "@nomicfoundation/ethereumjs-evm";

import { Item } from "../types";
import {
colorKey,
colorLabel,
colorMstore,
colorValue,
parseBytes32,
parseHex,
shallowCopyStack2,
} from "../utils";

Expand All @@ -21,8 +20,8 @@ function parse(step: InterpreterStep): Item<MSTORE8> {
throw new Error("[hardhat-tracer]: Faulty MSTORE");
}

const offset = parseHex(stack.pop()!, 4);
const value = parseHex(stack.pop()!, 1);
const offset = parseBytes32(stack.pop()!);
const value = parseBytes32(stack.pop()!);

return {
opcode: "MSTORE8",
Expand All @@ -34,7 +33,7 @@ function parse(step: InterpreterStep): Item<MSTORE8> {
}

function format(item: Item<MSTORE8>): string {
return `${colorLabel("[MSTORE8]")} ${colorKey(
return `${colorLabel("[MSTORE8]")} ${colorMstore(
item.params.offset
)}${colorValue(item.params.value)}`;
}
Expand Down
8 changes: 4 additions & 4 deletions src/opcodes/sload.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InterpreterStep } from "@nomicfoundation/ethereumjs-evm";

import { AwaitedItem, Item } from "../types";
import { colorKey, colorLabel, colorValue, parseBytes32 } from "../utils";
import { colorLabel, colorSload, colorValue, parseBytes32 } from "../utils";

export interface SLOAD {
key: string;
Expand Down Expand Up @@ -31,9 +31,9 @@ function parse(step: InterpreterStep): AwaitedItem<SLOAD> {
}

function format(item: Item<SLOAD>): string {
return `${colorLabel("[SLOAD]")} ${colorKey(item.params.key)}${colorValue(
item.params.value
)}`;
return `${colorLabel("[SLOAD]")} ${colorSload(
item.params.key
)}${colorValue(item.params.value)}`;
}

export default { parse, format };
8 changes: 4 additions & 4 deletions src/opcodes/sstore.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InterpreterStep } from "@nomicfoundation/ethereumjs-evm";

import { Item } from "../types";
import { colorKey, colorLabel, colorValue, parseBytes32 } from "../utils";
import { colorLabel, colorSstore, colorValue, parseBytes32 } from "../utils";

export interface SSTORE {
key: string;
Expand All @@ -22,9 +22,9 @@ function parse(step: InterpreterStep): Item<SSTORE> {
}

function format(item: Item<SSTORE>): string {
return `${colorLabel("[SSTORE]")} ${colorKey(item.params.key)}${colorValue(
item.params.value
)}`;
return `${colorLabel("[SSTORE]")} ${colorSstore(
item.params.key
)}${colorValue(item.params.value)}`;
}

export default { parse, format };
2 changes: 2 additions & 0 deletions src/utils/colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const colorValue = chalk.whiteBright;
export const colorExtra = chalk.gray;
export const colorSload = chalk.blueBright;
export const colorSstore = chalk.redBright;
export const colorMload = chalk.blueBright;
export const colorMstore = chalk.redBright;
export const colorNameTag = chalk.italic;
export const colorIndexed = chalk.italic;

Expand Down
8 changes: 0 additions & 8 deletions src/utils/hex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,6 @@ export function parseBytes32(str: string) {
return hexZeroPad(hexStripZeros(hexPrefix(str)), 32);
}

export function parseHex(str: string, bytes?: number) {
let val = hexStripZeros(hexPrefix(str));
if (bytes && val.length - 2 < bytes * 2) {
val = hexZeroPad(val, bytes);
}
return val;
}

export function parseMemory(strArr: string[]) {
return arrayify(hexPrefix(strArr.join("")));
}
Expand Down
5 changes: 5 additions & 0 deletions test/fixture-projects/hardhat-project/contracts/Hello.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,15 @@ contract Hello {
}

function firstCall() public returns (uint256) {
assembly {
let val := sload(1)
sstore(0, add(val, 1))
}
(, bytes memory ret) = address(this).staticcall(
abi.encodeCall(this.secondStaticCall, ())
);
assembly {
sstore(0, 1)
return(add(32, ret), mload(ret))
}
}
Expand Down

0 comments on commit 1696e1b

Please sign in to comment.