Skip to content

Commit

Permalink
Merge pull request zingolabs#1315 from fluidvanadium/old_wallet_loade…
Browse files Browse the repository at this point in the history
…r_tests

DRAFT: Old wallet loader tests
  • Loading branch information
zancas authored Aug 2, 2024
2 parents e279a1b + 44b66eb commit d5f8646
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 46 deletions.
2 changes: 1 addition & 1 deletion libtonode-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
49 changes: 16 additions & 33 deletions libtonode-tests/tests/concrete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,9 @@ 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, 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#"{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -3945,27 +3938,19 @@ mod slow {
expected_balance: u64,
num_addresses: usize,
) {
let config = zingoconfig::ZingoConfig::build(ChainType::Testnet)
.set_lightwalletd_uri(
("https://zcash.mysideoftheweb.com:19067")
.parse::<http::Uri>()
.unwrap(),
)
.create();
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)
Expand Down Expand Up @@ -4008,9 +3993,7 @@ mod slow {
assert!(addr.transparent().is_some());
}

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 {
Expand Down
2 changes: 1 addition & 1 deletion zingo-testutils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
3 changes: 3 additions & 0 deletions zingoconfig/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions zingoconfig/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,19 @@ impl ZingoConfig {
..ZingoConfigBuilder::default()
}
}

#[cfg(feature = "test-elevation")]
/// 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::<http::Uri>()
.unwrap(),
)
.create()
}

/// Convenience wrapper
pub fn sapling_activation_height(&self) -> u64 {
self.chain
Expand Down
1 change: 1 addition & 0 deletions zingolib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ edition = "2021"
deprecations = ["lightclient-deprecated"]
lightclient-deprecated = []
darkside_tests = []
test-elevation = []

[dependencies]
zingoconfig = { path = "../zingoconfig" }
Expand Down
20 changes: 10 additions & 10 deletions zingolib/src/lightclient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
pub async fn create_from_wallet_async(wallet: LightWallet) -> io::Result<Self> {
let mut buffer: Vec<u8> = vec![];
wallet.write(&mut buffer).await?;
let config = wallet.transaction_context.config.clone();
Ok(LightClient {
wallet,
config: config.clone(),
Expand Down Expand Up @@ -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;
Expand All @@ -342,10 +341,11 @@ pub mod instantiation {
wallet_base: WalletBase,
height: u64,
) -> io::Result<Self> {
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)
}
Expand Down
2 changes: 1 addition & 1 deletion zingolib/src/lightclient/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl LightClient {
) -> io::Result<Self> {
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 {}",
Expand Down
3 changes: 3 additions & 0 deletions zingolib/src/wallet/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,3 +241,6 @@ impl LightWallet {
Ok(lw)
}
}

#[cfg(any(test, feature = "test-elevation"))]
pub mod testing;
13 changes: 13 additions & 0 deletions zingolib/src/wallet/disk/testing.rs
Original file line number Diff line number Diff line change
@@ -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()
}
}

0 comments on commit d5f8646

Please sign in to comment.