Skip to content

Commit dc8884e

Browse files
committed
Co-locate pending payment indexes
Move the pending payment index record into the payment store module. This keeps the primary payment record and its pending index within the same persistence boundary. Co-Authored-By: HAL 9000
1 parent 5b0f459 commit dc8884e

3 files changed

Lines changed: 76 additions & 91 deletions

File tree

src/payment/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ pub(crate) mod asynchronous;
1111
mod bolt11;
1212
mod bolt12;
1313
mod onchain;
14-
pub(crate) mod pending_payment_store;
1514
mod spontaneous;
1615
pub(crate) mod store;
1716
mod unified;
@@ -20,8 +19,8 @@ pub use bolt11::Bolt11Payment;
2019
pub(crate) use bolt11::PaymentMetadata;
2120
pub use bolt12::Bolt12Payment;
2221
pub use onchain::OnchainPayment;
23-
pub(crate) use pending_payment_store::PendingPaymentDetails;
2422
pub use spontaneous::SpontaneousPayment;
23+
pub(crate) use store::PendingPaymentDetails;
2524
pub use store::{
2625
ConfirmationStatus, LSPS2Parameters, PaymentDetails, PaymentDirection, PaymentKind,
2726
PaymentStatus,

src/payment/pending_payment_store.rs

Lines changed: 0 additions & 89 deletions
This file was deleted.

src/payment/store.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,81 @@ impl StorableObjectUpdate<PaymentDetails> for PaymentDetailsUpdate {
586586
}
587587
}
588588

589+
/// Represents a pending payment
590+
#[derive(Clone, Debug, PartialEq, Eq)]
591+
pub(crate) struct PendingPaymentDetails {
592+
/// The full payment details
593+
pub details: PaymentDetails,
594+
/// Transaction IDs that have replaced or conflict with this payment.
595+
pub conflicting_txids: Vec<Txid>,
596+
}
597+
598+
impl PendingPaymentDetails {
599+
pub(crate) fn new(details: PaymentDetails, conflicting_txids: Vec<Txid>) -> Self {
600+
Self { details, conflicting_txids }
601+
}
602+
}
603+
604+
impl_writeable_tlv_based!(PendingPaymentDetails, {
605+
(0, details, required),
606+
(2, conflicting_txids, optional_vec),
607+
});
608+
609+
#[derive(Clone, Debug, PartialEq, Eq)]
610+
pub(crate) struct PendingPaymentDetailsUpdate {
611+
pub id: PaymentId,
612+
pub payment_update: Option<PaymentDetailsUpdate>,
613+
pub conflicting_txids: Option<Vec<Txid>>,
614+
}
615+
616+
impl StorableObject for PendingPaymentDetails {
617+
type Id = PaymentId;
618+
type Update = PendingPaymentDetailsUpdate;
619+
620+
fn id(&self) -> Self::Id {
621+
self.details.id
622+
}
623+
624+
fn update(&mut self, update: Self::Update) -> bool {
625+
let mut updated = false;
626+
627+
// Update the underlying payment details if present
628+
if let Some(payment_update) = update.payment_update {
629+
updated |= self.details.update(payment_update);
630+
}
631+
632+
if let Some(new_conflicting_txids) = update.conflicting_txids {
633+
if self.conflicting_txids != new_conflicting_txids {
634+
self.conflicting_txids = new_conflicting_txids;
635+
updated = true;
636+
}
637+
}
638+
639+
updated
640+
}
641+
642+
fn to_update(&self) -> Self::Update {
643+
self.into()
644+
}
645+
}
646+
647+
impl StorableObjectUpdate<PendingPaymentDetails> for PendingPaymentDetailsUpdate {
648+
fn id(&self) -> <PendingPaymentDetails as StorableObject>::Id {
649+
self.id
650+
}
651+
}
652+
653+
impl From<&PendingPaymentDetails> for PendingPaymentDetailsUpdate {
654+
fn from(value: &PendingPaymentDetails) -> Self {
655+
let conflicting_txids = if value.conflicting_txids.is_empty() {
656+
None
657+
} else {
658+
Some(value.conflicting_txids.clone())
659+
};
660+
Self { id: value.id(), payment_update: Some(value.details.to_update()), conflicting_txids }
661+
}
662+
}
663+
589664
#[cfg(test)]
590665
mod tests {
591666
use lightning::util::ser::{Readable, Writeable};

0 commit comments

Comments
 (0)