Skip to content

Commit

Permalink
Merge pull request #94 from 0xPolygonHermez/feature/verbose-fulltracer
Browse files Browse the repository at this point in the history
verbose full-tracer
  • Loading branch information
zkronos73 committed Dec 14, 2022
2 parents 75d658e + 7ea9dc7 commit a9aafe5
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/main_executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const argv = require("yargs")
.alias("T", "tracer")
.alias("c", "counters")
.alias("N", "stepsN")
.alias("V", "verboseFullTracer")
.argv;

async function run() {
Expand Down Expand Up @@ -114,7 +115,8 @@ async function run() {
tracer: (argv.tracer === true),
counters: (argv.counters === true),
stats,
stepsN: (typeof argv.stepsN === 'undefined' ? undefined : argv.stepsN)
stepsN: (typeof argv.stepsN === 'undefined' ? undefined : argv.stepsN),
verboseFullTracer: (argv.verboseFullTracer === true)
}
let metadata = {};
const N = cmPols.Main.PC.length;
Expand Down
36 changes: 29 additions & 7 deletions src/sm/sm_main/debug/full-tracer.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
const fs = require("fs");
const path = require("path");
const codes = require("./opcodes");
const { scalar2fea, fea2scalar } = require("@0xpolygonhermez/zkevm-commonjs").smtUtils;
const { fea2scalar } = require("@0xpolygonhermez/zkevm-commonjs").smtUtils;
const { ethers } = require("ethers");
const { Scalar } = require("ffjavascript");

const codes = require("./opcodes");
const Verbose = require("./verbose-tracer");
const { getTransactionHash, findOffsetLabel, getVarFromCtx, getCalldataFromStack, getRegFromCtx, getFromMemory } = require("./full-tracer-utils");

const opIncContext = ['CALL', 'STATICCALL', 'DELEGATECALL', 'CALLCODE', 'CREATE', 'CREATE2'];
const responseErrors = ['OOCS', 'OOCK', 'OOCB', 'OOCM', 'OOCA', 'OOCPA', 'OOCPO', 'intrinsic_invalid_signature', 'intrinsic_invalid_chain_id', 'intrinsic_invalid_nonce', `intrinsic_invalid_gas_limit`, `intrinsic_invalid_balance`, `intrinsic_invalid_batch_gas_limit`, `intrinsic_invalid_sender_code`];
const { Scalar } = require("ffjavascript");
const generate_call_trace = true;
const generate_execute_trace = false;
const { getTransactionHash, findOffsetLabel, getVarFromCtx, getCalldataFromStack, getRegFromCtx, getFromMemory } = require("./full-tracer-utils");

/**
* Tracer service to output the logs of a batch of transactions. A complete log is created with all the transactions embedded
Expand All @@ -20,8 +23,10 @@ class FullTracer {
/**
* Constructor, instantation of global vars
* @param {String} logFileName Name of the output file
*/
constructor(logFileName) {
* @param {Object} options full-tracer options
* @param {Bool} options.verbose flag to print traces
*/
constructor(logFileName, options) {
// Opcode step traces of the all the processed tx
this.info = [];
// Stack of the transaction
Expand All @@ -43,6 +48,9 @@ class FullTracer {
this.txGAS = {};
this.accBatchGas = 0;
this.logs = [];

// options
this.verbose = new Verbose(options.verbose);
}

/**
Expand Down Expand Up @@ -77,7 +85,9 @@ class FullTracer {
*/
onError(ctx, tag) {
const errorName = tag.params[1].varName
//Intrinsic error should be set at tx level (not opcode)
this.verbose.printError(errorName);

// Intrinsic error should be set at tx level (not opcode)
if (responseErrors.includes(errorName)) {
if (this.finalTrace.responses[this.txCount]) {
this.finalTrace.responses[this.txCount].error = errorName;
Expand Down Expand Up @@ -190,6 +200,8 @@ class FullTracer {
this.depth = 0;
this.deltaStorage = { 0: {} };
this.txGAS[this.depth] = context.gas;

this.verbose.printTx(`start ${this.txCount}`);
}

/**
Expand Down Expand Up @@ -300,6 +312,9 @@ class FullTracer {
}

fs.writeFileSync(`${this.pathLogFile}_${this.txCount}.json`, JSON.stringify(this.finalTrace.responses[this.txCount], null, 2));

this.verbose.printTx(`finish ${this.txCount}`);

// Increase transaction count
this.txCount++;
}
Expand All @@ -321,6 +336,8 @@ class FullTracer {
this.finalTrace.timestamp = Number(getVarFromCtx(ctx, true, "timestamp"));
this.finalTrace.sequencerAddr = ethers.utils.hexlify(getVarFromCtx(ctx, true, "sequencerAddr"));
this.finalTrace.responses = [];

this.verbose.printBatch("start");
}

/**
Expand All @@ -345,6 +362,9 @@ class FullTracer {
this.finalTrace.new_acc_input_hash = ethers.utils.hexlify(getVarFromCtx(ctx, true, "newAccInputHash"));
this.finalTrace.new_local_exit_root = ethers.utils.hexlify(getVarFromCtx(ctx, true, "newLocalExitRoot"));
this.finalTrace.new_batch_num = ethers.utils.hexlify(getVarFromCtx(ctx, true, "newNumBatch"));

this.verbose.printBatch("finish");

// Create ouput files and dirs
this.exportTrace();
}
Expand Down Expand Up @@ -497,6 +517,8 @@ class FullTracer {
if (opIncContext.includes(singleInfo.opcode)) {
this.deltaStorage[this.depth + 1] = {};
}

this.verbose.printOpcode(opcode);
}

/**
Expand Down
43 changes: 43 additions & 0 deletions src/sm/sm_main/debug/verbose-tracer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const chalk = require("chalk");


class VerboseTracer {

constructor(enable) {
this.enable = enable == true;
}

printOpcode(message) {
if (this.enable !== true) return;

let info = `${chalk.magenta("OPCODE".padEnd(7))} | `;
info += `${message}`;
console.log(info);
}

printTx(message) {
if (this.enable !== true) return;

let info = `${chalk.yellowBright("TX".padEnd(7))} | `;
info += `${message}`;
console.log(info);
}

printBatch(message) {
if (this.enable !== true) return;

let info = `${chalk.blue("BATCH".padEnd(7))} | `;
info += `${message}`;
console.log(info);
}

printError(message) {
if (this.enable !== true) return;

let info = `${chalk.red("ERROR".padEnd(7))} | `;
info += `${message}`;
console.log(info);
}
}

module.exports = VerboseTracer;
7 changes: 6 additions & 1 deletion src/sm/sm_main/sm_main_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ module.exports = async function execute(pols, input, rom, config = {}, metadata

const debug = config && config.debug;
const flagTracer = config && config.tracer;
const verboseFullTracer = config.verboseFullTracer;
const N = pols.zkPC.length;
const stepsN = (debug && config.stepsN) ? config.stepsN : N;
const skipAddrRelControl = (config && config.skipAddrRelControl) || false;
Expand Down Expand Up @@ -100,7 +101,11 @@ module.exports = async function execute(pols, input, rom, config = {}, metadata
initState(Fr, pols, ctx);

if (debug && flagTracer) {
fullTracer = new FullTracer(config.debugInfo.inputName)
fullTracer = new FullTracer(config.debugInfo.inputName,
{
verbose: verboseFullTracer
}
);
}

const iPrint = new Prints(ctx, smt);
Expand Down

0 comments on commit a9aafe5

Please sign in to comment.