Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offline Mode for zingo-mobile #1540

Merged
merged 8 commits into from
Nov 27, 2024
61 changes: 52 additions & 9 deletions zingolib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
sync::{Arc, RwLock},
};

use log::LevelFilter;
use log::{info, LevelFilter};
use log4rs::{
append::rolling_file::{
policy::compound::{
Expand Down Expand Up @@ -63,25 +63,29 @@ pub fn margin_fee() -> u64 {
zcash_primitives::transaction::fees::zip317::MARGINAL_FEE.into_u64()
}

/// TODO: Add Doc Comment Here!
/// Same as load_clientconfig but doesn't panic when the server can't be reached
pub fn load_clientconfig(
lightwallet_uri: http::Uri,
data_dir: Option<PathBuf>,
chain: ChainType,
monitor_mempool: bool,
) -> std::io::Result<ZingoConfig> {
use std::net::ToSocketAddrs;
format!(
match format!(
"{}:{}",
lightwallet_uri.host().unwrap(),
zancas marked this conversation as resolved.
Show resolved Hide resolved
lightwallet_uri.port().unwrap()
)
.to_socket_addrs()?
.next()
.ok_or(std::io::Error::new(
ErrorKind::ConnectionRefused,
"Couldn't resolve server!",
))?;
.to_socket_addrs()
{
Ok(_) => {
info!("Connected to {}", lightwallet_uri);
}
Err(e) => {
info!("Couldn't resolve server: {}", e);
}
}
info!("Connected to {}", lightwallet_uri);

// Create a Light Client Config
let config = ZingoConfig {
Expand Down Expand Up @@ -726,3 +730,42 @@ impl ActivationHeights {
}
}
}

mod tests {

#[tokio::test]
async fn test_load_clientconfig_serverless() {
rustls::crypto::ring::default_provider()
.install_default()
.expect("Ring to work as a default");
tracing_subscriber::fmt().init();

let valid_uri = crate::config::construct_lightwalletd_uri(Some(
crate::config::DEFAULT_LIGHTWALLETD_SERVER.to_string(),
));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really think this test is correct, I see valid_uri with a valid uri, hehehehe, obviously, and IMO this test needs a different value here, IDK like http::Uri::default() which for me is a empty server uri.

// let invalid_uri = construct_lightwalletd_uri(Some("Invalid URI".to_string()));
let temp_dir = tempfile::TempDir::new().unwrap();

let temp_path = temp_dir.path().to_path_buf();
// let temp_path_invalid = temp_dir.path().to_path_buf();

let valid_config = crate::config::load_clientconfig(
valid_uri.clone(),
Some(temp_path),
crate::config::ChainType::Mainnet,
true,
)
.unwrap();

assert_eq!(valid_config.get_lightwalletd_uri(), valid_uri);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this make no sense to me... in a serverless scenario... in fact the normal situation for serverless is:

  • server uri: empty.
  • chain type: empty.

assert_eq!(valid_config.chain, crate::config::ChainType::Mainnet);

// let invalid_config = load_clientconfig_serverless(
// invalid_uri.clone(),
// Some(temp_path_invalid),
// ChainType::Mainnet,
// true,
// );
// assert_eq!(invalid_config.is_err(), true);
}
}