diff --git a/src/config.rs b/src/config.rs index c59b8e2..2968c38 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,7 +10,7 @@ use url::Url; #[serde(rename_all = "kebab-case")] pub enum UrlProvider { Lookahead, - UrlMapping, + Registry, } #[derive(Debug, Deserialize)] @@ -27,31 +27,36 @@ pub struct Lookahead { pub chain_id: u16, #[serde(rename = "relays")] pub relays: Vec, - #[serde(rename = "registry", deserialize_with = "deserialize_registry")] - pub registry: HashMap, + #[serde(rename = "registry", default, deserialize_with = "deserialize_registry")] + pub registry: Option>, #[serde(rename = "url-provider")] pub url_provider: UrlProvider, } -fn deserialize_registry<'de, D>(deserializer: D) -> Result, D::Error> +fn deserialize_registry<'de, D>( + deserializer: D, +) -> Result>, D::Error> where D: Deserializer<'de>, { - let temp_registry: HashMap = HashMap::deserialize(deserializer)?; - let mut registry: HashMap = HashMap::new(); + let temp_registry: Option> = Option::deserialize(deserializer)?; + if let Some(temp_registry) = temp_registry { + let mut registry: HashMap = HashMap::new(); - for (key, value) in temp_registry { - match BlsPublicKey::from_str(key.as_str()) { - Ok(bls_key) => { - registry.insert(bls_key, Url::from_str(&value).unwrap()); - } - Err(_) => { - return Err(serde::de::Error::custom(format!("Failed to convert key: {}", key))); + for (key, value) in temp_registry { + match BlsPublicKey::from_str(key.as_str()) { + Ok(bls_key) => { + registry.insert(bls_key, Url::from_str(&value).unwrap()); + } + Err(_) => { + return Err(serde::de::Error::custom(format!("Failed to convert key: {}", key))); + } } } + Ok(Some(registry)) + } else { + Ok(None) } - - Ok(registry) } impl Config { @@ -88,7 +93,33 @@ mod tests { let expected_lookahead = Lookahead { chain_id: 1, relays: vec!["relay1".to_string(), "relay2".to_string()], - registry: expected_registry, + registry: Some(expected_registry), + url_provider: UrlProvider::Lookahead, + }; + + let _expected_config = Config { + lookahead_providers_relays: vec![expected_lookahead], + beacon_nodes: vec!["node1".to_string(), "node2".to_string()], + }; + + let config: Config = toml::from_str(data).unwrap(); + assert!(matches!(config, _expected_config)); + } + + #[test] + fn test_deserialize_config_no_lookahead_registry() { + let data = r#" + beacon-nodes = ["node1", "node2"] + [[lookahead]] + chain-id = 1 + url-provider = "lookahead" + relays = ["relay1", "relay2"] + "#; + + let expected_lookahead = Lookahead { + chain_id: 1, + relays: vec!["relay1".to_string(), "relay2".to_string()], + registry: None, url_provider: UrlProvider::Lookahead, }; diff --git a/src/lookahead/manager.rs b/src/lookahead/manager.rs index 0ff0138..1dc9978 100644 --- a/src/lookahead/manager.rs +++ b/src/lookahead/manager.rs @@ -116,7 +116,9 @@ pub fn lookahead_managers_from_config( .build_relay_provider(); let url_provider = match r_c.url_provider { crate::config::UrlProvider::Lookahead => UrlProvider::LookaheadEntry, - crate::config::UrlProvider::UrlMapping => UrlProvider::UrlMap(r_c.registry), + crate::config::UrlProvider::Registry => { + UrlProvider::UrlMap(r_c.registry.expect("registry is empty")) + } }; map.insert(r_c.chain_id, LookaheadManager::new(lookahead, provider, url_provider)); }