From f9de24db3a9e2121483553bb9d7265285e7bf4ab Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Thu, 26 Mar 2026 19:56:00 +0000 Subject: [PATCH 1/2] Enable matching on the 'main' or 'test' strings that get_lightd_info returns from the indexer. --- zingolib/src/config.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zingolib/src/config.rs b/zingolib/src/config.rs index f60389b80..a260d4196 100644 --- a/zingolib/src/config.rs +++ b/zingolib/src/config.rs @@ -54,8 +54,8 @@ impl TryFrom<&str> for ChainType { fn try_from(value: &str) -> Result { match value { - "mainnet" => Ok(ChainType::Mainnet), - "testnet" => Ok(ChainType::Testnet), + "mainnet" | "main" => Ok(ChainType::Mainnet), + "testnet" | "test" => Ok(ChainType::Testnet), "regtest" => Ok(ChainType::Regtest(ActivationHeights::default())), _ => Err(InvalidChainType(value.to_string())), } From 5e2183342f8ca2b31ff4bd8cd9b21ebb2f08cd63 Mon Sep 17 00:00:00 2001 From: fluidvanadium Date: Thu, 26 Mar 2026 20:17:13 +0000 Subject: [PATCH 2/2] Add unit tests of chaintype strings (thx Claude). --- zingolib/src/config.rs | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/zingolib/src/config.rs b/zingolib/src/config.rs index a260d4196..bb1cfc770 100644 --- a/zingolib/src/config.rs +++ b/zingolib/src/config.rs @@ -49,6 +49,11 @@ impl std::fmt::Display for ChainType { } } +/// Invalid chain type. +#[derive(thiserror::Error, Debug)] +#[error("Invalid chain type '{0}'. Expected one of: 'mainnet', 'testnet' or 'regtest'.")] +pub struct InvalidChainType(String); + impl TryFrom<&str> for ChainType { type Error = InvalidChainType; @@ -107,11 +112,6 @@ pub(crate) mod consealed { } } -/// Invalid chain type. -#[derive(thiserror::Error, Debug)] -#[error("Invalid chain type '{0}'. Expected one of: 'mainnet', 'testnet' or 'regtest'.")] -pub struct InvalidChainType(String); - /// Configuration data for the construction of a [`crate::wallet::LightWallet`]. #[derive(Clone, Debug, PartialEq, Eq)] pub enum WalletConfig { @@ -462,7 +462,9 @@ fn wallet_dir_or_default(opt_wallet_dir: Option, chain: ChainType) -> P #[cfg(test)] mod tests { - use crate::config::{ChainType, ClientConfig}; + use crate::config::ChainType; + use crate::config::ClientConfig; + use zingo_common_components::protocol::ActivationHeights; #[tokio::test] async fn test_load_clientconfig() { @@ -483,4 +485,30 @@ mod tests { assert_eq!(valid_config.indexer_uri(), valid_uri); assert_eq!(valid_config.chain_type(), ChainType::Mainnet); } + + #[test] + fn test_chaintypes_from_strings() { + assert_eq!(ChainType::try_from("mainnet").unwrap(), ChainType::Mainnet); + assert_eq!(ChainType::try_from("testnet").unwrap(), ChainType::Testnet); + // the following 2 variants match the string from get_lightd_info, from the LightdInfo struct + // as referenced in librustzcash/zcash_client_backend/src/proto/service.rs 17a4830c70 + // and provided by an indexer (previously called lightwallet) + assert_eq!(ChainType::try_from("main").unwrap(), ChainType::Mainnet); + assert_eq!(ChainType::try_from("test").unwrap(), ChainType::Testnet); + assert_eq!( + ChainType::try_from("regtest").unwrap(), + ChainType::Regtest(ActivationHeights::default()) + ); + assert!(ChainType::try_from("invalid").is_err()); + let err = ChainType::try_from("badvalue").unwrap_err(); + assert_eq!(err.0, "badvalue"); + assert!(ChainType::try_from("").is_err()); + assert!(ChainType::try_from("Mainnet").is_err()); + assert!(ChainType::try_from("MAINNET").is_err()); + assert!(ChainType::try_from("Testnet").is_err()); + assert!(ChainType::try_from("Regtest").is_err()); + assert!(ChainType::try_from("mainnet ").is_err()); // trailing space + assert!(ChainType::try_from(" mainnet").is_err()); // leading space + assert!(ChainType::try_from("regtест").is_err()); // lookalike chars + } }