Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Pino for new Logging system #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions dist/comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.uncommentLinesInFile = exports.commentLinesInFile = exports.unCommentAllFiles = exports.commentAllFiles = void 0;
exports.commentAllFiles = commentAllFiles;
exports.unCommentAllFiles = unCommentAllFiles;
exports.commentLinesInFile = commentLinesInFile;
exports.uncommentLinesInFile = uncommentLinesInFile;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
function commentAllFiles(folderPath_1, searchString_1) {
Expand All @@ -25,7 +28,6 @@ function commentAllFiles(folderPath_1, searchString_1) {
}
});
}
exports.commentAllFiles = commentAllFiles;
function unCommentAllFiles(folderPath_1, searchString_1) {
return __awaiter(this, arguments, void 0, function* (folderPath, searchString, comment = "//SPIN_INTERMEDITE_COMMENT@") {
const files = yield getFiles(folderPath);
Expand All @@ -36,7 +38,6 @@ function unCommentAllFiles(folderPath_1, searchString_1) {
}
});
}
exports.unCommentAllFiles = unCommentAllFiles;
function getFiles(dir_1) {
return __awaiter(this, arguments, void 0, function* (dir, allFiles = []) {
const files = yield fs_1.default.promises.readdir(dir);
Expand Down Expand Up @@ -66,7 +67,6 @@ function commentLinesInFile(filePath, searchString, comment) {
yield fs_1.default.promises.writeFile(filePath, commentedLines.join("\n"), "utf8");
});
}
exports.commentLinesInFile = commentLinesInFile;
function uncommentLinesInFile(filePath, searchString, comment) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield fs_1.default.promises.readFile(filePath, "utf8");
Expand All @@ -82,4 +82,3 @@ function uncommentLinesInFile(filePath, searchString, comment) {
return;
});
}
exports.uncommentLinesInFile = uncommentLinesInFile;
77 changes: 39 additions & 38 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const path_1 = __importDefault(require("path"));
const zkwasm_1 = require("./zkwasm");
const ethers_1 = require("ethers");
const comment_1 = require("./comment");
const logger_1 = __importDefault(require("./logger"));
const args = process.argv.slice(2);
const FOLDER_IGNORE_LIST = [
"node_modules",
Expand All @@ -42,12 +43,12 @@ const ZK_CLOUD_URL = "https://rpc.zkwasmhub.com:8090";
*/
function copyFolderSync(src, dest) {
if (!fs_1.default.existsSync(src)) {
console.error(`Source folder does not exist: ${src}`);
logger_1.default.error(`Source folder does not exist: ${src}`);
return;
}
// Create destination folder if it doesn't exist
if (fs_1.default.existsSync(dest)) {
console.error(`Destination folder already exists: ${dest}`);
logger_1.default.error(`Destination folder already exists: ${dest}`);
}
fs_1.default.mkdirSync(dest, { recursive: true });
// Read the contents of the source folder
Expand All @@ -69,15 +70,15 @@ function copyFolderSync(src, dest) {
function init() {
// Argument Parsing and Validation
if (args.length < 2) {
console.error("Please provide a folder name.");
console.error(" Usage: npx spin init [folderName] --[optionalArgs]");
logger_1.default.error("Please provide a folder name.");
logger_1.default.error(" Usage: npx spin init [folderName] --[optionalArgs]");
process.exit(1);
}
const optionalArgs = args.filter((arg) => arg.startsWith("--"));
const folderName = args[1];
if (optionalArgs.includes(folderName)) {
console.error("Please provide a valid folder name. Provided: ", folderName);
console.error(" Usage: npx spin init [folderName] --[optionalArgs]");
logger_1.default.error("Please provide a valid folder name. Provided: ", folderName);
logger_1.default.error(" Usage: npx spin init [folderName] --[optionalArgs]");
}
const sourcePath = path_1.default.join(__dirname, "..", "sdk");
const destinationPath = path_1.default.join(process.cwd(), folderName);
Expand All @@ -93,31 +94,31 @@ function init() {
const sourceDirGameplay = path_1.default.join(sourcePath, "gameplay");
const destinationDirGameplay = path_1.default.join(destinationPath, "gameplay");
copyFolderSync(sourceDirGameplay, destinationDirGameplay);
console.log(`Successfully initialized under folder: ${destinationPath}`);
logger_1.default.info(`Successfully initialized under folder: ${destinationPath}`);
}
function help() {
console.log("Usage: npx spin [command] \n");
console.log("Commands:");
console.log(" init [folderName] Initialize project with gameplay folder");
console.log(" help Show help information");
console.log(" build-image Build the project wasm image");
console.log(" publish-image Publish the project wasm image");
console.log(" dry-run Run a dry-run of the wasm image");
console.log(" version Show the version of spin");
console.log("Options:");
console.log(" --path Path to the provable_game_logic folder");
console.log(" --zkwasm Path to the zkwasm-cli folder");
console.log(" --public Public inputs for the dry-run");
console.log(" --private Private inputs for the dry-run");
console.log(" --seed Seed for the dry-run");
console.log(" --keyCode KeyCode for the dry-run");
logger_1.default.info("Usage: npx spin [command] \n");
logger_1.default.info("Commands:");
logger_1.default.info(" init [folderName] Initialize project with gameplay folder");
logger_1.default.info(" help Show help information");
logger_1.default.info(" build-image Build the project wasm image");
logger_1.default.info(" publish-image Publish the project wasm image");
logger_1.default.info(" dry-run Run a dry-run of the wasm image");
logger_1.default.info(" version Show the version of spin");
logger_1.default.info("Options:");
logger_1.default.info(" --path Path to the provable_game_logic folder");
logger_1.default.info(" --zkwasm Path to the zkwasm-cli folder");
logger_1.default.info(" --public Public inputs for the dry-run");
logger_1.default.info(" --private Private inputs for the dry-run");
logger_1.default.info(" --seed Seed for the dry-run");
logger_1.default.info(" --keyCode KeyCode for the dry-run");
}
function build() {
return __awaiter(this, void 0, void 0, function* () {
console.log("Building project...");
console.log("Args: ", args);
logger_1.default.info("Building project...");
logger_1.default.info(`Args: ${JSON.stringify(args)}`);
const optionalArgs = args.filter((arg) => arg.startsWith("--"));
console.log("Optional Args: ", optionalArgs);
logger_1.default.info(`Optional Args: ${JSON.stringify(optionalArgs)}`);
if (!optionalArgs.includes("--path")) {
console.error("--path flag is required, path of the provable_game_logic folder.");
console.error("Usage: npx spin build-image --path [path]");
Expand All @@ -131,9 +132,9 @@ function build() {
// if (optionalArgs.includes("--out")) {
// outDir = parsePath(args[args.indexOf("--out") + 1]);
// }
console.log("Building project at path:", projectPath);
logger_1.default.info("Building project at path:", projectPath);
const { spawnSync } = require("child_process");
console.log("Building javascript packages...");
logger_1.default.info("Building javascript packages...");
spawnSync("make", [
"--makefile",
makeFilePath,
Expand All @@ -144,7 +145,7 @@ function build() {
cwd: exportPath,
stdio: "inherit",
});
console.log("Building wasm packages for proving...");
logger_1.default.info("Building wasm packages for proving...");
// !!! Caveat for build WASM for proving:
// zkWASM doesn't support #[wasm_bindgen] for Struct types.
// As a workaround, we need to comment out the #[wasm_bindgen]
Expand Down Expand Up @@ -176,7 +177,7 @@ function build() {
}
function publish() {
return __awaiter(this, void 0, void 0, function* () {
console.log("Publishing project...");
logger_1.default.info("Publishing project...");
const optionalArgs = args.filter((arg) => arg.startsWith("--"));
if (!optionalArgs.includes("--path")) {
console.error("--path flag is required, a path to provable_game_logic folder.");
Expand All @@ -193,7 +194,7 @@ function publish() {
console.error("Path must contain a .wasm file.");
process.exit(1);
}
console.log("Publishing wasm image at path:", filePath);
logger_1.default.info("Publishing wasm image at path:", filePath);
const { imageCommitment, md5 } = yield (0, zkwasm_1.addImage)({
USER_ADDRESS: ZK_CLOUD_USER_ADDRESS,
USER_PRIVATE_KEY: ZK_CLOUD_USER_PRIVATE_KEY,
Expand All @@ -204,11 +205,11 @@ function publish() {
return ethers_1.ethers.solidityPackedKeccak256(["uint256", "uint256", "uint256"], [imageCommitment[0], imageCommitment[1], imageCommitment[2]]);
}
const gameID = createCommit2();
console.log("--------------------");
console.log("Record The Following Information:");
console.log("Game ID: ", gameID);
console.log("Image Hash", md5);
console.log("Image Commitments: ", imageCommitment);
logger_1.default.info("--------------------");
logger_1.default.info("Record The Following Information:");
logger_1.default.info("Game ID: ", gameID);
logger_1.default.info("Image Hash", md5);
logger_1.default.info("Image Commitments: ", imageCommitment);
return imageCommitment;
});
}
Expand Down Expand Up @@ -258,7 +259,7 @@ function dryRun() {
privateInputs.push(args[i + 1]);
}
}
console.log("Running dry-run for wasm at path:", filePath);
logger_1.default.info("Running dry-run for wasm at path:", filePath);
const { spawnSync } = require("child_process");
const runSetup = spawnSync(`${wasmPath}/zkwasm-cli`, [
"--params",
Expand All @@ -282,15 +283,15 @@ function dryRun() {
`${privateInputs.length}:i64`,
...privateInputs.flatMap((i) => ["--private", `${i}:i64`]),
];
console.log("Running dry-run with args:", wasmArgs.join(" "));
logger_1.default.info("Running dry-run with args:", wasmArgs.join(" "));
const runDryRun = spawnSync(`${wasmPath}/zkwasm-cli`, wasmArgs, {
stdio: "inherit",
});
}
const VERSION = "0.5.0";
function entry() {
return __awaiter(this, void 0, void 0, function* () {
console.log("Running Spin version", VERSION);
logger_1.default.info("Running Spin version", VERSION);
if (args[0] === "init") {
init();
}
Expand Down
38 changes: 38 additions & 0 deletions dist/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const pino_1 = __importDefault(require("pino"));
const logLevels = {
'--trace': 'trace',
'--debug': 'debug',
'--info': 'info',
'--warn': 'warn',
'--error': 'error',
'--fatal': 'fatal'
};
function getLogLevel(args) {
let logLevel = 'info'; // Default log level
for (const arg of args) {
if (logLevels.hasOwnProperty(arg)) {
logLevel = logLevels[arg];
break;
}
}
return logLevel;
}
const args = process.argv.slice(2);
const logLevel = getLogLevel(args);
const logger = (0, pino_1.default)({
level: logLevel,
transport: {
target: 'pino-pretty',
options: {
colorize: true,
translateTime: 'SYS:standard',
ignore: 'pid,hostname',
}
}
});
exports.default = logger;
5 changes: 2 additions & 3 deletions dist/zkwasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getImageCommitmentBigInts = exports.addImage = void 0;
exports.addImage = addImage;
exports.getImageCommitmentBigInts = getImageCommitmentBigInts;
/* SDK FILE*/
const fs_1 = __importDefault(require("fs"));
const path_1 = require("path");
Expand Down Expand Up @@ -62,7 +63,6 @@ function addImage(cloudCredential, wasm_path) {
return { imageCommitment, md5 };
});
}
exports.addImage = addImage;
function getImageCommitmentBigInts(cloudCredential) {
return __awaiter(this, void 0, void 0, function* () {
const helper = new zkwasm_service_helper_1.ZkWasmServiceHelper(cloudCredential.CLOUD_RPC_URL, "", "");
Expand All @@ -75,7 +75,6 @@ function getImageCommitmentBigInts(cloudCredential) {
return commitment;
});
}
exports.getImageCommitmentBigInts = getImageCommitmentBigInts;
/* This function is used to convert the commitment hex to hex string
* in the format of verifying instance
* @param x: x hex string
Expand Down
Loading