Skip to content

Commit

Permalink
Merge pull request #210 from 0xPolygonHermez/feature/add-verbose-options
Browse files Browse the repository at this point in the history
add options save return outputs from execution
  • Loading branch information
krlosMata authored Sep 7, 2023
2 parents 78489d7 + fa82a80 commit 5c9ee65
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 5 deletions.
24 changes: 24 additions & 0 deletions src/sm/sm_main/debug/full-tracer-utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable default-param-last */
const { ethers } = require('ethers');
const { Scalar } = require('ffjavascript');
const { toHexStringRlp, addressToHexStringRlp } = require('@0xpolygonhermez/zkevm-commonjs').processorUtils;
const { scalar2fea, fea2scalar } = require('@0xpolygonhermez/zkevm-commonjs').smtUtils;

Expand Down Expand Up @@ -164,6 +165,28 @@ function bnToPaddedHex(bn, paddingLength) {
return `0x${ethers.utils.hexlify(bn).slice(2).padStart(paddingLength, '0')}`;
}

function convertBigIntsToNumbers(obj) {
if (typeof (obj) === 'bigint') {
if (Scalar.gt(obj, Number.MAX_SAFE_INTEGER)) {
throw new Error(`Cannot convert ${obj} to number. Greater than MAX_SAFE_INTEGER`);
}

return Number(obj);
} if (Array.isArray(obj)) {
return obj.map(convertBigIntsToNumbers);
} if (typeof obj === 'object') {
const res = {};
const keys = Object.keys(obj);
keys.forEach((k) => {
res[k] = convertBigIntsToNumbers(obj[k]);
});

return res;
}

return obj;
}

module.exports = {
getTransactionHash,
findOffsetLabel,
Expand All @@ -172,4 +195,5 @@ module.exports = {
getFromMemory,
getConstantFromCtx,
bnToPaddedHex,
convertBigIntsToNumbers,
};
13 changes: 11 additions & 2 deletions src/sm/sm_main/debug/full-tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,9 @@ class FullTracer {
// write single tx trace
fs.writeFileSync(`${this.pathLogFile}_${this.txCount}.json`, JSON.stringify(this.finalTrace.responses[this.txCount], null, 2));

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

// Increase transaction count
this.txCount += 1;

Expand All @@ -411,8 +414,6 @@ class FullTracer {
this.execution_trace = [];
this.logs = [];
this.callData = [];
// verbose
this.verbose.printTx(`finish ${this.txCount}`);
}

/**
Expand Down Expand Up @@ -816,6 +817,14 @@ class FullTracer {
}
fs.writeFileSync(`${this.pathLogFile}.json`, JSON.stringify(this.finalTrace, null, 2));
}

/**
* Prints outcome of the execution
* @param {Object} returnData required outputs
*/
printReturn(returnData) {
this.verbose.printSaveReturn(returnData);
}
}

module.exports = FullTracer;
42 changes: 40 additions & 2 deletions src/sm/sm_main/debug/verbose-tracer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const chalk = require('chalk');
const { Scalar } = require('ffjavascript');

const { smtUtils, stateUtils, utils } = require('@0xpolygonhermez/zkevm-commonjs');
const { convertBigIntsToNumbers } = require('./full-tracer-utils');

class VerboseTracer {
/**
Expand All @@ -22,13 +23,16 @@ class VerboseTracer {
this.initFinalState = options.initFinalState;
this.saveInitFinalState = options.saveInitFinalState;
this.bytecode = options.bytecode;
this.printReturn = options.printReturn;
this.saveReturn = options.saveReturn;

this.smt = smt;
this.touched = {};

// Logs init-final state path
this.folderLogs = path.join(__dirname, '../logs-verbose');
this.pathLogFile = `${this.folderLogs}/${fileName.split('.')[0]}-verbose`;
this.pathLogFileState = `${this.folderLogs}/${fileName.split('.')[0]}-pre-post-state`;
this.pathLogFileReturn = `${this.folderLogs}/${fileName.split('.')[0]}-return-data`;
}

saveInitStateRoot(initSR) {
Expand Down Expand Up @@ -121,7 +125,7 @@ class VerboseTracer {
if (!fs.existsSync(this.folderLogs)) {
fs.mkdirSync(this.folderLogs);
}
fs.writeFileSync(`${this.pathLogFile}.json`, JSON.stringify(fullInfo, null, 2));
fs.writeFileSync(`${this.pathLogFileState}.json`, JSON.stringify(fullInfo, null, 2));
}
}

Expand Down Expand Up @@ -156,6 +160,40 @@ class VerboseTracer {

return info;
}

printSaveReturn(returnData) {
if (this.enable !== true) return;

if (this.printReturn) {
let info = `${chalk.whiteBright('/////////////////////////////\n')}`;
info += `${chalk.whiteBright('//////////RETURN////////////\n')}`;
info += `${chalk.whiteBright('///////////////////////////')}`;
console.log(info);

info = `${chalk.magenta('---> OUTPUTS\n')}`;
console.log(returnData.outputs);

info = `${chalk.magenta('\n---> COUNTERS\n')}`;
console.log(returnData.counters);

info = `${chalk.magenta('\n---> ERRORS\n')}`;
console.log(returnData.errors);
}

if (this.saveReturn) {
const saveData = {
outputs: convertBigIntsToNumbers(returnData.outputs),
counters: convertBigIntsToNumbers(returnData.counters),
errors: returnData.errors,
logs: returnData.logs,
};

if (!fs.existsSync(this.folderLogs)) {
fs.mkdirSync(this.folderLogs);
}
fs.writeFileSync(`${this.pathLogFileReturn}.json`, JSON.stringify(saveData, null, 2));
}
}
}

module.exports = VerboseTracer;
11 changes: 11 additions & 0 deletions src/sm/sm_main/sm_main_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2037,6 +2037,17 @@ module.exports = async function execute(pols, input, rom, config = {}, metadata
newNumBatch: ctx.PC,
}

if (fullTracer) {
if (fullTracer.options.verbose.enable) {
fullTracer.printReturn({
outputs: required.output,
counters: required.counters,
logs: required.logs,
errors: required.errors
});
}
}

return required;
}

Expand Down
5 changes: 4 additions & 1 deletion verbose-config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
"filterOpcodes": ["EXTCODECOPY"],
"initFinalState": true,
"bytecode": true,
"saveInitFinalState": true
"saveInitFinalState": true,
"outputs": true,
"saveOutputs": true
},
"zkPC": false,
"batchL2Data": false,
"getNameSelector": "https://api.openchain.xyz/signature-database/v1/lookup?filter=false&function=",
"step": 1000000
}

0 comments on commit 5c9ee65

Please sign in to comment.