From d49474ec4d247b1b3d29345a751ff3347764a5f4 Mon Sep 17 00:00:00 2001 From: sleepyqadir Date: Fri, 21 Oct 2022 15:55:04 +0500 Subject: [PATCH] fix(debug\): resolve the glitch in input logs and optimized the code resolve the glitch in input logs and optimized the code --- utils/logger.ts | 30 ++++++++++++------------------ utils/utils.ts | 16 +++++++++++++++- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/utils/logger.ts b/utils/logger.ts index 8d689cc..d3798a6 100644 --- a/utils/logger.ts +++ b/utils/logger.ts @@ -7,6 +7,7 @@ import { import { readWtnsHeader } from "./witness"; // @ts-ignore import { Scalar } from "ffjavascript"; +import { arrayLogger } from "./utils"; const TYPES = { info: "info", @@ -55,7 +56,7 @@ export const logSignals = async ( r1cs: any, wtnsFile: any, symFile: any, - inputSignals: any + jsonInputs: any ) => { if (r1cs) { const { fd: fdWtns, sections: sectionsWtns } = await readBinFile( @@ -70,7 +71,6 @@ export const logSignals = async ( const buffWitness = await readSection(fdWtns, sectionsWtns, 2); let outputPrefixes: any = {}; - let inputPrefixes: any = {}; let lastPos = 0; let dec = new TextDecoder("utf-8"); for (let i = 0; i < symFile.length; i++) { @@ -80,8 +80,6 @@ export const logSignals = async ( if (wireNo <= r1cs.nOutputs) { outputPrefixes[wireNo] = line.split(",")[3].replace("main.", "") + " = "; - } else { - inputPrefixes[line.split(",")[3].replace("main.", "")] = 0; } lastPos = i; } @@ -109,29 +107,25 @@ export const logSignals = async ( } } - const sortedKeys = Object.keys(inputPrefixes).sort(); - const sortedSignals = Object.keys(inputSignals).sort(); + const sortedSignals = Object.keys(jsonInputs).sort(); + + let inputSignals: any = {}; - let iterator = 0; for (const key of sortedSignals) { - if (Object.prototype.hasOwnProperty.call(inputSignals, key)) { - const element = inputSignals[key]; + if (Object.prototype.hasOwnProperty.call(jsonInputs, key)) { + const element = jsonInputs[key]; if (typeof element === "object") { - const flatArray = element.flat(); - for (const signal of flatArray) { - inputPrefixes[sortedKeys[iterator]] = signal; - iterator++; - } + inputSignals = { ...inputSignals, ...arrayLogger("matrix", element) }; } else { - inputPrefixes[sortedKeys[iterator]] = element; - iterator++; + inputSignals[key] = jsonInputs[key]; } + } else { } } - if (Object.keys(inputSignals).length !== 0) { + if (Object.keys(jsonInputs).length !== 0) { console.log(chalk.cyan(`\nInput Signals:\n`)); - console.table(inputPrefixes); + console.table(inputSignals); } else { console.log(chalk.yellow(`No input signal found:\n`)); } diff --git a/utils/utils.ts b/utils/utils.ts index f4a51b5..4806ef0 100644 --- a/utils/utils.ts +++ b/utils/utils.ts @@ -157,7 +157,6 @@ export const createInterface = async ( } }; - export const bumpSolidityVersion = async ( SOLIDITY_VERSION: string, CIRCUIT_NAME: string, @@ -203,3 +202,18 @@ export const bumpSolidityVersion = async ( throw error; } }; + + +export const arrayLogger = (key: string, array: any[]): any => { + let prefixes: any = {}; + for (let index = 0; index < array.length; index++) { + const element = array[index]; + if (typeof element === "object") { + const subPrefixes = arrayLogger(`${key}[${index}]`, element); + prefixes = { ...prefixes, ...subPrefixes }; + } else { + prefixes[`${key}[${index}]`] = element; + } + } + return prefixes; +};