Skip to content

Commit

Permalink
try chnage from Rc to Arc
Browse files Browse the repository at this point in the history
  • Loading branch information
yymone committed Oct 9, 2023
1 parent dd67c8c commit db8f212
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 36 deletions.
10 changes: 6 additions & 4 deletions crates/cli/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::fs;
use std::io::Write;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::Mutex;

use crate::exec::exec_dry_run;

Expand Down Expand Up @@ -143,7 +145,7 @@ pub trait AppBuilder: CommandBuilder {
public_inputs,
private_inputs,
context_in,
Rc::new(RefCell::new(vec![])),
Arc::new(Mutex::new(vec![])),
)?;

write_context_output(&context_output.borrow(), context_out_path)?;
Expand All @@ -158,7 +160,7 @@ pub trait AppBuilder: CommandBuilder {
let context_out_path: Option<PathBuf> =
Self::parse_context_out_path_arg(&sub_matches);

let context_out = Rc::new(RefCell::new(vec![]));
let context_out = Arc::new(Mutex::new(vec![]));

assert!(public_inputs.len() <= Self::MAX_PUBLIC_INPUT_SIZE);

Expand All @@ -174,7 +176,7 @@ pub trait AppBuilder: CommandBuilder {
context_out.clone(),
)?;

write_context_output(&context_out.borrow(), context_out_path)?;
write_context_output(&context_out.lock().unwrap(), context_out_path)?;

Ok(())
}
Expand All @@ -199,7 +201,7 @@ pub trait AppBuilder: CommandBuilder {
let context_inputs = public_inputs.iter().map(|_| vec![]).collect();
let context_outputs = public_inputs
.iter()
.map(|_| Rc::new(RefCell::new(vec![])))
.map(|_| Arc::new(Mutex::new(vec![])))
.collect();

for instances in &public_inputs {
Expand Down
14 changes: 7 additions & 7 deletions crates/cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ use notify::RecursiveMode;
use notify::Watcher;
use serde::Deserialize;
use serde::Serialize;
use std::cell::RefCell;
use std::fs;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::Mutex;
use wasmi::RuntimeValue;

use crate::app_builder::write_context_output;
Expand Down Expand Up @@ -170,7 +170,7 @@ pub fn exec_dry_run_service(
let context_inputs = parse_args(
sequence.context_input.iter().map(|s| s.as_str()).collect(),
);
let context_outputs = Rc::new(RefCell::new(vec![]));
let context_outputs = Arc::new(Mutex::new(vec![]));

let loader = ZkWasmLoader::<Bn256>::new(
zkwasm_k,
Expand All @@ -190,7 +190,7 @@ pub fn exec_dry_run_service(
println!("return value: {:?}", r);

write_context_output(
&context_outputs.borrow().to_vec(),
&context_outputs.lock().unwrap().to_vec(),
sequence.context_output,
)
.unwrap();
Expand Down Expand Up @@ -230,7 +230,7 @@ pub fn exec_dry_run(
public_inputs: Vec<u64>,
private_inputs: Vec<u64>,
context_inputs: Vec<u64>,
context_outputs: Rc<RefCell<Vec<u64>>>,
context_outputs: Arc<Mutex<Vec<u64>>>,
) -> Result<()> {
let loader = ZkWasmLoader::<Bn256>::new(zkwasm_k, wasm_binary, phantom_functions)?;

Expand All @@ -253,7 +253,7 @@ pub fn exec_create_proof(
public_inputs: Vec<u64>,
private_inputs: Vec<u64>,
context_inputs: Vec<u64>,
context_outputs: Rc<RefCell<Vec<u64>>>,
context_outputs: Arc<Mutex<Vec<u64>>>,
) -> Result<()> {
let loader = ZkWasmLoader::<Bn256>::new(zkwasm_k, wasm_binary, phantom_functions)?;

Expand Down Expand Up @@ -350,7 +350,7 @@ pub fn exec_aggregate_create_proof(
public_inputs: Vec<Vec<u64>>,
private_inputs: Vec<Vec<u64>>,
context_inputs: Vec<Vec<u64>>,
context_outputs: Vec<Rc<RefCell<Vec<u64>>>>,
context_outputs: Vec<Arc<Mutex<Vec<u64>>>>,
) -> Result<()> {
assert_eq!(public_inputs.len(), private_inputs.len());

Expand Down
13 changes: 8 additions & 5 deletions crates/zkwasm/src/foreign/context/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::cell::RefCell;

use std::rc::Rc;

use specs::host_function::HostPlugin;
Expand All @@ -9,15 +9,18 @@ use wasmi::RuntimeArgs;
use crate::runtime::host::host_env::HostEnv;
use crate::runtime::host::ForeignContext;

use std::sync::Arc;
use std::sync::Mutex;

use super::Op;

struct Context {
inputs: Vec<u64>,
outputs: Rc<RefCell<Vec<u64>>>,
outputs: Arc<Mutex<Vec<u64>>>,
}

impl Context {
fn new(context_input: Vec<u64>, context_output: Rc<RefCell<Vec<u64>>>) -> Self {
fn new(context_input: Vec<u64>, context_output: Arc<Mutex<Vec<u64>>>) -> Self {
let mut inputs = context_input.clone();
inputs.reverse();

Expand All @@ -28,7 +31,7 @@ impl Context {
}

fn push_output(&mut self, value: u64) {
self.outputs.borrow_mut().push(value)
self.outputs.lock().unwrap().push(value)
}

fn pop_input(&mut self) -> u64 {
Expand All @@ -43,7 +46,7 @@ impl ForeignContext for Context {}
pub fn register_context_foreign(
env: &mut HostEnv,
context_input: Vec<u64>,
context_output: Rc<RefCell<Vec<u64>>>,
context_output: Arc<Mutex<Vec<u64>>>,
) {
env.internal_env.register_plugin(
HostPlugin::Context,
Expand Down
6 changes: 3 additions & 3 deletions crates/zkwasm/src/foreign/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::Mutex;

use crate::circuits::cell::AllocatedUnlimitedCell;
use crate::circuits::config::zkwasm_k;
Expand Down Expand Up @@ -57,7 +57,7 @@ impl HostEnv {
public_inputs: Vec<u64>,
private_inputs: Vec<u64>,
context_input: Vec<u64>,
context_output: Rc<RefCell<Vec<u64>>>,
context_output: Arc<Mutex<Vec<u64>>>,
) -> (Self, WasmRuntimeIO) {
let mut env = HostEnv::new();
let wasm_runtime_io = register_wasm_input_foreign(&mut env, public_inputs, private_inputs);
Expand Down
19 changes: 5 additions & 14 deletions crates/zkwasm/src/loader/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::cell::RefCell;
use std::marker::PhantomData;
use std::rc::Rc;

use anyhow::Result;
use halo2_proofs::arithmetic::MultiMillerLoop;
Expand Down Expand Up @@ -37,6 +35,8 @@ use crate::runtime::wasmi_interpreter::Execution;
use crate::runtime::CompiledImage;
use crate::runtime::ExecutionResult;
use crate::runtime::WasmInterpreter;
use std::sync::Arc;
use std::sync::Mutex;
use anyhow::anyhow;

mod err;
Expand All @@ -51,7 +51,7 @@ pub struct ExecutionArg {
/// Context inputs for `wasm_read_context()`
pub context_inputs: Vec<u64>,
/// Context outputs for `wasm_write_context()`
pub context_outputs: Rc<RefCell<Vec<u64>>>,
pub context_outputs: Arc<Mutex<Vec<u64>>>,
}

pub struct ExecutionReturn {
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<E: MultiMillerLoop> ZkWasmLoader<E> {
vec![],
vec![],
vec![],
Rc::new(RefCell::new(vec![])),
Arc::new(Mutex::new(vec![])),
);

let compiled_module = self.compile(&env)?;
Expand Down Expand Up @@ -158,7 +158,7 @@ impl<E: MultiMillerLoop> ZkWasmLoader<E> {
vec![],
vec![],
vec![],
Rc::new(RefCell::new(vec![])),
Arc::new(Mutex::new(vec![])),
);
let compiled = self.compile(&env)?;

Expand All @@ -172,15 +172,6 @@ impl<E: MultiMillerLoop> ZkWasmLoader<E> {
}

impl<E: MultiMillerLoop> ZkWasmLoader<E> {
pub fn dry_run_without_output(&self, public_inputs: Vec<u64>, private_inputs: Vec<u64>, context_inputs: Vec<u64>) -> Result<Option<RuntimeValue>> {
let context_outputs = Rc::new(RefCell::new(vec![]));
self.dry_run(ExecutionArg {
public_inputs,
private_inputs,
context_inputs,
context_outputs,
})
}

pub fn dry_run(&self, arg: ExecutionArg) -> Result<Option<RuntimeValue>> {
let (mut env, _) = HostEnv::new_with_full_foreign_plugins(
Expand Down
6 changes: 3 additions & 3 deletions crates/zkwasm/src/test/test_rlp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::Mutex;

use crate::circuits::TestCircuit;
use crate::loader::ExecutionArg;
Expand Down Expand Up @@ -155,7 +155,7 @@ fn build_circuit() -> Result<(ZkWasmLoader<Bn256>, TestCircuit<Fr>, Vec<Fr>)> {
public_inputs,
private_inputs,
context_inputs: vec![],
context_outputs: Rc::new(RefCell::new(vec![])),
context_outputs: Arc::new(Mutex::new(vec![])),
})?;

Ok((loader, circuit, instances))
Expand Down

0 comments on commit db8f212

Please sign in to comment.