Skip to content

Commit

Permalink
can spawn_blocking and then spawn a rayon task in a fork join scope i…
Browse files Browse the repository at this point in the history
…nside it
  • Loading branch information
ec2 committed Sep 13, 2024
1 parent cc15183 commit b29d8ce
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 36 deletions.
116 changes: 90 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down Expand Up @@ -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" }
33 changes: 24 additions & 9 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = 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))
}

Expand All @@ -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();

Expand All @@ -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();

Expand Down

0 comments on commit b29d8ce

Please sign in to comment.