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

Parse empty uri strings correctly in load_clientconfig #1584

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix(load_clientconfig): parse empty uri strings correctly
  • Loading branch information
dorianvp committed Dec 9, 2024
commit f690eb31fddfe1fcbbaa9188fe10264c335ecb38
82 changes: 59 additions & 23 deletions zingolib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,21 +71,28 @@ pub fn load_clientconfig(
monitor_mempool: bool,
) -> std::io::Result<ZingoConfig> {
use std::net::ToSocketAddrs;
match format!(
"{}:{}",
lightwallet_uri.host().unwrap(),
lightwallet_uri.port().unwrap()
)
.to_socket_addrs()
{
Ok(_) => {
info!("Connected to {}", lightwallet_uri);
}
Err(e) => {
info!("Couldn't resolve server: {}", e);

let host = lightwallet_uri.host();
let port = lightwallet_uri.port();

if host.is_none() || port.is_none() {
info!("Using offline mode");
} else {
match format!(
"{}:{}",
lightwallet_uri.host().unwrap(),
lightwallet_uri.port().unwrap()
)
.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 All @@ -105,18 +112,23 @@ pub fn load_clientconfig(
/// TODO: Add Doc Comment Here!
pub fn construct_lightwalletd_uri(server: Option<String>) -> http::Uri {
match server {
Some(s) => {
let mut s = if s.starts_with("http") {
Some(s) => match s.is_empty() {
true => {
return http::Uri::default();
}
false => {
let mut s = if s.starts_with("http") {
s
} else {
"http://".to_string() + &s
};
let uri: http::Uri = s.parse().unwrap();
if uri.port().is_none() {
s += ":9067";
}
s
} else {
"http://".to_string() + &s
};
let uri: http::Uri = s.parse().unwrap();
if uri.port().is_none() {
s += ":9067";
}
s
}
},
None => DEFAULT_LIGHTWALLETD_SERVER.to_string(),
}
.parse()
Expand Down Expand Up @@ -733,6 +745,30 @@ impl ActivationHeights {

mod tests {

/// Validate that the load_clientconfig function creates a valid config from an empty uri
#[tokio::test]
async fn test_load_clientconfig() {
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("".to_string()));

let temp_dir = tempfile::TempDir::new().unwrap();

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

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

assert_eq!(valid_config.is_ok(), true);
}

#[tokio::test]
async fn test_load_clientconfig_serverless() {
rustls::crypto::ring::default_provider()
Expand Down