Skip to content
Open
Changes from 2 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
44 changes: 36 additions & 8 deletions zingolib/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,18 @@ 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);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

i dont understand why this has moved in between the ChainType impls? please can we keep the impls together


impl TryFrom<&str> for ChainType {
type Error = InvalidChainType;

fn try_from(value: &str) -> Result<Self, Self::Error> {
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())),
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -462,7 +462,9 @@ fn wallet_dir_or_default(opt_wallet_dir: Option<PathBuf>, 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;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this the type we prefer for this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

IDK, this PR didnt change that aspect, it only is imported here to complete a unit test.


#[tokio::test]
async fn test_load_clientconfig() {
Expand All @@ -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
}
}
Loading