Skip to content

Commit

Permalink
refactor: extract TraceBackend as a trait
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason committed Sep 8, 2024
1 parent c41679f commit ffe1566
Show file tree
Hide file tree
Showing 16 changed files with 278 additions and 251 deletions.
4 changes: 2 additions & 2 deletions crates/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use halo2_proofs::plonk::Circuit;
use halo2_proofs::plonk::CircuitData;
use halo2_proofs::poly::commitment::Params;
use specs::slice::Slice;
use specs::slice_backend::memory::InMemoryBackend;
use specs::CompilationTable;
use specs::TraceBackend;

use crate::args::HostMode;
use crate::config::Config;
Expand Down Expand Up @@ -160,8 +160,8 @@ impl SetupArg {
let mut monitor = TableMonitor::new(
self.k,
env_builder.create_flush_strategy(),
Box::new(InMemoryBackend::default()),
&self.phantom_functions,
TraceBackend::Memory,
&env,
);

Expand Down
19 changes: 13 additions & 6 deletions crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ use halo2_proofs::poly::commitment::Params;
use indicatif::ProgressBar;
use serde::Deserialize;
use serde::Serialize;
use specs::TraceBackend;
use specs::slice_backend::SliceBackend;

use crate::args::HostMode;
use crate::names::name_of_circuit_data;
use crate::names::name_of_etable_slice;
use crate::names::name_of_external_host_call_table_slice;
use crate::names::name_of_frame_table_slice;
use crate::names::name_of_instance;
use crate::names::name_of_loadinfo;
Expand Down Expand Up @@ -244,7 +246,7 @@ impl Config {
arg: ExecutionArg,
context_output_filename: Option<String>,
mock_test: bool,
table_backend: TraceBackend,
slice_backend: Box<dyn SliceBackend>,
skip: usize,
padding: Option<usize>,
) -> anyhow::Result<()> {
Expand All @@ -261,8 +263,8 @@ impl Config {
let mut monitor = TableMonitor::new(
self.k,
env_builder.create_flush_strategy(),
slice_backend,
&self.phantom_functions,
table_backend,
&env,
);

Expand Down Expand Up @@ -308,7 +310,12 @@ impl Config {
style("[5/8]").bold().dim(),
dir
);
tables.write(&dir, |slice| name_of_frame_table_slice(&self.name, slice));
tables.write(
&dir,
|index| name_of_frame_table_slice(&self.name, index),
|index| name_of_etable_slice(&self.name, index),
|index| name_of_external_host_call_table_slice(&self.name, index),
);
}

println!("{} Build circuit(s)...", style("[6/8]").bold().dim(),);
Expand All @@ -324,9 +331,9 @@ impl Config {
ProofGenerationInfo::new(&self.name, self.k as usize, HashType::Poseidon);

let progress_bar = ProgressBar::new(if let Some(padding) = padding {
usize::max(tables.execution_tables.etable.len(), padding) as u64
usize::max(tables.execution_tables.slice_backend.len(), padding) as u64
} else {
tables.execution_tables.etable.len() as u64
tables.execution_tables.slice_backend.len() as u64
});

if skip != 0 {
Expand Down
106 changes: 106 additions & 0 deletions crates/cli/src/file_backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use std::collections::VecDeque;
use std::path::PathBuf;

use specs::etable::EventTable;
use specs::external_host_call_table::ExternalHostCallTable;
use specs::jtable::FrameTable;
use specs::slice_backend::Slice;
use specs::slice_backend::SliceBackend;

use crate::names::name_of_etable_slice;
use crate::names::name_of_external_host_call_table_slice;
use crate::names::name_of_frame_table_slice;

struct SlicePath {
event_table: PathBuf,
frame_table: PathBuf,
external_host_call_table: PathBuf,
}

impl Into<Slice> for &SlicePath {
fn into(self) -> Slice {
Slice {
etable: EventTable::read(&self.event_table).unwrap(),
frame_table: FrameTable::read(&self.frame_table).unwrap(),
external_host_call_table: ExternalHostCallTable::read(&self.external_host_call_table)
.unwrap(),
}
}
}

pub(crate) struct FileBackend {
dir_path: PathBuf,
name: String,
slices: VecDeque<SlicePath>,
}

impl FileBackend {
pub(crate) fn new(name: String, dir_path: PathBuf) -> Self {
FileBackend {
dir_path,
name,
slices: VecDeque::new(),
}
}
}

impl Iterator for FileBackend {
type Item = Slice;

fn next(&mut self) -> Option<Self::Item> {
self.slices.pop_front().map(|slice| (&slice).into())
}
}

impl SliceBackend for FileBackend {
fn push(&mut self, slice: Slice) {
let index = self.slices.len();

let event_table = {
let path = self
.dir_path
.join(PathBuf::from(name_of_etable_slice(&self.name, index)));
slice.etable.write(&path).unwrap();
path
};

let frame_table = {
let path = self
.dir_path
.join(PathBuf::from(name_of_frame_table_slice(&self.name, index)));
slice.frame_table.write(&path).unwrap();
path
};

let external_host_call_table = {
let path = self
.dir_path
.join(PathBuf::from(name_of_external_host_call_table_slice(
&self.name, index,
)));
slice.external_host_call_table.write(&path).unwrap();
path
};

self.slices.push_back(SlicePath {
event_table,
frame_table,
external_host_call_table,
});
}

fn len(&self) -> usize {
self.slices.len()
}

fn is_empty(&self) -> bool {
self.slices.is_empty()
}

fn for_each1<'a>(&'a self, f: Box<dyn Fn((usize, &Slice)) + 'a>) {
self.slices.iter().enumerate().for_each(|(index, slice)| {
let slice: Slice = slice.into();
f((index, &slice))
})
}
}
48 changes: 7 additions & 41 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@ use delphinus_zkwasm::runtime::host::default_env::ExecutionArg;
use args::HostMode;
use config::Config;
use delphinus_zkwasm::runtime::host::HostEnvBuilder;
use file_backend::FileBackend;
use names::name_of_config;
use names::name_of_etable_slice;
use names::name_of_frame_table_slice;
use specs::args::parse_args;
use specs::etable::EventTable;
use specs::jtable::FrameTable;
use specs::TraceBackend;
use specs::slice_backend::memory::InMemoryBackend;
use specs::slice_backend::SliceBackend;

mod app_builder;
mod args;
mod command;
mod config;
mod file_backend;
mod names;

const TRIVIAL_WASM: &str = r#"
Expand Down Expand Up @@ -104,43 +103,10 @@ fn main() -> Result<()> {
let private_inputs = parse_args(&arg.running_arg.private_inputs);
let context_inputs = parse_args(&arg.running_arg.context_inputs);

let trace_backend: TraceBackend = if arg.file_backend {
let event_table_writer = {
let name = cli.name.clone();
let trace_dir = trace_dir.clone();

Box::new(move |slice, etable: &EventTable| {
let filename_of_etable_slice =
PathBuf::from(name_of_etable_slice(&name, slice));
let path = trace_dir.join(filename_of_etable_slice);

etable.write(&path).unwrap();

path
})
};

let frame_table_writer = {
let name = cli.name.clone();
let trace_dir = trace_dir;

Box::new(move |slice, frame_table: &FrameTable| {
let filename_of_frame_table_slice =
PathBuf::from(name_of_frame_table_slice(&name, slice));
let path = trace_dir.join(filename_of_frame_table_slice);

frame_table.write(&path).unwrap();

path
})
};

TraceBackend::File {
event_table_writer,
frame_table_writer,
}
let trace_backend: Box<dyn SliceBackend> = if arg.file_backend {
Box::new(FileBackend::new(cli.name, trace_dir))
} else {
TraceBackend::Memory
Box::new(InMemoryBackend::default())
};

let env_builder: Box<dyn HostEnvBuilder> = match config.host_mode {
Expand Down
5 changes: 5 additions & 0 deletions crates/cli/src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,8 @@ pub(crate) fn name_of_etable_slice(name: &str, index: usize) -> String {
pub(crate) fn name_of_frame_table_slice(name: &str, index: usize) -> String {
format!("{}.frame_table.{}.data", name, index)
}

#[inline(always)]
pub(crate) fn name_of_external_host_call_table_slice(_name: &str, index: usize) -> String {
format!("external_host_table.{}.json", index)
}
41 changes: 24 additions & 17 deletions crates/specs/src/external_host_call_table/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use serde::ser::SerializeStruct;
use std::io::Read;
use std::io::Write;
use std::path::PathBuf;

use serde::Deserialize;
use serde::Serialize;

Expand All @@ -8,7 +11,7 @@ use crate::types::ValueType;
pub mod encode;
mod table;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Ord)]
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
pub enum ExternalHostCallSignature {
Argument,
Return,
Expand All @@ -35,26 +38,14 @@ impl From<ExternalHostCallSignature> for Signature {
}
}

#[derive(Serialize, Deserialize)]
pub struct ExternalHostCallEntry {
pub op: usize,
pub value: u64,
pub sig: ExternalHostCallSignature,
}

impl Serialize for ExternalHostCallEntry {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut s = serializer.serialize_struct("ExternalHostCallEntry", 3)?;
s.serialize_field("op", &self.op)?;
s.serialize_field("value", &self.value)?;
s.serialize_field("is_ret", &self.sig.is_ret())?;
s.end()
}
pub is_ret: bool,
}

#[derive(Default, Serialize)]
#[derive(Default, Serialize, Deserialize)]
pub struct ExternalHostCallTable(pub(crate) Vec<ExternalHostCallEntry>);

impl ExternalHostCallTable {
Expand All @@ -69,4 +60,20 @@ impl ExternalHostCallTable {
pub fn push(&mut self, entry: ExternalHostCallEntry) {
self.0.push(entry);
}

pub fn write(&self, path: &PathBuf) -> std::io::Result<()> {
let mut fd = std::fs::File::create(path)?;

fd.write_all(serde_json::to_string_pretty(self).unwrap().as_bytes())?;

Ok(())
}

pub fn read(path: &PathBuf) -> std::io::Result<Self> {
let mut fd = std::fs::File::open(path)?;
let mut buf = Vec::new();
fd.read_to_end(&mut buf)?;

Ok(serde_json::from_slice(&buf).unwrap())
}
}
2 changes: 1 addition & 1 deletion crates/specs/src/external_host_call_table/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ impl TryFrom<&StepInfo> for ExternalHostCallEntry {
StepInfo::ExternalHostCall { op, value, sig, .. } => Ok(ExternalHostCallEntry {
op: *op,
value: value.unwrap(),
sig: *sig,
is_ret: sig.is_ret(),
}),
_ => Err(()),
}
Expand Down
Loading

0 comments on commit ffe1566

Please sign in to comment.