Skip to content

Commit

Permalink
Implement KVStoreUnpersister
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Sep 26, 2022
1 parent 88b8e62 commit c9ae999
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ chrono = "0.4"
futures = "0.3"
serde_json = { version = "1.0" }
tokio = { version = "1", features = [ "io-util", "macros", "rt", "rt-multi-thread", "sync", "net", "time" ] }
libc = "0.2"



[profile.release]
Expand Down
37 changes: 37 additions & 0 deletions src/io_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ use crate::{FilesystemLogger, LdkLiteConfig, NetworkGraph, Scorer};

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::PathBuf;
use std::sync::Arc;

pub(crate) fn read_or_generate_seed_file(config: Arc<LdkLiteConfig>) -> Result<[u8; 32], Error> {
Expand Down Expand Up @@ -83,3 +86,37 @@ pub(crate) fn read_payment_info(config: Arc<LdkLiteConfig>) -> Result<Vec<Paymen

Ok(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)?;
let parent_directory = dest_file.parent().unwrap();
let dir_file = fs::OpenOptions::new().read(true).open(parent_directory)?;
#[cfg(not(target_os = "windows"))]
{
unsafe {
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);
}
}
17 changes: 12 additions & 5 deletions src/payment_store.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::hex_utils;
use crate::io_utils::KVStoreUnpersister;
use crate::Error;

use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
Expand Down Expand Up @@ -123,12 +124,12 @@ impl Writeable for PaymentStatus {
/// The payment information will be persisted under this prefix.
pub(crate) const PAYMENT_INFO_PERSISTENCE_PREFIX: &str = "payments";

pub(crate) struct PaymentInfoStorage<K: KVStorePersister> {
pub(crate) struct PaymentInfoStorage<K: KVStorePersister + KVStoreUnpersister> {
payments: Mutex<HashMap<PaymentHash, PaymentInfo>>,
persister: Arc<K>,
}

impl<K: KVStorePersister> PaymentInfoStorage<K> {
impl<K: KVStorePersister + KVStoreUnpersister> PaymentInfoStorage<K> {
pub(crate) fn new(persister: Arc<K>) -> Self {
let payments = Mutex::new(HashMap::new());
Self { payments, persister }
Expand Down Expand Up @@ -157,9 +158,15 @@ impl<K: KVStorePersister> PaymentInfoStorage<K> {
return Ok(());
}

// TODO: Need an `unpersist` method for this?
//pub(crate) fn remove_payment(&self, payment_hash: &PaymentHash) -> Result<(), Error> {
//}
pub(crate) fn remove_payment(&self, payment_hash: &PaymentHash) -> Result<(), Error> {
let key = format!(
"{}/{}",
PAYMENT_INFO_PERSISTENCE_PREFIX,
hex_utils::to_string(&payment_hash.0)
);
self.persister.unpersist(&key)?;
Ok(())
}

pub(crate) fn payment(&self, payment_hash: &PaymentHash) -> Option<PaymentInfo> {
self.payments.lock().unwrap().get(payment_hash).cloned()
Expand Down

0 comments on commit c9ae999

Please sign in to comment.