From b29d8cefea3420d6d35f819ccb11eaa0b47da695 Mon Sep 17 00:00:00 2001 From: Eric Tu Date: Fri, 13 Sep 2024 12:39:21 -0400 Subject: [PATCH] can spawn_blocking and then spawn a rayon task in a fork join scope inside it --- Cargo.lock | 116 ++++++++++++++++++++++++++++++++++++++----------- Cargo.toml | 5 ++- tests/tests.rs | 33 ++++++++++---- 3 files changed, 118 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 118fee4..2c19408 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -951,9 +951,9 @@ checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "js-sys" -version = "0.3.69" +version = "0.3.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" dependencies = [ "wasm-bindgen", ] @@ -1092,6 +1092,16 @@ dependencies = [ "nonempty", ] +[[package]] +name = "minicov" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c71e683cd655513b99affab7d317deb690528255a0d5f717f1024093c12b169" +dependencies = [ + "cc", + "walkdir", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1464,9 +1474,9 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.36" +version = "1.0.37" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" dependencies = [ "proc-macro2", ] @@ -1689,6 +1699,15 @@ dependencies = [ "untrusted", ] +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + [[package]] name = "sapling-crypto" version = "0.2.0" @@ -1851,9 +1870,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.72" +version = "2.0.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc4b9b9bf2add8093d3f2c0204471e951b2285580335de42f9d2534f3ae7a8af" +checksum = "9f35bcdf61fd8e7be6caf75f429fdca8beb3ed76584befb503b1569faee373ed" dependencies = [ "proc-macro2", "quote", @@ -2006,6 +2025,30 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio_with_wasm" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "302f7684e5f0b3c60bf229658f4e2f35e1dad50fa2425043997acdc1e87445aa" +dependencies = [ + "js-sys", + "tokio", + "tokio_with_wasm_proc", + "wasm-bindgen", + "wasm-bindgen-futures", + "web-sys", +] + +[[package]] +name = "tokio_with_wasm_proc" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1fef2e92c3cba6bbf5b7ee3528302c01e21bd7378795bf7cc7eff17da9ecd70" +dependencies = [ + "quote", + "syn", +] + [[package]] name = "tonic" version = "0.12.2" @@ -2318,6 +2361,16 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a7b6d5a78adc3e8f198e9cd730f219a695431467f7ec29dcfc63ade885feebe1" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + [[package]] name = "want" version = "0.3.1" @@ -2335,19 +2388,20 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" dependencies = [ "cfg-if", + "once_cell", "wasm-bindgen-macro", ] [[package]] name = "wasm-bindgen-backend" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" dependencies = [ "bumpalo", "log", @@ -2360,9 +2414,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.42" +version = "0.4.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0" +checksum = "61e9300f63a621e96ed275155c108eb6f843b6a26d053f122ab69724559dc8ed" dependencies = [ "cfg-if", "js-sys", @@ -2372,9 +2426,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2382,9 +2436,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ "proc-macro2", "quote", @@ -2396,8 +2450,7 @@ dependencies = [ [[package]] name = "wasm-bindgen-rayon" version = "1.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c9e02b7785fe15de188657b7d3a234ef042bfd8da10822016915e06d4e29cba7" +source = "git+https://github.com/9SMTM6/wasm-bindgen-rayon?rev=d1816e5bc2bf928ff5442355c04500a381d66a41#d1816e5bc2bf928ff5442355c04500a381d66a41" dependencies = [ "crossbeam-channel", "js-sys", @@ -2407,18 +2460,19 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.92" +version = "0.2.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" [[package]] name = "wasm-bindgen-test" -version = "0.3.42" +version = "0.3.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9bf62a58e0780af3e852044583deee40983e5886da43a271dd772379987667b" +checksum = "68497a05fb21143a08a7d24fc81763384a3072ee43c44e86aad1744d6adef9d9" dependencies = [ "console_error_panic_hook", "js-sys", + "minicov", "scoped-tls", "wasm-bindgen", "wasm-bindgen-futures", @@ -2427,9 +2481,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-test-macro" -version = "0.3.42" +version = "0.3.43" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b7f89739351a2e03cb94beb799d47fb2cac01759b40ec441f7de39b00cbf7ef0" +checksum = "4b8220be1fa9e4c889b30fd207d4906657e7e90b12e0e6b0c8b8d8709f5de021" dependencies = [ "proc-macro2", "quote", @@ -2462,9 +2516,9 @@ dependencies = [ [[package]] name = "web-sys" -version = "0.3.69" +version = "0.3.70" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef" +checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0" dependencies = [ "js-sys", "wasm-bindgen", @@ -2498,6 +2552,7 @@ dependencies = [ "sha2", "thiserror", "tokio", + "tokio_with_wasm", "tonic", "tonic-web-wasm-client", "tracing", @@ -2544,6 +2599,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys 0.59.0", +] + [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index aa787a1..2d22dd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -45,6 +45,7 @@ wasm-bindgen-rayon = { version = "1.2.1", optional = true } tracing-web = { version = "0.1.3", optional = true } console_error_panic_hook = { version = "0.1.7", optional = true } tonic-web-wasm-client = "0.6.0" +tokio_with_wasm = { version = "0.7.1", features = ["rt", "rt-multi-thread", "sync", "macros"] } ## Zcash dependencies zcash_keys = { git = "https://github.com/ChainSafe/librustzcash", rev = "a77e8a0204dab421fdbf5822e585716339567b96", features = ["transparent-inputs", "orchard", "sapling", "unstable"] } @@ -76,9 +77,11 @@ tracing = "0.1.40" rayon = "1.8" [dev-dependencies] -wasm-bindgen-test = "0.3.42" +wasm-bindgen-test = "0.3.43" # Used in Native tests tokio = { version = "1.0", features = ["rt", "macros"] } [patch.crates-io] zip32 = { git = "https://github.com/zcash/zip32.git", branch = "diversifier_index_ord"} +# See: https://github.com/RReverser/wasm-bindgen-rayon/pull/12 +wasm-bindgen-rayon = { git = "https://github.com/9SMTM6/wasm-bindgen-rayon", rev = "d1816e5bc2bf928ff5442355c04500a381d66a41" } \ No newline at end of file diff --git a/tests/tests.rs b/tests/tests.rs index e9c1022..0e027d3 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -5,24 +5,28 @@ use zcash_address::ZcashAddress; use zcash_primitives::consensus::Network; wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); - +macro_rules! console_log { + ($($t:tt)*) => (web_sys::console::log_1(&format!($($t)*).into())) +} const SEED: &str = "visit armed kite pen cradle toward reward clay marble oil write dove blind oyster silk oyster original message skate bench tone enable stadium element"; const HD_INDEX: u32 = 0; const BIRTHDAY: Option = Some(2577329); const THREADS: usize = 5; +use tokio_with_wasm::alias as tokio; +static INIT: Once = Once::new(); // Required to initialize the logger and panic hooks only once use std::{num::NonZeroU32, sync::Once}; -static INIT: Once = Once::new(); pub fn initialize() { + console_log!("Calling initialize"); INIT.call_once(|| { webz_core::init::start(); }); } #[cfg(all(feature = "wasm-parallel"))] async fn init_threadpool(threads: usize) -> wasm_bindgen_futures::JsFuture { - tracing::info!("Initializing thread pool with {} threads", threads); + console_log!("Initializing thread pool with {} threads", threads); wasm_bindgen_futures::JsFuture::from(wasm_bindgen_rayon::init_thread_pool(threads)) } @@ -31,10 +35,7 @@ async fn test_get_and_scan_range() { initialize(); #[cfg(all(feature = "wasm-parallel"))] - let _ = init_threadpool(THREADS).await; - - let num_parallel = rayon::current_num_threads(); - tracing::info!("WASM rayon has {} threads", num_parallel); + init_threadpool(THREADS).await; let mut w = WebWallet::new("test", "https://zcash-testnet.chainsafe.dev", 1).unwrap(); @@ -54,19 +55,33 @@ async fn test_get_and_scan_range() { tracing::info!("Wallet summary: {:?}", summary); tracing::info!("Proposing a transaction"); + let handle = tokio::task::spawn_blocking(|| { + rayon::scope(|s| { + s.spawn(|_| { + let num_parallel = rayon::current_num_threads(); + tracing::info!("WASM rayon has {} threads", num_parallel); + }) + }); + }); w.transfer(SEED, 0, "utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu".to_string(), 1000).await.unwrap(); tracing::info!("Transaction proposed"); let summary = w.get_wallet_summary().unwrap(); tracing::info!("Wallet summary: {:?}", summary); + + handle.await.unwrap(); } #[cfg(feature = "native")] #[tokio::test] async fn test_get_and_scan_range_native() { initialize(); - let num_parallel = rayon::current_num_threads(); - tracing::info!("Native rayon has {} threads", num_parallel); + + rayon::spawn(|| { + let num_parallel = rayon::current_num_threads(); + tracing::info!("Native rayon has {} threads", num_parallel); + }); + let url = "https://testnet.zec.rocks:443"; let c = tonic::transport::Channel::from_shared(url).unwrap();