Skip to content

Commit

Permalink
Merge pull request #2 from ethstorage/dryrun_profiling
Browse files Browse the repository at this point in the history
dryrun time consuming profiling
  • Loading branch information
dajuguan authored Sep 26, 2023
2 parents e08a9dd + 326e299 commit 0b0ded4
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 26 deletions.
9 changes: 5 additions & 4 deletions crates/cli/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use delphinus_zkwasm::circuits::config::MIN_K;
use log::info;
use log::warn;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::fs;
use std::io::Write;
use std::path::PathBuf;
Expand Down Expand Up @@ -115,8 +116,8 @@ pub trait AppBuilder: CommandBuilder {
}
Some(("dry-run", sub_matches)) => {
let public_inputs: Vec<u64> = Self::parse_single_public_arg(&sub_matches);
let private_inputs: Vec<u64> = match sub_matches.contains_id("private_file") {
true => Self::parse_private_file(&sub_matches),
let private_inputs: VecDeque<u64> = match sub_matches.contains_id("private_file") {
true => Self::parse_private_file(&sub_matches).into(),
false => Self::parse_single_private_arg(&sub_matches)
};
let context_in: Vec<u64> = Self::parse_context_in_arg(&sub_matches);
Expand Down Expand Up @@ -156,7 +157,7 @@ pub trait AppBuilder: CommandBuilder {
}
Some(("single-prove", sub_matches)) => {
let public_inputs: Vec<u64> = Self::parse_single_public_arg(&sub_matches);
let private_inputs: Vec<u64> = Self::parse_single_private_arg(&sub_matches);
let private_inputs: VecDeque<u64> = Self::parse_single_private_arg(&sub_matches);
let context_in: Vec<u64> = Self::parse_context_in_arg(&sub_matches);
let context_out_path: Option<PathBuf> =
Self::parse_context_out_path_arg(&sub_matches);
Expand Down Expand Up @@ -197,7 +198,7 @@ pub trait AppBuilder: CommandBuilder {
}
Some(("aggregate-prove", sub_matches)) => {
let public_inputs: Vec<Vec<u64>> = Self::parse_aggregate_public_args(&sub_matches);
let private_inputs: Vec<Vec<u64>> =
let private_inputs: Vec<VecDeque<u64>> =
Self::parse_aggregate_private_args(&sub_matches);
let context_inputs = public_inputs.iter().map(|_| vec![]).collect();
let context_outputs = public_inputs
Expand Down
11 changes: 6 additions & 5 deletions crates/cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use clap::Arg;
use clap::ArgAction;
use clap::ArgMatches;
use std::fs;
use std::collections::VecDeque;

pub fn parse_args(values: Vec<&str>) -> Vec<u64> {
values
Expand Down Expand Up @@ -59,7 +60,7 @@ pub fn parse_args(values: Vec<&str>) -> Vec<u64> {
.collect()
}

pub fn parse_binary(filepath:String) -> Vec<u64> {
pub fn parse_binary(filepath:String) -> VecDeque<u64> {
let bytes = fs::read(filepath).unwrap();
let bytes = bytes.chunks(8);
let data = bytes
Expand All @@ -70,7 +71,7 @@ pub fn parse_binary(filepath:String) -> Vec<u64> {

u64::from_be_bytes(data)
})
.collect::<Vec<u64>>();
.collect::<VecDeque<u64>>();
data
}

Expand Down Expand Up @@ -174,10 +175,10 @@ pub trait ArgBuilder {
fn parse_aggregate_public_args(matches: &ArgMatches) -> Vec<Vec<u64>>;

fn single_private_arg<'a>() -> Arg<'a>;
fn parse_single_private_arg(matches: &ArgMatches) -> Vec<u64>;
fn parse_single_private_arg(matches: &ArgMatches) -> VecDeque<u64>;

fn private_file_arg<'a>() -> Arg<'a>;
fn parse_private_file(matches: &ArgMatches) -> Vec<u64>{
fn parse_private_file(matches: &ArgMatches) -> VecDeque<u64>{
let filepath = matches
.get_one::<String>("private_file")
.expect("private_file is required")
Expand All @@ -186,7 +187,7 @@ pub trait ArgBuilder {
}

fn aggregate_private_args<'a>() -> Arg<'a>;
fn parse_aggregate_private_args(matches: &ArgMatches) -> Vec<Vec<u64>>;
fn parse_aggregate_private_args(matches: &ArgMatches) -> Vec<VecDeque<u64>>;

fn single_instance_path_arg<'a>() -> Arg<'a> {
arg!(
Expand Down
9 changes: 5 additions & 4 deletions crates/cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use notify::Watcher;
use serde::Deserialize;
use serde::Serialize;
use std::cell::RefCell;
use std::collections::VecDeque;
use std::fs;
use std::io::Write;
use std::path::Path;
Expand Down Expand Up @@ -163,7 +164,7 @@ pub fn exec_dry_run_service(

let private_inputs = parse_args(
sequence.private_inputs.iter().map(|s| s.as_str()).collect(),
);
).into();
let public_inputs = parse_args(
sequence.public_inputs.iter().map(|s| s.as_str()).collect(),
);
Expand Down Expand Up @@ -228,7 +229,7 @@ pub fn exec_dry_run(
wasm_binary: Vec<u8>,
phantom_functions: Vec<String>,
public_inputs: Vec<u64>,
private_inputs: Vec<u64>,
private_inputs: VecDeque<u64>,
context_inputs: Vec<u64>,
context_outputs: Rc<RefCell<Vec<u64>>>,
) -> Result<()> {
Expand All @@ -251,7 +252,7 @@ pub fn exec_create_proof(
phantom_functions: Vec<String>,
output_dir: &PathBuf,
public_inputs: Vec<u64>,
private_inputs: Vec<u64>,
private_inputs: VecDeque<u64>,
context_inputs: Vec<u64>,
context_outputs: Rc<RefCell<Vec<u64>>>,
) -> Result<()> {
Expand Down Expand Up @@ -348,7 +349,7 @@ pub fn exec_aggregate_create_proof(
phantom_functions: Vec<String>,
output_dir: &PathBuf,
public_inputs: Vec<Vec<u64>>,
private_inputs: Vec<Vec<u64>>,
private_inputs: Vec<VecDeque<u64>>,
context_inputs: Vec<Vec<u64>>,
context_outputs: Vec<Rc<RefCell<Vec<u64>>>>,
) -> Result<()> {
Expand Down
8 changes: 5 additions & 3 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::VecDeque;

use anyhow::Result;
use app_builder::AppBuilder;
use args::parse_args;
Expand Down Expand Up @@ -61,21 +63,21 @@ impl ArgBuilder for SampleApp {
.help("Private arguments of your wasm program arguments of format value:type where type=i64|bytes|bytes-packed")
.min_values(0)
}
fn parse_single_private_arg(matches: &ArgMatches) -> Vec<u64> {
fn parse_single_private_arg(matches: &ArgMatches) -> VecDeque<u64> {
let inputs: Vec<&str> = matches
.get_many("private")
.unwrap_or_default()
.map(|v: &String| v.as_str())
.collect();

parse_args(inputs.into())
parse_args(inputs.into()).into()
}

fn aggregate_private_args<'a>() -> Arg<'a> {
// We only aggregate one proof in the sample program.
Self::single_private_arg()
}
fn parse_aggregate_private_args(matches: &ArgMatches) -> Vec<Vec<u64>> {
fn parse_aggregate_private_args(matches: &ArgMatches) -> Vec<VecDeque<u64>> {
let inputs = Self::parse_single_private_arg(matches);

vec![inputs]
Expand Down
3 changes: 2 additions & 1 deletion crates/zkwasm/src/foreign/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::collections::VecDeque;

use crate::circuits::cell::AllocatedUnlimitedCell;
use crate::circuits::config::zkwasm_k;
Expand Down Expand Up @@ -55,7 +56,7 @@ pub(crate) trait InternalHostPluginBuilder {
impl HostEnv {
pub fn new_with_full_foreign_plugins(
public_inputs: Vec<u64>,
private_inputs: Vec<u64>,
private_inputs: VecDeque<u64>,
context_input: Vec<u64>,
context_output: Rc<RefCell<Vec<u64>>>,
) -> (Self, WasmRuntimeIO) {
Expand Down
9 changes: 5 additions & 4 deletions crates/zkwasm/src/foreign/wasm_input_helper/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::collections::VecDeque;

use specs::host_function::HostPlugin;
use specs::types::ValueType;
Expand All @@ -12,15 +13,15 @@ use super::Op;

struct Context {
public_inputs: Vec<u64>,
private_inputs: Vec<u64>,
private_inputs: VecDeque<u64>,
instance: Rc<RefCell<Vec<u64>>>,
output: Rc<RefCell<Vec<u64>>>,
}

impl Context {
pub fn new(
public_inputs: Vec<u64>,
private_inputs: Vec<u64>,
private_inputs: VecDeque<u64>,
instance: Rc<RefCell<Vec<u64>>>,
output: Rc<RefCell<Vec<u64>>>,
) -> Self {
Expand All @@ -43,7 +44,7 @@ impl Context {
if self.private_inputs.is_empty() {
panic!("failed to read private input, please checkout your input");
}
self.private_inputs.remove(0)
self.private_inputs.pop_front().unwrap()
}

pub fn push_public(&mut self, value: u64) {
Expand All @@ -65,7 +66,7 @@ impl ForeignContext for Context {}
pub fn register_wasm_input_foreign(
env: &mut HostEnv,
public_inputs: Vec<u64>,
private_inputs: Vec<u64>,
private_inputs: VecDeque<u64>,
) -> WasmRuntimeIO {
let public_inputs_and_outputs = Rc::new(RefCell::new(vec![]));
let outputs = Rc::new(RefCell::new(vec![]));
Expand Down
11 changes: 6 additions & 5 deletions crates/zkwasm/src/loader/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::cell::RefCell;
use std::marker::PhantomData;
use std::rc::Rc;
use std::collections::VecDeque;

use anyhow::Result;
use halo2_proofs::arithmetic::MultiMillerLoop;
Expand Down Expand Up @@ -47,7 +48,7 @@ pub struct ExecutionArg {
/// Public inputs for `wasm_input(1)`
pub public_inputs: Vec<u64>,
/// Private inputs for `wasm_input(0)`
pub private_inputs: Vec<u64>,
pub private_inputs: VecDeque<u64>,
/// Context inputs for `wasm_read_context()`
pub context_inputs: Vec<u64>,
/// Context outputs for `wasm_write_context()`
Expand Down Expand Up @@ -111,7 +112,7 @@ impl<E: MultiMillerLoop> ZkWasmLoader<E> {
fn circuit_without_witness(&self) -> Result<TestCircuit<E::Scalar>> {
let (env, wasm_runtime_io) = HostEnv::new_with_full_foreign_plugins(
vec![],
vec![],
vec![].into(),
vec![],
Rc::new(RefCell::new(vec![])),
);
Expand Down Expand Up @@ -156,7 +157,7 @@ impl<E: MultiMillerLoop> ZkWasmLoader<E> {
pub fn checksum(&self, params: &Params<E::G1Affine>) -> Result<Vec<E::G1Affine>> {
let (env, _) = HostEnv::new_with_full_foreign_plugins(
vec![],
vec![],
vec![].into(),
vec![],
Rc::new(RefCell::new(vec![])),
);
Expand All @@ -175,7 +176,7 @@ impl<E: MultiMillerLoop> ZkWasmLoader<E> {
pub fn dry_run(&self, arg: ExecutionArg) -> Result<Option<RuntimeValue>> {
let (mut env, _) = HostEnv::new_with_full_foreign_plugins(
arg.public_inputs,
arg.private_inputs,
arg.private_inputs.into(),
arg.context_inputs,
arg.context_outputs,
);
Expand All @@ -188,7 +189,7 @@ impl<E: MultiMillerLoop> ZkWasmLoader<E> {
pub fn run(&self, arg: ExecutionArg) -> Result<ExecutionResult<RuntimeValue>> {
let (mut env, wasm_runtime_io) = HostEnv::new_with_full_foreign_plugins(
arg.public_inputs,
arg.private_inputs,
arg.private_inputs.into(),
arg.context_inputs,
arg.context_outputs,
);
Expand Down

0 comments on commit 0b0ded4

Please sign in to comment.