Skip to content

Commit

Permalink
Reorganize IO things into io submodules
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Mar 23, 2023
1 parent 1d4c40d commit ee9e4a8
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
PaymentInfo, PaymentInfoStorage, PaymentStatus, Wallet,
};

use crate::io_utils::KVStoreUnpersister;
use crate::io::KVStoreUnpersister;
use crate::logger::{log_error, log_info, Logger};

use lightning::chain::chaininterface::{BroadcasterInterface, ConfirmationTarget, FeeEstimator};
Expand Down
47 changes: 47 additions & 0 deletions src/io/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
pub(crate) mod utils;

use lightning_persister::FilesystemPersister;

use std::fs;
use std::os::unix::io::AsRawFd;
use std::path::PathBuf;

/// Provides an interface that allows a previously persisted key to be unpersisted.
pub trait KVStoreUnpersister {
/// Unpersist (i.e., remove) the writeable previously persisted under the provided key.
/// Returns `true` if the key was present, and `false` otherwise.
fn unpersist(&self, key: &str) -> std::io::Result<bool>;
}

impl KVStoreUnpersister for FilesystemPersister {
fn unpersist(&self, key: &str) -> std::io::Result<bool> {
let mut dest_file = PathBuf::from(self.get_data_dir());
dest_file.push(key);

if !dest_file.is_file() {
return Ok(false);
}

fs::remove_file(&dest_file)?;
#[cfg(not(target_os = "windows"))]
{
let parent_directory = dest_file.parent().unwrap();
let dir_file = fs::OpenOptions::new().read(true).open(parent_directory)?;
unsafe {
// The above call to `fs::remove_file` corresponds to POSIX `unlink`, whose changes
// to the inode might get cached (and hence possibly lost on crash), depending on
// the target platform and file system.
//
// In order to assert we permanently removed the file in question we therefore
// call `fsync` on the parent directory on platforms that support it,
libc::fsync(dir_file.as_raw_fd());
}
}

if dest_file.is_file() {
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Unpersisting key failed"));
}

return Ok(true);
}
}
43 changes: 0 additions & 43 deletions src/io_utils.rs → src/io/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ use crate::{Config, FilesystemLogger, NetworkGraph, Scorer, WALLET_KEYS_SEED_LEN

use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};
use lightning::util::ser::{Readable, ReadableArgs};
use lightning_persister::FilesystemPersister;

use rand::{thread_rng, RngCore};

use std::fs;
use std::io::{BufReader, Write};
use std::os::unix::io::AsRawFd;
use std::path::Path;
use std::path::PathBuf;
use std::sync::Arc;

pub(crate) fn read_or_generate_seed_file(keys_seed_path: &str) -> [u8; WALLET_KEYS_SEED_LEN] {
Expand Down Expand Up @@ -86,43 +83,3 @@ pub(crate) fn read_payment_info(config: &Config) -> Vec<PaymentInfo> {

payments
}

/// Provides an interface that allows a previously persisted key to be unpersisted.
pub trait KVStoreUnpersister {
/// Unpersist (i.e., remove) the writeable previously persisted under the provided key.
/// Returns `true` if the key was present, and `false` otherwise.
fn unpersist(&self, key: &str) -> std::io::Result<bool>;
}

impl KVStoreUnpersister for FilesystemPersister {
fn unpersist(&self, key: &str) -> std::io::Result<bool> {
let mut dest_file = PathBuf::from(self.get_data_dir());
dest_file.push(key);

if !dest_file.is_file() {
return Ok(false);
}

fs::remove_file(&dest_file)?;
#[cfg(not(target_os = "windows"))]
{
let parent_directory = dest_file.parent().unwrap();
let dir_file = fs::OpenOptions::new().read(true).open(parent_directory)?;
unsafe {
// The above call to `fs::remove_file` corresponds to POSIX `unlink`, whose changes
// to the inode might get cached (and hence possibly lost on crash), depending on
// the target platform and file system.
//
// In order to assert we permanently removed the file in question we therefore
// call `fsync` on the parent directory on platforms that support it,
libc::fsync(dir_file.as_raw_fd());
}
}

if dest_file.is_file() {
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Unpersisting key failed"));
}

return Ok(true);
}
}
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
mod error;
mod event;
mod hex_utils;
mod io_utils;
mod io;
mod logger;
mod payment_store;
mod peer_store;
Expand Down Expand Up @@ -245,13 +245,13 @@ impl Builder {
match entropy_source {
WalletEntropySource::SeedBytes(bytes) => bytes.clone(),
WalletEntropySource::SeedFile(seed_path) => {
io_utils::read_or_generate_seed_file(seed_path)
io::utils::read_or_generate_seed_file(seed_path)
}
}
} else {
// Default to read or generate from the default location generate a seed file.
let seed_path = format!("{}/keys_seed", config.storage_dir_path);
io_utils::read_or_generate_seed_file(&seed_path)
io::utils::read_or_generate_seed_file(&seed_path)
};

let xprv = bitcoin::util::bip32::ExtendedPrivKey::new_master(config.network, &seed_bytes)
Expand Down Expand Up @@ -313,8 +313,8 @@ impl Builder {

// Initialize the network graph, scorer, and router
let network_graph =
Arc::new(io_utils::read_network_graph(config.as_ref(), Arc::clone(&logger)));
let scorer = Arc::new(Mutex::new(io_utils::read_scorer(
Arc::new(io::utils::read_network_graph(config.as_ref(), Arc::clone(&logger)));
let scorer = Arc::new(Mutex::new(io::utils::read_scorer(
config.as_ref(),
Arc::clone(&network_graph),
Arc::clone(&logger),
Expand Down Expand Up @@ -423,7 +423,7 @@ impl Builder {
));

// Init payment info storage
let payments = io_utils::read_payment_info(config.as_ref());
let payments = io::utils::read_payment_info(config.as_ref());
let payment_store =
Arc::new(PaymentInfoStorage::from_payments(payments, Arc::clone(&persister)));

Expand Down
2 changes: 1 addition & 1 deletion src/payment_store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::hex_utils;
use crate::io_utils::KVStoreUnpersister;
use crate::io::KVStoreUnpersister;
use crate::Error;

use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
Expand Down
2 changes: 1 addition & 1 deletion src/tests/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::io_utils::KVStoreUnpersister;
use crate::io::KVStoreUnpersister;
use lightning::util::persist::KVStorePersister;
use lightning::util::ser::Writeable;

Expand Down

0 comments on commit ee9e4a8

Please sign in to comment.