Skip to content

Run ledger tests in wasm #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
925 changes: 483 additions & 442 deletions .github/workflows/ci.yaml

Large diffs are not rendered by default.

156 changes: 105 additions & 51 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion ledger/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ serde_with = "3.6.1"
anyhow = "1.0.75"
thiserror = "1.0.60"
fraction = { version = "=0.15.1", default-features = false, features = ["with-serde-support"] }
zip = { version = "2.2", default-features = false }

[target.'cfg(target_family = "wasm")'.dependencies]
getrandom = { version = "0.2", features = ["js"] }
Expand All @@ -93,7 +94,8 @@ rand_seeder = "0.2"
tuple-map = "0.4.0"

[target.'cfg(target_family = "wasm")'.dev-dependencies]
wasm-bindgen-test = "0.3.0"
wasm-bindgen = "=0.2.92"
wasm-bindgen-test = "0.3"
web-sys = { version = "0.3", features = ["Blob", "DedicatedWorkerGlobalScope", "MessageEvent", "Url", "Worker", "WorkerType", "WorkerOptions", "console", "Window", "Performance" ] }

[features]
Expand Down
3 changes: 1 addition & 2 deletions ledger/src/account/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1759,9 +1759,8 @@ mod tests {
#[cfg(not(target_family = "wasm"))]
const SIZE: usize = 280;

// FIXME: was 2496bytes before zkapp got boxed, what should be the size now?
#[cfg(target_family = "wasm")]
const SIZE: usize = 280;
const SIZE: usize = 264;

assert_eq!(std::mem::size_of::<Account>(), SIZE);
}
Expand Down
145 changes: 87 additions & 58 deletions ledger/src/proofs/circuit_blobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,67 +52,96 @@
format!("{RELEASES_PATH}/{filename_str}")
}

#[cfg(not(target_family = "wasm"))]
pub fn fetch(filename: &impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
use std::path::PathBuf;
// #[cfg(not(target_family = "wasm"))]
// pub async fn fetch(filename: &impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
// use std::path::PathBuf;

// fn try_base_dir<P: Into<PathBuf>>(base_dir: P, filename: &impl AsRef<Path>) -> Option<PathBuf> {
// let mut path = base_dir.into();
// path.push(filename);
// path.exists().then_some(path)
// }

// fn to_io_err(err: impl std::fmt::Display) -> std::io::Error {
// std::io::Error::new(
// std::io::ErrorKind::Other,
// format!(
// "failed to find circuit-blobs locally and to fetch the from github! error: {err}"
// ),
// )
// }

// let home_base_dir = home_base_dir();
// let found = None
// .or_else(|| {
// try_base_dir(
// std::env::var("OPENMINA_CIRCUIT_BLOBS_BASE_DIR").ok()?,
// filename,
// )
// })
// .or_else(|| try_base_dir(env!("CARGO_MANIFEST_DIR").to_string(), filename))
// .or_else(|| try_base_dir(home_base_dir.clone()?, filename))
// .or_else(|| try_base_dir("/usr/local/lib/openmina/circuit-blobs", filename));

// if let Some(path) = found {
// return std::fs::read(path);
// }

// eprintln!(
// "circuit-blobs '{}' not found locally, so fetching it...",
// filename.as_ref().to_str().unwrap()
// );
// let base_dir = home_base_dir.expect("$HOME env not set!");

// let bytes = reqwest::blocking::get(git_release_url(filename))
// .map_err(to_io_err)?
// .bytes()
// .map_err(to_io_err)?
// .to_vec();

// // cache it to home dir.
// let cache_path = base_dir.join(filename);
// eprintln!("caching circuit-blobs to {}", cache_path.to_str().unwrap());
// let _ = std::fs::create_dir_all(cache_path.parent().unwrap());
// let _ = std::fs::write(cache_path, &bytes);

// Ok(bytes)
// }

// #[cfg(not(test))]
// #[cfg(target_family = "wasm")]
// pub async fn fetch(filename: &impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
// let prefix =
// option_env!("CIRCUIT_BLOBS_HTTP_PREFIX").unwrap_or("/assets/webnode/circuit-blobs");
// let url = format!("{prefix}/{}", filename.as_ref().to_str().unwrap());
// http::get_bytes(&url).await
// // http::get_bytes(&git_release_url(filename)).await
// }

// #[cfg(test)]
// #[cfg(target_family = "wasm")]
pub const CIRCUIT_BLOBS: &[u8] = include_bytes!("../../circuit-blobs.zip");

Check failure on line 123 in ledger/src/proofs/circuit_blobs.rs

View workflow job for this annotation

GitHub Actions / Lint

couldn't read `ledger/src/proofs/../../circuit-blobs.zip`: No such file or directory (os error 2)

// #[cfg(test)]
// #[cfg(target_family = "wasm")]
pub async fn fetch(filename: &impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
use std::io::Read;

fn try_base_dir<P: Into<PathBuf>>(base_dir: P, filename: &impl AsRef<Path>) -> Option<PathBuf> {
let mut path = base_dir.into();
path.push(filename);
path.exists().then_some(path)
}
const MAX_SIZE: usize = 32 * 1024 * 1024;
let mut file = std::io::Cursor::new(CIRCUIT_BLOBS);
let mut zip = zip::ZipArchive::new(&mut file).unwrap();

fn to_io_err(err: impl std::fmt::Display) -> std::io::Error {
std::io::Error::new(
std::io::ErrorKind::Other,
format!(
"failed to find circuit-blobs locally and to fetch the from github! error: {err}"
),
)
}
let filename = filename.as_ref().to_str().unwrap();
eprintln!("fetching {:?}", filename);

let home_base_dir = home_base_dir();
let found = None
.or_else(|| {
try_base_dir(
std::env::var("OPENMINA_CIRCUIT_BLOBS_BASE_DIR").ok()?,
filename,
)
})
.or_else(|| try_base_dir(env!("CARGO_MANIFEST_DIR").to_string(), filename))
.or_else(|| try_base_dir(home_base_dir.clone()?, filename))
.or_else(|| try_base_dir("/usr/local/lib/openmina/circuit-blobs", filename));

if let Some(path) = found {
return std::fs::read(path);
for i in 0..zip.len() {
let file = zip.by_index(i).unwrap();
if file.name().ends_with(filename) {
let mut buffer = Vec::with_capacity(MAX_SIZE);
file.take(MAX_SIZE as _).read_to_end(&mut buffer).unwrap();
return Ok(buffer);
}
}

eprintln!(
"circuit-blobs '{}' not found locally, so fetching it...",
filename.as_ref().to_str().unwrap()
);
let base_dir = home_base_dir.expect("$HOME env not set!");

let bytes = reqwest::blocking::get(git_release_url(filename))
.map_err(to_io_err)?
.bytes()
.map_err(to_io_err)?
.to_vec();

// cache it to home dir.
let cache_path = base_dir.join(filename);
eprintln!("caching circuit-blobs to {}", cache_path.to_str().unwrap());
let _ = std::fs::create_dir_all(cache_path.parent().unwrap());
let _ = std::fs::write(cache_path, &bytes);

Ok(bytes)
}

#[cfg(target_family = "wasm")]
pub async fn fetch(filename: &impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
let prefix =
option_env!("CIRCUIT_BLOBS_HTTP_PREFIX").unwrap_or("/assets/webnode/circuit-blobs");
let url = format!("{prefix}/{}", filename.as_ref().to_str().unwrap());
http::get_bytes(&url).await
// http::get_bytes(&git_release_url(filename)).await
panic!("{} not found in zip", filename);
}
Loading
Loading