Skip to content

Commit 2db97ba

Browse files
enigbetnull
authored andcommitted
fix: ensure seed file creation does not fail
Seed file generation failed if the parent directory did not exist. This is an existing bug that was masked by the implicit creation of the parent directory by the former `FilesystemLogger`. While the new `Logger` maintains this behaviour, it only created the parent directory for the default log file path. As a result, if the node's configured storage directory differed from the default, seed file creation would fail. This fix addresses this bug as well as modifies the logger setup logic to ensure that the Node's configured storage directory is created, not just the default storage directory.
1 parent 8a3b59b commit 2db97ba

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/builder.rs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
use crate::chain::{ChainSource, DEFAULT_ESPLORA_SERVER_URL};
99
use crate::config::{
10-
default_user_config, Config, EsploraSyncConfig, FilesystemLoggerConfig, DEFAULT_LOG_FILE_PATH,
10+
default_user_config, Config, EsploraSyncConfig, FilesystemLoggerConfig, DEFAULT_LOG_FILENAME,
1111
DEFAULT_LOG_LEVEL, WALLET_KEYS_SEED_LEN,
1212
};
1313

@@ -432,7 +432,7 @@ impl NodeBuilder {
432432
) -> Result<Node, BuildError> {
433433
use bitcoin::key::Secp256k1;
434434

435-
let logger = setup_logger(&self.log_writer_config)?;
435+
let logger = setup_logger(&self.log_writer_config, &self.config)?;
436436

437437
let seed_bytes = seed_bytes_from_config(
438438
&self.config,
@@ -497,7 +497,7 @@ impl NodeBuilder {
497497
pub fn build_with_vss_store_and_header_provider(
498498
&self, vss_url: String, store_id: String, header_provider: Arc<dyn VssHeaderProvider>,
499499
) -> Result<Node, BuildError> {
500-
let logger = setup_logger(&self.log_writer_config)?;
500+
let logger = setup_logger(&self.log_writer_config, &self.config)?;
501501

502502
let seed_bytes = seed_bytes_from_config(
503503
&self.config,
@@ -529,7 +529,7 @@ impl NodeBuilder {
529529

530530
/// Builds a [`Node`] instance according to the options previously configured.
531531
pub fn build_with_store(&self, kv_store: Arc<DynStore>) -> Result<Node, BuildError> {
532-
let logger = setup_logger(&self.log_writer_config)?;
532+
let logger = setup_logger(&self.log_writer_config, &self.config)?;
533533

534534
let seed_bytes = seed_bytes_from_config(
535535
&self.config,
@@ -1293,17 +1293,21 @@ fn build_with_store_internal(
12931293
}
12941294

12951295
/// Sets up the node logger.
1296-
fn setup_logger(config_opt: &Option<LogWriterConfig>) -> Result<Arc<Logger>, BuildError> {
1297-
let default_config = LogWriterConfig::default();
1298-
let config = if let Some(conf) = config_opt { conf } else { &default_config };
1299-
1300-
let logger = match config {
1296+
///
1297+
/// If `log_writer_conf` is set to None, uses [`LogWriterConfig::default()`].
1298+
/// The `node_conf`is provided to access the configured storage directory.
1299+
fn setup_logger(
1300+
log_writer_conf: &Option<LogWriterConfig>, node_conf: &Config,
1301+
) -> Result<Arc<Logger>, BuildError> {
1302+
let default_lw_config = LogWriterConfig::default();
1303+
let log_writer_config =
1304+
if let Some(conf) = log_writer_conf { conf } else { &default_lw_config };
1305+
1306+
let logger = match log_writer_config {
13011307
LogWriterConfig::File(fs_logger_config) => {
1302-
let log_file_path = if let Some(fp) = &fs_logger_config.log_file_path {
1303-
fp
1304-
} else {
1305-
DEFAULT_LOG_FILE_PATH
1306-
};
1308+
let fp = format!("{}/{}", node_conf.storage_dir_path, DEFAULT_LOG_FILENAME);
1309+
let log_file_path =
1310+
if let Some(fp) = &fs_logger_config.log_file_path { fp } else { &fp };
13071311
let log_level = fs_logger_config.log_level.unwrap_or(DEFAULT_LOG_LEVEL);
13081312

13091313
Logger::new_fs_writer(log_file_path, log_level)

src/config.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ const DEFAULT_ANCHOR_PER_CHANNEL_RESERVE_SATS: u64 = 25_000;
3333
/// The default log level.
3434
pub const DEFAULT_LOG_LEVEL: LogLevel = LogLevel::Debug;
3535

36-
/// The default log file path.
37-
pub const DEFAULT_LOG_FILE_PATH: &'static str = "/tmp/ldk_node/ldk_node.log";
36+
/// The default log filename.
37+
pub const DEFAULT_LOG_FILENAME: &'static str = "ldk_node.log";
3838

3939
// The 'stop gap' parameter used by BDK's wallet sync. This seems to configure the threshold
4040
// number of derivation indexes after which BDK stops looking for new scripts belonging to the wallet.
@@ -440,7 +440,7 @@ pub struct FilesystemLoggerConfig {
440440
///
441441
/// This specifies the log file path if a destination other than the storage
442442
/// directory, i.e. [`Config::storage_dir_path`], is preferred. If unconfigured,
443-
/// defaults to [`DEFAULT_LOG_FILE_PATH`]
443+
/// defaults to [`DEFAULT_LOG_FILENAME`] in default storage directory.
444444
pub log_file_path: Option<String>,
445445
/// This specifies the log level.
446446
///
@@ -451,7 +451,7 @@ pub struct FilesystemLoggerConfig {
451451
impl Default for FilesystemLoggerConfig {
452452
fn default() -> Self {
453453
Self {
454-
log_file_path: Some(DEFAULT_LOG_FILE_PATH.to_string()),
454+
log_file_path: Some(format!("{}/{}", DEFAULT_STORAGE_DIR_PATH, DEFAULT_LOG_FILENAME)),
455455
log_level: Some(DEFAULT_LOG_LEVEL),
456456
}
457457
}

src/io/utils.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ where
9898
let mut key = [0; WALLET_KEYS_SEED_LEN];
9999
thread_rng().fill_bytes(&mut key);
100100

101+
if let Some(parent_dir) = Path::new(&keys_seed_path).parent() {
102+
fs::create_dir_all(parent_dir).map_err(|e| {
103+
log_error!(
104+
logger,
105+
"Failed to create parent directory for key seed file: {}.",
106+
keys_seed_path
107+
);
108+
e
109+
})?;
110+
}
111+
101112
let mut f = fs::File::create(keys_seed_path).map_err(|e| {
102113
log_error!(logger, "Failed to create keys seed file: {}", keys_seed_path);
103114
e

0 commit comments

Comments
 (0)