@@ -7,11 +7,10 @@ use lightning::ln::{PaymentHash, PaymentPreimage, PaymentSecret};
7
7
use lightning:: util:: ser:: Writeable ;
8
8
use lightning:: { impl_writeable_tlv_based, impl_writeable_tlv_based_enum} ;
9
9
10
- use std:: collections:: hash_map;
11
- use std:: collections:: { HashMap , HashSet } ;
10
+ use std:: collections:: HashMap ;
12
11
use std:: iter:: FromIterator ;
13
12
use std:: ops:: Deref ;
14
- use std:: sync:: { Mutex , MutexGuard } ;
13
+ use std:: sync:: Mutex ;
15
14
16
15
/// Represents a payment.
17
16
#[ derive( Clone , Debug , PartialEq , Eq ) ]
@@ -97,17 +96,13 @@ where
97
96
Self { payments, kv_store, logger }
98
97
}
99
98
100
- pub ( crate ) fn insert ( & self , payment_info : PaymentInfo ) -> Result < ( ) , Error > {
99
+ pub ( crate ) fn insert ( & self , payment_info : PaymentInfo ) -> Result < bool , Error > {
101
100
let mut locked_payments = self . payments . lock ( ) . unwrap ( ) ;
102
101
103
102
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)
111
106
}
112
107
113
108
pub ( crate ) fn remove ( & self , payment_hash : & PaymentHash ) -> Result < ( ) , Error > {
@@ -133,16 +128,36 @@ where
133
128
self . payments . lock ( ) . unwrap ( ) . contains_key ( payment_hash)
134
129
}
135
130
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 ;
139
137
let mut locked_payments = self . payments . lock ( ) . unwrap ( ) ;
140
138
141
139
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
+
143
156
self . write_info_and_commit ( payment_hash, payment_info) ?;
157
+ updated = true ;
144
158
}
145
- Ok ( ( ) )
159
+
160
+ Ok ( updated)
146
161
}
147
162
148
163
fn write_info_and_commit (
@@ -184,59 +199,6 @@ where
184
199
}
185
200
}
186
201
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
-
240
202
#[ cfg( test) ]
241
203
mod tests {
242
204
use super :: * ;
@@ -262,7 +224,25 @@ mod tests {
262
224
} ;
263
225
264
226
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( ) ) ) ;
266
229
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 ! (
235
+ Ok ( true ) ,
236
+ payment_info_store. update(
237
+ & payment_hash,
238
+ None ,
239
+ None ,
240
+ None ,
241
+ Some ( PaymentStatus :: Succeeded )
242
+ )
243
+ ) ;
244
+ assert ! ( store. get_and_clear_did_persist( ) ) ;
245
+
246
+ assert_eq ! ( PaymentStatus :: Succeeded , payment_info_store. get( & payment_hash) . unwrap( ) . status) ;
267
247
}
268
248
}
0 commit comments