Skip to content

Commit

Permalink
spawn task in sync2 instead and remove spawn from simple-sync-and-sen…
Browse files Browse the repository at this point in the history
…d test
  • Loading branch information
ec2 committed Sep 24, 2024
1 parent f9c0203 commit ed0987f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
18 changes: 14 additions & 4 deletions src/bindgen/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use wasm_bindgen::prelude::*;

use tonic_web_wasm_client::Client;

use crate::error::Error;
use crate::{BlockRange, MemoryWallet, Wallet, PRUNING_DEPTH};
use wasm_thread as thread;
use zcash_address::ZcashAddress;
use zcash_client_backend::proto::service::compact_tx_streamer_client::CompactTxStreamerClient;
use zcash_client_memory::MemoryWalletDb;
use zcash_keys::keys::UnifiedFullViewingKey;
use zcash_primitives::consensus::{self, BlockHeight};

use crate::error::Error;
use crate::{BlockRange, MemoryWallet, Wallet, PRUNING_DEPTH};

/// # A Zcash wallet
///
/// A wallet is a set of accounts that can be synchronized together with the blockchain.
Expand Down Expand Up @@ -133,7 +133,17 @@ impl WebWallet {

/// Synchronize the wallet with the blockchain up to the tip using zcash_client_backend's algo
pub async fn sync2(&self) -> Result<(), Error> {
self.inner.sync2().await
let db = self.inner.clone();
let main_handler = thread::Builder::new()
.spawn_async(|| async {
assert!(thread::is_web_worker_thread());
let db = db;
db.sync2().await.unwrap();
})
.unwrap()
.join_async();
main_handler.await.unwrap();
Ok(())
}

pub async fn get_wallet_summary(&self) -> Result<Option<WalletSummary>, Error> {
Expand Down
60 changes: 27 additions & 33 deletions tests/simple-sync-and-send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,37 +31,31 @@ async fn test_get_and_scan_range() {
let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap();
tracing::info!("Created account with id: {}", id);
let w_clone = w.clone();
let main_handler = thread::Builder::new().spawn_async(move || async {
let w = w_clone;
assert!(thread::is_web_worker_thread());


#[cfg(not(feature = "sync2"))]
{
w.sync(&js_sys::Function::new_with_args(
"scanned_to, tip",
"console.log('Scanned: ', scanned_to, '/', tip)",
))
.await
.unwrap();
}
#[cfg(feature = "sync2")]
{
tracing::info!("Syncing wallet with sync2");
w.sync2().await.unwrap();
}
tracing::info!("Syncing complete :)");

let summary = w.get_wallet_summary().await.unwrap();
tracing::info!("Wallet summary: {:?}", summary);

tracing::info!("Proposing a transaction");
w.transfer(SEED, 0, "utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu".to_string(), 1000).await.unwrap();
tracing::info!("Transaction proposed");

let summary = w.get_wallet_summary().await.unwrap();
tracing::info!("Wallet summary: {:?}", summary);
}).unwrap().join_async();

main_handler.await.unwrap_throw();
let w = w_clone;

#[cfg(not(feature = "sync2"))]
{
w.sync(&js_sys::Function::new_with_args(
"scanned_to, tip",
"console.log('Scanned: ', scanned_to, '/', tip)",
))
.await
.unwrap();
}
#[cfg(feature = "sync2")]
{
tracing::info!("Syncing wallet with sync2");
w.sync2().await.unwrap();
}
tracing::info!("Syncing complete :)");

let summary = w.get_wallet_summary().await.unwrap();
tracing::info!("Wallet summary: {:?}", summary);

tracing::info!("Proposing a transaction");
w.transfer(SEED, 0, "utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu".to_string(), 1000).await.unwrap();
tracing::info!("Transaction proposed");

let summary = w.get_wallet_summary().await.unwrap();
tracing::info!("Wallet summary: {:?}", summary);
}

0 comments on commit ed0987f

Please sign in to comment.