Skip to content

Commit

Permalink
add back testutils fns
Browse files Browse the repository at this point in the history
  • Loading branch information
Oscar-Pepper committed Oct 1, 2024
1 parent eec7ddb commit 565aa2c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
16 changes: 10 additions & 6 deletions libtonode-tests/tests/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use zingolib::testutils::lightclient::from_inputs;
use zingolib::testutils::{build_fvk_client, increase_height_and_wait_for_client, scenarios};
use zingolib::utils::conversion::address_from_str;
use zingolib::wallet::data::summaries::TransactionSummaryInterface;
use zingolib::wallet::keys::unified::UnifiedKeyStore;
use zingolib::wallet::propose::ProposeSendError;
use zingolib::{check_client_balances, get_base_address_macro, get_otd, validate_otds};

Expand Down Expand Up @@ -69,15 +70,18 @@ fn check_view_capability_bounds(
sent_t_value: Option<u64>,
notes: &JsonValue,
) {
let UnifiedKeyStore::View(ufvk) = watch_wc.unified_key_store() else {
panic!("should be viewing key!")
};
//Orchard
if !fvks.contains(&ovk) {
assert!(!watch_wc.orchard.can_view());
assert!(ufvk.orchard().is_none());
assert_eq!(balance.orchard_balance, None);
assert_eq!(balance.verified_orchard_balance, None);
assert_eq!(balance.unverified_orchard_balance, None);
assert_eq!(notes["unspent_orchard_notes"].members().count(), 0);
} else {
assert!(watch_wc.orchard.can_view());
assert!(ufvk.orchard().is_some());
assert_eq!(balance.orchard_balance, sent_o_value);
assert_eq!(balance.verified_orchard_balance, sent_o_value);
assert_eq!(balance.unverified_orchard_balance, Some(0));
Expand All @@ -87,24 +91,24 @@ fn check_view_capability_bounds(
}
//Sapling
if !fvks.contains(&svk) {
assert!(!watch_wc.sapling.can_view());
assert!(ufvk.sapling().is_none());
assert_eq!(balance.sapling_balance, None);
assert_eq!(balance.verified_sapling_balance, None);
assert_eq!(balance.unverified_sapling_balance, None);
assert_eq!(notes["unspent_sapling_notes"].members().count(), 0);
} else {
assert!(watch_wc.sapling.can_view());
assert!(ufvk.sapling().is_some());
assert_eq!(balance.sapling_balance, sent_s_value);
assert_eq!(balance.verified_sapling_balance, sent_s_value);
assert_eq!(balance.unverified_sapling_balance, Some(0));
assert_eq!(notes["unspent_sapling_notes"].members().count(), 1);
}
if !fvks.contains(&tvk) {
assert!(!watch_wc.transparent.can_view());
assert!(ufvk.transparent().is_none());
assert_eq!(balance.transparent_balance, None);
assert_eq!(notes["utxos"].members().count(), 0);
} else {
assert!(watch_wc.transparent.can_view());
assert!(ufvk.transparent().is_some());
assert_eq!(balance.transparent_balance, sent_t_value);
assert_eq!(notes["utxos"].members().count(), 1);
}
Expand Down
37 changes: 25 additions & 12 deletions zingolib/src/testutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::wallet::data::summaries::{
OrchardNoteSummary, SaplingNoteSummary, SpendSummary, TransactionSummary,
TransactionSummaryInterface as _, TransparentCoinSummary,
};
use crate::wallet::keys::unified::{UnifiedKeyStore, WalletCapability};
use crate::wallet::WalletBase;
use grpc_proxy::ProxyServer;
pub use incrementalmerkletree;
Expand All @@ -20,7 +21,7 @@ use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::Duration;
use tokio::task::JoinHandle;
use zcash_keys::keys::UnifiedFullViewingKey;
use zcash_address::unified::Fvk;

use crate::config::ZingoConfig;
use crate::lightclient::LightClient;
Expand All @@ -44,19 +45,31 @@ pub mod paths;
pub mod regtest;

/// TODO: Add Doc Comment Here!
pub async fn build_fvk_client(
ufvk: UnifiedFullViewingKey,
zingoconfig: &ZingoConfig,
) -> LightClient {
LightClient::create_unconnected(
zingoconfig,
WalletBase::Ufvk(ufvk.encode(&zingoconfig.chain)),
0,
)
.await
.unwrap()
pub fn build_fvks_from_wallet_capability(wallet_capability: &WalletCapability) -> [Fvk; 3] {
let UnifiedKeyStore::Spend(usk) = wallet_capability.unified_key_store() else {
panic!("should be spending key!")
};
let o_fvk = Fvk::Orchard(orchard::keys::FullViewingKey::from(usk.orchard()).to_bytes());
let s_fvk = Fvk::Sapling(usk.sapling().to_diversifiable_full_viewing_key().to_bytes());
let mut t_fvk_bytes = [0u8; 65];
t_fvk_bytes.copy_from_slice(&usk.transparent().to_account_pubkey().serialize());
let t_fvk = Fvk::P2pkh(t_fvk_bytes);
[o_fvk, s_fvk, t_fvk]
}

/// TODO: Add Doc Comment Here!
pub async fn build_fvk_client(fvks: &[&Fvk], zingoconfig: &ZingoConfig) -> LightClient {
let ufvk = zcash_address::unified::Encoding::encode(
&<zcash_address::unified::Ufvk as zcash_address::unified::Encoding>::try_from_items(
fvks.iter().copied().cloned().collect(),
)
.unwrap(),
&zcash_address::Network::Regtest,
);
LightClient::create_unconnected(zingoconfig, WalletBase::Ufvk(ufvk), 0)
.await
.unwrap()
}
async fn get_synced_wallet_height(client: &LightClient) -> Result<u32, String> {
client.do_sync(true).await?;
Ok(client
Expand Down
8 changes: 4 additions & 4 deletions zingolib/src/wallet/disk/testing/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use bip0039::Mnemonic;

use crate::get_base_address_macro;
use crate::lightclient::LightClient;
use crate::wallet::keys::unified::UnifiedKeyStore;

use super::super::LightWallet;
use super::assert_wallet_capability_matches_seed;
Expand Down Expand Up @@ -210,8 +211,6 @@ async fn reload_wallet_from_buffer() {
use zcash_primitives::consensus::Parameters;

use crate::testvectors::seeds::CHIMNEY_BETTER_SEED;
use crate::wallet::disk::Capability;
use crate::wallet::keys::extended_transparent::ExtendedPrivKey;
use crate::wallet::WalletBase;
use crate::wallet::WalletCapability;

Expand Down Expand Up @@ -245,9 +244,10 @@ async fn reload_wallet_from_buffer() {
.unwrap();
let wc = wallet.wallet_capability();

let Capability::Spend(orchard_sk) = &wc.orchard else {
panic!("Expected Orchard Spending Key");
let UnifiedKeyStore::Spend(usk) = wc.unified_key_store() else {
panic!("should be spending key!")
};

assert_eq!(
orchard_sk.to_bytes(),
orchard::keys::SpendingKey::try_from(&expected_wc)
Expand Down

0 comments on commit 565aa2c

Please sign in to comment.