From eda1f88fabbbb6e3330537be148943cfede23272 Mon Sep 17 00:00:00 2001 From: Eric Tu Date: Tue, 17 Sep 2024 14:14:43 -0400 Subject: [PATCH 1/4] use their s\ync algo --- Cargo.lock | 256 ++++++++++++++++++++++++++++++++++++---- Cargo.toml | 19 ++- examples/batchrunner.rs | 120 +++++++++++++++++++ justfile | 10 +- src/bindgen/wallet.rs | 9 ++ src/wallet.rs | 8 ++ tests/batchrunner.rs | 54 +++++++++ 7 files changed, 452 insertions(+), 24 deletions(-) create mode 100644 examples/batchrunner.rs create mode 100644 tests/batchrunner.rs diff --git a/Cargo.lock b/Cargo.lock index ae47bf6..cdfe4b5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -83,6 +83,21 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + [[package]] name = "anyhow" version = "1.0.86" @@ -349,6 +364,19 @@ dependencies = [ "zeroize", ] +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "serde", + "windows-targets", +] + [[package]] name = "cipher" version = "0.4.4" @@ -376,6 +404,12 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2" +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + [[package]] name = "cpufeatures" version = "0.2.12" @@ -453,6 +487,41 @@ dependencies = [ "petgraph", ] +[[package]] +name = "darling" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f63b86c8a8826a49b8c21f08a2d07338eec8d900540f8630dc76284be802989" +dependencies = [ + "darling_core", + "darling_macro", +] + +[[package]] +name = "darling_core" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95133861a8032aaea082871032f5815eb9e98cef03fa916ab4500513994df9e5" +dependencies = [ + "fnv", + "ident_case", + "proc-macro2", + "quote", + "strsim", + "syn", +] + +[[package]] +name = "darling_macro" +version = "0.20.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" +dependencies = [ + "darling_core", + "quote", + "syn", +] + [[package]] name = "delegate-display" version = "2.1.1" @@ -472,6 +541,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4" dependencies = [ "powerfmt", + "serde", ] [[package]] @@ -503,7 +573,6 @@ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "equihash" version = "0.2.0" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" dependencies = [ "blake2b_simd", "byteorder", @@ -528,7 +597,6 @@ dependencies = [ [[package]] name = "f4jumble" version = "0.1.0" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" dependencies = [ "blake2b_simd", ] @@ -931,11 +999,39 @@ dependencies = [ "tracing", ] +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "ident_case" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39" + [[package]] name = "incrementalmerkletree" version = "0.6.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75346da3bd8e3d8891d02508245ed2df34447ca6637e343829f8d08986e9cde2" +source = "git+https://github.com/ec2/incrementalmerkletree.git?rev=16eff253ad2575d48feec04f7387e6507a7dd698#16eff253ad2575d48feec04f7387e6507a7dd698" dependencies = [ "either", ] @@ -965,6 +1061,7 @@ checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", "hashbrown 0.12.3", + "serde", ] [[package]] @@ -975,6 +1072,7 @@ checksum = "de3fc2e30ba82dd1b3911c8de1ffc143c74a914a14e99514d7637e3099df5ea0" dependencies = [ "equivalent", "hashbrown 0.14.5", + "serde", ] [[package]] @@ -1068,6 +1166,16 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4ce301924b7887e9d637144fdade93f9dfff9b60981d4ac161db09720d39aa5" +[[package]] +name = "lock_api" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07af8b9cdd281b7915f413fa73f29ebd5d55d0d3f0155584dade1ff18cea1b17" +dependencies = [ + "autocfg", + "scopeguard", +] + [[package]] name = "log" version = "0.4.22" @@ -1334,6 +1442,29 @@ dependencies = [ "group", ] +[[package]] +name = "parking_lot" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bf18183cf54e8d6059647fc3063646a1801cf30896933ec2311622cc4b9a27" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e401f977ab385c9e4e3ab30627d6f26d00e2c73eef317493c4ec6d468726cf8" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-targets", +] + [[package]] name = "password-hash" version = "0.5.0" @@ -1627,6 +1758,15 @@ dependencies = [ "zeroize", ] +[[package]] +name = "redox_syscall" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" +dependencies = [ + "bitflags", +] + [[package]] name = "regex" version = "1.10.6" @@ -1771,6 +1911,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "ryu" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" + [[package]] name = "sapling-crypto" version = "0.2.0" @@ -1832,6 +1978,12 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + [[package]] name = "secp256k1" version = "0.27.0" @@ -1879,6 +2031,48 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_json" +version = "1.0.128" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" +dependencies = [ + "itoa", + "memchr", + "ryu", + "serde", +] + +[[package]] +name = "serde_with" +version = "3.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cecfa94848272156ea67b2b1a53f20fc7bc638c4a46d2f8abde08f05f4b857" +dependencies = [ + "base64 0.22.1", + "chrono", + "hex", + "indexmap 1.9.3", + "indexmap 2.3.0", + "serde", + "serde_derive", + "serde_json", + "serde_with_macros", + "time", +] + +[[package]] +name = "serde_with_macros" +version = "3.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8fee4991ef4f274617a51ad4af30519438dacb2f56ac773b08a1922ff743350" +dependencies = [ + "darling", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "sha2" version = "0.10.8" @@ -1902,8 +2096,7 @@ dependencies = [ [[package]] name = "shardtree" version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78222845cd8bbe5eb95687407648ff17693a35de5e8abaa39a4681fb21e033f9" +source = "git+https://github.com/ec2/incrementalmerkletree.git?rev=16eff253ad2575d48feec04f7387e6507a7dd698#16eff253ad2575d48feec04f7387e6507a7dd698" dependencies = [ "bitflags", "either", @@ -1948,6 +2141,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f" +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "subtle" version = "2.6.1" @@ -2651,6 +2850,15 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -2745,20 +2953,19 @@ dependencies = [ [[package]] name = "zcash_address" version = "0.5.0" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" dependencies = [ "bech32", "bs58", "f4jumble", - "zcash_encoding", + "zcash_encoding 0.2.1", "zcash_protocol", ] [[package]] name = "zcash_client_backend" version = "0.13.0" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" dependencies = [ + "async-trait", "base64 0.21.7", "bech32", "bls12_381", @@ -2766,6 +2973,7 @@ dependencies = [ "byteorder", "crossbeam-channel", "document-features", + "futures-util", "group", "hex", "hyper-util", @@ -2789,7 +2997,7 @@ dependencies = [ "tracing", "which", "zcash_address", - "zcash_encoding", + "zcash_encoding 0.2.1", "zcash_keys", "zcash_note_encryption", "zcash_primitives", @@ -2801,27 +3009,30 @@ dependencies = [ [[package]] name = "zcash_client_memory" version = "0.1.0" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" dependencies = [ "bs58", "byteorder", "group", "incrementalmerkletree", "jubjub", - "maybe-rayon", "nonempty", "orchard", + "parking_lot", "prost 0.13.2", + "rayon", "sapling-crypto", "secrecy", + "serde", + "serde_with", "shardtree", "static_assertions", "subtle", "thiserror", + "tokio", "tracing", "zcash_address", "zcash_client_backend", - "zcash_encoding", + "zcash_encoding 0.2.1", "zcash_keys", "zcash_primitives", "zcash_protocol", @@ -2857,13 +3068,21 @@ dependencies = [ "uuid", "zcash_address", "zcash_client_backend", - "zcash_encoding", + "zcash_encoding 0.2.1 (git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b)", "zcash_keys", "zcash_primitives", "zcash_protocol", "zip32", ] +[[package]] +name = "zcash_encoding" +version = "0.2.1" +dependencies = [ + "byteorder", + "nonempty", +] + [[package]] name = "zcash_encoding" version = "0.2.1" @@ -2876,7 +3095,6 @@ dependencies = [ [[package]] name = "zcash_keys" version = "0.3.0" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" dependencies = [ "bech32", "bip32", @@ -2895,7 +3113,7 @@ dependencies = [ "subtle", "tracing", "zcash_address", - "zcash_encoding", + "zcash_encoding 0.2.1", "zcash_primitives", "zcash_protocol", "zip32", @@ -2917,7 +3135,6 @@ dependencies = [ [[package]] name = "zcash_primitives" version = "0.17.0" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" dependencies = [ "aes", "bip32", @@ -2945,7 +3162,7 @@ dependencies = [ "subtle", "tracing", "zcash_address", - "zcash_encoding", + "zcash_encoding 0.2.1", "zcash_note_encryption", "zcash_protocol", "zcash_spec", @@ -2955,7 +3172,6 @@ dependencies = [ [[package]] name = "zcash_proofs" version = "0.17.0" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" dependencies = [ "bellman", "blake2b_simd", @@ -2975,7 +3191,6 @@ dependencies = [ [[package]] name = "zcash_protocol" version = "0.3.0" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" dependencies = [ "document-features", "memuse", @@ -3044,7 +3259,6 @@ dependencies = [ [[package]] name = "zip321" version = "0.1.0" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" dependencies = [ "base64 0.21.7", "nom", diff --git a/Cargo.toml b/Cargo.toml index f031c5e..bab9418 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,6 +18,10 @@ path = "examples/simple-sync.rs" name = "message-board-sync" path = "examples/message-board-sync.rs" +[[example]] +name = "batchrunner" +path = "examples/batchrunner.rs" + [profile.release] # Tell `rustc` to optimize for small code size. opt-level = 3 @@ -49,7 +53,7 @@ tonic-web-wasm-client = "0.6.0" ## Zcash dependencies zcash_keys = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b", features = ["transparent-inputs", "orchard", "sapling", "unstable"] } -zcash_client_backend = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b", default-features = false, features = ["lightwalletd-tonic", "wasm-bindgen"] } +zcash_client_backend = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b", default-features = false, features = ["sync", "lightwalletd-tonic", "wasm-bindgen"] } zcash_client_memory = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b", features = ["orchard"] } zcash_primitives = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" } zcash_address = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" } @@ -85,3 +89,16 @@ tempfile = "3.12" [patch.crates-io] zip32 = { git = "https://github.com/zcash/zip32.git", branch = "diversifier_index_ord" } +# TODO: Remove these once the PRs are merged +shardtree = { git = "https://github.com/ec2/incrementalmerkletree.git", rev = "16eff253ad2575d48feec04f7387e6507a7dd698" } +incrementalmerkletree = { git = "https://github.com/ec2/incrementalmerkletree.git", rev = "16eff253ad2575d48feec04f7387e6507a7dd698" } + +[patch.'https://github.com/chainsafe/librustzcash'] +zcash_address = { path = "../librustzcash/components/zcash_address" } +zcash_client_backend = { path = "../librustzcash/zcash_client_backend" } +#zcash_client_sqlite = { path = "../librustzcash/zcash_client_sqlite" } +zcash_client_memory = { path = "../librustzcash/zcash_client_memory" } +zcash_keys = { path = "../librustzcash/zcash_keys" } +zcash_primitives = { path = "../librustzcash/zcash_primitives" } +zcash_proofs = { path = "../librustzcash/zcash_proofs" } +zcash_protocol = { path = "../librustzcash/components/zcash_protocol" } \ No newline at end of file diff --git a/examples/batchrunner.rs b/examples/batchrunner.rs new file mode 100644 index 0000000..31091ba --- /dev/null +++ b/examples/batchrunner.rs @@ -0,0 +1,120 @@ +use std::collections::BTreeMap; +use std::sync::Once; + +use std::num::NonZeroU32; +use webz_core::Wallet; +use zcash_address::ZcashAddress; +use zcash_primitives::consensus::Network; + +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); + +static INIT: Once = Once::new(); +pub fn initialize() { + INIT.call_once(|| { + webz_core::init::start(); + }); +} + +use std::sync::{Arc, Mutex}; +use zcash_client_backend::data_api::{ + chain::{error, BlockCache, BlockSource}, + scanning::{ScanPriority, ScanRange}, +}; +use zcash_client_backend::proto::compact_formats::CompactBlock; +use zcash_client_backend::proto::service::compact_tx_streamer_client::CompactTxStreamerClient; +use zcash_client_backend::sync::run; +use zcash_client_memory::MemBlockCache; +use zcash_keys::keys::UnifiedFullViewingKey; +use zcash_primitives::consensus::BlockHeight; +use zcash_primitives::constants; + +const SAPLING_EFVK: &str = "zxviews1q0duytgcqqqqpqre26wkl45gvwwwd706xw608hucmvfalr759ejwf7qshjf5r9aa7323zulvz6plhttp5mltqcgs9t039cx2d09mgq05ts63n8u35hyv6h9nc9ctqqtue2u7cer2mqegunuulq2luhq3ywjcz35yyljewa4mgkgjzyfwh6fr6jd0dzd44ghk0nxdv2hnv4j5nxfwv24rwdmgllhe0p8568sgqt9ckt02v2kxf5ahtql6s0ltjpkckw8gtymxtxuu9gcr0swvz"; + +#[cfg(feature = "native")] +#[tokio::main] +async fn main() { + let db_cache = tempfile::tempdir().unwrap(); + let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap(); + + initialize(); + let url = "https://zec.rocks:443"; + let c = tonic::transport::Channel::from_shared(url).unwrap(); + + let tls = tonic::transport::ClientTlsConfig::new() + .domain_name("zec.rocks") + .with_webpki_roots(); + let channel = c.tls_config(tls).unwrap(); + let client = channel.connect().await.unwrap(); + #[cfg(feature = "sqlite-db")] + let wallet_db = { + use zcash_client_sqlite::{ + chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb, + }; + + let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap(); + let mut wallet_db = WalletDb::for_path(&_db_data, Network::MainNetwork).unwrap(); + init_blockmeta_db(&mut db_cache).unwrap(); + init_wallet_db(&mut wallet_db, None).unwrap(); + wallet_db + }; + + #[cfg(not(feature = "sqlite-db"))] + let wallet_db = + zcash_client_memory::MemoryWalletDb::new(Network::TestNetwork, webz_core::PRUNING_DEPTH); + + let mut w = Wallet::new( + wallet_db, + client.clone(), + Network::MainNetwork, + NonZeroU32::try_from(1).unwrap(), + ) + .unwrap(); + let s = zcash_keys::encoding::decode_extended_full_viewing_key( + constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, + SAPLING_EFVK.trim(), + ) + .unwrap(); + + let ufvk = UnifiedFullViewingKey::from_sapling_extended_full_viewing_key(s).unwrap(); + let id = w.import_ufvk(&ufvk, Some(2477329)).await.unwrap(); + tracing::info!("Created account with id: {}", id); + + + // let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap(); + // tracing::info!("Created account with id: {}", id); + let db_cache = MemBlockCache::new(); + let mut cpt_client = CompactTxStreamerClient::new(client.clone()); + + run( + &mut cpt_client, + &Network::TestNetwork, + &db_cache, + w.inner_mut(), + 10000, + ) + .await + .unwrap(); + + // tracing::info!("Syncing wallet"); + // w.sync(|scanned_to, tip| { + // println!("Scanned: {}/{}", scanned_to, tip); + // }) + // .await + // .unwrap(); + // + // tracing::info!("Syncing complete :)"); + // + // let summary = w.get_wallet_summary().unwrap(); + // tracing::info!("Wallet summary: {:?}", summary); + // + // tracing::info!("Proposing a transaction"); + // let addr = ZcashAddress::try_from_encoded("utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu"); + // + // w.transfer(SEED, 0, addr.unwrap(), 1000).await.unwrap(); + // tracing::info!("Transaction proposed"); + + let summary = w.get_wallet_summary().unwrap(); + tracing::info!("Wallet summary: {:?}", summary); +} diff --git a/justfile b/justfile index d97a854..85b5bfd 100644 --- a/justfile +++ b/justfile @@ -7,10 +7,10 @@ build: ## Wasm Tests test-web: WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" - + test-message-board-web: WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" --test message-board-sync - + test-simple-web: WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" --test simple-sync-and-send @@ -28,5 +28,11 @@ example-message-board: example-message-board-sqlite: cargo run -r --example message-board-sync --features="sqlite-db" +example-batchrunner: + RUST_LOG=info,zcash_client_backend::sync cargo run -r --example batchrunner + +example-batchrunner-sqlite: + RUST_LOG=info,zcash_client_backend::sync cargo run -r --features="sqlite-db" --example batchrunner + check: cargo check diff --git a/src/bindgen/wallet.rs b/src/bindgen/wallet.rs index 542f48c..7bdb9b9 100644 --- a/src/bindgen/wallet.rs +++ b/src/bindgen/wallet.rs @@ -6,6 +6,7 @@ use wasm_bindgen::prelude::*; use tonic_web_wasm_client::Client; 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}; @@ -45,6 +46,14 @@ impl WebWallet { _ => Err(Error::InvalidNetwork(network.to_string())), } } + + pub fn client(&mut self) -> &mut CompactTxStreamerClient { + self.inner.client() + } + + pub fn inner_mut(&mut self) -> &mut MemoryWallet { + &mut self.inner + } } #[wasm_bindgen] diff --git a/src/wallet.rs b/src/wallet.rs index 1df6572..d916355 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -106,6 +106,14 @@ where }) } + pub fn inner_mut(&mut self) -> &mut W { + &mut self.db + } + + pub fn client(&mut self) -> &mut CompactTxStreamerClient { + &mut self.client + } + /// Add a new account to the wallet /// /// # Arguments diff --git a/tests/batchrunner.rs b/tests/batchrunner.rs new file mode 100644 index 0000000..415612a --- /dev/null +++ b/tests/batchrunner.rs @@ -0,0 +1,54 @@ +use wasm_bindgen_test::*; +wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); + +use std::sync::Once; +use zcash_client_backend::proto::service::compact_tx_streamer_client::CompactTxStreamerClient; +use zcash_client_backend::sync::run; +use zcash_client_memory::MemBlockCache; +use zcash_primitives::consensus::Network; +use webz_core::bindgen::wallet::WebWallet; + +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); + +// Required to initialize the logger and panic hooks only once +static INIT: Once = Once::new(); +pub fn initialize() { + INIT.call_once(|| { + webz_core::init::start(); + }); +} + +#[wasm_bindgen_test] +async fn test_get_and_scan_range() { + initialize(); + + let mut w = WebWallet::new("test", "https://zcash-testnet.chainsafe.dev", 1).unwrap(); + + let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap(); + tracing::info!("Created account with id: {}", id); + + let db_cache = MemBlockCache::new(); + let mut cpt_client = w.client().clone(); + + run( + &mut cpt_client, + &Network::TestNetwork, + &db_cache, + w.inner_mut().inner_mut(), + 10000, + ) + .await + .unwrap(); + + let summary = w.get_wallet_summary().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().unwrap(); + tracing::info!("Wallet summary: {:?}", summary); +} From c1fc1774a5852541a260a257916582bcf4a8b17b Mon Sep 17 00:00:00 2001 From: Eric Tu Date: Tue, 17 Sep 2024 14:23:14 -0400 Subject: [PATCH 2/4] web is failing because its acquireing lock on mainthread --- justfile | 4 ++++ tests/batchrunner.rs | 2 +- tests/message-board-sync.rs | 27 +++++++++++++++++++++------ 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/justfile b/justfile index 85b5bfd..80c74db 100644 --- a/justfile +++ b/justfile @@ -14,6 +14,10 @@ test-message-board-web: test-simple-web: WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" --test simple-sync-and-send +test-batchrunner-web: + WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" --test batchrunner + + ## Native Examples example-simple: diff --git a/tests/batchrunner.rs b/tests/batchrunner.rs index 415612a..550acdd 100644 --- a/tests/batchrunner.rs +++ b/tests/batchrunner.rs @@ -21,7 +21,7 @@ pub fn initialize() { } #[wasm_bindgen_test] -async fn test_get_and_scan_range() { +async fn batchrunner() { initialize(); let mut w = WebWallet::new("test", "https://zcash-testnet.chainsafe.dev", 1).unwrap(); diff --git a/tests/message-board-sync.rs b/tests/message-board-sync.rs index 41ffac2..cc98509 100644 --- a/tests/message-board-sync.rs +++ b/tests/message-board-sync.rs @@ -2,6 +2,8 @@ use wasm_bindgen_test::*; wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); use std::sync::Once; +use zcash_client_backend::sync::run; +use zcash_client_memory::MemBlockCache; use webz_core::bindgen::wallet::WebWallet; use zcash_keys::keys::UnifiedFullViewingKey; use zcash_primitives::consensus::Network; @@ -33,14 +35,27 @@ async fn test_message_board() { let id = w.import_ufvk(&ufvk_str, Some(2477329)).await.unwrap(); tracing::info!("Created account with id: {}", id); - tracing::info!("Syncing wallet"); - w.sync(&js_sys::Function::new_with_args( - "scanned_to, tip", - "console.log('Scanned: ', scanned_to, '/', tip)", - )) + // tracing::info!("Syncing wallet"); + // w.sync(&js_sys::Function::new_with_args( + // "scanned_to, tip", + // "console.log('Scanned: ', scanned_to, '/', tip)", + // )) + // .await + // .unwrap(); + // tracing::info!("Syncing complete :)"); + + let db_cache = MemBlockCache::new(); + let mut cpt_client = w.client().clone(); + + run( + &mut cpt_client, + &Network::TestNetwork, + &db_cache, + w.inner_mut().inner_mut(), + 10000, + ) .await .unwrap(); - tracing::info!("Syncing complete :)"); let summary = w.get_wallet_summary().unwrap(); tracing::info!("Wallet summary: {:?}", summary); From 137f38c5fcb56bd6492ba69977989cf36d9be20a Mon Sep 17 00:00:00 2001 From: Eric Tu Date: Tue, 17 Sep 2024 15:55:43 -0400 Subject: [PATCH 3/4] refactor sync --- Cargo.lock | 34 +++++----- Cargo.toml | 37 +++++----- examples/batchrunner.rs | 120 --------------------------------- examples/message-board-sync.rs | 20 ++++-- examples/simple-sync.rs | 20 ++++-- justfile | 43 +++++------- src/bindgen/wallet.rs | 7 +- src/error.rs | 18 +++++ src/wallet.rs | 28 ++++++-- tests/batchrunner.rs | 54 --------------- tests/message-board-sync.rs | 38 +++++------ tests/simple-sync-and-send.rs | 21 ++++-- 12 files changed, 157 insertions(+), 283 deletions(-) delete mode 100644 examples/batchrunner.rs delete mode 100644 tests/batchrunner.rs diff --git a/Cargo.lock b/Cargo.lock index cdfe4b5..1f4099d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -573,6 +573,7 @@ checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" [[package]] name = "equihash" version = "0.2.0" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "blake2b_simd", "byteorder", @@ -597,6 +598,7 @@ dependencies = [ [[package]] name = "f4jumble" version = "0.1.0" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "blake2b_simd", ] @@ -2953,17 +2955,19 @@ dependencies = [ [[package]] name = "zcash_address" version = "0.5.0" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "bech32", "bs58", "f4jumble", - "zcash_encoding 0.2.1", + "zcash_encoding", "zcash_protocol", ] [[package]] name = "zcash_client_backend" version = "0.13.0" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "async-trait", "base64 0.21.7", @@ -2997,7 +3001,7 @@ dependencies = [ "tracing", "which", "zcash_address", - "zcash_encoding 0.2.1", + "zcash_encoding", "zcash_keys", "zcash_note_encryption", "zcash_primitives", @@ -3009,6 +3013,7 @@ dependencies = [ [[package]] name = "zcash_client_memory" version = "0.1.0" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "bs58", "byteorder", @@ -3032,7 +3037,7 @@ dependencies = [ "tracing", "zcash_address", "zcash_client_backend", - "zcash_encoding 0.2.1", + "zcash_encoding", "zcash_keys", "zcash_primitives", "zcash_protocol", @@ -3042,7 +3047,7 @@ dependencies = [ [[package]] name = "zcash_client_sqlite" version = "0.11.2" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "bs58", "byteorder", @@ -3068,7 +3073,7 @@ dependencies = [ "uuid", "zcash_address", "zcash_client_backend", - "zcash_encoding 0.2.1 (git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b)", + "zcash_encoding", "zcash_keys", "zcash_primitives", "zcash_protocol", @@ -3078,15 +3083,7 @@ dependencies = [ [[package]] name = "zcash_encoding" version = "0.2.1" -dependencies = [ - "byteorder", - "nonempty", -] - -[[package]] -name = "zcash_encoding" -version = "0.2.1" -source = "git+https://github.com/ChainSafe/librustzcash?rev=c97a3f6e60446523fafa63cdf77b6c2584ac9f3b#c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "byteorder", "nonempty", @@ -3095,6 +3092,7 @@ dependencies = [ [[package]] name = "zcash_keys" version = "0.3.0" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "bech32", "bip32", @@ -3113,7 +3111,7 @@ dependencies = [ "subtle", "tracing", "zcash_address", - "zcash_encoding 0.2.1", + "zcash_encoding", "zcash_primitives", "zcash_protocol", "zip32", @@ -3135,6 +3133,7 @@ dependencies = [ [[package]] name = "zcash_primitives" version = "0.17.0" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "aes", "bip32", @@ -3162,7 +3161,7 @@ dependencies = [ "subtle", "tracing", "zcash_address", - "zcash_encoding 0.2.1", + "zcash_encoding", "zcash_note_encryption", "zcash_protocol", "zcash_spec", @@ -3172,6 +3171,7 @@ dependencies = [ [[package]] name = "zcash_proofs" version = "0.17.0" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "bellman", "blake2b_simd", @@ -3191,6 +3191,7 @@ dependencies = [ [[package]] name = "zcash_protocol" version = "0.3.0" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "document-features", "memuse", @@ -3259,6 +3260,7 @@ dependencies = [ [[package]] name = "zip321" version = "0.1.0" +source = "git+https://github.com/ChainSafe/librustzcash?rev=0fdd2fbb992a6f84eba45f488ee74a75d08d449b#0fdd2fbb992a6f84eba45f488ee74a75d08d449b" dependencies = [ "base64 0.21.7", "nom", diff --git a/Cargo.toml b/Cargo.toml index bab9418..1ba3a2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,9 +18,6 @@ path = "examples/simple-sync.rs" name = "message-board-sync" path = "examples/message-board-sync.rs" -[[example]] -name = "batchrunner" -path = "examples/batchrunner.rs" [profile.release] # Tell `rustc` to optimize for small code size. @@ -39,6 +36,8 @@ native = ["dep:tokio", "tonic/channel", "tonic/gzip", "tonic/tls-webpki-roots"] sqlite-db = ["dep:zcash_client_sqlite"] console_error_panic_hook = ["dep:console_error_panic_hook"] +sync2 = [] + [dependencies] ## Web dependencies wasm-bindgen = "0.2.84" @@ -52,12 +51,12 @@ console_error_panic_hook = { version = "0.1.7", optional = true } tonic-web-wasm-client = "0.6.0" ## Zcash dependencies -zcash_keys = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b", features = ["transparent-inputs", "orchard", "sapling", "unstable"] } -zcash_client_backend = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b", default-features = false, features = ["sync", "lightwalletd-tonic", "wasm-bindgen"] } -zcash_client_memory = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b", features = ["orchard"] } -zcash_primitives = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" } -zcash_address = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b" } -zcash_proofs = { git = "https://github.com/ChainSafe/librustzcash", rev = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b", default-features = false, features = ["bundled-prover"] } +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"] } +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"] } ## gRPC Web dependencies prost = { version = "0.12", default-features = false } @@ -67,7 +66,7 @@ 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 = "c97a3f6e60446523fafa63cdf77b6c2584ac9f3b", default-features = false, features = ["unstable", "orchard"], optional = true } +zcash_client_sqlite = { git = "https://github.com/ChainSafe/librustzcash", rev = "0fdd2fbb992a6f84eba45f488ee74a75d08d449b", default-features = false, features = ["unstable", "orchard"], optional = true } getrandom = { version = "0.2", features = ["js"] } thiserror = "1.0.63" @@ -93,12 +92,12 @@ zip32 = { git = "https://github.com/zcash/zip32.git", branch = "diversifier_inde shardtree = { git = "https://github.com/ec2/incrementalmerkletree.git", rev = "16eff253ad2575d48feec04f7387e6507a7dd698" } incrementalmerkletree = { git = "https://github.com/ec2/incrementalmerkletree.git", rev = "16eff253ad2575d48feec04f7387e6507a7dd698" } -[patch.'https://github.com/chainsafe/librustzcash'] -zcash_address = { path = "../librustzcash/components/zcash_address" } -zcash_client_backend = { path = "../librustzcash/zcash_client_backend" } -#zcash_client_sqlite = { path = "../librustzcash/zcash_client_sqlite" } -zcash_client_memory = { path = "../librustzcash/zcash_client_memory" } -zcash_keys = { path = "../librustzcash/zcash_keys" } -zcash_primitives = { path = "../librustzcash/zcash_primitives" } -zcash_proofs = { path = "../librustzcash/zcash_proofs" } -zcash_protocol = { path = "../librustzcash/components/zcash_protocol" } \ No newline at end of file +#[patch.'https://github.com/chainsafe/librustzcash'] +#zcash_address = { path = "../librustzcash/components/zcash_address" } +#zcash_client_backend = { path = "../librustzcash/zcash_client_backend" } +##zcash_client_sqlite = { path = "../librustzcash/zcash_client_sqlite" } +#zcash_client_memory = { path = "../librustzcash/zcash_client_memory" } +#zcash_keys = { path = "../librustzcash/zcash_keys" } +#zcash_primitives = { path = "../librustzcash/zcash_primitives" } +#zcash_proofs = { path = "../librustzcash/zcash_proofs" } +#zcash_protocol = { path = "../librustzcash/components/zcash_protocol" } \ No newline at end of file diff --git a/examples/batchrunner.rs b/examples/batchrunner.rs deleted file mode 100644 index 31091ba..0000000 --- a/examples/batchrunner.rs +++ /dev/null @@ -1,120 +0,0 @@ -use std::collections::BTreeMap; -use std::sync::Once; - -use std::num::NonZeroU32; -use webz_core::Wallet; -use zcash_address::ZcashAddress; -use zcash_primitives::consensus::Network; - -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); - -static INIT: Once = Once::new(); -pub fn initialize() { - INIT.call_once(|| { - webz_core::init::start(); - }); -} - -use std::sync::{Arc, Mutex}; -use zcash_client_backend::data_api::{ - chain::{error, BlockCache, BlockSource}, - scanning::{ScanPriority, ScanRange}, -}; -use zcash_client_backend::proto::compact_formats::CompactBlock; -use zcash_client_backend::proto::service::compact_tx_streamer_client::CompactTxStreamerClient; -use zcash_client_backend::sync::run; -use zcash_client_memory::MemBlockCache; -use zcash_keys::keys::UnifiedFullViewingKey; -use zcash_primitives::consensus::BlockHeight; -use zcash_primitives::constants; - -const SAPLING_EFVK: &str = "zxviews1q0duytgcqqqqpqre26wkl45gvwwwd706xw608hucmvfalr759ejwf7qshjf5r9aa7323zulvz6plhttp5mltqcgs9t039cx2d09mgq05ts63n8u35hyv6h9nc9ctqqtue2u7cer2mqegunuulq2luhq3ywjcz35yyljewa4mgkgjzyfwh6fr6jd0dzd44ghk0nxdv2hnv4j5nxfwv24rwdmgllhe0p8568sgqt9ckt02v2kxf5ahtql6s0ltjpkckw8gtymxtxuu9gcr0swvz"; - -#[cfg(feature = "native")] -#[tokio::main] -async fn main() { - let db_cache = tempfile::tempdir().unwrap(); - let _db_data = tempfile::NamedTempFile::new_in(db_cache.path()).unwrap(); - - initialize(); - let url = "https://zec.rocks:443"; - let c = tonic::transport::Channel::from_shared(url).unwrap(); - - let tls = tonic::transport::ClientTlsConfig::new() - .domain_name("zec.rocks") - .with_webpki_roots(); - let channel = c.tls_config(tls).unwrap(); - let client = channel.connect().await.unwrap(); - #[cfg(feature = "sqlite-db")] - let wallet_db = { - use zcash_client_sqlite::{ - chain::init::init_blockmeta_db, wallet::init::init_wallet_db, FsBlockDb, WalletDb, - }; - - let mut db_cache = FsBlockDb::for_path(&db_cache).unwrap(); - let mut wallet_db = WalletDb::for_path(&_db_data, Network::MainNetwork).unwrap(); - init_blockmeta_db(&mut db_cache).unwrap(); - init_wallet_db(&mut wallet_db, None).unwrap(); - wallet_db - }; - - #[cfg(not(feature = "sqlite-db"))] - let wallet_db = - zcash_client_memory::MemoryWalletDb::new(Network::TestNetwork, webz_core::PRUNING_DEPTH); - - let mut w = Wallet::new( - wallet_db, - client.clone(), - Network::MainNetwork, - NonZeroU32::try_from(1).unwrap(), - ) - .unwrap(); - let s = zcash_keys::encoding::decode_extended_full_viewing_key( - constants::mainnet::HRP_SAPLING_EXTENDED_FULL_VIEWING_KEY, - SAPLING_EFVK.trim(), - ) - .unwrap(); - - let ufvk = UnifiedFullViewingKey::from_sapling_extended_full_viewing_key(s).unwrap(); - let id = w.import_ufvk(&ufvk, Some(2477329)).await.unwrap(); - tracing::info!("Created account with id: {}", id); - - - // let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap(); - // tracing::info!("Created account with id: {}", id); - let db_cache = MemBlockCache::new(); - let mut cpt_client = CompactTxStreamerClient::new(client.clone()); - - run( - &mut cpt_client, - &Network::TestNetwork, - &db_cache, - w.inner_mut(), - 10000, - ) - .await - .unwrap(); - - // tracing::info!("Syncing wallet"); - // w.sync(|scanned_to, tip| { - // println!("Scanned: {}/{}", scanned_to, tip); - // }) - // .await - // .unwrap(); - // - // tracing::info!("Syncing complete :)"); - // - // let summary = w.get_wallet_summary().unwrap(); - // tracing::info!("Wallet summary: {:?}", summary); - // - // tracing::info!("Proposing a transaction"); - // let addr = ZcashAddress::try_from_encoded("utest1z00xn09t4eyeqw9zmjss75sf460423dymgyfjn8rtlj26cffy0yad3eea82xekk24s00wnm38cvyrm2c6x7fxlc0ns4a5j7utgl6lchvglfvl9g9p56fqwzvzvj9d3z6r6ft88j654d7dj0ep6myq5duz9s8x78fdzmtx04d2qn8ydkxr4lfdhlkx9ktrw98gd97dateegrr68vl8xu"); - // - // w.transfer(SEED, 0, addr.unwrap(), 1000).await.unwrap(); - // tracing::info!("Transaction proposed"); - - let summary = w.get_wallet_summary().unwrap(); - tracing::info!("Wallet summary: {:?}", summary); -} diff --git a/examples/message-board-sync.rs b/examples/message-board-sync.rs index dcb4496..70d8318 100644 --- a/examples/message-board-sync.rs +++ b/examples/message-board-sync.rs @@ -63,12 +63,20 @@ async fn main() { let id = w.import_ufvk(&ufvk, Some(2477329)).await.unwrap(); tracing::info!("Created account with id: {}", id); - tracing::info!("Syncing wallet"); - w.sync(|scanned_to, tip| { - println!("Scanned: {}/{}", scanned_to, tip); - }) - .await - .unwrap(); + #[cfg(not(feature = "sync2"))] + { + tracing::info!("Syncing wallet with our sync impl"); + w.sync(|scanned_to, tip| { + println!("Scanned: {}/{}", scanned_to, tip); + }) + .await + .unwrap(); + } + #[cfg(feature = "sync2")] + { + tracing::info!("Syncing wallet with sync2"); + w.sync2().await.unwrap(); + } tracing::info!("Syncing complete :)"); diff --git a/examples/simple-sync.rs b/examples/simple-sync.rs index fb7dcf1..fd2bc16 100644 --- a/examples/simple-sync.rs +++ b/examples/simple-sync.rs @@ -59,12 +59,20 @@ async fn main() { let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap(); tracing::info!("Created account with id: {}", id); - tracing::info!("Syncing wallet"); - w.sync(|scanned_to, tip| { - println!("Scanned: {}/{}", scanned_to, tip); - }) - .await - .unwrap(); + #[cfg(not(feature = "sync2"))] + { + tracing::info!("Syncing wallet with our sync impl"); + w.sync(|scanned_to, tip| { + println!("Scanned: {}/{}", scanned_to, tip); + }) + .await + .unwrap(); + } + #[cfg(feature = "sync2")] + { + tracing::info!("Syncing wallet with sync2"); + w.sync2().await.unwrap(); + } tracing::info!("Syncing complete :)"); diff --git a/justfile b/justfile index 80c74db..c835ac8 100644 --- a/justfile +++ b/justfile @@ -4,39 +4,26 @@ default: build: wasm-pack build -t web --release --out-dir ./packages/webz-core -Z --no-default-features --features="wasm" build-std="panic_abort,std" -## Wasm Tests -test-web: - WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" +# All Wasm Tests +test-web *features: + WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features "wasm {{features}}" -Z build-std="panic_abort,std" -test-message-board-web: - WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" --test message-board-sync +# sync message board in the web: addigional args: sync2 +test-message-board-web *features: + WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features "wasm {{features}}" -Z build-std="panic_abort,std" --test message-board-sync -test-simple-web: - WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" --test simple-sync-and-send +# simple example in the web: additional args: sync2 +test-simple-web *features: + WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features "wasm {{features}}" -Z build-std="panic_abort,std" --test simple-sync-and-send -test-batchrunner-web: - WASM_BINDGEN_TEST_TIMEOUT=99999 wasm-pack test --release --firefox --no-default-features --features="wasm" -Z build-std="panic_abort,std" --test batchrunner +# simple example: additional args: sync2, sqlite-db +example-simple *features: + RUST_LOG="info,zcash_client_backend::sync=debug" cargo run -r --example simple-sync --features "native {{features}}" - -## Native Examples -example-simple: - cargo run -r --example simple-sync - -example-simple-sqlite: - cargo run -r --example simple-sync --features="sqlite-db" - -example-message-board: - cargo run -r --example message-board-sync - -example-message-board-sqlite: - cargo run -r --example message-board-sync --features="sqlite-db" - -example-batchrunner: - RUST_LOG=info,zcash_client_backend::sync cargo run -r --example batchrunner - -example-batchrunner-sqlite: - RUST_LOG=info,zcash_client_backend::sync cargo run -r --features="sqlite-db" --example batchrunner +# sync the message board: additional args: sync2, sqlite-db +example-message-board *features: + RUST_LOG=info,zcash_client_backend::sync=debug cargo run -r --example message-board-sync --features "native {{features}}" check: cargo check diff --git a/src/bindgen/wallet.rs b/src/bindgen/wallet.rs index 7bdb9b9..1291c0b 100644 --- a/src/bindgen/wallet.rs +++ b/src/bindgen/wallet.rs @@ -50,7 +50,7 @@ impl WebWallet { pub fn client(&mut self) -> &mut CompactTxStreamerClient { self.inner.client() } - + pub fn inner_mut(&mut self) -> &mut MemoryWallet { &mut self.inner } @@ -130,6 +130,11 @@ impl WebWallet { Ok(()) } + /// Synchronize the wallet with the blockchain up to the tip using zcash_client_backend's algo + pub async fn sync2(&mut self) -> Result<(), Error> { + self.inner.sync2().await + } + pub fn get_wallet_summary(&self) -> Result, Error> { Ok(self.inner.get_wallet_summary()?.map(Into::into)) } diff --git a/src/error.rs b/src/error.rs index 0987210..27977ca 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,7 @@ // Copyright 2024 ChainSafe Systems // SPDX-License-Identifier: Apache-2.0, MIT +use std::fmt::Display; use wasm_bindgen::JsValue; #[derive(thiserror::Error, Debug)] @@ -47,6 +48,12 @@ pub enum Error { #[error("Failed to parse key: {0}")] KeyParseError(String), + // TODO: The error type from librustzcash backend is generic. Handle that later. + // Perhaps we make our error struct generic as well + // See: zcash_client_backend::sync::Error + #[error("Syncing Error: {0}")] + SyncError(String), + #[cfg(feature = "sqlite-db")] #[error("Sqlite error: {0}")] SqliteError(#[from] zcash_client_sqlite::error::SqliteClientError), @@ -79,3 +86,14 @@ impl From for Error { Self::ScanError(e) } } + +impl From> for Error +where + A: Display, + B: Display, + C: Display, +{ + fn from(e: zcash_client_backend::sync::Error) -> Self { + Self::SyncError(e.to_string()) + } +} diff --git a/src/wallet.rs b/src/wallet.rs index d916355..acaf396 100644 --- a/src/wallet.rs +++ b/src/wallet.rs @@ -31,7 +31,7 @@ use zcash_client_backend::scanning::{scan_block, Nullifiers, ScanningKeys}; use zcash_client_backend::wallet::OvkPolicy; use zcash_client_backend::zip321::{Payment, TransactionRequest}; use zcash_client_backend::ShieldedProtocol; -use zcash_client_memory::MemoryWalletDb; +use zcash_client_memory::{MemBlockCache, MemoryWalletDb}; use zcash_keys::keys::{UnifiedFullViewingKey, UnifiedSpendingKey}; use zcash_primitives::consensus::{self, BlockHeight, Network}; use zcash_primitives::transaction::components::amount::NonNegativeAmount; @@ -40,6 +40,7 @@ use zcash_primitives::transaction::TxId; use zcash_proofs::prover::LocalTxProver; use zcash_client_backend::proposal::Proposal; +use zcash_client_backend::sync::run; const BATCH_SIZE: u32 = 10000; @@ -85,8 +86,11 @@ where NoteRef: Copy + Eq + Ord + Debug, Error: From<::Error>, + ::Error: std::error::Error + Send + Sync + 'static, + ::Error: std::error::Error + Send + Sync + 'static, + // GRPC connection Trait Bounds - T: GrpcService, + T: GrpcService + Clone, T::Error: Into, T::ResponseBody: Body + std::marker::Send + 'static, ::Error: Into + std::marker::Send, @@ -106,10 +110,10 @@ where }) } - pub fn inner_mut(&mut self) -> &mut W { + pub fn db_mut(&mut self) -> &mut W { &mut self.db } - + pub fn client(&mut self) -> &mut CompactTxStreamerClient { &mut self.client } @@ -216,6 +220,22 @@ where })?) } + pub async fn sync2(&mut self) -> Result<(), Error> { + let mut client = self.client().clone(); + // TODO: This should be held in the Wallet struct so we can download in parallel + let db_cache = MemBlockCache::new(); + + run( + &mut client, + &self.network.clone(), + &db_cache, + self.db_mut(), + BATCH_SIZE, + ) + .await + .map_err(Into::into) + } + /// Synchronize the wallet with the blockchain up to the tip /// The passed callback will be called for every batch of blocks processed with the current progress pub async fn sync(&mut self, callback: impl Fn(BlockHeight, BlockHeight)) -> Result<(), Error> { diff --git a/tests/batchrunner.rs b/tests/batchrunner.rs deleted file mode 100644 index 550acdd..0000000 --- a/tests/batchrunner.rs +++ /dev/null @@ -1,54 +0,0 @@ -use wasm_bindgen_test::*; -wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); - -use std::sync::Once; -use zcash_client_backend::proto::service::compact_tx_streamer_client::CompactTxStreamerClient; -use zcash_client_backend::sync::run; -use zcash_client_memory::MemBlockCache; -use zcash_primitives::consensus::Network; -use webz_core::bindgen::wallet::WebWallet; - -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); - -// Required to initialize the logger and panic hooks only once -static INIT: Once = Once::new(); -pub fn initialize() { - INIT.call_once(|| { - webz_core::init::start(); - }); -} - -#[wasm_bindgen_test] -async fn batchrunner() { - initialize(); - - let mut w = WebWallet::new("test", "https://zcash-testnet.chainsafe.dev", 1).unwrap(); - - let id = w.create_account(SEED, HD_INDEX, BIRTHDAY).await.unwrap(); - tracing::info!("Created account with id: {}", id); - - let db_cache = MemBlockCache::new(); - let mut cpt_client = w.client().clone(); - - run( - &mut cpt_client, - &Network::TestNetwork, - &db_cache, - w.inner_mut().inner_mut(), - 10000, - ) - .await - .unwrap(); - - let summary = w.get_wallet_summary().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().unwrap(); - tracing::info!("Wallet summary: {:?}", summary); -} diff --git a/tests/message-board-sync.rs b/tests/message-board-sync.rs index cc98509..d222044 100644 --- a/tests/message-board-sync.rs +++ b/tests/message-board-sync.rs @@ -2,9 +2,9 @@ use wasm_bindgen_test::*; wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); use std::sync::Once; +use webz_core::bindgen::wallet::WebWallet; use zcash_client_backend::sync::run; use zcash_client_memory::MemBlockCache; -use webz_core::bindgen::wallet::WebWallet; use zcash_keys::keys::UnifiedFullViewingKey; use zcash_primitives::consensus::Network; use zcash_primitives::constants; @@ -35,27 +35,21 @@ async fn test_message_board() { let id = w.import_ufvk(&ufvk_str, Some(2477329)).await.unwrap(); tracing::info!("Created account with id: {}", id); - // tracing::info!("Syncing wallet"); - // w.sync(&js_sys::Function::new_with_args( - // "scanned_to, tip", - // "console.log('Scanned: ', scanned_to, '/', tip)", - // )) - // .await - // .unwrap(); - // tracing::info!("Syncing complete :)"); - - let db_cache = MemBlockCache::new(); - let mut cpt_client = w.client().clone(); - - run( - &mut cpt_client, - &Network::TestNetwork, - &db_cache, - w.inner_mut().inner_mut(), - 10000, - ) - .await - .unwrap(); + #[cfg(not(feature = "sync2"))] + { + tracing::info!("Syncing wallet with our sync impl"); + w.sync(|scanned_to, tip| { + println!("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().unwrap(); tracing::info!("Wallet summary: {:?}", summary); diff --git a/tests/simple-sync-and-send.rs b/tests/simple-sync-and-send.rs index 610cd67..4b421ba 100644 --- a/tests/simple-sync-and-send.rs +++ b/tests/simple-sync-and-send.rs @@ -25,13 +25,20 @@ 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); - tracing::info!("Syncing wallet"); - w.sync(&js_sys::Function::new_with_args( - "scanned_to, tip", - "console.log('Scanned: ', scanned_to, '/', tip)", - )) - .await - .unwrap(); + #[cfg(not(feature = "sync2"))] + { + tracing::info!("Syncing wallet with our sync impl"); + w.sync(|scanned_to, tip| { + println!("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().unwrap(); From 905a2b6b06f43ad47bd814122284e692602169d4 Mon Sep 17 00:00:00 2001 From: Eric Tu Date: Tue, 17 Sep 2024 16:02:39 -0400 Subject: [PATCH 4/4] compiles again --- tests/message-board-sync.rs | 9 ++++----- tests/simple-sync-and-send.rs | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/tests/message-board-sync.rs b/tests/message-board-sync.rs index d222044..bb81f21 100644 --- a/tests/message-board-sync.rs +++ b/tests/message-board-sync.rs @@ -3,8 +3,6 @@ wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); use std::sync::Once; use webz_core::bindgen::wallet::WebWallet; -use zcash_client_backend::sync::run; -use zcash_client_memory::MemBlockCache; use zcash_keys::keys::UnifiedFullViewingKey; use zcash_primitives::consensus::Network; use zcash_primitives::constants; @@ -38,9 +36,10 @@ async fn test_message_board() { #[cfg(not(feature = "sync2"))] { tracing::info!("Syncing wallet with our sync impl"); - w.sync(|scanned_to, tip| { - println!("Scanned: {}/{}", scanned_to, tip); - }) + w.sync(&js_sys::Function::new_with_args( + "scanned_to, tip", + "console.log('Scanned: ', scanned_to, '/', tip)", + )) .await .unwrap(); } diff --git a/tests/simple-sync-and-send.rs b/tests/simple-sync-and-send.rs index 4b421ba..099eb18 100644 --- a/tests/simple-sync-and-send.rs +++ b/tests/simple-sync-and-send.rs @@ -27,10 +27,10 @@ async fn test_get_and_scan_range() { #[cfg(not(feature = "sync2"))] { - tracing::info!("Syncing wallet with our sync impl"); - w.sync(|scanned_to, tip| { - println!("Scanned: {}/{}", scanned_to, tip); - }) + w.sync(&js_sys::Function::new_with_args( + "scanned_to, tip", + "console.log('Scanned: ', scanned_to, '/', tip)", + )) .await .unwrap(); }