@@ -10,8 +10,8 @@ use std::ops::Deref;
1010use std:: sync:: { Arc , Mutex } ;
1111
1212use bitcoin:: Txid ;
13- use lightning:: impl_writeable_tlv_based;
1413use lightning:: ln:: channelmanager:: PaymentId ;
14+ use lightning:: { impl_writeable_tlv_based, impl_writeable_tlv_based_enum} ;
1515use lightning_types:: payment:: PaymentHash ;
1616
1717use 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 ) ]
4967pub 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
6280impl 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
85107impl_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
101123impl 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