Skip to content

Commit

Permalink
Added default config files feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Shourya742 committed Dec 9, 2023
1 parent cb44ce5 commit e5904cd
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 11 deletions.
39 changes: 34 additions & 5 deletions src/maker/config.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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,
Expand All @@ -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)?
}
};

Expand Down Expand Up @@ -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::*;
Expand Down
35 changes: 31 additions & 4 deletions src/taker/config.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)?
}
};

Expand Down Expand Up @@ -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::*;
Expand Down
15 changes: 13 additions & 2 deletions src/utill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -263,6 +263,17 @@ pub fn parse_field<T: std::str::FromStr>(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;
Expand Down

0 comments on commit e5904cd

Please sign in to comment.