Skip to content

Commit

Permalink
Add migration to config_file_version = 2 (#3634)
Browse files Browse the repository at this point in the history
* Add config_file_version and migration to version 2

* Generate `config_file_version = 2` as default
  • Loading branch information
trevyn authored Apr 27, 2021
1 parent 9ed0cd6 commit 9e27e6f
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
11 changes: 8 additions & 3 deletions config/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::collections::HashMap;
fn comments() -> HashMap<String, String> {
let mut retval = HashMap::new();
retval.insert(
"[server]".to_string(),
"config_file_version".to_string(),
"
# Generated Server Configuration File for Grin
#
Expand All @@ -31,7 +31,13 @@ fn comments() -> HashMap<String, String> {
# -[user home]/.grin
#
#########################################
"
.to_string(),
);

retval.insert(
"[server]".to_string(),
"#########################################
### SERVER CONFIGURATION ###
#########################################
Expand Down Expand Up @@ -323,7 +329,6 @@ fn comments() -> HashMap<String, String> {
"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(),
);
Expand Down
55 changes: 50 additions & 5 deletions config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -141,6 +140,7 @@ pub fn initial_setup_server(chain_type: &global::ChainTypes) -> Result<GlobalCon
impl Default for ConfigMembers {
fn default() -> ConfigMembers {
ConfigMembers {
config_file_version: Some(2),
server: ServerConfig::default(),
logging: Some(LoggingConfig::default()),
}
Expand Down Expand Up @@ -222,10 +222,14 @@ impl GlobalConfig {

/// Read config
fn read_config(mut self) -> Result<GlobalConfig, 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 = 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<ConfigMembers, toml::de::Error> = toml::from_str(&fixed);
match decoded {
Ok(gc) => {
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions config/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>,
/// Server config
#[serde(default)]
pub server: ServerConfig,
Expand Down
6 changes: 1 addition & 5 deletions src/bin/grin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 9e27e6f

Please sign in to comment.