migrate to v2

- lots to check and do
This commit is contained in:
Tommy Verrall
2025-04-01 17:06:21 +02:00
parent 400aa6ba6d
commit 4e9f1bc0ed
2 changed files with 1949 additions and 1935 deletions
+139 -133
View File
@@ -11,6 +11,7 @@ use nym_validator_client::nyxd::AccountId as CosmosAccountId;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use strum::IntoEnumIterator;
use tauri::Manager;
use url::Url;
use nym_config::defaults::{DenomDetailsOwned, NymNetworkDetails, ValidatorDetails};
@@ -104,32 +105,35 @@ impl NetworkConfig {
}
impl Config {
fn root_directory() -> PathBuf {
tauri::api::path::config_dir().expect("Failed to get config directory")
fn root_directory(app_handle: &tauri::AppHandle) -> PathBuf {
app_handle
.path()
.local_data_dir()
.expect("Failed to get config directory")
}
fn config_directory() -> PathBuf {
Self::root_directory().join(CONFIG_DIR_NAME)
fn config_directory(app_handle: &tauri::AppHandle) -> PathBuf {
Self::root_directory(app_handle).join(CONFIG_DIR_NAME)
}
fn config_file_path(network: Option<WalletNetwork>) -> PathBuf {
fn config_file_path(app_handle: &tauri::AppHandle, network: Option<WalletNetwork>) -> PathBuf {
if let Some(network) = network {
let network_filename = format!("{}.toml", network.as_key());
Self::config_directory().join(network_filename)
Self::config_directory(app_handle).join(network_filename)
} else {
Self::config_directory().join(CONFIG_FILENAME)
Self::config_directory(app_handle).join(CONFIG_FILENAME)
}
}
pub fn save_to_files(&self) -> io::Result<()> {
pub fn save_to_files(&self, app_handle: &tauri::AppHandle) -> io::Result<()> {
log::trace!("Config::save_to_file");
// Make sure the whole directory structure actually exists
fs::create_dir_all(Self::config_directory())?;
fs::create_dir_all(Self::config_directory(app_handle))?;
// Global config
if let Some(global) = &self.global {
let location = Self::config_file_path(None);
let location = Self::config_file_path(app_handle, None);
match toml::to_string_pretty(&global)
.map_err(|toml_err| io::Error::new(io::ErrorKind::Other, toml_err))
@@ -150,7 +154,7 @@ impl Config {
}
};
let location = Self::config_file_path(Some(network));
let location = Self::config_file_path(app_handle, Some(network));
match toml::to_string_pretty(config)
.map_err(|toml_err| io::Error::new(io::ErrorKind::Other, toml_err))
.map(|toml| fs::write(location.clone(), toml))
@@ -162,10 +166,10 @@ impl Config {
Ok(())
}
pub fn load_from_files() -> Self {
pub fn load_from_files(app_handle: &tauri::AppHandle) -> Self {
// Global
let global = {
let file = Self::config_file_path(None);
let file = Self::config_file_path(app_handle, None);
match load_from_file::<GlobalConfig>(file.clone()) {
Ok(global) => {
log::debug!("Loaded from file {:#?}", file);
@@ -181,7 +185,7 @@ impl Config {
// One file per network
let mut networks = HashMap::new();
for network in WalletNetwork::iter() {
let file = Self::config_file_path(Some(network));
let file = Self::config_file_path(app_handle, Some(network));
match load_from_file::<NetworkConfig>(file.clone()) {
Ok(config) => {
log::trace!("Loaded from file {:#?}", file);
@@ -261,6 +265,8 @@ impl Config {
}
}
////////
pub fn select_nyxd_url(&mut self, nyxd_url: Url, network: WalletNetwork) {
if let Some(net) = self.networks.get_mut(&network.as_key()) {
net.selected_nyxd_url = Some(nyxd_url);
@@ -531,139 +537,139 @@ impl From<NymNetworkDetails> for NetworkDetails {
}
}
#[cfg(test)]
mod tests {
use super::*;
// #[cfg(test)]
// mod tests {
// use super::*;
fn test_config() -> Config {
let netconfig = NetworkConfig {
selected_nyxd_url: None,
selected_api_url: Some("https://my_api_url.com".parse().unwrap()),
// fn test_config() -> Config {
// let netconfig = NetworkConfig {
// selected_nyxd_url: None,
// selected_api_url: Some("https://my_api_url.com".parse().unwrap()),
nyxd_urls: Some(vec![
ValidatorConfigEntry {
nyxd_url: "https://foo".parse().unwrap(),
nyxd_name: Some("FooName".to_string()),
api_url: None,
},
ValidatorConfigEntry {
nyxd_url: "https://bar".parse().unwrap(),
nyxd_name: None,
api_url: Some("https://bar/api".parse().unwrap()),
},
ValidatorConfigEntry {
nyxd_url: "https://baz".parse().unwrap(),
nyxd_name: None,
api_url: Some("https://baz/api".parse().unwrap()),
},
]),
..NetworkConfig::default()
};
// nyxd_urls: Some(vec![
// ValidatorConfigEntry {
// nyxd_url: "https://foo".parse().unwrap(),
// nyxd_name: Some("FooName".to_string()),
// api_url: None,
// },
// ValidatorConfigEntry {
// nyxd_url: "https://bar".parse().unwrap(),
// nyxd_name: None,
// api_url: Some("https://bar/api".parse().unwrap()),
// },
// ValidatorConfigEntry {
// nyxd_url: "https://baz".parse().unwrap(),
// nyxd_name: None,
// api_url: Some("https://baz/api".parse().unwrap()),
// },
// ]),
// ..NetworkConfig::default()
// };
Config {
base: Base::default(),
global: Some(GlobalConfig::default()),
networks: [(WalletNetwork::MAINNET.as_key(), netconfig)]
.into_iter()
.collect(),
}
}
// Config {
// base: Base::default(),
// global: Some(GlobalConfig::default()),
// networks: [(WalletNetwork::MAINNET.as_key(), netconfig)]
// .into_iter()
// .collect(),
// }
// }
#[test]
fn serialize_to_toml() {
let config = test_config();
let netconfig = &config.networks[&WalletNetwork::MAINNET.as_key()];
assert_eq!(
toml::to_string_pretty(netconfig).unwrap(),
r#"version = 1
selected_api_url = 'https://my_api_url.com/'
// #[test]
// fn serialize_to_toml() {
// let config = test_config();
// let netconfig = &config.networks[&WalletNetwork::MAINNET.as_key()];
// assert_eq!(
// toml::to_string_pretty(netconfig).unwrap(),
// r#"version = 1
// selected_api_url = 'https://my_api_url.com/'
[[nyxd_urls]]
nyxd_url = 'https://foo/'
nyxd_name = 'FooName'
// [[nyxd_urls]]
// nyxd_url = 'https://foo/'
// nyxd_name = 'FooName'
[[nyxd_urls]]
nyxd_url = 'https://bar/'
api_url = 'https://bar/api'
// [[nyxd_urls]]
// nyxd_url = 'https://bar/'
// api_url = 'https://bar/api'
[[nyxd_urls]]
nyxd_url = 'https://baz/'
api_url = 'https://baz/api'
"#
);
}
// [[nyxd_urls]]
// nyxd_url = 'https://baz/'
// api_url = 'https://baz/api'
// "#
// );
// }
#[test]
fn serialize_to_json() {
let config = test_config();
let netconfig = &config.networks[&WalletNetwork::MAINNET.as_key()];
println!("{}", serde_json::to_string_pretty(netconfig).unwrap());
assert_eq!(
serde_json::to_string_pretty(netconfig).unwrap(),
r#"{
"version": 1,
"selected_nyxd_url": null,
"default_nyxd_url": null,
"selected_api_url": "https://my_api_url.com/",
"nyxd_urls": [
{
"nyxd_url": "https://foo/",
"nyxd_name": "FooName",
"api_url": null
},
{
"nyxd_url": "https://bar/",
"nyxd_name": null,
"api_url": "https://bar/api"
},
{
"nyxd_url": "https://baz/",
"nyxd_name": null,
"api_url": "https://baz/api"
}
]
}"#
);
}
// #[test]
// fn serialize_to_json() {
// let config = test_config();
// let netconfig = &config.networks[&WalletNetwork::MAINNET.as_key()];
// println!("{}", serde_json::to_string_pretty(netconfig).unwrap());
// assert_eq!(
// serde_json::to_string_pretty(netconfig).unwrap(),
// r#"{
// "version": 1,
// "selected_nyxd_url": null,
// "default_nyxd_url": null,
// "selected_api_url": "https://my_api_url.com/",
// "nyxd_urls": [
// {
// "nyxd_url": "https://foo/",
// "nyxd_name": "FooName",
// "api_url": null
// },
// {
// "nyxd_url": "https://bar/",
// "nyxd_name": null,
// "api_url": "https://bar/api"
// },
// {
// "nyxd_url": "https://baz/",
// "nyxd_name": null,
// "api_url": "https://baz/api"
// }
// ]
// }"#
// );
// }
#[test]
fn serialize_and_deserialize_to_toml() {
let config = test_config();
let netconfig = &config.networks[&WalletNetwork::MAINNET.as_key()];
let config_str = toml::to_string_pretty(netconfig).unwrap();
let config_from_toml: NetworkConfig = toml::from_str(&config_str).unwrap();
assert_eq!(netconfig, &config_from_toml);
}
// #[test]
// fn serialize_and_deserialize_to_toml() {
// let config = test_config();
// let netconfig = &config.networks[&WalletNetwork::MAINNET.as_key()];
// let config_str = toml::to_string_pretty(netconfig).unwrap();
// let config_from_toml: NetworkConfig = toml::from_str(&config_str).unwrap();
// assert_eq!(netconfig, &config_from_toml);
// }
#[test]
fn get_urls_parsed_from_config() {
let config = test_config();
// #[test]
// fn get_urls_parsed_from_config() {
// let config = test_config();
let nyxd_url = config
.get_configured_validators(WalletNetwork::MAINNET)
.next()
.map(|v| v.nyxd_url)
.unwrap();
assert_eq!(nyxd_url.as_ref(), "https://foo/");
// let nyxd_url = config
// .get_configured_validators(WalletNetwork::MAINNET)
// .next()
// .map(|v| v.nyxd_url)
// .unwrap();
// assert_eq!(nyxd_url.as_ref(), "https://foo/");
// The first entry is missing an API URL
let api_url = config
.get_configured_validators(WalletNetwork::MAINNET)
.next()
.and_then(|v| v.api_url);
assert_eq!(api_url, None);
}
// // The first entry is missing an API URL
// let api_url = config
// .get_configured_validators(WalletNetwork::MAINNET)
// .next()
// .and_then(|v| v.api_url);
// assert_eq!(api_url, None);
// }
#[test]
fn get_urls_from_defaults() {
let config = Config::default();
// #[test]
// fn get_urls_from_defaults() {
// let config = Config::default();
let nyxd_url = config
.get_base_validators(WalletNetwork::MAINNET)
.next()
.map(|v| v.nyxd_url)
.unwrap();
assert_eq!(nyxd_url.as_ref(), "https://rpc.nymtech.net/");
// let nyxd_url = config
// .get_base_validators(WalletNetwork::MAINNET)
// .next()
// .map(|v| v.nyxd_url)
// .unwrap();
// assert_eq!(nyxd_url.as_ref(), "https://rpc.nymtech.net/");
let api_url = config
.get_base_validators(WalletNetwork::MAINNET)
File diff suppressed because it is too large Load Diff