Skip to content

Commit

Permalink
fix unit tests & simplify creation in Args
Browse files Browse the repository at this point in the history
  • Loading branch information
aspect committed Aug 27, 2023
1 parent 3117ce2 commit c7d2a5a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 28 deletions.
55 changes: 31 additions & 24 deletions consensus/core/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ pub enum NetworkIdError {
#[error("Missing network suffix: '{0}'")]
MissingNetworkSuffix(String),

#[error("Network suffix required for network type: '{0}'")]
NetworkSuffixRequired(String),

#[error("Invalid network id: '{0}'")]
InvalidNetworkId(String),
}
Expand All @@ -165,9 +168,21 @@ pub struct NetworkId {

impl NetworkId {
pub const fn new(network_type: NetworkType) -> Self {
if !matches!(network_type, NetworkType::Mainnet | NetworkType::Devnet | NetworkType::Simnet) {
panic!("network suffix required for this network type");
}

Self { network_type, suffix: None }
}

pub fn try_new(network_type: NetworkType) -> Result<Self, NetworkIdError> {
if !matches!(network_type, NetworkType::Mainnet | NetworkType::Devnet | NetworkType::Simnet) {
return Err(NetworkIdError::NetworkSuffixRequired(network_type.to_string()));
}

Ok(Self { network_type, suffix: None })
}

pub const fn with_suffix(network_type: NetworkType, suffix: u32) -> Self {
Self { network_type, suffix: Some(suffix) }
}
Expand Down Expand Up @@ -229,9 +244,10 @@ impl Deref for NetworkId {
}
}

impl From<NetworkType> for NetworkId {
fn from(value: NetworkType) -> Self {
Self::new(value)
impl TryFrom<NetworkType> for NetworkId {
type Error = NetworkIdError;
fn try_from(value: NetworkType) -> Result<Self, Self::Error> {
Self::try_new(value)
}
}

Expand All @@ -256,7 +272,7 @@ impl FromStr for NetworkId {
// disallow network types without suffix (other than mainnet)
// lack of suffix makes it impossible to distinguish between
// multiple testnet networks
if !matches!(network_type, NetworkType::Mainnet) && suffix.is_none() {
if !matches!(network_type, NetworkType::Mainnet | NetworkType::Devnet | NetworkType::Simnet) && suffix.is_none() {
return Err(NetworkIdError::MissingNetworkSuffix(network_name.to_string()));
}
match parts.next() {
Expand Down Expand Up @@ -354,11 +370,6 @@ impl TryFrom<&JsValue> for NetworkId {
}
}

// #[wasm_bindgen(js_name = "validateNetworkId")]
// pub fn validate_network_id_str(network_id: &str) -> Result<(), JsValue> {
// NetworkId::from_str(network_id).map(|_| ()).map_err(|err| JsValue::from_str(&err.to_string()))
// }

pub mod wasm {
use super::*;

Expand Down Expand Up @@ -399,10 +410,12 @@ mod tests {
#[test]
fn test_network_id_parse_roundtrip() {
for nt in NetworkType::iter() {
let ni = NetworkId::from(nt);
if matches!(nt, NetworkType::Mainnet | NetworkType::Devnet | NetworkType::Simnet) {
let ni = NetworkId::try_from(nt).expect("failed to create network id");
assert_eq!(nt, *NetworkId::from_str(ni.to_string().as_str()).unwrap());
assert_eq!(ni, NetworkId::from_str(ni.to_string().as_str()).unwrap());
}
let nis = NetworkId::with_suffix(nt, 1);
assert_eq!(nt, *NetworkId::from_str(ni.to_string().as_str()).unwrap());
assert_eq!(ni, NetworkId::from_str(ni.to_string().as_str()).unwrap());
assert_eq!(nt, *NetworkId::from_str(nis.to_string().as_str()).unwrap());
assert_eq!(nis, NetworkId::from_str(nis.to_string().as_str()).unwrap());

Expand All @@ -419,24 +432,18 @@ mod tests {
}

let tests = vec![
Test { name: "Valid mainnet", expr: "kaspa-mainnet", expected: Ok(NetworkId::new(NetworkType::Mainnet)) },
Test { name: "Valid testnet", expr: "kaspa-testnet-88", expected: Ok(NetworkId::with_suffix(NetworkType::Testnet, 88)) },
Test { name: "Missing prefix", expr: "testnet", expected: Err(NetworkIdError::InvalidPrefix("testnet".to_string())) },
Test { name: "Invalid prefix", expr: "K-testnet", expected: Err(NetworkIdError::InvalidPrefix("K".to_string())) },
Test {
name: "Missing network",
expr: "kaspa-",
expected: Err(NetworkTypeError::InvalidNetworkType("".to_string()).into()),
},
Test { name: "Valid mainnet", expr: "mainnet", expected: Ok(NetworkId::new(NetworkType::Mainnet)) },
Test { name: "Valid testnet", expr: "testnet-88", expected: Ok(NetworkId::with_suffix(NetworkType::Testnet, 88)) },
Test { name: "Missing network", expr: "", expected: Err(NetworkTypeError::InvalidNetworkType("".to_string()).into()) },
Test {
name: "Invalid network",
expr: "kaspa-gamenet",
expr: "gamenet",
expected: Err(NetworkTypeError::InvalidNetworkType("gamenet".to_string()).into()),
},
Test { name: "Invalid suffix", expr: "kaspa-testnet-x", expected: Err(NetworkIdError::InvalidSuffix("x".to_string())) },
Test { name: "Invalid suffix", expr: "testnet-x", expected: Err(NetworkIdError::InvalidSuffix("x".to_string())) },
Test {
name: "Unexpected extra token",
expr: "kaspa-testnet-10-x",
expr: "testnet-10-x",
expected: Err(NetworkIdError::UnexpectedExtraToken("x".to_string())),
},
];
Expand Down
6 changes: 3 additions & 3 deletions kaspad/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ impl Args {

pub fn network(&self) -> NetworkId {
match (self.testnet, self.devnet, self.simnet) {
(false, false, false) => NetworkType::Mainnet.into(),
(false, false, false) => NetworkId::new(NetworkType::Mainnet),
(true, false, false) => NetworkId::with_suffix(NetworkType::Testnet, self.testnet_suffix),
(false, true, false) => NetworkType::Devnet.into(),
(false, false, true) => NetworkType::Simnet.into(),
(false, true, false) => NetworkId::new(NetworkType::Devnet),
(false, false, true) => NetworkId::new(NetworkType::Simnet),
_ => panic!("only a single net should be activated"),
}
}
Expand Down
2 changes: 1 addition & 1 deletion wallet/bip32/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ pub enum Error {

#[error("Poison error -> {0:?}")]
PoisonError(String),

#[error(transparent)]
WorkflowWasm(#[from] workflow_wasm::error::Error),
}
Expand Down

0 comments on commit c7d2a5a

Please sign in to comment.