diff --git a/libtonode-tests/tests/wallet.rs b/libtonode-tests/tests/wallet.rs index f88ee4762..ea6a1eab0 100644 --- a/libtonode-tests/tests/wallet.rs +++ b/libtonode-tests/tests/wallet.rs @@ -73,7 +73,7 @@ mod load_wallet { let _cph = regtest_manager.launch(false).unwrap(); println!("loading wallet"); - let wallet = LightWallet::load_example_wallet( + let wallet = LightWallet::load_example_wallet_legacy( zingolib::wallet::disk::testing::examples::LegacyWalletCase::OldWalletReorgTestWallet, ) .await; diff --git a/zingolib/src/config.rs b/zingolib/src/config.rs index c65cb5ab8..cb3677137 100644 --- a/zingolib/src/config.rs +++ b/zingolib/src/config.rs @@ -253,6 +253,18 @@ impl ZingoConfig { .create() } + #[cfg(any(test, feature = "test-elevation"))] + /// create a ZingoConfig that helps a LightClient connect to a server. + pub fn create_mainnet() -> ZingoConfig { + ZingoConfig::build(ChainType::Mainnet) + .set_lightwalletd_uri( + ("https://zcash.mysideoftheweb.com:19067") + .parse::() + .unwrap(), + ) + .create() + } + #[cfg(feature = "test-elevation")] /// create a ZingoConfig that signals a LightClient not to connect to a server. pub fn create_unconnected(chain: ChainType, dir: Option) -> ZingoConfig { diff --git a/zingolib/src/wallet/disk/testing.rs b/zingolib/src/wallet/disk/testing.rs index 0545fedf0..ac8d1d3b1 100644 --- a/zingolib/src/wallet/disk/testing.rs +++ b/zingolib/src/wallet/disk/testing.rs @@ -4,14 +4,6 @@ use super::LightWallet; impl LightWallet { - /// parses a wallet as an testnet wallet, aimed at a default testnet server - pub async fn unsafe_from_buffer_testnet(data: &[u8]) -> Self { - let config = crate::config::ZingoConfig::create_testnet(); - Self::read_internal(data, &config) - .await - .map_err(|e| format!("Cannot deserialize LightWallet file!: {}", e)) - .unwrap() - } /// connects a wallet to a local regtest node. pub async fn unsafe_from_buffer_regtest(data: &[u8]) -> Self { // this step starts a TestEnvironment and picks a new port! @@ -30,8 +22,28 @@ impl LightWallet { .map_err(|e| format!("Cannot deserialize LightWallet file!: {}", e)) .unwrap() } + /// parses a wallet as an testnet wallet, aimed at a default testnet server + pub async fn unsafe_from_buffer_testnet(data: &[u8]) -> Self { + let config = crate::config::ZingoConfig::create_testnet(); + Self::read_internal(data, &config) + .await + .map_err(|e| format!("Cannot deserialize LightWallet file!: {}", e)) + .unwrap() + } + /// parses a wallet as an testnet wallet, aimed at a default testnet server + pub async fn unsafe_from_buffer_mainnet(data: &[u8]) -> Self { + let config = crate::config::ZingoConfig::create_mainnet(); + Self::read_internal(data, &config) + .await + .map_err(|e| format!("Cannot deserialize LightWallet file!: {}", e)) + .unwrap() + } } +// async fn assert_test_wallet(case: examples::LegacyWalletCase) { +// let wallet = LightWallet::load_example_wallet(case).await; +// } + /// example wallets /// including from different versions of the software. pub mod examples; diff --git a/zingolib/src/wallet/disk/testing/examples.rs b/zingolib/src/wallet/disk/testing/examples.rs index 9d808ea64..a8b8b2278 100644 --- a/zingolib/src/wallet/disk/testing/examples.rs +++ b/zingolib/src/wallet/disk/testing/examples.rs @@ -1,8 +1,84 @@ use super::super::LightWallet; +/// as opposed to [LegacyWalletCase], which enumerates test cases compiled from the history of zingo wallt tests, this ExampleWalletNetworkCase is meant to fully organize the set of test cases. +#[non_exhaustive] +#[derive(Clone)] +pub enum ExampleWalletNetworkCase { + /// / + Mainnet(ExampleMainnetWalletSeedCase), + /// / + Testnet(ExampleTestnetWalletSeedCase), + /// / + Regtest(ExampleRegtestWalletSeedCase), +} + +/// / +#[non_exhaustive] +#[derive(Clone)] +pub enum ExampleMainnetWalletSeedCase { + /// this is a mainnet seed + VTFCORFBCBPCTCFUPMEGMWBP(ExampleVTFCORFBCBPCTCFUPMEGMWBPWalletVersionCase), +} +/// / +#[non_exhaustive] +#[derive(Clone)] +pub enum ExampleVTFCORFBCBPCTCFUPMEGMWBPWalletVersionCase { + /// wallet was last saved in this serialization version + V28, +} +/// / +#[non_exhaustive] +#[derive(Clone)] +pub enum ExampleTestnetWalletSeedCase { + /// This is a testnet seed. + MSKMGDBHOTBPETCJWCSPGOPP(ExampleMSKMGDBHOTBPETCJWCSPGOPPWalletVersionCase), +} +/// / +#[non_exhaustive] +#[derive(Clone)] +pub enum ExampleMSKMGDBHOTBPETCJWCSPGOPPWalletVersionCase { + /// wallet was last saved by the code in this commit + Gab72a38b, +} +/// / +#[non_exhaustive] +#[derive(Clone)] +pub enum ExampleRegtestWalletSeedCase {} + +/// loads test wallets +impl LightWallet { + /// loads any one of the test wallets included in the examples + pub async fn load_example_wallet(case: ExampleWalletNetworkCase) -> Self { + match case { + ExampleWalletNetworkCase::Mainnet( + ExampleMainnetWalletSeedCase::VTFCORFBCBPCTCFUPMEGMWBP( + ExampleVTFCORFBCBPCTCFUPMEGMWBPWalletVersionCase::V28, + ), + ) => { + LightWallet::unsafe_from_buffer_mainnet(include_bytes!( + "examples/mainnet/vtfcorfbcbpctcfupmegmwbp/v28/zingo-wallet.dat" + )) + .await + } + ExampleWalletNetworkCase::Testnet( + ExampleTestnetWalletSeedCase::MSKMGDBHOTBPETCJWCSPGOPP( + ExampleMSKMGDBHOTBPETCJWCSPGOPPWalletVersionCase::Gab72a38b, + ), + ) => { + LightWallet::unsafe_from_buffer_testnet(include_bytes!( + "examples/testnet/mskmgdbhotbpetcjwcspgopp/Gab72a38b/zingo-wallet.dat" + )) + .await + } + _ => unimplemented!(), + } + } +} + /// i do not know the difference between these wallets but i will find out soon /// what can these files do? #[non_exhaustive] +#[derive(Clone)] pub enum LegacyWalletCaseZingoV26 { /// / One, @@ -13,6 +89,7 @@ pub enum LegacyWalletCaseZingoV26 { } /// an enumeration of cases to test #[non_exhaustive] +#[derive(Clone)] pub enum LegacyWalletCase { /// at this version, legacy testing began ZingoV26(LegacyWalletCaseZingoV26), @@ -25,17 +102,17 @@ pub enum LegacyWalletCase { /// loads test wallets impl LightWallet { /// loads any one of the test wallets included in the examples - pub async fn load_example_wallet(case: LegacyWalletCase) -> Self { + pub async fn load_example_wallet_legacy(case: LegacyWalletCase) -> Self { match case { LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::One) => { LightWallet::unsafe_from_buffer_testnet(include_bytes!( - "examples/zingo-wallet-v26.dat" + "examples/v26-1/zingo-wallet.dat" )) .await } LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::Two) => { LightWallet::unsafe_from_buffer_testnet(include_bytes!( - "examples/zingo-wallet-v26.dat" + "examples/v26-2/zingo-wallet.dat" )) .await } @@ -59,4 +136,26 @@ impl LightWallet { } } } + + /// each wallet file has a saved balance + pub fn example_expected_balance(case: LegacyWalletCase) -> u64 { + match case { + LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::One) => 0, + LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::Two) => 10177826, + LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::RegtestSapOnly) => todo!(), + LegacyWalletCase::ZingoV28 => 10342837, + LegacyWalletCase::OldWalletReorgTestWallet => todo!(), + } + } + + /// each wallet file has a saved balance + pub fn example_expected_num_addresses(case: LegacyWalletCase) -> usize { + match case { + LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::One) => 3, + LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::Two) => 1, + LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::RegtestSapOnly) => todo!(), + LegacyWalletCase::ZingoV28 => 3, + LegacyWalletCase::OldWalletReorgTestWallet => todo!(), + } + } } diff --git a/zingolib/src/wallet/disk/testing/examples/missing_data_test/zingo-wallet.dat b/zingolib/src/wallet/disk/testing/examples/mainnet/vtfcorfbcbpctcfupmegmwbp/v28/zingo-wallet.dat similarity index 100% rename from zingolib/src/wallet/disk/testing/examples/missing_data_test/zingo-wallet.dat rename to zingolib/src/wallet/disk/testing/examples/mainnet/vtfcorfbcbpctcfupmegmwbp/v28/zingo-wallet.dat diff --git a/zingolib/src/wallet/disk/testing/examples/testnet/mskmgdbhotbpetcjwcspgopp/Gab72a38b/zingo-wallet.dat b/zingolib/src/wallet/disk/testing/examples/testnet/mskmgdbhotbpetcjwcspgopp/Gab72a38b/zingo-wallet.dat new file mode 100644 index 000000000..15bb72c4d Binary files /dev/null and b/zingolib/src/wallet/disk/testing/examples/testnet/mskmgdbhotbpetcjwcspgopp/Gab72a38b/zingo-wallet.dat differ diff --git a/zingolib/src/wallet/disk/testing/examples/zingo-wallet-v26.dat b/zingolib/src/wallet/disk/testing/examples/v26-1/zingo-wallet.dat similarity index 100% rename from zingolib/src/wallet/disk/testing/examples/zingo-wallet-v26.dat rename to zingolib/src/wallet/disk/testing/examples/v26-1/zingo-wallet.dat diff --git a/zingolib/src/wallet/disk/testing/examples/zingo-wallet-v26-2.dat b/zingolib/src/wallet/disk/testing/examples/v26-2/zingo-wallet.dat similarity index 100% rename from zingolib/src/wallet/disk/testing/examples/zingo-wallet-v26-2.dat rename to zingolib/src/wallet/disk/testing/examples/v26-2/zingo-wallet.dat diff --git a/zingolib/src/wallet/disk/testing/tests.rs b/zingolib/src/wallet/disk/testing/tests.rs index dd482a750..e5ea3a644 100644 --- a/zingolib/src/wallet/disk/testing/tests.rs +++ b/zingolib/src/wallet/disk/testing/tests.rs @@ -6,10 +6,36 @@ use crate::lightclient::LightClient; use super::super::LightWallet; +use super::examples::ExampleMainnetWalletSeedCase; use super::examples::LegacyWalletCase; use super::examples::LegacyWalletCaseZingoV26; -async fn loaded_wallet_assert(wallet: LightWallet, expected_balance: u64, num_addresses: usize) { +#[tokio::test] +async fn verify_example_wallet_mainnet_vtfcorfbcbpctcfupmegmwbp_v28() { + let _wallet = + LightWallet::load_example_wallet(super::examples::ExampleWalletNetworkCase::Mainnet( + ExampleMainnetWalletSeedCase::VTFCORFBCBPCTCFUPMEGMWBP( + super::examples::ExampleVTFCORFBCBPCTCFUPMEGMWBPWalletVersionCase::V28, + ), + )) + .await; +} +#[tokio::test] +async fn verify_example_wallet_mainnet_mskmgdbhotbpetcjwcspgopp_gab72a38b() { + let _wallet = + LightWallet::load_example_wallet(super::examples::ExampleWalletNetworkCase::Testnet( + super::examples::ExampleTestnetWalletSeedCase::MSKMGDBHOTBPETCJWCSPGOPP( + super::examples::ExampleMSKMGDBHOTBPETCJWCSPGOPPWalletVersionCase::Gab72a38b, + ), + )) + .await; +} + +async fn loaded_wallet_assert( + wallet: LightWallet, + expected_balance: u64, + expected_num_addresses: usize, +) { let expected_mnemonic = ( zcash_primitives::zip339::Mnemonic::from_phrase( crate::testvectors::seeds::CHIMNEY_BETTER_SEED.to_string(), @@ -60,7 +86,7 @@ async fn loaded_wallet_assert(wallet: LightWallet, expected_balance: u64, num_ad .unwrap() ); - assert_eq!(wc.addresses().len(), num_addresses); + assert_eq!(wc.addresses().len(), expected_num_addresses); for addr in wc.addresses().iter() { assert!(addr.orchard().is_some()); assert!(addr.sapling().is_some()); @@ -95,7 +121,7 @@ async fn loaded_wallet_assert(wallet: LightWallet, expected_balance: u64, num_ad #[tokio::test] async fn load_and_parse_different_wallet_versions() { - let _loaded_wallet = LightWallet::load_example_wallet(LegacyWalletCase::ZingoV26( + let _loaded_wallet = LightWallet::load_example_wallet_legacy(LegacyWalletCase::ZingoV26( LegacyWalletCaseZingoV26::RegtestSapOnly, )) .await; @@ -116,14 +142,19 @@ async fn load_wallet_from_v26_dat_file() { // --seed "chimney better bulb horror rebuild whisper improve intact letter giraffe brave rib appear bulk aim burst snap salt hill sad merge tennis phrase raise" // with 3 addresses containing all receivers. // including orchard and sapling transactions - let wallet = - LightWallet::load_example_wallet(LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::One)) - .await; + let case = LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::One); + + let wallet = LightWallet::load_example_wallet_legacy(case.clone()).await; - loaded_wallet_assert(wallet, 0, 3).await; + loaded_wallet_assert( + wallet, + LightWallet::example_expected_balance(case.clone()), + LightWallet::example_expected_num_addresses(case), + ) + .await; } -#[ignore = "flakey test"] +#[ignore = "test proves note has no index bug is a breaker"] #[tokio::test] async fn load_wallet_from_v26_2_dat_file() { // We test that the LightWallet can be read from v26 .dat file @@ -139,22 +170,34 @@ async fn load_wallet_from_v26_2_dat_file() { // --seed "chimney better bulb horror rebuild whisper improve intact letter giraffe brave rib appear bulk aim burst snap salt hill sad merge tennis phrase raise" // with 3 addresses containing all receivers. // including orchard and sapling transactions - let wallet = - LightWallet::load_example_wallet(LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::Two)) - .await; + let case = LegacyWalletCase::ZingoV26(LegacyWalletCaseZingoV26::Two); + + let wallet = LightWallet::load_example_wallet_legacy(case.clone()).await; - loaded_wallet_assert(wallet, 10177826, 1).await; + loaded_wallet_assert( + wallet, + LightWallet::example_expected_balance(case.clone()), + LightWallet::example_expected_num_addresses(case), + ) + .await; } -#[ignore = "flakey test"] +#[ignore = "test fails because ZFZ panics in regtest"] #[tokio::test] async fn load_wallet_from_v28_dat_file() { // We test that the LightWallet can be read from v28 .dat file // --seed "chimney better bulb horror rebuild whisper improve intact letter giraffe brave rib appear bulk aim burst snap salt hill sad merge tennis phrase raise" // with 3 addresses containing all receivers. - let wallet = LightWallet::load_example_wallet(LegacyWalletCase::ZingoV28).await; + let case = LegacyWalletCase::ZingoV28; - loaded_wallet_assert(wallet, 10342837, 3).await; + let wallet = LightWallet::load_example_wallet_legacy(case.clone()).await; + + loaded_wallet_assert( + wallet, + LightWallet::example_expected_balance(case.clone()), + LightWallet::example_expected_num_addresses(case), + ) + .await; } #[tokio::test] @@ -173,7 +216,7 @@ async fn reload_wallet_from_buffer() { // --birthday 0 // --nosync // with 3 addresses containing all receivers. - let mid_wallet = LightWallet::load_example_wallet(LegacyWalletCase::ZingoV28).await; + let mid_wallet = LightWallet::load_example_wallet_legacy(LegacyWalletCase::ZingoV28).await; let mid_client = LightClient::create_from_wallet_async(mid_wallet) .await