Skip to content

Commit

Permalink
Merge pull request zingolabs#1584 from dorianvp/loadclientconfig_empt…
Browse files Browse the repository at this point in the history
…y_uri

Parse empty uri strings correctly in `load_clientconfig`
  • Loading branch information
dorianvp authored Dec 9, 2024
2 parents b145371 + f690eb3 commit f51c7f3
Showing 1 changed file with 59 additions and 23 deletions.
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

0 comments on commit f51c7f3

Please sign in to comment.