From 2328e28a6e40f0e00e77a64c86ad8e22050a81e2 Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Mon, 29 Jul 2024 15:46:37 +0000 Subject: [PATCH 1/5] helperization --- libtonode-tests/tests/concrete.rs | 8 +------- zingoconfig/src/lib.rs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/libtonode-tests/tests/concrete.rs b/libtonode-tests/tests/concrete.rs index c039366a3..c1a75fd4b 100644 --- a/libtonode-tests/tests/concrete.rs +++ b/libtonode-tests/tests/concrete.rs @@ -3945,13 +3945,7 @@ mod slow { expected_balance: u64, num_addresses: usize, ) { - let config = zingoconfig::ZingoConfig::build(ChainType::Testnet) - .set_lightwalletd_uri( - ("https://zcash.mysideoftheweb.com:19067") - .parse::() - .unwrap(), - ) - .create(); + let config = zingoconfig::ZingoConfig::create_testnet(); let wallet = LightWallet::read_internal(data, &config) .await .map_err(|e| format!("Cannot deserialize LightWallet file!: {}", e)) diff --git a/zingoconfig/src/lib.rs b/zingoconfig/src/lib.rs index 42c8d6aea..224f3f3d3 100644 --- a/zingoconfig/src/lib.rs +++ b/zingoconfig/src/lib.rs @@ -250,6 +250,18 @@ impl ZingoConfig { ..ZingoConfigBuilder::default() } } + + /// create a ZingoConfig that helps a LightClient connect to a server. + pub fn create_testnet() -> ZingoConfig { + ZingoConfig::build(ChainType::Testnet) + .set_lightwalletd_uri( + ("https://zcash.mysideoftheweb.com:19067") + .parse::() + .unwrap(), + ) + .create() + } + /// Convenience wrapper pub fn sapling_activation_height(&self) -> u64 { self.chain From 9a1db4698a4ed5c1dc7639f8369e680949a228ac Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Mon, 29 Jul 2024 16:05:19 +0000 Subject: [PATCH 2/5] created test-elevation flag to avoid adding default pub items --- zingo-testutils/Cargo.toml | 2 +- zingoconfig/Cargo.toml | 3 +++ zingoconfig/src/lib.rs | 1 + 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/zingo-testutils/Cargo.toml b/zingo-testutils/Cargo.toml index 64d3aa6da..4f45e5d38 100644 --- a/zingo-testutils/Cargo.toml +++ b/zingo-testutils/Cargo.toml @@ -10,7 +10,7 @@ default = ["grpc-proxy"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -zingoconfig = { path = "../zingoconfig" } +zingoconfig = { path = "../zingoconfig", features = ["test-elevation"]} zingolib = { path = "../zingolib" } zingo-netutils = { path = "../zingo-netutils", features = ["test-features"] } zingo-testvectors = { path = "../zingo-testvectors" } diff --git a/zingoconfig/Cargo.toml b/zingoconfig/Cargo.toml index a486f5a9c..08cdfbc87 100644 --- a/zingoconfig/Cargo.toml +++ b/zingoconfig/Cargo.toml @@ -5,6 +5,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[features] +test-elevation = [] + [dependencies] zcash_primitives.workspace = true http.workspace = true diff --git a/zingoconfig/src/lib.rs b/zingoconfig/src/lib.rs index 224f3f3d3..ded47e3df 100644 --- a/zingoconfig/src/lib.rs +++ b/zingoconfig/src/lib.rs @@ -251,6 +251,7 @@ impl ZingoConfig { } } + #[cfg(feature = "test-elevation")] /// create a ZingoConfig that helps a LightClient connect to a server. pub fn create_testnet() -> ZingoConfig { ZingoConfig::build(ChainType::Testnet) From e151a0e5b9b8d1ef5999a14a2c5374c796088d0c Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Mon, 29 Jul 2024 16:43:56 +0000 Subject: [PATCH 3/5] feature flags and helpers --- libtonode-tests/Cargo.toml | 2 +- libtonode-tests/tests/concrete.rs | 17 ++++++++--------- zingolib/Cargo.toml | 1 + zingolib/src/wallet/disk.rs | 3 +++ zingolib/src/wallet/disk/testing.rs | 13 +++++++++++++ 5 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 zingolib/src/wallet/disk/testing.rs diff --git a/libtonode-tests/Cargo.toml b/libtonode-tests/Cargo.toml index 7c1805c10..e82090b05 100644 --- a/libtonode-tests/Cargo.toml +++ b/libtonode-tests/Cargo.toml @@ -8,7 +8,7 @@ chain_generic_tests = [] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -zingolib = { path = "../zingolib", features = ["deprecations"] } +zingolib = { path = "../zingolib", features = ["deprecations", "test-elevation"] } zingo-status = { path = "../zingo-status" } zingo-testutils = { path = "../zingo-testutils" } zingo-testvectors = { path = "../zingo-testvectors" } diff --git a/libtonode-tests/tests/concrete.rs b/libtonode-tests/tests/concrete.rs index c1a75fd4b..95a56e911 100644 --- a/libtonode-tests/tests/concrete.rs +++ b/libtonode-tests/tests/concrete.rs @@ -3945,21 +3945,19 @@ mod slow { expected_balance: u64, num_addresses: usize, ) { - let config = zingoconfig::ZingoConfig::create_testnet(); - let wallet = LightWallet::read_internal(data, &config) - .await - .map_err(|e| format!("Cannot deserialize LightWallet file!: {}", e)) - .unwrap(); - + let wallet = LightWallet::unsafe_from_buffer_testnet(data).await; let expected_mnemonic = ( Mnemonic::from_phrase(CHIMNEY_BETTER_SEED.to_string()).unwrap(), 0, ); assert_eq!(wallet.mnemonic(), Some(&expected_mnemonic)); - let expected_wc = - WalletCapability::new_from_phrase(&config, &expected_mnemonic.0, expected_mnemonic.1) - .unwrap(); + let expected_wc = WalletCapability::new_from_phrase( + &wallet.transaction_context.config, + &expected_mnemonic.0, + expected_mnemonic.1, + ) + .unwrap(); let wc = wallet.wallet_capability(); // We don't want the WalletCapability to impl. `Eq` (because it stores secret keys) @@ -4002,6 +4000,7 @@ mod slow { assert!(addr.transparent().is_some()); } + let config = wallet.transaction_context.config.clone(); let client = LightClient::create_from_wallet_async(wallet, config) .await .unwrap(); diff --git a/zingolib/Cargo.toml b/zingolib/Cargo.toml index 3ead763d7..cd3e86785 100644 --- a/zingolib/Cargo.toml +++ b/zingolib/Cargo.toml @@ -10,6 +10,7 @@ edition = "2021" deprecations = ["lightclient-deprecated"] lightclient-deprecated = [] darkside_tests = [] +test-elevation = [] [dependencies] zingoconfig = { path = "../zingoconfig" } diff --git a/zingolib/src/wallet/disk.rs b/zingolib/src/wallet/disk.rs index 152bb4cd2..3e49252b5 100644 --- a/zingolib/src/wallet/disk.rs +++ b/zingolib/src/wallet/disk.rs @@ -241,3 +241,6 @@ impl LightWallet { Ok(lw) } } + +#[cfg(any(test, feature = "test-elevation"))] +pub mod testing; diff --git a/zingolib/src/wallet/disk/testing.rs b/zingolib/src/wallet/disk/testing.rs new file mode 100644 index 000000000..d8326c80c --- /dev/null +++ b/zingolib/src/wallet/disk/testing.rs @@ -0,0 +1,13 @@ +//! functionality for testing the save and load functions of LightWallet. +//! do not compile test-elevation feature for production. + +impl super::LightWallet { + /// connects a wallet to TestNet server. + pub async fn unsafe_from_buffer_testnet(data: &[u8]) -> Self { + let config = zingoconfig::ZingoConfig::create_testnet(); + Self::read_internal(data, &config) + .await + .map_err(|e| format!("Cannot deserialize LightWallet file!: {}", e)) + .unwrap() + } +} From 8af28158bd8c3ee398ec94abd5bd446be6833df6 Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Mon, 29 Jul 2024 16:51:32 +0000 Subject: [PATCH 4/5] remove extra argument from LightWallet builder --- libtonode-tests/tests/concrete.rs | 25 ++++++++----------------- zingolib/src/lightclient.rs | 20 ++++++++++---------- zingolib/src/lightclient/read.rs | 2 +- 3 files changed, 19 insertions(+), 28 deletions(-) diff --git a/libtonode-tests/tests/concrete.rs b/libtonode-tests/tests/concrete.rs index 95a56e911..a63f6b6cf 100644 --- a/libtonode-tests/tests/concrete.rs +++ b/libtonode-tests/tests/concrete.rs @@ -320,9 +320,7 @@ mod fast { let wallet_dir = wallet_path.parent().unwrap(); let (wallet, config) = zingo_testutils::load_wallet(wallet_dir.to_path_buf(), ChainType::Mainnet).await; - let client = LightClient::create_from_wallet_async(wallet, config) - .await - .unwrap(); + let client = LightClient::create_from_wallet_async(wallet).await.unwrap(); let transactions = client.do_list_transactions().await[0].clone(); //env_logger::init(); let expected_consumer_ui_note = r#"{ @@ -462,10 +460,9 @@ mod fast { .unwrap(); // Create client based on config and wallet of faucet - let faucet_copy = - LightClient::create_from_wallet_async(faucet_wallet, zingo_config.clone()) - .await - .unwrap(); + let faucet_copy = LightClient::create_from_wallet_async(faucet_wallet) + .await + .unwrap(); assert_eq!( &faucet_copy.do_seed_phrase().await.unwrap(), &faucet.do_seed_phrase().await.unwrap() @@ -637,7 +634,7 @@ mod fast { .map_err(|e| format!("Cannot deserialize LightWallet version 28 file: {}", e)) .unwrap(); - let mid_client = LightClient::create_from_wallet_async(mid_wallet, config.clone()) + let mid_client = LightClient::create_from_wallet_async(mid_wallet) .await .unwrap(); let mid_buffer = mid_client.export_save_buffer_async().await.unwrap(); @@ -700,9 +697,7 @@ mod fast { let vv_string = vv.encode(&config.chain.network_type()); assert_eq!(ufvk_string, vv_string); - let client = LightClient::create_from_wallet_async(wallet, config) - .await - .unwrap(); + let client = LightClient::create_from_wallet_async(wallet).await.unwrap(); let balance = client.do_balance().await; assert_eq!(balance.orchard_balance, Some(10342837)); } @@ -2943,9 +2938,7 @@ mod slow { println!("setting uri"); *conf.lightwalletd_uri.write().unwrap() = faucet.get_server_uri(); println!("creating lightclient"); - let recipient = LightClient::create_from_wallet_async(wallet, conf) - .await - .unwrap(); + let recipient = LightClient::create_from_wallet_async(wallet).await.unwrap(); println!( "pre-sync transactions: {}", recipient.do_list_transactions().await.pretty(2) @@ -4001,9 +3994,7 @@ mod slow { } let config = wallet.transaction_context.config.clone(); - let client = LightClient::create_from_wallet_async(wallet, config) - .await - .unwrap(); + let client = LightClient::create_from_wallet_async(wallet).await.unwrap(); let balance = client.do_balance().await; assert_eq!(balance.orchard_balance, Some(expected_balance)); if expected_balance > 0 { diff --git a/zingolib/src/lightclient.rs b/zingolib/src/lightclient.rs index 0371c8c04..d48848968 100644 --- a/zingolib/src/lightclient.rs +++ b/zingolib/src/lightclient.rs @@ -266,12 +266,10 @@ pub mod instantiation { // toDo rework ZingoConfig. /// This is the fundamental invocation of a LightClient. It lives in an asyncronous runtime. - pub async fn create_from_wallet_async( - wallet: LightWallet, - config: ZingoConfig, - ) -> io::Result { + pub async fn create_from_wallet_async(wallet: LightWallet) -> io::Result { let mut buffer: Vec = vec![]; wallet.write(&mut buffer).await?; + let config = wallet.transaction_context.config.clone(); Ok(LightClient { wallet, config: config.clone(), @@ -319,10 +317,11 @@ pub mod instantiation { )); } } - let lightclient = LightClient::create_from_wallet_async( - LightWallet::new(config.clone(), wallet_base, birthday)?, + let lightclient = LightClient::create_from_wallet_async(LightWallet::new( config.clone(), - ) + wallet_base, + birthday, + )?) .await?; lightclient.set_wallet_initial_state(birthday).await; @@ -342,10 +341,11 @@ pub mod instantiation { wallet_base: WalletBase, height: u64, ) -> io::Result { - let lightclient = LightClient::create_from_wallet_async( - LightWallet::new(config.clone(), wallet_base, height)?, + let lightclient = LightClient::create_from_wallet_async(LightWallet::new( config.clone(), - ) + wallet_base, + height, + )?) .await?; Ok(lightclient) } diff --git a/zingolib/src/lightclient/read.rs b/zingolib/src/lightclient/read.rs index 6506cbcbc..b262927d0 100644 --- a/zingolib/src/lightclient/read.rs +++ b/zingolib/src/lightclient/read.rs @@ -20,7 +20,7 @@ impl LightClient { ) -> io::Result { let wallet = LightWallet::read_internal(&mut reader, config).await?; - let lc = LightClient::create_from_wallet_async(wallet, config.clone()).await?; + let lc = LightClient::create_from_wallet_async(wallet).await?; debug!( "Read wallet with birthday {}", From 8f4dc3590acb34ba79bf050f957146c66d60a2d2 Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Mon, 29 Jul 2024 16:54:13 +0000 Subject: [PATCH 5/5] _unused_vars --- libtonode-tests/tests/concrete.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libtonode-tests/tests/concrete.rs b/libtonode-tests/tests/concrete.rs index a63f6b6cf..bc1dddd5c 100644 --- a/libtonode-tests/tests/concrete.rs +++ b/libtonode-tests/tests/concrete.rs @@ -318,7 +318,7 @@ mod fast { ); let wallet_path = Path::new(&wallet_nym); let wallet_dir = wallet_path.parent().unwrap(); - let (wallet, config) = + let (wallet, _config) = zingo_testutils::load_wallet(wallet_dir.to_path_buf(), ChainType::Mainnet).await; let client = LightClient::create_from_wallet_async(wallet).await.unwrap(); let transactions = client.do_list_transactions().await[0].clone(); @@ -3993,7 +3993,6 @@ mod slow { assert!(addr.transparent().is_some()); } - let config = wallet.transaction_context.config.clone(); let client = LightClient::create_from_wallet_async(wallet).await.unwrap(); let balance = client.do_balance().await; assert_eq!(balance.orchard_balance, Some(expected_balance));