Add migration to config_file_version = 2 (#3634)

* Add config_file_version and migration to version 2

* Generate `config_file_version = 2` as default
This commit is contained in:
trevyn 2021-04-27 06:33:56 -07:00 committed by GitHub
parent 9ed0cd65ef
commit 9e27e6f9d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 61 additions and 13 deletions

View file

@ -19,7 +19,7 @@ use std::collections::HashMap;
fn comments() -> HashMap<String, String> { fn comments() -> HashMap<String, String> {
let mut retval = HashMap::new(); let mut retval = HashMap::new();
retval.insert( retval.insert(
"[server]".to_string(), "config_file_version".to_string(),
" "
# Generated Server Configuration File for Grin # Generated Server Configuration File for Grin
# #
@ -31,7 +31,13 @@ fn comments() -> HashMap<String, String> {
# -[user home]/.grin # -[user home]/.grin
# #
######################################### "
.to_string(),
);
retval.insert(
"[server]".to_string(),
"#########################################
### SERVER CONFIGURATION ### ### SERVER CONFIGURATION ###
######################################### #########################################
@ -323,7 +329,6 @@ fn comments() -> HashMap<String, String> {
"accept_fee_base".to_string(), "accept_fee_base".to_string(),
" "
#base fee that's accepted into the pool #base fee that's accepted into the pool
#a setting to 1000000 will be overridden to 500000 to respect the fixfees RFC
" "
.to_string(), .to_string(),
); );

View file

@ -20,7 +20,6 @@ use std::env;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::prelude::*; use std::io::prelude::*;
use std::io::BufReader; use std::io::BufReader;
use std::io::Read;
use std::path::PathBuf; use std::path::PathBuf;
use crate::comments::insert_comments; use crate::comments::insert_comments;
@ -141,6 +140,7 @@ pub fn initial_setup_server(chain_type: &global::ChainTypes) -> Result<GlobalCon
impl Default for ConfigMembers { impl Default for ConfigMembers {
fn default() -> ConfigMembers { fn default() -> ConfigMembers {
ConfigMembers { ConfigMembers {
config_file_version: Some(2),
server: ServerConfig::default(), server: ServerConfig::default(),
logging: Some(LoggingConfig::default()), logging: Some(LoggingConfig::default()),
} }
@ -222,10 +222,14 @@ impl GlobalConfig {
/// Read config /// Read config
fn read_config(mut self) -> Result<GlobalConfig, ConfigError> { fn read_config(mut self) -> Result<GlobalConfig, ConfigError> {
let mut file = File::open(self.config_file_path.as_mut().unwrap())?; let config_file_path = self.config_file_path.as_ref().unwrap();
let mut contents = String::new(); let contents = fs::read_to_string(config_file_path)?;
file.read_to_string(&mut contents)?; let migrated = GlobalConfig::migrate_config_file_version_none_to_2(contents.clone());
let fixed = GlobalConfig::fix_warning_level(contents); if contents != migrated {
fs::write(config_file_path, &migrated)?;
}
let fixed = GlobalConfig::fix_warning_level(migrated);
let decoded: Result<ConfigMembers, toml::de::Error> = toml::from_str(&fixed); let decoded: Result<ConfigMembers, toml::de::Error> = toml::from_str(&fixed);
match decoded { match decoded {
Ok(gc) => { Ok(gc) => {
@ -305,6 +309,47 @@ impl GlobalConfig {
Ok(()) 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` // For forwards compatibility old config needs `Warning` log level changed to standard log::Level `WARN`
fn fix_warning_level(conf: String) -> String { fn fix_warning_level(conf: String) -> String {
conf.replace("Warning", "WARN") conf.replace("Warning", "WARN")

View file

@ -88,6 +88,8 @@ pub struct GlobalConfig {
/// want serialised or deserialised /// want serialised or deserialised
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] #[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct ConfigMembers { pub struct ConfigMembers {
/// Config file version (None == version 1)
pub config_file_version: Option<u32>,
/// Server config /// Server config
#[serde(default)] #[serde(default)]
pub server: ServerConfig, pub server: ServerConfig,

View file

@ -167,11 +167,7 @@ fn real_main() -> i32 {
.server .server
.pool_config .pool_config
.accept_fee_base; .accept_fee_base;
let fix_afb = match afb { global::init_global_accept_fee_base(afb);
1_000_000 => 500_000,
_ => afb,
};
global::init_global_accept_fee_base(fix_afb);
info!("Accept Fee Base: {:?}", global::get_accept_fee_base()); info!("Accept Fee Base: {:?}", global::get_accept_fee_base());
global::init_global_future_time_limit(config.members.unwrap().server.future_time_limit); global::init_global_future_time_limit(config.members.unwrap().server.future_time_limit);
info!("Future Time Limit: {:?}", global::get_future_time_limit()); info!("Future Time Limit: {:?}", global::get_future_time_limit());