Skip to content

Commit c9ae999

Browse files
committed
Implement KVStoreUnpersister
1 parent 88b8e62 commit c9ae999

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ chrono = "0.4"
2424
futures = "0.3"
2525
serde_json = { version = "1.0" }
2626
tokio = { version = "1", features = [ "io-util", "macros", "rt", "rt-multi-thread", "sync", "net", "time" ] }
27+
libc = "0.2"
28+
2729

2830

2931
[profile.release]

src/io_utils.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ use crate::{FilesystemLogger, LdkLiteConfig, NetworkGraph, Scorer};
55

66
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};
77
use lightning::util::ser::{Readable, ReadableArgs};
8+
use lightning_persister::FilesystemPersister;
89

910
use rand::{thread_rng, RngCore};
1011

1112
use std::fs;
1213
use std::io::{BufReader, Write};
14+
use std::os::unix::io::AsRawFd;
15+
use std::path::PathBuf;
1316
use std::sync::Arc;
1417

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

8487
Ok(payments)
8588
}
89+
90+
/// Provides an interface that allows a previously persisted key to be unpersisted.
91+
pub trait KVStoreUnpersister {
92+
/// Unpersist (i.e., remove) the writeable previously persisted under the provided key.
93+
/// Returns `true` if the key was present, and `false` otherwise.
94+
fn unpersist(&self, key: &str) -> std::io::Result<bool>;
95+
}
96+
97+
impl KVStoreUnpersister for FilesystemPersister {
98+
fn unpersist(&self, key: &str) -> std::io::Result<bool> {
99+
let mut dest_file = PathBuf::from(self.get_data_dir());
100+
dest_file.push(key);
101+
102+
if !dest_file.is_file() {
103+
return Ok(false);
104+
}
105+
106+
fs::remove_file(&dest_file)?;
107+
let parent_directory = dest_file.parent().unwrap();
108+
let dir_file = fs::OpenOptions::new().read(true).open(parent_directory)?;
109+
#[cfg(not(target_os = "windows"))]
110+
{
111+
unsafe {
112+
libc::fsync(dir_file.as_raw_fd());
113+
}
114+
}
115+
116+
if dest_file.is_file() {
117+
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Unpersisting key failed"));
118+
}
119+
120+
return Ok(true);
121+
}
122+
}

src/payment_store.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::hex_utils;
2+
use crate::io_utils::KVStoreUnpersister;
23
use crate::Error;
34

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

126-
pub(crate) struct PaymentInfoStorage<K: KVStorePersister> {
127+
pub(crate) struct PaymentInfoStorage<K: KVStorePersister + KVStoreUnpersister> {
127128
payments: Mutex<HashMap<PaymentHash, PaymentInfo>>,
128129
persister: Arc<K>,
129130
}
130131

131-
impl<K: KVStorePersister> PaymentInfoStorage<K> {
132+
impl<K: KVStorePersister + KVStoreUnpersister> PaymentInfoStorage<K> {
132133
pub(crate) fn new(persister: Arc<K>) -> Self {
133134
let payments = Mutex::new(HashMap::new());
134135
Self { payments, persister }
@@ -157,9 +158,15 @@ impl<K: KVStorePersister> PaymentInfoStorage<K> {
157158
return Ok(());
158159
}
159160

160-
// TODO: Need an `unpersist` method for this?
161-
//pub(crate) fn remove_payment(&self, payment_hash: &PaymentHash) -> Result<(), Error> {
162-
//}
161+
pub(crate) fn remove_payment(&self, payment_hash: &PaymentHash) -> Result<(), Error> {
162+
let key = format!(
163+
"{}/{}",
164+
PAYMENT_INFO_PERSISTENCE_PREFIX,
165+
hex_utils::to_string(&payment_hash.0)
166+
);
167+
self.persister.unpersist(&key)?;
168+
Ok(())
169+
}
163170

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

0 commit comments

Comments
 (0)