From eda1f88fabbbb6e3330537be148943cfede23272 Mon Sep 17 00:00:00 2001 From: Eric Tu Date: Tue, 17 Sep 2024 14:14:43 -0400 Subject: [PATCH] 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); +}