From 46a4991c12027f1e941dbcf5eac9c8268e2e017e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20H=C3=A4ggblad?= Date: Tue, 5 Apr 2022 13:08:19 +0200 Subject: [PATCH] wallet: add version number to network config files --- nym-wallet/src-tauri/src/config/mod.rs | 45 ++++++++++++++++++++------ 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/nym-wallet/src-tauri/src/config/mod.rs b/nym-wallet/src-tauri/src/config/mod.rs index c896b027fd..71012940f6 100644 --- a/nym-wallet/src-tauri/src/config/mod.rs +++ b/nym-wallet/src-tauri/src/config/mod.rs @@ -19,6 +19,9 @@ use url::Url; pub const REMOTE_SOURCE_OF_VALIDATOR_URLS: &str = "https://nymtech.net/.wellknown/wallet/validators.json"; +const CURRENT_GLOBAL_CONFIG_VERSION: u32 = 1; +const CURRENT_NETWORK_CONFIG_VERSION: u32 = 1; + #[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct Config { // Base configuration is not part of the configuration file as it's not intended to be changed. @@ -37,13 +40,16 @@ struct Base { networks: SupportedNetworks, } -#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq)] +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] pub struct GlobalConfig { + version: u32, // TODO: there are no global settings (yet) } -#[derive(Debug, Default, Deserialize, Serialize, Clone, PartialEq, Eq)] +#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq)] pub struct NetworkConfig { + version: u32, + // User selected urls selected_nymd_url: Option, selected_api_url: Option, @@ -53,12 +59,6 @@ pub struct NetworkConfig { validator_urls: Option>, } -impl NetworkConfig { - fn validators(&self) -> impl Iterator { - self.validator_urls.iter().flat_map(|v| v.iter()) - } -} - impl Default for Base { fn default() -> Self { let networks = WalletNetwork::iter().map(Into::into).collect(); @@ -68,6 +68,31 @@ impl Default for Base { } } +impl Default for GlobalConfig { + fn default() -> Self { + Self { + version: CURRENT_GLOBAL_CONFIG_VERSION, + } + } +} + +impl Default for NetworkConfig { + fn default() -> Self { + Self { + version: CURRENT_NETWORK_CONFIG_VERSION, + selected_nymd_url: None, + selected_api_url: None, + validator_urls: None, + } + } +} + +impl NetworkConfig { + fn validators(&self) -> impl Iterator { + self.validator_urls.iter().flat_map(|v| v.iter()) + } +} + impl Config { fn root_directory() -> PathBuf { tauri::api::path::config_dir().expect("Failed to get config directory") @@ -409,6 +434,7 @@ mod tests { api_url: Some("https://baz/api".parse().unwrap()), }, ]), + ..NetworkConfig::default() }; Config { @@ -426,7 +452,8 @@ mod tests { let netconfig = &config.networks[&WalletNetwork::MAINNET.as_key()]; assert_eq!( toml::to_string_pretty(netconfig).unwrap(), - r#"selected_api_url = 'https://my_api_url.com/' + r#"version = 1 +selected_api_url = 'https://my_api_url.com/' [[validator_urls]] nymd_url = 'https://foo/'