diff --git a/config/src/comments.rs b/config/src/comments.rs index 5cfbf0dc6..62d0884d8 100644 --- a/config/src/comments.rs +++ b/config/src/comments.rs @@ -19,6 +19,14 @@ use std::collections::HashMap; fn comments() -> HashMap { let mut retval = HashMap::new(); + retval.insert( + "[config_file_version]".to_string(), + " +#Generated Configuration File for Grin Wallet +" + .to_string(), + ); + retval.insert( "[wallet]".to_string(), " @@ -217,7 +225,7 @@ fn comments() -> HashMap { retval.insert( "bridge_line".to_string(), " -#TOR Bridge relays: Allows people to send and receive Grin in a country where TOR is censored. +#TOR Bridge relays: Allow to send and receive via TOR in a country where it is censored. #Enable it by entering a single bridge line. To disable it, you must let it empty. #Bridge line must be in the following format: `obfs4 [IP:PORT] [FINGERPRINT] cert=[CERT] iat-mode=[IAT-MODE]` " diff --git a/config/src/config.rs b/config/src/config.rs index 20908880a..0851a0352 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -21,7 +21,6 @@ use std::env; use std::fs::{self, File}; use std::io::prelude::*; use std::io::BufReader; -use std::io::Read; use std::path::PathBuf; use toml; @@ -187,6 +186,7 @@ pub fn initial_setup_wallet( impl Default for GlobalWalletConfigMembers { fn default() -> GlobalWalletConfigMembers { GlobalWalletConfigMembers { + config_file_version: Some(2), logging: Some(LoggingConfig::default()), tor: Some(TorConfig::default()), wallet: WalletConfig::default(), @@ -245,10 +245,13 @@ impl GlobalWalletConfig { /// Read config fn read_config(mut self) -> Result { - let mut file = File::open(self.config_file_path.as_mut().unwrap())?; - let mut contents = String::new(); - file.read_to_string(&mut contents)?; - let fixed = GlobalWalletConfig::fix_warning_level(contents); + let config_file_path = self.config_file_path.as_mut().unwrap(); + let contents = fs::read_to_string(config_file_path.clone())?; + let migrated = GlobalWalletConfig::migrate_config_file_version_none_to_2( + contents, + config_file_path.to_owned(), + )?; + let fixed = GlobalWalletConfig::fix_warning_level(migrated); let decoded: Result = toml::from_str(&fixed); match decoded { Ok(gc) => { @@ -314,6 +317,31 @@ impl GlobalWalletConfig { file.write_all(commented_config.as_bytes())?; Ok(()) } + /// This migration does the following: + /// - Adds "config_file_version = 2" + /// - Adds bridge_line = "" + fn migrate_config_file_version_none_to_2( + config_str: String, + config_file_path: PathBuf, + ) -> Result { + let config: GlobalWalletConfigMembers = + toml::from_str(&GlobalWalletConfig::fix_warning_level(config_str.clone())).unwrap(); + if config.config_file_version != None { + return Ok(config_str); + }; + let adjusted_config = GlobalWalletConfigMembers { + config_file_version: GlobalWalletConfigMembers::default().config_file_version, + ..config + }; + let mut gc = GlobalWalletConfig { + members: Some(adjusted_config), + config_file_path: Some(config_file_path.clone()), + }; + let str_path = config_file_path.into_os_string().into_string().unwrap(); + gc.write_to_file(&str_path)?; + let adjusted_config_str = fs::read_to_string(str_path.clone())?; + Ok(adjusted_config_str) + } // For forwards compatibility old config needs `Warning` log level changed to standard log::Level `WARN` fn fix_warning_level(conf: String) -> String { diff --git a/config/src/types.rs b/config/src/types.rs index 213ab9f27..71b2cce49 100644 --- a/config/src/types.rs +++ b/config/src/types.rs @@ -165,6 +165,7 @@ pub struct TorConfig { /// Send configuration directory pub send_config_dir: String, /// Send configuration directory + #[serde(default)] pub bridge_line: String, } @@ -200,6 +201,9 @@ pub struct GlobalWalletConfig { /// Wallet internal members #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct GlobalWalletConfigMembers { + /// Config file version (None == version 1) + #[serde(default)] + pub config_file_version: Option, /// Wallet configuration #[serde(default)] pub wallet: WalletConfig, diff --git a/impls/src/lifecycle/default.rs b/impls/src/lifecycle/default.rs index 0f71cbcee..d10f5658f 100644 --- a/impls/src/lifecycle/default.rs +++ b/impls/src/lifecycle/default.rs @@ -79,6 +79,10 @@ where tor_config: Option, ) -> Result<(), Error> { let mut default_config = GlobalWalletConfig::for_chain(&chain_type); + let config_file_version = match default_config.members.as_ref() { + Some(m) => m.clone().config_file_version, + None => None, + }; let logging = match logging_config { Some(l) => Some(l), None => match default_config.members.as_ref() { @@ -102,6 +106,7 @@ where }; default_config = GlobalWalletConfig { members: Some(GlobalWalletConfigMembers { + config_file_version, wallet, tor, logging, diff --git a/src/bin/grin-wallet.yml b/src/bin/grin-wallet.yml index 2ad4a2689..a069703a3 100644 --- a/src/bin/grin-wallet.yml +++ b/src/bin/grin-wallet.yml @@ -72,7 +72,7 @@ subcommands: takes_value: false - bridge: help: Enable bridge relay with TOR listener - short: b + short: g long: bridge takes_value: true - owner_api: @@ -156,7 +156,7 @@ subcommands: takes_value: true - bridge: help: Enable bridge relay when sending via Slatepack workflow - short: b + short: g long: bridge takes_value: true - unpack: