Skip to content

Commit

Permalink
Implement few common opcodes (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
zemse committed Jul 18, 2022
1 parent 9dfbfa1 commit 8c6cc77
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 1 deletion.
35 changes: 35 additions & 0 deletions src/opcodes/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { colorLabel } from "../colors";
import { DEPTH_INDENTATION } from "../constants";
import { StructLog, TracerDependenciesExtended } from "../types";
import { parseHex, shallowCopyStack } from "../utils";
import { formatParam } from "../format/param";
import { formatGasCost } from "../format/gas-cost";

export async function printAdd(
structLog: StructLog,
index: number,
structLogs: StructLog[],
dependencies: TracerDependenciesExtended
) {
const stack = shallowCopyStack(structLog.stack);
if (stack.length < 2) {
console.log("Faulty ADD");
return;
}
const a = parseHex(stack.pop()!);
const b = parseHex(stack.pop()!);
const stackAfter = shallowCopyStack(structLogs[index + 1].stack);
const result = parseHex(stackAfter.pop()!);

const str = `${formatParam(a, dependencies)} + ${formatParam(
b,
dependencies
)} => (${formatParam(result, dependencies)})`;
console.log(
DEPTH_INDENTATION.repeat(structLog.depth) +
colorLabel("ADD") +
" " +
str +
formatGasCost(structLog, null, dependencies)
);
}
35 changes: 35 additions & 0 deletions src/opcodes/div.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { colorLabel } from "../colors";
import { DEPTH_INDENTATION } from "../constants";
import { StructLog, TracerDependenciesExtended } from "../types";
import { parseHex, shallowCopyStack } from "../utils";
import { formatParam } from "../format/param";
import { formatGasCost } from "../format/gas-cost";

export async function printDiv(
structLog: StructLog,
index: number,
structLogs: StructLog[],
dependencies: TracerDependenciesExtended
) {
const stack = shallowCopyStack(structLog.stack);
if (stack.length < 2) {
console.log("Faulty DIV");
return;
}
const a = parseHex(stack.pop()!);
const b = parseHex(stack.pop()!);
const stackAfter = shallowCopyStack(structLogs[index + 1].stack);
const result = parseHex(stackAfter.pop()!);

const str = `${formatParam(a, dependencies)} / ${formatParam(
b,
dependencies
)} => (${formatParam(result, dependencies)})`;
console.log(
DEPTH_INDENTATION.repeat(structLog.depth) +
colorLabel("DIV") +
" " +
str +
formatGasCost(structLog, null, dependencies)
);
}
35 changes: 35 additions & 0 deletions src/opcodes/eq.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { colorLabel } from "../colors";
import { DEPTH_INDENTATION } from "../constants";
import { StructLog, TracerDependenciesExtended } from "../types";
import { parseHex, shallowCopyStack } from "../utils";
import { formatParam } from "../format/param";
import { formatGasCost } from "../format/gas-cost";

export async function printEq(
structLog: StructLog,
index: number,
structLogs: StructLog[],
dependencies: TracerDependenciesExtended
) {
const stack = shallowCopyStack(structLog.stack);
if (stack.length < 2) {
console.log("Faulty EQ");
return;
}
const a = parseHex(stack.pop()!);
const b = parseHex(stack.pop()!);
const stackAfter = shallowCopyStack(structLogs[index + 1].stack);
const result = parseHex(stackAfter.pop()!);

const str = `${formatParam(a, dependencies)} == ${formatParam(
b,
dependencies
)} => (${formatParam(result, dependencies)})`;
console.log(
DEPTH_INDENTATION.repeat(structLog.depth) +
colorLabel("EQ") +
" " +
str +
formatGasCost(structLog, null, dependencies)
);
}
35 changes: 35 additions & 0 deletions src/opcodes/gt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { colorLabel } from "../colors";
import { DEPTH_INDENTATION } from "../constants";
import { StructLog, TracerDependenciesExtended } from "../types";
import { parseHex, parseNumber, shallowCopyStack } from "../utils";
import { formatParam } from "../format/param";
import { formatGasCost } from "../format/gas-cost";

export async function printGt(
structLog: StructLog,
index: number,
structLogs: StructLog[],
dependencies: TracerDependenciesExtended
) {
const stack = shallowCopyStack(structLog.stack);
if (stack.length < 2) {
console.log("Faulty GT");
return;
}
const a = parseHex(stack.pop()!);
const b = parseHex(stack.pop()!);
const stackAfter = shallowCopyStack(structLogs[index + 1].stack);
const result = parseHex(stackAfter.pop()!);

const str = `${formatParam(a, dependencies)} > ${formatParam(
b,
dependencies
)} => (${parseNumber(result) ? "true" : "false"})`;
console.log(
DEPTH_INDENTATION.repeat(structLog.depth) +
colorLabel("GT") +
" " +
str +
formatGasCost(structLog, null, dependencies)
);
}
35 changes: 35 additions & 0 deletions src/opcodes/lt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { colorLabel } from "../colors";
import { DEPTH_INDENTATION } from "../constants";
import { StructLog, TracerDependenciesExtended } from "../types";
import { parseHex, parseNumber, shallowCopyStack } from "../utils";
import { formatParam } from "../format/param";
import { formatGasCost } from "../format/gas-cost";

export async function printLt(
structLog: StructLog,
index: number,
structLogs: StructLog[],
dependencies: TracerDependenciesExtended
) {
const stack = shallowCopyStack(structLog.stack);
if (stack.length < 2) {
console.log("Faulty LT");
return;
}
const a = parseHex(stack.pop()!);
const b = parseHex(stack.pop()!);
const stackAfter = shallowCopyStack(structLogs[index + 1].stack);
const result = parseHex(stackAfter.pop()!);

const str = `${formatParam(a, dependencies)} < ${formatParam(
b,
dependencies
)} => (${parseNumber(result) ? "true" : "false"})`;
console.log(
DEPTH_INDENTATION.repeat(structLog.depth) +
colorLabel("LT") +
" " +
str +
formatGasCost(structLog, null, dependencies)
);
}
35 changes: 35 additions & 0 deletions src/opcodes/mul.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { colorLabel } from "../colors";
import { DEPTH_INDENTATION } from "../constants";
import { StructLog, TracerDependenciesExtended } from "../types";
import { parseHex, shallowCopyStack } from "../utils";
import { formatParam } from "../format/param";
import { formatGasCost } from "../format/gas-cost";

export async function printMul(
structLog: StructLog,
index: number,
structLogs: StructLog[],
dependencies: TracerDependenciesExtended
) {
const stack = shallowCopyStack(structLog.stack);
if (stack.length < 2) {
console.log("Faulty MUL");
return;
}
const a = parseHex(stack.pop()!);
const b = parseHex(stack.pop()!);
const stackAfter = shallowCopyStack(structLogs[index + 1].stack);
const result = parseHex(stackAfter.pop()!);

const str = `${formatParam(a, dependencies)} * ${formatParam(
b,
dependencies
)} => (${formatParam(result, dependencies)})`;
console.log(
DEPTH_INDENTATION.repeat(structLog.depth) +
colorLabel("MUL") +
" " +
str +
formatGasCost(structLog, null, dependencies)
);
}
40 changes: 40 additions & 0 deletions src/opcodes/sha3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { colorLabel } from "../colors";
import { DEPTH_INDENTATION } from "../constants";
import { StructLog, TracerDependenciesExtended } from "../types";
import { parseHex, parseMemory, parseNumber, shallowCopyStack } from "../utils";
import { formatParam } from "../format/param";
import { formatGasCost } from "../format/gas-cost";
import { hexlify } from "ethers/lib/utils";

export async function printSha3(
structLog: StructLog,
index: number,
structLogs: StructLog[],
dependencies: TracerDependenciesExtended
) {
const stack = shallowCopyStack(structLog.stack);
if (stack.length < 1) {
console.log("Faulty SHA3");
return;
}
const start = parseNumber(stack.pop()!);
const length = parseNumber(stack.pop()!);

const memory = parseMemory(structLog.memory);
const inputToBeHashed = hexlify(memory.slice(start, start + length));

const stackAfter = shallowCopyStack(structLogs[index + 1].stack);
const result = parseHex(stackAfter.pop()!);

const str = `${formatParam(inputToBeHashed, dependencies)} => (${formatParam(
result,
dependencies
)})`;
console.log(
DEPTH_INDENTATION.repeat(structLog.depth) +
colorLabel("SHA3") +
" " +
str +
formatGasCost(structLog, null, dependencies)
);
}
35 changes: 35 additions & 0 deletions src/opcodes/sub.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { colorLabel } from "../colors";
import { DEPTH_INDENTATION } from "../constants";
import { StructLog, TracerDependenciesExtended } from "../types";
import { parseHex, shallowCopyStack } from "../utils";
import { formatParam } from "../format/param";
import { formatGasCost } from "../format/gas-cost";

export async function printSub(
structLog: StructLog,
index: number,
structLogs: StructLog[],
dependencies: TracerDependenciesExtended
) {
const stack = shallowCopyStack(structLog.stack);
if (stack.length < 2) {
console.log("Faulty SUB");
return;
}
const a = parseHex(stack.pop()!);
const b = parseHex(stack.pop()!);
const stackAfter = shallowCopyStack(structLogs[index + 1].stack);
const result = parseHex(stackAfter.pop()!);

const str = `${formatParam(a, dependencies)} - ${formatParam(
b,
dependencies
)} => (${formatParam(result, dependencies)})`;
console.log(
DEPTH_INDENTATION.repeat(structLog.depth) +
colorLabel("SUB") +
" " +
str +
formatGasCost(structLog, null, dependencies)
);
}
40 changes: 39 additions & 1 deletion src/print/struct-log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ import { printRevert } from "../opcodes/revert";
import { printSload } from "../opcodes/sload";
import { printSstore } from "../opcodes/sstore";
import { printStaticCall } from "../opcodes/staticcall";
import { printAdd } from "../opcodes/add";
import { printSub } from "../opcodes/sub";
import { printMul } from "../opcodes/mul";
import { printEq } from "../opcodes/eq";
import { printLt } from "../opcodes/lt";
import { printGt } from "../opcodes/gt";
import { printSha3 } from "../opcodes/sha3";

/**
* Prints the given structLog to the console.
Expand Down Expand Up @@ -113,15 +120,46 @@ export async function printStructLog(
await printRevert(structLog, dependencies);
addressStack.pop();
break;

case "RETURN":
addressStack.pop();
break;
case "STOP":
addressStack.pop();
break;

default:
if (dependencies.tracerEnv.opcodes.includes(structLog.op)) {
console.log(DEPTH_INDENTATION.repeat(structLog.depth) + structLog.op);
switch (structLog.op) {
case "ADD":
await printAdd(structLog, index, structLogs, dependencies);
break;
case "SUB":
await printSub(structLog, index, structLogs, dependencies);
break;
case "MUL":
await printMul(structLog, index, structLogs, dependencies);
break;
case "DIV":
await printMul(structLog, index, structLogs, dependencies);
break;
case "EQ":
await printEq(structLog, index, structLogs, dependencies);
break;
case "LT":
await printLt(structLog, index, structLogs, dependencies);
break;
case "GT":
await printGt(structLog, index, structLogs, dependencies);
break;
case "SHA3":
await printSha3(structLog, index, structLogs, dependencies);
break;
default:
console.log(
DEPTH_INDENTATION.repeat(structLog.depth) + structLog.op
);
}
}
break;
}
Expand Down

0 comments on commit 8c6cc77

Please sign in to comment.