From 9e27e6f9d3e960466b75f666057143c06e2f5cfc Mon Sep 17 00:00:00 2001 From: trevyn Date: Tue, 27 Apr 2021 06:33:56 -0700 Subject: [PATCH] Add migration to `config_file_version = 2` (#3634) * Add config_file_version and migration to version 2 * Generate `config_file_version = 2` as default --- config/src/comments.rs | 11 ++++++--- config/src/config.rs | 55 ++++++++++++++++++++++++++++++++++++++---- config/src/types.rs | 2 ++ src/bin/grin.rs | 6 +---- 4 files changed, 61 insertions(+), 13 deletions(-) diff --git a/config/src/comments.rs b/config/src/comments.rs index d415c4732..be3a58634 100644 --- a/config/src/comments.rs +++ b/config/src/comments.rs @@ -19,7 +19,7 @@ use std::collections::HashMap; fn comments() -> HashMap { let mut retval = HashMap::new(); retval.insert( - "[server]".to_string(), + "config_file_version".to_string(), " # Generated Server Configuration File for Grin # @@ -31,7 +31,13 @@ fn comments() -> HashMap { # -[user home]/.grin # -######################################### +" + .to_string(), + ); + + retval.insert( + "[server]".to_string(), + "######################################### ### SERVER CONFIGURATION ### ######################################### @@ -323,7 +329,6 @@ fn comments() -> HashMap { "accept_fee_base".to_string(), " #base fee that's accepted into the pool -#a setting to 1000000 will be overridden to 500000 to respect the fixfees RFC " .to_string(), ); diff --git a/config/src/config.rs b/config/src/config.rs index 34c0e3572..314573c59 100644 --- a/config/src/config.rs +++ b/config/src/config.rs @@ -20,7 +20,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 crate::comments::insert_comments; @@ -141,6 +140,7 @@ pub fn initial_setup_server(chain_type: &global::ChainTypes) -> Result ConfigMembers { ConfigMembers { + config_file_version: Some(2), server: ServerConfig::default(), logging: Some(LoggingConfig::default()), } @@ -222,10 +222,14 @@ impl GlobalConfig { /// 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 = GlobalConfig::fix_warning_level(contents); + let config_file_path = self.config_file_path.as_ref().unwrap(); + let contents = fs::read_to_string(config_file_path)?; + let migrated = GlobalConfig::migrate_config_file_version_none_to_2(contents.clone()); + if contents != migrated { + fs::write(config_file_path, &migrated)?; + } + + let fixed = GlobalConfig::fix_warning_level(migrated); let decoded: Result = toml::from_str(&fixed); match decoded { Ok(gc) => { @@ -305,6 +309,47 @@ impl GlobalConfig { Ok(()) } + /// This migration does the following: + /// - Adds "config_file_version = 2" + /// - If server.pool_config.accept_fee_base is 1000000, change it to 500000 + /// - Remove "#a setting to 1000000 will be overridden to 500000 to respect the fixfees RFC" + fn migrate_config_file_version_none_to_2(config_str: String) -> String { + // Parse existing config and return unchanged if not eligible for migration + + let mut config: ConfigMembers = + toml::from_str(&GlobalConfig::fix_warning_level(config_str.clone())).unwrap(); + if config.config_file_version != None { + return config_str; + } + + // Apply changes both textually and structurally + + let config_str = config_str.replace("\n#########################################\n### SERVER CONFIGURATION ###", "\nconfig_file_version = 2\n\n#########################################\n### SERVER CONFIGURATION ###"); + config.config_file_version = Some(2); + + let config_str = config_str.replace( + "\naccept_fee_base = 1000000\n", + "\naccept_fee_base = 500000\n", + ); + if config.server.pool_config.accept_fee_base == 1000000 { + config.server.pool_config.accept_fee_base = 500000; + } + + let config_str = config_str.replace( + "\n#a setting to 1000000 will be overridden to 500000 to respect the fixfees RFC\n", + "\n", + ); + + // Verify equivalence + + assert_eq!( + config, + toml::from_str(&GlobalConfig::fix_warning_level(config_str.clone())).unwrap() + ); + + config_str + } + // For forwards compatibility old config needs `Warning` log level changed to standard log::Level `WARN` fn fix_warning_level(conf: String) -> String { conf.replace("Warning", "WARN") diff --git a/config/src/types.rs b/config/src/types.rs index 82cd1440d..2b71d99ce 100644 --- a/config/src/types.rs +++ b/config/src/types.rs @@ -88,6 +88,8 @@ pub struct GlobalConfig { /// want serialised or deserialised #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] pub struct ConfigMembers { + /// Config file version (None == version 1) + pub config_file_version: Option, /// Server config #[serde(default)] pub server: ServerConfig, diff --git a/src/bin/grin.rs b/src/bin/grin.rs index c5110f822..84ad6d58b 100644 --- a/src/bin/grin.rs +++ b/src/bin/grin.rs @@ -167,11 +167,7 @@ fn real_main() -> i32 { .server .pool_config .accept_fee_base; - let fix_afb = match afb { - 1_000_000 => 500_000, - _ => afb, - }; - global::init_global_accept_fee_base(fix_afb); + global::init_global_accept_fee_base(afb); info!("Accept Fee Base: {:?}", global::get_accept_fee_base()); global::init_global_future_time_limit(config.members.unwrap().server.future_time_limit); info!("Future Time Limit: {:?}", global::get_future_time_limit());