Skip to content

Commit

Permalink
migration config_file_version=2
Browse files Browse the repository at this point in the history
  • Loading branch information
deevope committed May 19, 2021
1 parent c8d1a5d commit a2f2a92
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 8 deletions.
10 changes: 9 additions & 1 deletion config/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ use std::collections::HashMap;
fn comments() -> HashMap<String, String> {
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(),
"
Expand Down Expand Up @@ -217,7 +225,7 @@ fn comments() -> HashMap<String, String> {
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]`
"
Expand Down
38 changes: 33 additions & 5 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -245,10 +245,13 @@ impl GlobalWalletConfig {

/// Read config
fn read_config(mut self) -> Result<GlobalWalletConfig, ConfigError> {
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<GlobalWalletConfigMembers, toml::de::Error> = toml::from_str(&fixed);
match decoded {
Ok(gc) => {
Expand Down Expand Up @@ -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<String, ConfigError> {
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 {
Expand Down
4 changes: 4 additions & 0 deletions config/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ pub struct TorConfig {
/// Send configuration directory
pub send_config_dir: String,
/// Send configuration directory
#[serde(default)]
pub bridge_line: String,
}

Expand Down Expand Up @@ -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<u32>,
/// Wallet configuration
#[serde(default)]
pub wallet: WalletConfig,
Expand Down
5 changes: 5 additions & 0 deletions impls/src/lifecycle/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ where
tor_config: Option<TorConfig>,
) -> 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() {
Expand All @@ -102,6 +106,7 @@ where
};
default_config = GlobalWalletConfig {
members: Some(GlobalWalletConfigMembers {
config_file_version,
wallet,
tor,
logging,
Expand Down
4 changes: 2 additions & 2 deletions src/bin/grin-wallet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit a2f2a92

Please sign in to comment.