From e5904cdd7bbd5746b2d3143c86cd76713cb08832 Mon Sep 17 00:00:00 2001 From: Shourya742 Date: Sat, 9 Dec 2023 16:27:37 +0530 Subject: [PATCH] Added default config files feature --- src/maker/config.rs | 39 ++++++++++++++++++++++++++++++++++----- src/taker/config.rs | 35 +++++++++++++++++++++++++++++++---- src/utill.rs | 15 +++++++++++++-- 3 files changed, 78 insertions(+), 11 deletions(-) diff --git a/src/maker/config.rs b/src/maker/config.rs index 6804e0db..dff349c6 100644 --- a/src/maker/config.rs +++ b/src/maker/config.rs @@ -1,8 +1,8 @@ //! Maker Configuration. Controlling various behaviors. -use std::{collections::HashMap, io, path::PathBuf}; +use std::{io, path::PathBuf}; -use crate::utill::{parse_field, parse_toml}; +use crate::utill::{get_config_dir, parse_field, parse_toml, write_default_config}; /// Maker Configuration, controlling various maker behavior. #[derive(Debug, Clone, PartialEq)] @@ -41,7 +41,7 @@ impl Default for MakerConfig { rpc_ping_interval_secs: 60, directory_servers_refresh_interval_secs: 60 * 60 * 12, //12 Hours idle_connection_timeout: 300, - onion_addrs: "myhiddenserviceaddress.onion".to_string(), + onion_addrs: "'myhiddenserviceaddress.onion'".to_string(), absolute_fee_sats: 1000, amount_relative_fee_ppb: 10_000_000, time_relative_fee_ppb: 100_000, @@ -61,22 +61,30 @@ impl MakerConfig { if path.exists() { parse_toml(path)? } else { + let default_maker_config_path = get_config_dir().join("maker.toml"); + if default_maker_config_path.exists() == false { + create_default_maker_dirs(&default_maker_config_path); + } log::warn!( "Maker config file not found at path : {}, using default config", path.display() ); - HashMap::new() + parse_toml(&default_maker_config_path)? } } else { let default_path = PathBuf::from("maker.toml"); if default_path.exists() { parse_toml(&default_path)? } else { + let default_maker_config_path = get_config_dir().join("maker.toml"); + if default_maker_config_path.exists() == false { + create_default_maker_dirs(&default_maker_config_path); + } log::warn!( "Maker config file not found in default path: {}, using default config", default_path.display() ); - HashMap::new() + parse_toml(&default_maker_config_path)? } }; @@ -143,6 +151,27 @@ impl MakerConfig { } } +fn create_default_maker_dirs(default_maker_config_path: &PathBuf) { + let config_string = String::from( + "\ + [maker_config]\n\ + port = 6102\n\ + heart_beat_interval_secs = 3\n\ + rpc_ping_interval_secs = 60\n\ + directory_servers_refresh_interval_secs = 43200\n\ + idle_connection_timeout = 300\n\ + onion_addrs = myhiddenserviceaddress.onion\n\ + absolute_fee_sats = 1000\n\ + amount_relative_fee_ppb = 10000000\n\ + time_relative_fee_ppb = 100000\n\ + required_confirms = 1\n\ + min_contract_reaction_time = 48\n\ + min_size = 10000\n", + ); + + write_default_config(default_maker_config_path, config_string).unwrap(); +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/taker/config.rs b/src/taker/config.rs index bc57a495..f44004aa 100644 --- a/src/taker/config.rs +++ b/src/taker/config.rs @@ -1,8 +1,8 @@ //! Taker configuration. Controlling various behavior. -use std::{collections::HashMap, io, path::PathBuf}; +use std::{io, path::PathBuf}; -use crate::utill::{parse_field, parse_toml}; +use crate::utill::{get_config_dir, parse_field, parse_toml, write_default_config}; /// TODO: Optionally read this from a config file. #[derive(Debug, Clone, PartialEq)] pub struct TakerConfig { @@ -45,22 +45,30 @@ impl TakerConfig { if path.exists() { parse_toml(path)? } else { + let default_taker_config_path = get_config_dir().join("taker.toml"); + if default_taker_config_path.exists() == false { + create_default_taker_dirs(&default_taker_config_path); + } log::warn!( "Taker config file not found at path : {}, using default config", path.display() ); - HashMap::new() + parse_toml(&default_taker_config_path)? } } else { let default_path = PathBuf::from("taker.toml"); if default_path.exists() { parse_toml(&default_path)? } else { + let default_taker_config_path = get_config_dir().join("taker.toml"); + if default_taker_config_path.exists() == false { + create_default_taker_dirs(&default_taker_config_path); + } log::warn!( "Taker config file not found in default path: {}, using default config", default_path.display() ); - HashMap::new() + parse_toml(&default_taker_config_path)? } }; @@ -121,6 +129,25 @@ impl TakerConfig { } } +fn create_default_taker_dirs(default_taker_config_path: &PathBuf) { + let config_string = String::from( + "\ + [taker_config]\n\ + refund_locktime = 48\n\ + refund_locktime_step = 48\n\ + first_connect_attempts = 5\n\ + first_connect_sleep_delay_sec = 1\n\ + first_connect_attempt_timeout_sec = 20\n\ + reconnect_attempts = 3200\n\ + reconnect_short_sleep_delay = 10\n\ + reconnect_long_sleep_delay = 60\n\ + short_long_sleep_delay_transition = 60\n\ + reconnect_attempt_timeout_sec = 300\n\ + ", + ); + write_default_config(&default_taker_config_path, config_string).unwrap(); +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/utill.rs b/src/utill.rs index f0971ea4..73702740 100644 --- a/src/utill.rs +++ b/src/utill.rs @@ -15,8 +15,8 @@ use bitcoin::{ use std::{ collections::HashMap, - fs::File, - io::{self, BufRead}, + fs::{self, File}, + io::{self, BufRead, Write}, }; use serde_json::Value; @@ -263,6 +263,17 @@ pub fn parse_field(value: Option<&String>, default: T) -> } } +/// Function to write data to default toml files +pub fn write_default_config(path: &PathBuf, toml_data: String) -> std::io::Result<()> { + if let Some(parent) = path.parent() { + fs::create_dir_all(parent)?; + } + let mut file = File::create(path)?; + file.write_all(toml_data.as_bytes())?; + file.flush()?; + Ok(()) +} + #[cfg(test)] mod tests { use std::str::FromStr;