Skip to content

Commit d4e67d1

Browse files
committed
f Use enum pending expiries
Represent pending-payment pruning conditions as either wall-clock time or block height so later manual BOLT11 handling can keep the claimable phase tied to the HTLC claim deadline. Co-Authored-By: HAL 9000
1 parent 59e9707 commit d4e67d1

2 files changed

Lines changed: 37 additions & 15 deletions

File tree

src/payment/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ pub(crate) use bolt11::PaymentMetadata;
2121
pub use bolt12::Bolt12Payment;
2222
pub use onchain::OnchainPayment;
2323
pub(crate) use pending_payment_store::{
24-
FundingTxCandidate, PendingPaymentDetails, PendingPaymentStore,
24+
FundingTxCandidate, PendingPaymentDetails, PendingPaymentExpiry, PendingPaymentStore,
2525
};
2626
pub use spontaneous::SpontaneousPayment;
2727
pub use store::{

src/payment/pending_payment_store.rs

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use std::ops::Deref;
1010
use std::sync::{Arc, Mutex};
1111

1212
use bitcoin::Txid;
13-
use lightning::impl_writeable_tlv_based;
1413
use lightning::ln::channelmanager::PaymentId;
14+
use lightning::{impl_writeable_tlv_based, impl_writeable_tlv_based_enum};
1515
use lightning_types::payment::PaymentHash;
1616

1717
use crate::data_store::{DataStore, StorableObject, StorableObjectUpdate};
@@ -44,6 +44,24 @@ impl_writeable_tlv_based!(FundingTxCandidate, {
4444
(4, fee_paid_msat, option),
4545
});
4646

47+
/// The condition after which this pending payment can be pruned.
48+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
49+
pub(crate) enum PendingPaymentExpiry {
50+
/// The Unix timestamp after which this pending payment can be pruned.
51+
Time { timestamp: u64 },
52+
/// The block height after which this pending payment can be pruned.
53+
Height { height: u32 },
54+
}
55+
56+
impl_writeable_tlv_based_enum!(PendingPaymentExpiry,
57+
(0, Time) => {
58+
(0, timestamp, required),
59+
},
60+
(2, Height) => {
61+
(0, height, required),
62+
},
63+
);
64+
4765
/// Represents a pending payment
4866
#[derive(Clone, Debug, PartialEq, Eq)]
4967
pub struct PendingPaymentDetails {
@@ -55,38 +73,42 @@ pub struct PendingPaymentDetails {
5573
/// RBF history, keyed by each candidate's txid. Empty for non-funding payments and for
5674
/// records written before per-candidate tracking existed.
5775
pub(crate) candidates: Vec<FundingTxCandidate>,
58-
/// The timestamp after which this pending payment can be pruned.
59-
pub expires_at: Option<u64>,
76+
/// The condition after which this pending payment can be pruned.
77+
pub(crate) expiry: Option<PendingPaymentExpiry>,
6078
}
6179

6280
impl PendingPaymentDetails {
6381
pub(crate) fn new(
6482
details: PaymentDetails, conflicting_txids: Vec<Txid>, candidates: Vec<FundingTxCandidate>,
6583
) -> Self {
66-
Self { details, conflicting_txids, candidates, expires_at: None }
84+
Self { details, conflicting_txids, candidates, expiry: None }
6785
}
6886

6987
pub(crate) fn new_with_expiry(
70-
details: PaymentDetails, conflicting_txids: Vec<Txid>, expires_at: Option<u64>,
88+
details: PaymentDetails, conflicting_txids: Vec<Txid>, expiry: Option<PendingPaymentExpiry>,
7189
) -> Self {
72-
Self { details, conflicting_txids, candidates: Vec::new(), expires_at }
90+
Self { details, conflicting_txids, candidates: Vec::new(), expiry }
7391
}
7492

7593
/// Returns this node's recorded funding figures for the candidate with the given txid, if any.
7694
pub(crate) fn candidate(&self, txid: Txid) -> Option<&FundingTxCandidate> {
7795
self.candidates.iter().find(|candidate| candidate.txid == txid)
7896
}
7997

80-
pub(crate) fn has_expired(&self, now: u64) -> bool {
81-
self.expires_at.map_or(false, |expires_at| expires_at <= now)
98+
pub(crate) fn has_expired(&self, now: u64, current_height: u32) -> bool {
99+
match self.expiry {
100+
Some(PendingPaymentExpiry::Time { timestamp }) => timestamp <= now,
101+
Some(PendingPaymentExpiry::Height { height }) => height <= current_height,
102+
None => false,
103+
}
82104
}
83105
}
84106

85107
impl_writeable_tlv_based!(PendingPaymentDetails, {
86108
(0, details, required),
87109
(2, conflicting_txids, optional_vec),
88110
(4, candidates, optional_vec),
89-
(6, expires_at, option),
111+
(6, expiry, option),
90112
});
91113

92114
#[derive(Clone, Debug, PartialEq, Eq)]
@@ -95,7 +117,7 @@ pub(crate) struct PendingPaymentDetailsUpdate {
95117
pub payment_update: Option<PaymentDetailsUpdate>,
96118
pub conflicting_txids: Option<Vec<Txid>>,
97119
pub candidates: Vec<FundingTxCandidate>,
98-
pub expires_at: Option<Option<u64>>,
120+
pub expiry: Option<Option<PendingPaymentExpiry>>,
99121
}
100122

101123
impl StorableObject for PendingPaymentDetails {
@@ -134,9 +156,9 @@ impl StorableObject for PendingPaymentDetails {
134156
updated = true;
135157
}
136158

137-
if let Some(new_expires_at) = update.expires_at {
138-
if self.expires_at != new_expires_at {
139-
self.expires_at = new_expires_at;
159+
if let Some(new_expiry) = update.expiry {
160+
if self.expiry != new_expiry {
161+
self.expiry = new_expiry;
140162
updated = true;
141163
}
142164
}
@@ -167,7 +189,7 @@ impl From<&PendingPaymentDetails> for PendingPaymentDetailsUpdate {
167189
payment_update: Some(value.details.to_update()),
168190
conflicting_txids,
169191
candidates: value.candidates.clone(),
170-
expires_at: Some(value.expires_at),
192+
expiry: Some(value.expiry),
171193
}
172194
}
173195
}

0 commit comments

Comments
 (0)