Skip to content

Commit 3711ed1

Browse files
committed
f Introduce ternary enum PaymentStoreUpdateResult
1 parent 6aa4916 commit 3711ed1

File tree

3 files changed

+49
-16
lines changed

3 files changed

+49
-16
lines changed

src/event.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::logger::Logger;
2020

2121
use crate::payment::store::{
2222
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
23-
PaymentStore,
23+
PaymentStore, PaymentStoreUpdateResult,
2424
};
2525

2626
use crate::io::{
@@ -859,7 +859,7 @@ where
859859
let payment_id = PaymentId(payment_hash.0);
860860
log_info!(
861861
self.logger,
862-
"Claimed payment with ID {} from payment hash {} of {}msat.",
862+
"Claiming payment with ID {} from payment hash {} of {}msat.",
863863
payment_id,
864864
hex_utils::to_string(&payment_hash.0),
865865
amount_msat,
@@ -906,9 +906,16 @@ where
906906
};
907907

908908
match self.payment_store.update(&update) {
909-
Ok(_) => (
910-
// No need to do anything if the update was applied.
911-
),
909+
Ok(PaymentStoreUpdateResult::EntryUpdated)
910+
| Ok(PaymentStoreUpdateResult::EntryUnchanged) => (),
911+
Ok(PaymentStoreUpdateResult::EntryNotFound) => {
912+
log_error!(
913+
self.logger,
914+
"Failed to claim payment as ID {} couldn't be found in store",
915+
payment_id,
916+
);
917+
return Ok(());
918+
},
912919
Err(e) => {
913920
log_error!(
914921
self.logger,

src/payment/bolt11.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::liquidity::LiquiditySource;
1616
use crate::logger::{log_error, log_info, LdkLogger, Logger};
1717
use crate::payment::store::{
1818
LSPFeeLimits, PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind,
19-
PaymentStatus, PaymentStore,
19+
PaymentStatus, PaymentStore, PaymentStoreUpdateResult,
2020
};
2121
use crate::payment::SendingParameters;
2222
use crate::peer_store::{PeerInfo, PeerStore};
@@ -394,8 +394,8 @@ impl Bolt11Payment {
394394
/// failed back, e.g., if the correct preimage can't be retrieved in time before the claim
395395
/// deadline has been reached.
396396
///
397-
/// Will check that the payment is known and pending before failing the payment, and will
398-
/// return an error otherwise.
397+
/// Will check that the payment is known before failing the payment, and will return an error
398+
/// otherwise.
399399
///
400400
/// [`receive_for_hash`]: Self::receive_for_hash
401401
/// [`receive_variable_amount_for_hash`]: Self::receive_variable_amount_for_hash
@@ -409,8 +409,9 @@ impl Bolt11Payment {
409409
};
410410

411411
match self.payment_store.update(&update) {
412-
Ok(true) => (),
413-
Ok(false) => {
412+
Ok(PaymentStoreUpdateResult::EntryUpdated)
413+
| Ok(PaymentStoreUpdateResult::EntryUnchanged) => (),
414+
Ok(PaymentStoreUpdateResult::EntryNotFound) => {
414415
log_error!(
415416
self.logger,
416417
"Failed to manually fail unknown payment with hash {}",
@@ -421,7 +422,7 @@ impl Bolt11Payment {
421422
Err(e) => {
422423
log_error!(
423424
self.logger,
424-
"Failed to manually fail unknown payment with hash {}: {}",
425+
"Failed to manually fail payment with hash {}: {}",
425426
payment_hash,
426427
e
427428
);

src/payment/store.rs

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,13 @@ impl From<&PaymentDetails> for PaymentDetailsUpdate {
590590
}
591591
}
592592

593+
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
594+
pub(crate) enum PaymentStoreUpdateResult {
595+
EntryUpdated,
596+
EntryUnchanged,
597+
EntryNotFound,
598+
}
599+
593600
pub(crate) struct PaymentStore<L: Deref>
594601
where
595602
L::Target: LdkLogger,
@@ -670,17 +677,22 @@ where
670677
self.payments.lock().unwrap().get(id).cloned()
671678
}
672679

673-
pub(crate) fn update(&self, update: &PaymentDetailsUpdate) -> Result<bool, Error> {
674-
let mut updated = false;
680+
pub(crate) fn update(
681+
&self, update: &PaymentDetailsUpdate,
682+
) -> Result<PaymentStoreUpdateResult, Error> {
675683
let mut locked_payments = self.payments.lock().unwrap();
676684

677685
if let Some(payment) = locked_payments.get_mut(&update.id) {
678-
updated = payment.update(update);
686+
let updated = payment.update(update);
679687
if updated {
680688
self.persist_info(&update.id, payment)?;
689+
Ok(PaymentStoreUpdateResult::EntryUpdated)
690+
} else {
691+
Ok(PaymentStoreUpdateResult::EntryUnchanged)
681692
}
693+
} else {
694+
Ok(PaymentStoreUpdateResult::EntryNotFound)
682695
}
683-
Ok(updated)
684696
}
685697

686698
pub(crate) fn list_filter<F: FnMut(&&PaymentDetails) -> bool>(
@@ -789,9 +801,22 @@ mod tests {
789801
assert_eq!(Ok(true), payment_store.insert(payment));
790802
assert!(payment_store.get(&id).is_some());
791803

804+
// Check update returns `EntryUpdated`
792805
let mut update = PaymentDetailsUpdate::new(id);
793806
update.status = Some(PaymentStatus::Succeeded);
794-
assert_eq!(Ok(true), payment_store.update(&update));
807+
assert_eq!(Ok(PaymentStoreUpdateResult::EntryUpdated), payment_store.update(&update));
808+
809+
// Check no-op update yields `EntryUnchanged`
810+
let mut update = PaymentDetailsUpdate::new(id);
811+
update.status = Some(PaymentStatus::Succeeded);
812+
assert_eq!(Ok(PaymentStoreUpdateResult::EntryUnchanged), payment_store.update(&update));
813+
814+
// Check bogus update yields `EntryNotFound`
815+
let bogus_id = PaymentId([84u8; 32]);
816+
let mut update = PaymentDetailsUpdate::new(bogus_id);
817+
update.status = Some(PaymentStatus::Succeeded);
818+
assert_eq!(Ok(PaymentStoreUpdateResult::EntryNotFound), payment_store.update(&update));
819+
795820
assert!(payment_store.get(&id).is_some());
796821

797822
assert_eq!(PaymentStatus::Succeeded, payment_store.get(&id).unwrap().status);

0 commit comments

Comments
 (0)