@@ -7,11 +7,10 @@ use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
77use lightning:: util:: ser:: Writeable ;
88use lightning:: { impl_writeable_tlv_based, impl_writeable_tlv_based_enum} ;
99
10- use std:: collections:: hash_map;
11- use std:: collections:: { HashMap , HashSet } ;
10+ use std:: collections:: HashMap ;
1211use std:: iter:: FromIterator ;
1312use std:: ops:: Deref ;
14- use std:: sync:: { Mutex , MutexGuard } ;
13+ use std:: sync:: Mutex ;
1514
1615/// Represents a payment.
1716#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -97,17 +96,13 @@ where
9796 Self { payments, kv_store, logger }
9897 }
9998
100- pub ( crate ) fn insert ( & self , payment_info : PaymentInfo ) -> Result < ( ) , Error > {
99+ pub ( crate ) fn insert ( & self , payment_info : PaymentInfo ) -> Result < bool , Error > {
101100 let mut locked_payments = self . payments . lock ( ) . unwrap ( ) ;
102101
103102 let payment_hash = payment_info. payment_hash . clone ( ) ;
104- locked_payments. insert ( payment_hash. clone ( ) , payment_info. clone ( ) ) ;
105- self . write_info_and_commit ( & payment_hash, & payment_info)
106- }
107-
108- pub ( crate ) fn lock ( & self ) -> Result < PaymentInfoGuard < K > , ( ) > {
109- let locked_store = self . payments . lock ( ) . map_err ( |_| ( ) ) ?;
110- Ok ( PaymentInfoGuard :: new ( locked_store, self . kv_store . clone ( ) ) )
103+ let updated = locked_payments. insert ( payment_hash. clone ( ) , payment_info. clone ( ) ) . is_some ( ) ;
104+ self . write_info_and_commit ( & payment_hash, & payment_info) ?;
105+ Ok ( updated)
111106 }
112107
113108 pub ( crate ) fn remove ( & self , payment_hash : & PaymentHash ) -> Result < ( ) , Error > {
@@ -133,16 +128,36 @@ where
133128 self . payments . lock ( ) . unwrap ( ) . contains_key ( payment_hash)
134129 }
135130
136- pub ( crate ) fn set_status (
137- & self , payment_hash : & PaymentHash , payment_status : PaymentStatus ,
138- ) -> Result < ( ) , Error > {
131+ pub ( crate ) fn update (
132+ & self , payment_hash : & PaymentHash , update_preimage : Option < Option < PaymentPreimage > > ,
133+ update_secret : Option < Option < PaymentSecret > > , update_amount_msat : Option < Option < u64 > > ,
134+ update_status : Option < PaymentStatus > ,
135+ ) -> Result < bool , Error > {
136+ let mut updated = false ;
139137 let mut locked_payments = self . payments . lock ( ) . unwrap ( ) ;
140138
141139 if let Some ( payment_info) = locked_payments. get_mut ( payment_hash) {
142- payment_info. status = payment_status;
140+ if let Some ( preimage_opt) = update_preimage {
141+ payment_info. preimage = preimage_opt;
142+ }
143+
144+ if let Some ( secret_opt) = update_secret {
145+ payment_info. secret = secret_opt;
146+ }
147+
148+ if let Some ( amount_opt) = update_amount_msat {
149+ payment_info. amount_msat = amount_opt;
150+ }
151+
152+ if let Some ( status) = update_status {
153+ payment_info. status = status;
154+ }
155+
143156 self . write_info_and_commit ( payment_hash, payment_info) ?;
157+ updated = true ;
144158 }
145- Ok ( ( ) )
159+
160+ Ok ( updated)
146161 }
147162
148163 fn write_info_and_commit (
@@ -184,59 +199,6 @@ where
184199 }
185200}
186201
187- pub ( crate ) struct PaymentInfoGuard < ' a , K : Deref >
188- where
189- K :: Target : KVStore ,
190- {
191- inner : MutexGuard < ' a , HashMap < PaymentHash , PaymentInfo > > ,
192- touched_keys : HashSet < PaymentHash > ,
193- kv_store : K ,
194- }
195-
196- impl < ' a , K : Deref > PaymentInfoGuard < ' a , K >
197- where
198- K :: Target : KVStore ,
199- {
200- pub fn new ( inner : MutexGuard < ' a , HashMap < PaymentHash , PaymentInfo > > , kv_store : K ) -> Self {
201- let touched_keys = HashSet :: new ( ) ;
202- Self { inner, touched_keys, kv_store }
203- }
204-
205- pub fn entry (
206- & mut self , payment_hash : PaymentHash ,
207- ) -> hash_map:: Entry < PaymentHash , PaymentInfo > {
208- self . touched_keys . insert ( payment_hash) ;
209- self . inner . entry ( payment_hash)
210- }
211- }
212-
213- impl < ' a , K : Deref > Drop for PaymentInfoGuard < ' a , K >
214- where
215- K :: Target : KVStore ,
216- {
217- fn drop ( & mut self ) {
218- for key in self . touched_keys . iter ( ) {
219- let store_key = hex_utils:: to_string ( & key. 0 ) ;
220-
221- match self . inner . entry ( * key) {
222- hash_map:: Entry :: Vacant ( _) => {
223- self . kv_store
224- . remove ( PAYMENT_INFO_PERSISTENCE_NAMESPACE , & store_key)
225- . expect ( "Persistence failed" ) ;
226- }
227- hash_map:: Entry :: Occupied ( e) => {
228- let mut writer = self
229- . kv_store
230- . write ( PAYMENT_INFO_PERSISTENCE_NAMESPACE , & store_key)
231- . expect ( "Persistence failed" ) ;
232- e. get ( ) . write ( & mut writer) . expect ( "Persistence failed" ) ;
233- writer. commit ( ) . expect ( "Persistence failed" ) ;
234- }
235- } ;
236- }
237- }
238- }
239-
240202#[ cfg( test) ]
241203mod tests {
242204 use super :: * ;
@@ -262,7 +224,16 @@ mod tests {
262224 } ;
263225
264226 assert ! ( !store. get_and_clear_did_persist( ) ) ;
265- payment_info_store. lock ( ) . unwrap ( ) . entry ( payment_hash) . or_insert ( payment_info) ;
227+
228+ assert_eq ! ( Ok ( false ) , payment_info_store. insert( payment_info. clone( ) ) ) ;
266229 assert ! ( store. get_and_clear_did_persist( ) ) ;
230+
231+ assert_eq ! ( Ok ( true ) , payment_info_store. insert( payment_info) ) ;
232+ assert ! ( store. get_and_clear_did_persist( ) ) ;
233+
234+ assert_eq ! ( Ok ( true ) , payment_info_store. update( & payment_hash, None , None , None , Some ( PaymentStatus :: Succeeded ) ) ) ;
235+ assert ! ( store. get_and_clear_did_persist( ) ) ;
236+
237+ assert_eq ! ( PaymentStatus :: Succeeded , payment_info_store. get( & payment_hash) . unwrap( ) . status) ;
267238 }
268239}
0 commit comments