diff --git a/Dockerfile b/Dockerfile index 54364cdba0..f43213d0e0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -155,6 +155,7 @@ COPY arbitrator/bench/Cargo.toml arbitrator/bench/ COPY arbitrator/jit/Cargo.toml arbitrator/jit/ COPY arbitrator/stylus/Cargo.toml arbitrator/stylus/ COPY arbitrator/tools/wasmer arbitrator/tools/wasmer +COPY arbitrator/wasm-libraries/forward arbitrator/wasm-libraries/forward COPY arbitrator/wasm-libraries/user-host-trait/Cargo.toml arbitrator/wasm-libraries/user-host-trait/Cargo.toml RUN bash -c 'mkdir arbitrator/{prover,jit,stylus}/src arbitrator/wasm-libraries/user-host-trait/src' RUN echo "fn test() {}" > arbitrator/jit/src/lib.rs && \ diff --git a/arbitrator/Cargo.lock b/arbitrator/Cargo.lock index d850235d2b..62772da1df 100644 --- a/arbitrator/Cargo.lock +++ b/arbitrator/Cargo.lock @@ -944,6 +944,14 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "forward" +version = "0.1.0" +dependencies = [ + "eyre", + "structopt", +] + [[package]] name = "funty" version = "2.0.0" @@ -1705,6 +1713,7 @@ dependencies = [ "enum-iterator 2.1.0", "eyre", "fnv", + "forward", "hex", "itertools", "lazy_static", diff --git a/arbitrator/prover/.gitignore b/arbitrator/prover/.gitignore new file mode 100644 index 0000000000..7201e9ec31 --- /dev/null +++ b/arbitrator/prover/.gitignore @@ -0,0 +1 @@ +src/forward_stub.wat \ No newline at end of file diff --git a/arbitrator/prover/Cargo.toml b/arbitrator/prover/Cargo.toml index a5b7b7e6de..a818ab156d 100644 --- a/arbitrator/prover/Cargo.toml +++ b/arbitrator/prover/Cargo.toml @@ -43,6 +43,9 @@ lru = "0.12.3" once_cell = "1.19.0" enum-iterator = "2.0.1" +[build-dependencies] +forward = { path = "../wasm-libraries/forward" } + [dev-dependencies] criterion = { version = "0.5.0", features = ["html_reports"] } rand = "0.8.4" diff --git a/arbitrator/prover/build.rs b/arbitrator/prover/build.rs new file mode 100644 index 0000000000..7cb25bea7f --- /dev/null +++ b/arbitrator/prover/build.rs @@ -0,0 +1,10 @@ +use std::fs::File; +use std::io::Write; + +fn main() { + println!("cargo:rerun-if-changed=../wasm-libraries/forward"); + let mut out = Vec::new(); + forward::forward_stub(&mut out).expect("Failed to write stub"); + let mut file = File::create("src/forward_stub.wat").unwrap(); + file.write_all(&out).unwrap(); +} diff --git a/arbitrator/prover/src/machine.rs b/arbitrator/prover/src/machine.rs index 3697c36d67..1e0a6764d9 100644 --- a/arbitrator/prover/src/machine.rs +++ b/arbitrator/prover/src/machine.rs @@ -334,8 +334,10 @@ lazy_static! { static ref USER_IMPORTS: HashMap = { let mut imports = HashMap::default(); - let forward = include_bytes!("../../../target/machines/latest/forward_stub.wasm"); - let forward = binary::parse(forward, Path::new("forward")).unwrap(); + let forward = include_bytes!("forward_stub.wat"); + let forward = wat::parse_bytes(forward).expect("failed to parse forward_stub.wat"); + let forward = binary::parse(&forward, Path::new("forward")) + .expect("failed to parse forward_stub.wasm"); for (name, &(export, kind)) in &forward.exports { if kind == ExportKind::Func { diff --git a/arbitrator/prover/src/test.rs b/arbitrator/prover/src/test.rs index b22ea0258b..90ed960f44 100644 --- a/arbitrator/prover/src/test.rs +++ b/arbitrator/prover/src/test.rs @@ -55,7 +55,7 @@ pub fn reject_ambiguous_imports() { #[test] pub fn test_compress() -> Result<()> { - let data = include_bytes!("../../../target/machines/latest/forward_stub.wasm"); + let data = include_bytes!("forward_stub.wat"); let mut last = vec![]; for dict in [Dictionary::Empty, Dictionary::StylusProgram] { diff --git a/arbitrator/wasm-libraries/Cargo.lock b/arbitrator/wasm-libraries/Cargo.lock index a5a066e5c9..fa6b2f1d9e 100644 --- a/arbitrator/wasm-libraries/Cargo.lock +++ b/arbitrator/wasm-libraries/Cargo.lock @@ -968,6 +968,7 @@ dependencies = [ "enum-iterator 2.1.0", "eyre", "fnv", + "forward", "hex", "itertools", "lazy_static", diff --git a/arbitrator/wasm-libraries/forward/src/bin/mod.rs b/arbitrator/wasm-libraries/forward/src/bin/mod.rs new file mode 100644 index 0000000000..dd82883da7 --- /dev/null +++ b/arbitrator/wasm-libraries/forward/src/bin/mod.rs @@ -0,0 +1,30 @@ +// Copyright 2022-2024, Offchain Labs, Inc. +// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md + +use eyre::Result; +use forward::{forward, forward_stub}; +use std::{fs::File, path::PathBuf}; +use structopt::StructOpt; + +#[derive(StructOpt)] +#[structopt(name = "arbitrator-prover")] +struct Opts { + #[structopt(long)] + path: PathBuf, + #[structopt(long)] + stub: bool, +} + +fn main() -> Result<()> { + let opts = Opts::from_args(); + let file = &mut File::options() + .create(true) + .write(true) + .truncate(true) + .open(opts.path)?; + + match opts.stub { + true => forward_stub(file), + false => forward(file), + } +} diff --git a/arbitrator/wasm-libraries/forward/src/main.rs b/arbitrator/wasm-libraries/forward/src/lib.rs similarity index 89% rename from arbitrator/wasm-libraries/forward/src/main.rs rename to arbitrator/wasm-libraries/forward/src/lib.rs index 4ee18b0b91..0c6e28aa52 100644 --- a/arbitrator/wasm-libraries/forward/src/main.rs +++ b/arbitrator/wasm-libraries/forward/src/lib.rs @@ -1,9 +1,9 @@ // Copyright 2022-2024, Offchain Labs, Inc. // For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE.md +use std::io::Write; + use eyre::Result; -use std::{fs::File, io::Write, path::PathBuf}; -use structopt::StructOpt; /// order matters! const HOSTIOS: [[&str; 3]; 42] = [ @@ -51,30 +51,7 @@ const HOSTIOS: [[&str; 3]; 42] = [ ["pay_for_memory_grow", "i32", ""], ]; -#[derive(StructOpt)] -#[structopt(name = "arbitrator-prover")] -struct Opts { - #[structopt(long)] - path: PathBuf, - #[structopt(long)] - stub: bool, -} - -fn main() -> Result<()> { - let opts = Opts::from_args(); - let file = &mut File::options() - .create(true) - .write(true) - .truncate(true) - .open(opts.path)?; - - match opts.stub { - true => forward_stub(file), - false => forward(file), - } -} - -fn forward(file: &mut File) -> Result<()> { +pub fn forward(file: &mut W) -> Result<()> { macro_rules! wln { ($($text:tt)*) => { writeln!(file, $($text)*)?; @@ -149,7 +126,7 @@ fn forward(file: &mut File) -> Result<()> { Ok(()) } -fn forward_stub(file: &mut File) -> Result<()> { +pub fn forward_stub(file: &mut W) -> Result<()> { macro_rules! wln { ($($text:tt)*) => { writeln!(file, $($text)*)?;