Skip to content

Commit

Permalink
Merge pull request #672 from get10101/fix/settings-in-data-dir
Browse files Browse the repository at this point in the history
Improve settings
  • Loading branch information
klochowicz authored May 20, 2023
2 parents 4869fa4 + 37abe8f commit 88f04b5
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,3 @@ data/**

# env file
.env

# coordinator settings
coordinator-settings.toml
2 changes: 1 addition & 1 deletion coordinator/src/bin/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async fn main() -> Result<()> {
let seed_path = data_dir.join("seed");
let seed = Bip39Seed::initialize(&seed_path)?;

let settings = Settings::new().await;
let settings = Settings::new(&data_dir).await;

let node = Arc::new(
ln_dlc_node::node::Node::new_coordinator(
Expand Down
25 changes: 17 additions & 8 deletions coordinator/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use ln_dlc_node::node::LnDlcNodeSettings;
use serde::Deserialize;
use serde::Serialize;
use std::path::Path;
use std::path::PathBuf;
use tokio::fs;
use tokio::io::AsyncWriteExt;

const SETTINGS_FILE_PATH: &str = "coordinator-settings.toml";
const SETTINGS_FILE_NAME: &str = "coordinator-settings.toml";

/// Top level settings
#[derive(Debug, Clone, Deserialize, Serialize)]
Expand All @@ -28,6 +29,8 @@ pub struct Settings {
pub max_allowed_tx_fee_rate_when_opening_channel: Option<u32>,

pub ln_dlc: LnDlcNodeSettings,
// Special parameter, where the settings file is located
pub path: Option<PathBuf>,
}

impl Default for Settings {
Expand All @@ -40,25 +43,31 @@ impl Default for Settings {
fallback_tx_fee_rate_high_priority: 5000,
max_allowed_tx_fee_rate_when_opening_channel: None,
ln_dlc: LnDlcNodeSettings::default(),
path: None,
}
}
}

async fn read_settings() -> Result<Settings> {
let settings_path = Path::new(SETTINGS_FILE_PATH);
async fn read_settings(data_dir: &Path) -> Result<Settings> {
let settings_path = data_dir.join(SETTINGS_FILE_NAME);
let data = fs::read_to_string(settings_path).await?;
toml::from_str(&data).context("Unable to parse settings file")
}

impl Settings {
pub async fn new() -> Self {
match read_settings().await {
pub async fn new(data_dir: &Path) -> Self {
match read_settings(data_dir).await {
Ok(settings) => settings,
Err(e) => {
tracing::warn!("Unable to read {SETTINGS_FILE_PATH} file, using defaults: {e}");
let new = Settings::default();
tracing::warn!("Unable to read {SETTINGS_FILE_NAME} file, using defaults: {e}");
let new = Settings {
path: Some(data_dir.join(SETTINGS_FILE_NAME)),
..Settings::default()
};
if let Err(e) = new.write_to_file().await {
tracing::error!("Unable to write default settings to file: {e}");
} else {
tracing::info!("Default settings written to file");
}
new
}
Expand All @@ -69,7 +78,7 @@ impl Settings {
let data =
toml::to_string_pretty(&self).context("Unable to serialize settings to TOML format")?;

let settings_path = Path::new(SETTINGS_FILE_PATH);
let settings_path = self.path.as_ref().context("Settings path not set")?.clone();
let mut file = fs::File::create(settings_path).await?;
file.write_all(data.as_bytes()).await?;
file.flush().await?;
Expand Down
14 changes: 4 additions & 10 deletions crates/ln-dlc-node/src/ln_dlc_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ use rust_bitcoin_coin_selection::select_coins;
use simple_wallet::WalletStorage;
use std::sync::Arc;

/// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
/// number of blocks after which BDK stops looking for scripts belonging to the wallet.
/// Note: This constant and value was copied from ldk_node
const BDK_CLIENT_STOP_GAP: usize = 20;
/// The number of concurrent requests made against the API provider.
/// Note: This constant and value was copied from ldk_node
const BDK_CLIENT_CONCURRENCY: u8 = 1;

/// This is a wrapper type introduced to be able to implement traits from `rust-dlc` on the
/// `ldk_node::LightningWallet`.
pub struct LnDlcWallet {
Expand All @@ -56,10 +48,12 @@ impl LnDlcWallet {
storage: Arc<SledStorageProvider>,
seed: Bip39Seed,
runtime_handle: tokio::runtime::Handle,
bdk_client_stop_gap: usize,
bdk_client_concurrency: u8,
) -> Self {
let blockchain =
EsploraBlockchain::from_client(esplora_client.client().clone(), BDK_CLIENT_STOP_GAP)
.with_concurrency(BDK_CLIENT_CONCURRENCY);
EsploraBlockchain::from_client(esplora_client.client().clone(), bdk_client_stop_gap)
.with_concurrency(bdk_client_concurrency);

let wallet = Arc::new(ldk_node_wallet::Wallet::new(
blockchain,
Expand Down
14 changes: 14 additions & 0 deletions crates/ln-dlc-node/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ pub struct LnDlcNodeSettings {
pub on_chain_sync_interval: Duration,
/// How often we update the fee rate
pub fee_rate_sync_interval: Duration,

/// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
/// number of blocks after which BDK stops looking for scripts belonging to the wallet.
/// Note: This constant and value was copied from ldk_node
/// XXX: Requires restart of the node to take effect
pub bdk_client_stop_gap: usize,
/// The number of concurrent requests made against the API provider.
/// Note: This constant and value was copied from ldk_node
/// XXX: Requires restart of the node to take effect
pub bdk_client_concurrency: u8,
}

impl Default for LnDlcNodeSettings {
Expand All @@ -143,6 +153,8 @@ impl Default for LnDlcNodeSettings {
off_chain_sync_interval: Duration::from_secs(5),
on_chain_sync_interval: Duration::from_secs(300),
fee_rate_sync_interval: Duration::from_secs(20),
bdk_client_stop_gap: 20,
bdk_client_concurrency: 1,
}
}
}
Expand Down Expand Up @@ -278,6 +290,8 @@ where
storage.clone(),
seed.clone(),
runtime_handle.clone(),
settings.bdk_client_stop_gap,
settings.bdk_client_concurrency,
))
};

Expand Down

0 comments on commit 88f04b5

Please sign in to comment.