Skip to content

Commit

Permalink
refactor files a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
lonerapier committed Sep 12, 2024
1 parent dc9bbc8 commit 335a756
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 55 deletions.
12 changes: 6 additions & 6 deletions circuits/test/http/codegen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ interface Response {
}


function executeCodegen(circuitName: string, inputFileName: string, lockfileName: string, outputFilename: string) {
function executeCodegen(circuitName: string, inputFileName: string, lockfileName: string) {
return new Promise((resolve, reject) => {
const inputFilePath = join(__dirname, "..", "..", "..", "examples", "http", inputFileName);
const lockfilePath = join(__dirname, "..", "..", "..", "examples", "http", "lockfile", lockfileName);

const codegen = spawn("cargo", ["run", "http", "--circuit-name", circuitName, "--input-file", inputFilePath, "--lockfile", lockfilePath, "--output-filename", outputFilename]);
const codegen = spawn("cargo", ["run", "codegen", "http", "--circuit-name", circuitName, "--input-file", inputFilePath, "--lockfile", lockfilePath]);

codegen.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
Expand Down Expand Up @@ -74,7 +74,7 @@ describe("HTTP :: Codegen :: Request", async () => {
let inputfile = "get_request.http";

// generate extractor circuit using codegen
await executeCodegen("get_request_test", inputfile, `${lockfile}.json`, lockfile);
await executeCodegen("get_request_test", inputfile, `${lockfile}.json`);

const lockData = readLockFile<Request>(`${lockfile}.json`);
console.log("lockData: ", JSON.stringify(lockData));
Expand Down Expand Up @@ -116,7 +116,7 @@ describe("HTTP :: Codegen :: Request", async () => {
let inputfile = "get_request.http";

// generate extractor circuit using codegen
await executeCodegen("get_request_test", inputfile, `${lockfile}.json`, lockfile);
await executeCodegen("get_request_test", inputfile, `${lockfile}.json`);

const lockData = readLockFile<Request>(`${lockfile}.json`);

Expand Down Expand Up @@ -162,7 +162,7 @@ describe("HTTP :: Codegen :: Response", async () => {
let inputfile = "get_response.http";

// generate extractor circuit using codegen
await executeCodegen("get_response_test", inputfile, `${lockfile}.json`, lockfile);
await executeCodegen("get_response_test", inputfile, `${lockfile}.json`);

const lockData = readLockFile<Response>(`${lockfile}.json`);
console.log("lockData: ", JSON.stringify(lockData));
Expand Down Expand Up @@ -208,7 +208,7 @@ describe("HTTP :: Codegen :: Response", async () => {
let inputfile = "get_response.http";

// generate extractor circuit using codegen
await executeCodegen("get_response_test", inputfile, `${lockfile}.json`, lockfile);
await executeCodegen("get_response_test", inputfile, `${lockfile}.json`);

const lockData = readLockFile<Response>(`${lockfile}.json`);

Expand Down
24 changes: 12 additions & 12 deletions circuits/test/json/extractor/extractor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import { join } from "path";
import { spawn } from "child_process";


function executeCodegen(circuitName: string, inputFileName: string, lockfileName: string, outputFilename: string) {
function executeCodegen(circuitName: string, inputFileName: string, lockfileName: string) {
return new Promise((resolve, reject) => {
const inputFilePath = join(__dirname, "..", "..", "..", "..", "examples", "json", "test", inputFileName);
const lockfilePath = join(__dirname, "..", "..", "..", "..", "examples", "json", "lockfile", lockfileName);

const codegen = spawn("cargo", ["run", "json", "--circuit-name", circuitName, "--input-file", inputFilePath, "--lockfile", lockfilePath, "--output-filename", outputFilename]);
const codegen = spawn("cargo", ["run", "codegen", "json", "--circuit-name", circuitName, "--input-file", inputFilePath, "--lockfile", lockfilePath]);

codegen.stdout.on('data', (data) => {
console.log(`stdout: ${data}`);
Expand All @@ -35,7 +35,7 @@ describe("ExtractValue", async () => {
let filename = "value_string";

// generate extractor circuit using codegen
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`);

// read JSON input file into bytes
let [input, keyUnicode, output] = readJSONInputFile(`${filename}.json`, ["k"]);
Expand All @@ -57,7 +57,7 @@ describe("ExtractValue", async () => {

it("two_keys: {\"key1\": \"abc\", \"key2\": \"def\" }", async () => {
let filename = "two_keys"
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`);
let [input, keyUnicode, output] = readJSONInputFile(`${filename}.json`, ["key2"]);

circuit = await circomkit.WitnessTester(`Extract`, {
Expand All @@ -72,7 +72,7 @@ describe("ExtractValue", async () => {

it("value_number: {\"k\": 69 }", async () => {
let filename = "value_number";
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`);
let [input, keyUnicode, output] = readJSONInputFile(`${filename}.json`, ["k"]);

circuit = await circomkit.WitnessTester(`Extract`, {
Expand All @@ -90,10 +90,10 @@ describe("ExtractValue", async () => {
it("value_array_string: { \"k\" : [ 420 , 69 , 4200 , 600 ], \"b\": [ \"ab\" , \"ba\", \"ccc\", \"d\" ] }", async () => {
let filename = "value_array_string";
let inputFileName = "value_array.json";
await executeCodegen(`${filename}_test`, `${inputFileName}`, `${filename}.json`, filename);
await executeCodegen(`${filename}_test`, inputFileName, `${filename}.json`);

for (let i = 0; i < 4; i++) {
let [input, keyUnicode, output] = readJSONInputFile(`${inputFileName}`, ["b", i]);
let [input, keyUnicode, output] = readJSONInputFile(inputFileName, ["b", i]);

circuit = await circomkit.WitnessTester(`Extract`, {
file: `main/${filename}`,
Expand All @@ -110,10 +110,10 @@ describe("ExtractValue", async () => {
let filename = "value_array_number";
let inputFileName = "value_array.json";

await executeCodegen(`${filename}_test`, `${inputFileName}`, `${filename}.json`, filename);
await executeCodegen(`${filename}_test`, inputFileName, `${filename}.json`);

for (let i = 0; i < 4; i++) {
let [input, keyUnicode, output] = readJSONInputFile(`${inputFileName}`, ["k", i]);
let [input, keyUnicode, output] = readJSONInputFile(inputFileName, ["k", i]);

circuit = await circomkit.WitnessTester(`Extract`, {
file: `main/${filename}`,
Expand All @@ -129,7 +129,7 @@ describe("ExtractValue", async () => {

it("value_array_nested: { \"a\": [[1,0],[0,1,3]] }", async () => {
let filename = "value_array_nested";
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`);
let index_0 = 1;
let index_1 = 0;
let [input, keyUnicode, output] = readJSONInputFile(`${filename}.json`, ["a", index_0, index_1]);
Expand All @@ -154,7 +154,7 @@ describe("ExtractValueMultiDepth", () => {
it("value_object: { \"a\": { \"d\" : \"e\", \"e\": \"c\" }, \"e\": { \"f\": \"a\", \"e\": \"2\" } }", async () => {
let filename = "value_object";

await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`);

let [input, keyUnicode, output] = readJSONInputFile(`${filename}.json`, ["e", "e"]);

Expand All @@ -180,7 +180,7 @@ describe("ExtractValueArrayObject", () => {
it("value_array_object: {\"a\":[{\"b\":[1,4]},{\"c\":\"b\"}]}", async () => {
let filename = "value_array_object";

await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`, filename);
await executeCodegen(`${filename}_test`, `${filename}.json`, `${filename}.json`);

let index_0 = 0;
let index_1 = 0;
Expand Down
23 changes: 14 additions & 9 deletions src/http.rs → src/codegen/http.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use codegen::CircomkitCircuitsInput;
use crate::{
codegen::{write_circuit_config, CircomkitCircuitsInput},
witness::read_input_file_as_bytes,
ExtractorArgs, FileType,
};
use regex::Regex;
use witness::read_input_file_as_bytes;
use serde::{Deserialize, Serialize};

use super::*;
use std::{
collections::HashMap,
error::Error,
fs::{self, create_dir_all},
};

use super::codegen::write_circuit_config;

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum HttpData {
Expand Down Expand Up @@ -535,8 +537,9 @@ fn parse_http_file(
fn build_circuit_config(
args: &ExtractorArgs,
lockfile: &HttpData,
codegen_filename: String,
) -> Result<CircomkitCircuitsInput, Box<dyn std::error::Error>> {
let input = read_input_file_as_bytes(WitnessType::Http, args.input_file.clone())?;
let input = read_input_file_as_bytes(FileType::Http, args.input_file.clone())?;

let (_, http_body) = parse_http_file(lockfile, input.clone())?;

Expand Down Expand Up @@ -570,7 +573,7 @@ fn build_circuit_config(
}

Ok(CircomkitCircuitsInput {
file: format!("main/{}", args.output_filename),
file: codegen_filename,
template: circuit_template_name,
params,
})
Expand All @@ -581,9 +584,11 @@ pub fn http_circuit(args: ExtractorArgs) -> Result<(), Box<dyn Error>> {

let http_data: HttpData = serde_json::from_slice(&data)?;

build_http_circuit(&http_data, &args.output_filename, args.debug)?;
let codegen_filename = format!("http_{}", args.circuit_name);

build_http_circuit(&http_data, &codegen_filename, args.debug)?;

let circomkit_circuit_input = build_circuit_config(&args, &http_data)?;
let circomkit_circuit_input = build_circuit_config(&args, &http_data, codegen_filename)?;

write_circuit_config(args.circuit_name, &circomkit_circuit_input)?;

Expand Down
18 changes: 12 additions & 6 deletions src/json.rs → src/codegen/json.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use super::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::{
cmp::max_by,
collections::HashMap,
fs::{self, create_dir_all},
str::FromStr,
};

use codegen::{write_circuit_config, CircomkitCircuitsInput};
use serde_json::Value;
use crate::{
codegen::{write_circuit_config, CircomkitCircuitsInput},
ExtractorArgs,
};

#[derive(Debug, Deserialize)]
pub enum ValueType {
Expand Down Expand Up @@ -539,6 +542,7 @@ fn build_json_circuit(
fn build_circuit_config(
args: &ExtractorArgs,
lockfile: &JsonLockfile,
codegen_filename: String,
) -> Result<CircomkitCircuitsInput, Box<dyn std::error::Error>> {
let input = fs::read(args.input_file.clone())?;

Expand Down Expand Up @@ -611,7 +615,7 @@ fn build_circuit_config(
params.push(value_bytes.as_bytes().len());

Ok(CircomkitCircuitsInput {
file: format!("main/{}", args.output_filename),
file: format!("main/{}", codegen_filename),
template: circuit_template_name,
params,
})
Expand All @@ -620,9 +624,11 @@ fn build_circuit_config(
pub fn json_circuit(args: ExtractorArgs) -> Result<(), Box<dyn std::error::Error>> {
let lockfile: JsonLockfile = serde_json::from_slice(&std::fs::read(&args.lockfile)?)?;

build_json_circuit(&lockfile, &args.output_filename, args.debug)?;
let codegen_filename = format!("json_{}", args.circuit_name);

build_json_circuit(&lockfile, &codegen_filename, args.debug)?;

let circomkit_circuit_input = build_circuit_config(&args, &lockfile)?;
let circomkit_circuit_input = build_circuit_config(&args, &lockfile, codegen_filename)?;

write_circuit_config(args.circuit_name, &circomkit_circuit_input)?;

Expand Down
3 changes: 3 additions & 0 deletions src/codegen.rs → src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ use std::{collections::HashMap, env};

use serde::{Deserialize, Serialize};

pub mod http;
pub mod json;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CircomkitCircuitsInput {
pub file: String,
Expand Down
25 changes: 11 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use clap::{Parser, Subcommand};
use serde::{Deserialize, Serialize};
use std::{error::Error, path::PathBuf};

pub mod codegen;
pub mod http;
pub mod json;
pub mod witness;

#[derive(Parser, Debug)]
Expand All @@ -18,14 +15,13 @@ pub struct Args {
pub enum Command {
ParserWitness(ParserWitnessArgs),
ExtractorWitness(ExtractorWitnessArgs),
Json(ExtractorArgs),
Http(ExtractorArgs),
Codegen(ExtractorArgs),
}

#[derive(Parser, Debug)]
pub struct ParserWitnessArgs {
#[arg(value_enum)]
subcommand: WitnessType,
subcommand: FileType,

/// Path to the JSON file
#[arg(long)]
Expand All @@ -39,7 +35,7 @@ pub struct ParserWitnessArgs {
#[derive(Parser, Debug)]
pub struct ExtractorWitnessArgs {
#[arg(value_enum)]
subcommand: WitnessType,
subcommand: FileType,

/// Name of the circuit (to be used in circomkit config)
#[arg(long)]
Expand All @@ -55,13 +51,16 @@ pub struct ExtractorWitnessArgs {
}

#[derive(clap::ValueEnum, Clone, Debug)]
pub enum WitnessType {
pub enum FileType {
Json,
Http,
}

#[derive(Parser, Debug)]
pub struct ExtractorArgs {
#[arg(value_enum)]
subcommand: FileType,

/// Name of the circuit (to be used in circomkit config)
#[arg(long)]
circuit_name: String,
Expand All @@ -74,10 +73,6 @@ pub struct ExtractorArgs {
#[arg(long)]
lockfile: PathBuf,

/// Output circuit file name (located in circuits/main/)
#[arg(long, short, default_value = "extractor")]
output_filename: String,

/// Optional circuit debug logs
#[arg(long, short, action = clap::ArgAction::SetTrue)]
debug: bool,
Expand All @@ -86,8 +81,10 @@ pub struct ExtractorArgs {
pub fn main() -> Result<(), Box<dyn Error>> {
match Args::parse().command {
Command::ParserWitness(args) => witness::parser_witness(args),
Command::Json(args) => json::json_circuit(args),
Command::Http(args) => http::http_circuit(args),
Command::Codegen(args) => match args.subcommand {
FileType::Http => codegen::http::http_circuit(args),
FileType::Json => codegen::json::json_circuit(args),
},
Command::ExtractorWitness(args) => witness::extractor_witness(args),
}
}
14 changes: 6 additions & 8 deletions src/witness.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use json::JsonLockfile;

use super::http::HttpData;
use super::*;
use crate::codegen::{http::HttpData, json::JsonLockfile};
use std::{collections::HashMap, io::Write};

#[derive(serde::Serialize)]
Expand Down Expand Up @@ -42,12 +40,12 @@ fn print_boxed_output(lines: Vec<String>) {
}

pub fn read_input_file_as_bytes(
file_type: WitnessType,
file_type: FileType,
file_path: PathBuf,
) -> Result<Vec<u8>, Box<dyn std::error::Error>> {
match file_type {
WitnessType::Json => Ok(std::fs::read(file_path)?),
WitnessType::Http => {
FileType::Json => Ok(std::fs::read(file_path)?),
FileType::Http => {
let mut data = std::fs::read(file_path)?;
let mut i = 0;
// convert LF to CRLF
Expand Down Expand Up @@ -162,7 +160,7 @@ fn http_extractor_witness(args: ExtractorWitnessArgs) -> Result<(), Box<dyn std:

pub fn extractor_witness(args: ExtractorWitnessArgs) -> Result<(), Box<dyn std::error::Error>> {
match args.subcommand {
WitnessType::Json => json_extractor_witness(args),
WitnessType::Http => http_extractor_witness(args),
FileType::Json => json_extractor_witness(args),
FileType::Http => http_extractor_witness(args),
}
}

0 comments on commit 335a756

Please sign in to comment.