mirror of
https://github.com/mimblewimble/grin.git
synced 2025-01-20 19:11:08 +03:00
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:
parent
9ed0cd65ef
commit
9e27e6f9d3
4 changed files with 61 additions and 13 deletions
|
@ -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
|
||||
#
|
||||
|
@ -31,7 +31,13 @@ fn comments() -> HashMap<String, String> {
|
|||
# -[user home]/.grin
|
||||
#
|
||||
|
||||
#########################################
|
||||
"
|
||||
.to_string(),
|
||||
);
|
||||
|
||||
retval.insert(
|
||||
"[server]".to_string(),
|
||||
"#########################################
|
||||
### SERVER CONFIGURATION ###
|
||||
#########################################
|
||||
|
||||
|
@ -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(),
|
||||
);
|
||||
|
|
|
@ -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<GlobalCon
|
|||
impl Default for ConfigMembers {
|
||||
fn default() -> 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<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) => {
|
||||
|
@ -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")
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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());
|
||||
|
|
Loading…
Reference in a new issue