Skip to content

Commit

Permalink
Merge pull request #23 from ChainSafe/ec2/mutex-wallet
Browse files Browse the repository at this point in the history
wrap wallet in a mutex
  • Loading branch information
ec2 authored Sep 23, 2024
2 parents d153322 + 5db9273 commit 0a8b70a
Show file tree
Hide file tree
Showing 8 changed files with 530 additions and 154 deletions.
479 changes: 394 additions & 85 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 12 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ multicore = ["zcash_proofs/multicore", "zcash_primitives/multicore", "zcash_clie
# WASM specific features
wasm = ["console_error_panic_hook", "dep:tracing-web", "zcash_client_backend/wasm-bindgen"]
wasm-parallel = ["wasm", "wasm-bindgen-rayon", "multicore"]
native = ["dep:tokio", "tonic/channel", "tonic/gzip", "tonic/tls-webpki-roots"]
native = ["tonic/channel", "tonic/gzip", "tonic/tls-webpki-roots", "tokio/macros", "tokio/rt", "tokio/rt-multi-thread"]
sqlite-db = ["dep:zcash_client_sqlite"]
console_error_panic_hook = ["dep:console_error_panic_hook"]
no-bundler = ["wasm-bindgen-rayon?/no-bundler"]
Expand All @@ -63,12 +63,12 @@ tokio_with_wasm = { version = "0.7.1", features = ["rt", "rt-multi-thread", "syn

## Zcash dependencies

zcash_keys = { git = "https://github.com/ChainSafe/librustzcash", rev = "0fdd2fbb992a6f84eba45f488ee74a75d08d449b", features = ["transparent-inputs", "orchard", "sapling", "unstable"] }
zcash_client_backend = { git = "https://github.com/ChainSafe/librustzcash", rev = "0fdd2fbb992a6f84eba45f488ee74a75d08d449b", default-features = false, features = ["sync", "lightwalletd-tonic", "wasm-bindgen", "orchard"] }
zcash_client_memory = { git = "https://github.com/ChainSafe/librustzcash", rev = "0fdd2fbb992a6f84eba45f488ee74a75d08d449b", features = ["orchard"] }
zcash_primitives = { git = "https://github.com/ChainSafe/librustzcash", rev = "0fdd2fbb992a6f84eba45f488ee74a75d08d449b" }
zcash_address = { git = "https://github.com/ChainSafe/librustzcash", rev = "0fdd2fbb992a6f84eba45f488ee74a75d08d449b" }
zcash_proofs = { git = "https://github.com/ChainSafe/librustzcash", rev = "0fdd2fbb992a6f84eba45f488ee74a75d08d449b", default-features = false, features = ["bundled-prover"] }
zcash_keys = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4", features = ["transparent-inputs", "orchard", "sapling", "unstable"] }
zcash_client_backend = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4", default-features = false, features = ["sync", "lightwalletd-tonic", "wasm-bindgen", "orchard"] }
zcash_client_memory = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4", features = ["orchard"] }
zcash_primitives = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4" }
zcash_address = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4" }
zcash_proofs = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4", default-features = false, features = ["bundled-prover"] }

## gRPC Web dependencies
prost = { version = "0.12", default-features = false }
Expand All @@ -78,8 +78,8 @@ tonic = { version = "0.12", default-features = false, features = [


# Used in Native tests
tokio = { version = "1.0", features = ["rt", "macros", "rt-multi-thread"], optional = true }
zcash_client_sqlite = { git = "https://github.com/ChainSafe/librustzcash", rev = "0fdd2fbb992a6f84eba45f488ee74a75d08d449b", default-features = false, features = ["unstable", "orchard"], optional = true }
tokio = { version = "1.0" }
zcash_client_sqlite = { git = "https://github.com/ChainSafe/librustzcash", rev = "b6d32dd9a57165fb1508e9c1c8ab1a3aba09c7f4", default-features = false, features = ["unstable", "orchard"], optional = true }

getrandom = { version = "0.2", features = ["js"] }
thiserror = "1.0.63"
Expand All @@ -99,12 +99,15 @@ wasm_thread = { git = "https://github.com/WilsonGramer/wasm_thread.git", rev = "

wasm_sync = "0.1.2"
http = { version = "1.1.0", default-features = false }
serde = { version = "1", features = ["derive"], default-features = false }
postcard = { version = "1.0.10", features = ["alloc"] }

[dev-dependencies]
wasm-bindgen-test = "0.3.43"
tempfile = "3.12"
# Used in Native tests
tokio = { version = "1.0", features = ["rt", "macros"] }
byte-unit = { version = "5.1.4", features = ["byte"] }


[patch.crates-io]
Expand Down
18 changes: 16 additions & 2 deletions examples/message-board-sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async fn main() {
.unwrap();

let ufvk = UnifiedFullViewingKey::from_sapling_extended_full_viewing_key(s).unwrap();
let id = w.import_ufvk(&ufvk, Some(2477329)).await.unwrap();
let id = w.import_ufvk(&ufvk, Some(2277329)).await.unwrap();
tracing::info!("Created account with id: {}", id);

#[cfg(not(feature = "sync2"))]
Expand All @@ -80,6 +80,20 @@ async fn main() {

tracing::info!("Syncing complete :)");

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

#[cfg(not(feature = "sqlite-db"))]
{
tracing::info!("Serializing wallet");
let serialized_wallet = w.to_vec_postcard().await;
let byte_count = byte_unit::Byte::from_u64(serialized_wallet.len() as u64);

tracing::info!(
"Wallet serialized: {}",
byte_count
.get_adjusted_unit(byte_unit::Unit::MB)
.to_string()
)
}
}
18 changes: 16 additions & 2 deletions examples/simple-sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async fn main() {

tracing::info!("Syncing complete :)");

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

tracing::info!("Proposing a transaction");
Expand All @@ -93,6 +93,20 @@ async fn main() {
w.transfer(SEED, 0, addr.unwrap(), 1000).await.unwrap();
tracing::info!("Transaction proposed");

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

#[cfg(not(feature = "sqlite-db"))]
{
tracing::info!("Serializing wallet");
let serialized_wallet = w.to_vec_postcard().await;
let byte_count = byte_unit::Byte::from_u64(serialized_wallet.len() as u64);

tracing::info!(
"Wallet serialized: {}",
byte_count
.get_adjusted_unit(byte_unit::Unit::MB)
.to_string()
)
}
}
12 changes: 6 additions & 6 deletions src/bindgen/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ impl WebWallet {
}
}

pub fn client(&mut self) -> &mut CompactTxStreamerClient<tonic_web_wasm_client::Client> {
self.inner.client()
pub fn client(&self) -> CompactTxStreamerClient<tonic_web_wasm_client::Client> {
self.inner.client.clone()
}

pub fn inner_mut(&mut self) -> &mut MemoryWallet<tonic_web_wasm_client::Client> {
Expand Down Expand Up @@ -109,8 +109,8 @@ impl WebWallet {
self.inner.import_ufvk(&ufvk, birthday_height).await
}

pub fn suggest_scan_ranges(&self) -> Result<Vec<BlockRange>, Error> {
self.inner.suggest_scan_ranges()
pub async fn suggest_scan_ranges(&self) -> Result<Vec<BlockRange>, Error> {
self.inner.suggest_scan_ranges().await
}

/// Synchronize the wallet with the blockchain up to the tip
Expand All @@ -135,8 +135,8 @@ impl WebWallet {
self.inner.sync2().await
}

pub fn get_wallet_summary(&self) -> Result<Option<WalletSummary>, Error> {
Ok(self.inner.get_wallet_summary()?.map(Into::into))
pub async fn get_wallet_summary(&self) -> Result<Option<WalletSummary>, Error> {
Ok(self.inner.get_wallet_summary().await?.map(Into::into))
}

///
Expand Down
Loading

0 comments on commit 0a8b70a

Please sign in to comment.