Skip to content

Commit

Permalink
Add 'rpc' feature instead of 'test' and 'execute'
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh committed Dec 19, 2024
1 parent 15518a0 commit f8936ca
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 77 deletions.
11 changes: 5 additions & 6 deletions tooling/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,20 @@ tracing.workspace = true
walkdir = "2.5.0"

# Some dependencies are optional so we can compile to Wasm.
rand = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
jsonrpsee = { workspace = true, optional = true }
tokio = { workspace = true, optional = true }
rand = { workspace = true, optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
noir_fuzzer = { workspace = true, optional = true }
proptest = { workspace = true, optional = true }
noir_fuzzer = { workspace = true }
proptest = { workspace = true }

[dev-dependencies]
jsonrpsee = { workspace = true, features = ["server"] }

[features]
default = ["execute", "test"]
default = ["rpc"]

# Execution currently uses HTTP based Oracle resolvers; does not compile to Wasm.
execute = ["jsonrpsee/http-client", "jsonrpsee/macros", "tokio/rt", "serde", "rand"]
test = ["execute", "noir_fuzzer", "proptest"]
rpc = ["jsonrpsee/http-client", "jsonrpsee/macros", "tokio/rt", "serde", "rand"]
59 changes: 59 additions & 0 deletions tooling/nargo/src/foreign_calls/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use std::path::PathBuf;

use acvm::AcirField;
use rand::Rng;
use serde::{Deserialize, Serialize};

use crate::PrintOutput;

use super::{
layers::{self, Layer, Layering},
mocker::MockForeignCallExecutor,
print::PrintForeignCallExecutor,
rpc::RPCForeignCallExecutor,
ForeignCallExecutor,
};

pub struct DefaultForeignCallExecutor;

impl DefaultForeignCallExecutor {
#[allow(clippy::new_ret_no_self)]
pub fn new<'a, F>(
output: PrintOutput<'a>,
resolver_url: Option<&str>,
root_path: Option<PathBuf>,
package_name: Option<String>,
) -> impl ForeignCallExecutor<F> + 'a
where
F: AcirField + Serialize + for<'de> Deserialize<'de> + 'a,
{
Self::with_base(layers::Empty, output, resolver_url, root_path, package_name)
}

pub fn with_base<'a, F, B>(
base: B,
output: PrintOutput<'a>,
resolver_url: Option<&str>,
root_path: Option<PathBuf>,
package_name: Option<String>,
) -> DefaultForeignCallLayers<'a, B, F>
where
F: AcirField + Serialize + for<'de> Deserialize<'de> + 'a,
B: ForeignCallExecutor<F> + 'a,
{
// Adding them in the opposite order, so print is the outermost layer.
base.add_layer(resolver_url.map(|resolver_url| {
let id = rand::thread_rng().gen();
RPCForeignCallExecutor::new(resolver_url, id, root_path, package_name)
}))
.add_layer(MockForeignCallExecutor::default())
.add_layer(PrintForeignCallExecutor::new(output))
}
}

/// Facilitate static typing of layers on a base layer, so inner layers can be accessed.
pub type DefaultForeignCallLayers<'a, B, F> = Layer<
PrintForeignCallExecutor<'a>,
Layer<MockForeignCallExecutor<F>, Layer<Option<RPCForeignCallExecutor>, B, F>, F>,
F,
>;
3 changes: 1 addition & 2 deletions tooling/nargo/src/foreign_calls/mocker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use acvm::{
AcirField,
};
use noirc_printable_type::{decode_string_value, ForeignCallError};
use serde::{Deserialize, Serialize};

use super::{ForeignCall, ForeignCallExecutor};

Expand Down Expand Up @@ -82,7 +81,7 @@ impl<F: AcirField> MockForeignCallExecutor<F> {

impl<F> ForeignCallExecutor<F> for MockForeignCallExecutor<F>
where
F: AcirField + Serialize + for<'a> Deserialize<'a>,
F: AcirField,
{
fn execute(
&mut self,
Expand Down
66 changes: 10 additions & 56 deletions tooling/nargo/src/foreign_calls/mod.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
use std::path::PathBuf;

use acvm::{acir::brillig::ForeignCallResult, pwg::ForeignCallWaitInfo, AcirField};
use layers::{Layer, Layering};
use mocker::MockForeignCallExecutor;
use acvm::{acir::brillig::ForeignCallResult, pwg::ForeignCallWaitInfo};
use noirc_printable_type::ForeignCallError;
use print::{PrintForeignCallExecutor, PrintOutput};
use rand::Rng;
use rpc::RPCForeignCallExecutor;
use serde::{Deserialize, Serialize};

pub mod layers;
pub(crate) mod mocker;
pub(crate) mod print;
pub(crate) mod rpc;
pub mod mocker;
pub mod print;

#[cfg(feature = "rpc")]
pub mod default;
#[cfg(feature = "rpc")]
pub mod rpc;
#[cfg(feature = "rpc")]
pub use default::DefaultForeignCallExecutor;

pub trait ForeignCallExecutor<F> {
fn execute(
Expand Down Expand Up @@ -65,47 +63,3 @@ impl ForeignCall {
}
}
}

pub struct DefaultForeignCallExecutor;

impl DefaultForeignCallExecutor {
#[allow(clippy::new_ret_no_self)]
pub fn new<'a, F>(
output: PrintOutput<'a>,
resolver_url: Option<&str>,
root_path: Option<PathBuf>,
package_name: Option<String>,
) -> impl ForeignCallExecutor<F> + 'a
where
F: AcirField + Serialize + for<'de> Deserialize<'de> + 'a,
{
Self::with_base(layers::Empty, output, resolver_url, root_path, package_name)
}

pub fn with_base<'a, F, B>(
base: B,
output: PrintOutput<'a>,
resolver_url: Option<&str>,
root_path: Option<PathBuf>,
package_name: Option<String>,
) -> DefaultForeignCallLayers<'a, B, F>
where
F: AcirField + Serialize + for<'de> Deserialize<'de> + 'a,
B: ForeignCallExecutor<F> + 'a,
{
// Adding them in the opposite order, so print is the outermost layer.
base.add_layer(resolver_url.map(|resolver_url| {
let id = rand::thread_rng().gen();
RPCForeignCallExecutor::new(resolver_url, id, root_path, package_name)
}))
.add_layer(MockForeignCallExecutor::default())
.add_layer(PrintForeignCallExecutor::new(output))
}
}

/// Facilitate static typing of layers on a base layer, so inner layers can be accessed.
pub type DefaultForeignCallLayers<'a, B, F> = Layer<
PrintForeignCallExecutor<'a>,
Layer<MockForeignCallExecutor<F>, Layer<Option<RPCForeignCallExecutor>, B, F>, F>,
F,
>;
5 changes: 1 addition & 4 deletions tooling/nargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
pub mod constants;
pub mod errors;
pub mod foreign_calls;
pub mod ops;
pub mod package;
pub mod workspace;

#[cfg(feature = "execute")]
pub mod foreign_calls;

pub use self::errors::NargoError;
#[cfg(feature = "execute")]
pub use self::foreign_calls::print::PrintOutput;

use std::{
Expand Down
9 changes: 2 additions & 7 deletions tooling/nargo/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ pub use self::compile::{
pub use self::optimize::{optimize_contract, optimize_program};
pub use self::transform::{transform_contract, transform_program};

#[cfg(feature = "execute")]
pub use self::execute::{execute_program, execute_program_with_profiling};
#[cfg(feature = "test")]
pub use self::test::{run_test, TestStatus};

mod check;
mod compile;
mod optimize;
mod transform;

#[cfg(feature = "execute")]
mod execute;
#[cfg(feature = "test")]
mod optimize;
mod test;
mod transform;
3 changes: 1 addition & 2 deletions tooling/nargo/src/ops/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use noirc_driver::{compile_no_check, CompileError, CompileOptions, DEFAULT_EXPRE
use noirc_errors::{debug_info::DebugInfo, FileDiagnostic};
use noirc_frontend::hir::{def_map::TestFunction, Context};
use noirc_printable_type::ForeignCallError;
use serde::{Deserialize, Serialize};

use crate::{
errors::try_to_diagnose_runtime_error,
Expand Down Expand Up @@ -287,7 +286,7 @@ impl<E> TestForeignCallExecutor<E> {

impl<E, F> ForeignCallExecutor<F> for TestForeignCallExecutor<E>
where
F: AcirField + Serialize + for<'b> Deserialize<'b>,
F: AcirField,
E: ForeignCallExecutor<F>,
{
fn execute(
Expand Down

0 comments on commit f8936ca

Please sign in to comment.