Skip to content

Commit 40f82b8

Browse files
committed
f Do updates via ChannelDetailsUpdate
1 parent c4aed78 commit 40f82b8

File tree

2 files changed

+56
-36
lines changed

2 files changed

+56
-36
lines changed

src/event.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use crate::{
2-
hex_utils, ChannelManager, Config, Error, KeysManager, NetworkGraph, PaymentDetails,
3-
PaymentDirection, PaymentStatus, PaymentStore, Wallet,
1+
use crate::{hex_utils, ChannelManager, Config, Error, KeysManager, NetworkGraph, Wallet};
2+
3+
use crate::payment_store::{
4+
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentStatus, PaymentStore,
45
};
56

67
use crate::io::{
@@ -338,9 +339,10 @@ where
338339
amount_msat,
339340
);
340341
self.channel_manager.fail_htlc_backwards(&payment_hash);
341-
self.payment_store
342-
.update(&payment_hash, None, None, None, Some(PaymentStatus::Failed))
343-
.expect("Failed to access payment store");
342+
343+
let mut update = PaymentDetailsUpdate::new(payment_hash);
344+
update.status = Some(PaymentStatus::Failed);
345+
self.payment_store.update(&update).expect("Failed to access payment store");
344346
return;
345347
}
346348
}
@@ -373,9 +375,10 @@ where
373375
hex_utils::to_string(&payment_hash.0),
374376
);
375377
self.channel_manager.fail_htlc_backwards(&payment_hash);
376-
self.payment_store
377-
.update(&payment_hash, None, None, None, Some(PaymentStatus::Failed))
378-
.expect("Failed to access payment store");
378+
379+
let mut update = PaymentDetailsUpdate::new(payment_hash);
380+
update.status = Some(PaymentStatus::Failed);
381+
self.payment_store.update(&update).expect("Failed to access payment store");
379382
}
380383
}
381384
LdkEvent::PaymentClaimed {
@@ -392,13 +395,12 @@ where
392395
);
393396
match purpose {
394397
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
395-
match self.payment_store.update(
396-
&payment_hash,
397-
Some(payment_preimage),
398-
Some(Some(payment_secret)),
399-
Some(Some(amount_msat)),
400-
Some(PaymentStatus::Succeeded),
401-
) {
398+
let mut update = PaymentDetailsUpdate::new(payment_hash);
399+
update.preimage = Some(payment_preimage);
400+
update.secret = Some(Some(payment_secret));
401+
update.amount_msat = Some(Some(amount_msat));
402+
update.status = Some(PaymentStatus::Succeeded);
403+
match self.payment_store.update(&update) {
402404
Ok(true) => (),
403405
Ok(false) => {
404406
log_error!(
@@ -488,9 +490,9 @@ where
488490
hex_utils::to_string(&payment_hash.0)
489491
);
490492

491-
self.payment_store
492-
.update(&payment_hash, None, None, None, Some(PaymentStatus::Failed))
493-
.expect("Failed to access payment store");
493+
let mut update = PaymentDetailsUpdate::new(payment_hash);
494+
update.status = Some(PaymentStatus::Failed);
495+
self.payment_store.update(&update).expect("Failed to access payment store");
494496
self.event_queue
495497
.add_event(Event::PaymentFailed { payment_hash })
496498
.expect("Failed to push to event queue");

src/payment_store.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl_writeable_tlv_based!(PaymentDetails, {
3939
});
4040

4141
/// Represents the direction of a payment.
42-
#[derive(Clone, Debug, PartialEq, Eq)]
42+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
4343
pub enum PaymentDirection {
4444
/// The payment is inbound.
4545
Inbound,
@@ -53,7 +53,7 @@ impl_writeable_tlv_based_enum!(PaymentDirection,
5353
);
5454

5555
/// Represents the current status of a payment.
56-
#[derive(Clone, Debug, PartialEq, Eq)]
56+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5757
pub enum PaymentStatus {
5858
/// The payment is still pending.
5959
Pending,
@@ -69,6 +69,29 @@ impl_writeable_tlv_based_enum!(PaymentStatus,
6969
(2, Failed) => {};
7070
);
7171

72+
#[derive(Clone, Debug, PartialEq, Eq)]
73+
pub(crate) struct PaymentDetailsUpdate {
74+
pub hash: PaymentHash,
75+
pub preimage: Option<Option<PaymentPreimage>>,
76+
pub secret: Option<Option<PaymentSecret>>,
77+
pub amount_msat: Option<Option<u64>>,
78+
pub direction: Option<PaymentDirection>,
79+
pub status: Option<PaymentStatus>,
80+
}
81+
82+
impl PaymentDetailsUpdate {
83+
pub fn new(hash: PaymentHash) -> Self {
84+
Self {
85+
hash,
86+
preimage: None,
87+
secret: None,
88+
amount_msat: None,
89+
direction: None,
90+
status: None,
91+
}
92+
}
93+
}
94+
7295
pub(crate) struct PaymentStore<K: Deref + Clone, L: Deref>
7396
where
7497
K::Target: KVStore,
@@ -122,32 +145,28 @@ where
122145
self.payments.lock().unwrap().contains_key(hash)
123146
}
124147

125-
pub(crate) fn update(
126-
&self, hash: &PaymentHash, update_preimage: Option<Option<PaymentPreimage>>,
127-
update_secret: Option<Option<PaymentSecret>>, update_amount_msat: Option<Option<u64>>,
128-
update_status: Option<PaymentStatus>,
129-
) -> Result<bool, Error> {
148+
pub(crate) fn update(&self, update: &PaymentDetailsUpdate) -> Result<bool, Error> {
130149
let mut updated = false;
131150
let mut locked_payments = self.payments.lock().unwrap();
132151

133-
if let Some(payment) = locked_payments.get_mut(hash) {
134-
if let Some(preimage_opt) = update_preimage {
152+
if let Some(payment) = locked_payments.get_mut(&update.hash) {
153+
if let Some(preimage_opt) = update.preimage {
135154
payment.preimage = preimage_opt;
136155
}
137156

138-
if let Some(secret_opt) = update_secret {
157+
if let Some(secret_opt) = update.secret {
139158
payment.secret = secret_opt;
140159
}
141160

142-
if let Some(amount_opt) = update_amount_msat {
161+
if let Some(amount_opt) = update.amount_msat {
143162
payment.amount_msat = amount_opt;
144163
}
145164

146-
if let Some(status) = update_status {
165+
if let Some(status) = update.status {
147166
payment.status = status;
148167
}
149168

150-
self.write_info_and_commit(hash, payment)?;
169+
self.write_info_and_commit(&update.hash, payment)?;
151170
updated = true;
152171
}
153172

@@ -237,10 +256,9 @@ mod tests {
237256
assert_eq!(Ok(true), payment_store.insert(payment));
238257
assert!(store.get_and_clear_did_persist());
239258

240-
assert_eq!(
241-
Ok(true),
242-
payment_store.update(&hash, None, None, None, Some(PaymentStatus::Succeeded))
243-
);
259+
let mut update = PaymentDetailsUpdate::new(hash);
260+
update.status = Some(PaymentStatus::Succeeded);
261+
assert_eq!(Ok(true), payment_store.update(&update));
244262
assert!(store.get_and_clear_did_persist());
245263

246264
assert_eq!(PaymentStatus::Succeeded, payment_store.get(&hash).unwrap().status);

0 commit comments

Comments
 (0)