Skip to content

Commit

Permalink
Merge pull request #1330 from fluidvanadium/owls-0.2
Browse files Browse the repository at this point in the history
Owls 0.2
  • Loading branch information
Oscar-Pepper authored Aug 23, 2024
2 parents 6306778 + 9603a15 commit 6706527
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 28 deletions.
2 changes: 1 addition & 1 deletion libtonode-tests/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 12 additions & 0 deletions zingolib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<http::Uri>()
.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<PathBuf>) -> ZingoConfig {
Expand Down
28 changes: 20 additions & 8 deletions zingolib/src/wallet/disk/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand All @@ -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;
Expand Down
105 changes: 102 additions & 3 deletions zingolib/src/wallet/disk/testing/examples.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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),
Expand All @@ -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
}
Expand All @@ -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!(),
}
}
}
Binary file not shown.
75 changes: 59 additions & 16 deletions zingolib/src/wallet/disk/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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
Expand Down

0 comments on commit 6706527

Please sign in to comment.