Skip to content

Commit 5ffc2c0

Browse files
committed
Add inital implementation of persisted payment store
1 parent 8314dbb commit 5ffc2c0

File tree

5 files changed

+280
-177
lines changed

5 files changed

+280
-177
lines changed

src/event.rs

+48-51
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
2-
hex_utils, ChannelManager, Config, Error, KeysManager, NetworkGraph, PaymentInfo,
3-
PaymentInfoStorage, PaymentStatus, Wallet,
2+
hex_utils, ChannelManager, Config, Error, KeysManager, NetworkGraph, PaymentDirection,
3+
PaymentInfo, PaymentInfoStorage, PaymentStatus, Wallet,
44
};
55

66
use crate::logger::{log_error, log_info, Logger};
@@ -18,7 +18,7 @@ use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
1818

1919
use bitcoin::secp256k1::Secp256k1;
2020
use rand::{thread_rng, Rng};
21-
use std::collections::{hash_map, VecDeque};
21+
use std::collections::VecDeque;
2222
use std::ops::Deref;
2323
use std::sync::{Arc, Condvar, Mutex};
2424
use std::time::Duration;
@@ -191,8 +191,7 @@ where
191191
channel_manager: Arc<ChannelManager>,
192192
network_graph: Arc<NetworkGraph>,
193193
keys_manager: Arc<KeysManager>,
194-
inbound_payments: Arc<PaymentInfoStorage>,
195-
outbound_payments: Arc<PaymentInfoStorage>,
194+
payment_store: Arc<PaymentInfoStorage<K>>,
196195
tokio_runtime: Arc<tokio::runtime::Runtime>,
197196
logger: L,
198197
_config: Arc<Config>,
@@ -206,18 +205,16 @@ where
206205
pub fn new(
207206
wallet: Arc<Wallet<bdk::database::SqliteDatabase>>, event_queue: Arc<EventQueue<K>>,
208207
channel_manager: Arc<ChannelManager>, network_graph: Arc<NetworkGraph>,
209-
keys_manager: Arc<KeysManager>, inbound_payments: Arc<PaymentInfoStorage>,
210-
outbound_payments: Arc<PaymentInfoStorage>, tokio_runtime: Arc<tokio::runtime::Runtime>,
211-
logger: L, _config: Arc<Config>,
208+
keys_manager: Arc<KeysManager>, payment_store: Arc<PaymentInfoStorage<K>>,
209+
tokio_runtime: Arc<tokio::runtime::Runtime>, logger: L, _config: Arc<Config>,
212210
) -> Self {
213211
Self {
214212
event_queue,
215213
wallet,
216214
channel_manager,
217215
network_graph,
218216
keys_manager,
219-
inbound_payments,
220-
outbound_payments,
217+
payment_store,
221218
logger,
222219
tokio_runtime,
223220
_config,
@@ -326,7 +323,9 @@ where
326323
hex_utils::to_string(&payment_hash.0),
327324
);
328325
self.channel_manager.fail_htlc_backwards(&payment_hash);
329-
self.inbound_payments.lock().unwrap().remove(&payment_hash);
326+
self.payment_store
327+
.set_status(&payment_hash, PaymentStatus::Failed)
328+
.expect("Failed to access payment store");
330329
}
331330
}
332331
LdkEvent::PaymentClaimed {
@@ -347,48 +346,49 @@ where
347346
}
348347
PaymentPurpose::SpontaneousPayment(preimage) => (Some(preimage), None),
349348
};
350-
let mut payments = self.inbound_payments.lock().unwrap();
351-
match payments.entry(payment_hash) {
352-
hash_map::Entry::Occupied(mut e) => {
353-
let payment = e.get_mut();
354-
payment.status = PaymentStatus::Succeeded;
355-
payment.preimage = payment_preimage;
356-
payment.secret = payment_secret;
357-
}
358-
hash_map::Entry::Vacant(e) => {
359-
e.insert(PaymentInfo {
349+
350+
let payment_info =
351+
if let Some(mut payment_info) = self.payment_store.get(&payment_hash) {
352+
payment_info.status = PaymentStatus::Succeeded;
353+
payment_info.preimage = payment_preimage;
354+
payment_info.secret = payment_secret;
355+
payment_info
356+
} else {
357+
PaymentInfo {
360358
preimage: payment_preimage,
359+
payment_hash,
361360
secret: payment_secret,
362-
status: PaymentStatus::Succeeded,
363361
amount_msat: Some(amount_msat),
364-
});
365-
}
366-
}
362+
direction: PaymentDirection::Inbound,
363+
status: PaymentStatus::Succeeded,
364+
}
365+
};
366+
367+
self.payment_store.insert(payment_info).expect("Failed to access payment store");
367368
self.event_queue
368369
.add_event(Event::PaymentReceived { payment_hash, amount_msat })
369370
.expect("Failed to push to event queue");
370371
}
371372
LdkEvent::PaymentSent { payment_preimage, payment_hash, fee_paid_msat, .. } => {
372-
let mut payments = self.outbound_payments.lock().unwrap();
373-
for (hash, payment) in payments.iter_mut() {
374-
if *hash == payment_hash {
375-
payment.preimage = Some(payment_preimage);
376-
payment.status = PaymentStatus::Succeeded;
377-
log_info!(
378-
self.logger,
379-
"Successfully sent payment of {} msats{} from \
380-
payment hash {:?} with preimage {:?}",
381-
payment.amount_msat.unwrap(),
382-
if let Some(fee) = fee_paid_msat {
383-
format!(" (fee {} msats)", fee)
384-
} else {
385-
"".to_string()
386-
},
387-
hex_utils::to_string(&payment_hash.0),
388-
hex_utils::to_string(&payment_preimage.0)
389-
);
390-
break;
391-
}
373+
if let Some(mut payment_info) = self.payment_store.get(&payment_hash) {
374+
payment_info.preimage = Some(payment_preimage);
375+
payment_info.status = PaymentStatus::Succeeded;
376+
self.payment_store
377+
.insert(payment_info.clone())
378+
.expect("Failed to access payment store");
379+
log_info!(
380+
self.logger,
381+
"Successfully sent payment of {} msats{} from \
382+
payment hash {:?} with preimage {:?}",
383+
payment_info.amount_msat.unwrap(),
384+
if let Some(fee) = fee_paid_msat {
385+
format!(" (fee {} msat)", fee)
386+
} else {
387+
"".to_string()
388+
},
389+
hex_utils::to_string(&payment_hash.0),
390+
hex_utils::to_string(&payment_preimage.0)
391+
);
392392
}
393393
self.event_queue
394394
.add_event(Event::PaymentSuccessful { payment_hash })
@@ -401,12 +401,9 @@ where
401401
hex_utils::to_string(&payment_hash.0)
402402
);
403403

404-
let mut payments = self.outbound_payments.lock().unwrap();
405-
if payments.contains_key(&payment_hash) {
406-
let payment = payments.get_mut(&payment_hash).unwrap();
407-
assert_eq!(payment.status, PaymentStatus::Pending);
408-
payment.status = PaymentStatus::Failed;
409-
}
404+
self.payment_store
405+
.set_status(&payment_hash, PaymentStatus::Failed)
406+
.expect("Failed to access payment store");
410407
self.event_queue
411408
.add_event(Event::PaymentFailed { payment_hash })
412409
.expect("Failed to push to event queue");

src/io_utils.rs

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use crate::payment_store::{PaymentInfo, PAYMENT_INFO_PERSISTENCE_PREFIX};
12
use crate::{Config, Error, FilesystemLogger, NetworkGraph, Scorer};
23

34
use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters};
4-
use lightning::util::ser::ReadableArgs;
5+
use lightning::util::ser::{Readable, ReadableArgs};
56

67
use rand::{thread_rng, RngCore};
78

@@ -59,3 +60,25 @@ pub(crate) fn read_scorer(
5960
}
6061
ProbabilisticScorer::new(params, network_graph, logger)
6162
}
63+
64+
pub(crate) fn read_payment_info(config: &Config) -> Vec<PaymentInfo> {
65+
let ldk_data_dir = format!("{}/ldk", config.storage_dir_path);
66+
let payment_store_path = format!("{}/{}", ldk_data_dir, PAYMENT_INFO_PERSISTENCE_PREFIX);
67+
let mut payments = Vec::new();
68+
69+
if let Ok(res) = fs::read_dir(payment_store_path) {
70+
for entry in res {
71+
if let Ok(entry) = entry {
72+
if entry.path().is_file() {
73+
if let Ok(mut f) = fs::File::open(entry.path()) {
74+
if let Ok(payment_info) = PaymentInfo::read(&mut f) {
75+
payments.push(payment_info);
76+
}
77+
}
78+
}
79+
}
80+
}
81+
}
82+
83+
payments
84+
}

0 commit comments

Comments
 (0)