1
1
use crate :: {
2
- hex_utils, ChannelManager , Config , Error , KeysManager , NetworkGraph , PaymentInfo ,
3
- PaymentInfoStorage , PaymentStatus , Wallet ,
2
+ hex_utils, ChannelManager , Config , Error , KeysManager , NetworkGraph , PaymentDirection ,
3
+ PaymentInfo , PaymentInfoStorage , PaymentStatus , Wallet ,
4
4
} ;
5
5
6
6
use crate :: logger:: { log_error, log_info, Logger } ;
@@ -18,7 +18,7 @@ use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
18
18
19
19
use bitcoin:: secp256k1:: Secp256k1 ;
20
20
use rand:: { thread_rng, Rng } ;
21
- use std:: collections:: { hash_map , VecDeque } ;
21
+ use std:: collections:: VecDeque ;
22
22
use std:: ops:: Deref ;
23
23
use std:: sync:: { Arc , Condvar , Mutex } ;
24
24
use std:: time:: Duration ;
@@ -191,8 +191,7 @@ where
191
191
channel_manager : Arc < ChannelManager > ,
192
192
network_graph : Arc < NetworkGraph > ,
193
193
keys_manager : Arc < KeysManager > ,
194
- inbound_payments : Arc < PaymentInfoStorage > ,
195
- outbound_payments : Arc < PaymentInfoStorage > ,
194
+ payment_store : Arc < PaymentInfoStorage < K > > ,
196
195
tokio_runtime : Arc < tokio:: runtime:: Runtime > ,
197
196
logger : L ,
198
197
_config : Arc < Config > ,
@@ -206,18 +205,16 @@ where
206
205
pub fn new (
207
206
wallet : Arc < Wallet < bdk:: database:: SqliteDatabase > > , event_queue : Arc < EventQueue < K > > ,
208
207
channel_manager : Arc < ChannelManager > , network_graph : Arc < NetworkGraph > ,
209
- keys_manager : Arc < KeysManager > , inbound_payments : Arc < PaymentInfoStorage > ,
210
- outbound_payments : Arc < PaymentInfoStorage > , tokio_runtime : Arc < tokio:: runtime:: Runtime > ,
211
- logger : L , _config : Arc < Config > ,
208
+ keys_manager : Arc < KeysManager > , payment_store : Arc < PaymentInfoStorage < K > > ,
209
+ tokio_runtime : Arc < tokio:: runtime:: Runtime > , logger : L , _config : Arc < Config > ,
212
210
) -> Self {
213
211
Self {
214
212
event_queue,
215
213
wallet,
216
214
channel_manager,
217
215
network_graph,
218
216
keys_manager,
219
- inbound_payments,
220
- outbound_payments,
217
+ payment_store,
221
218
logger,
222
219
tokio_runtime,
223
220
_config,
@@ -326,7 +323,9 @@ where
326
323
hex_utils:: to_string( & payment_hash. 0 ) ,
327
324
) ;
328
325
self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
329
- self . inbound_payments . lock ( ) . unwrap ( ) . remove ( & payment_hash) ;
326
+ self . payment_store
327
+ . set_status ( & payment_hash, PaymentStatus :: Failed )
328
+ . expect ( "Failed to access payment store" ) ;
330
329
}
331
330
}
332
331
LdkEvent :: PaymentClaimed {
@@ -347,48 +346,49 @@ where
347
346
}
348
347
PaymentPurpose :: SpontaneousPayment ( preimage) => ( Some ( preimage) , None ) ,
349
348
} ;
350
- let mut payments = self . inbound_payments . lock ( ) . unwrap ( ) ;
351
- match payments. entry ( payment_hash) {
352
- hash_map:: Entry :: Occupied ( mut e) => {
353
- let payment = e. get_mut ( ) ;
354
- payment. status = PaymentStatus :: Succeeded ;
355
- payment. preimage = payment_preimage;
356
- payment. secret = payment_secret;
357
- }
358
- hash_map:: Entry :: Vacant ( e) => {
359
- e. insert ( PaymentInfo {
349
+
350
+ let payment_info =
351
+ if let Some ( mut payment_info) = self . payment_store . get ( & payment_hash) {
352
+ payment_info. status = PaymentStatus :: Succeeded ;
353
+ payment_info. preimage = payment_preimage;
354
+ payment_info. secret = payment_secret;
355
+ payment_info
356
+ } else {
357
+ PaymentInfo {
360
358
preimage : payment_preimage,
359
+ hash : payment_hash,
361
360
secret : payment_secret,
362
- status : PaymentStatus :: Succeeded ,
363
361
amount_msat : Some ( amount_msat) ,
364
- } ) ;
365
- }
366
- }
362
+ direction : PaymentDirection :: Inbound ,
363
+ status : PaymentStatus :: Succeeded ,
364
+ }
365
+ } ;
366
+
367
+ self . payment_store . insert ( payment_info) . expect ( "Failed to access payment store" ) ;
367
368
self . event_queue
368
369
. add_event ( Event :: PaymentReceived { payment_hash, amount_msat } )
369
370
. expect ( "Failed to push to event queue" ) ;
370
371
}
371
372
LdkEvent :: PaymentSent { payment_preimage, payment_hash, fee_paid_msat, .. } => {
372
- let mut payments = self . outbound_payments . lock ( ) . unwrap ( ) ;
373
- for ( hash, payment) in payments. iter_mut ( ) {
374
- if * hash == payment_hash {
375
- payment. preimage = Some ( payment_preimage) ;
376
- payment. status = PaymentStatus :: Succeeded ;
377
- log_info ! (
378
- self . logger,
379
- "Successfully sent payment of {} msats{} from \
380
- payment hash {:?} with preimage {:?}",
381
- payment. amount_msat. unwrap( ) ,
382
- if let Some ( fee) = fee_paid_msat {
383
- format!( " (fee {} msats)" , fee)
384
- } else {
385
- "" . to_string( )
386
- } ,
387
- hex_utils:: to_string( & payment_hash. 0 ) ,
388
- hex_utils:: to_string( & payment_preimage. 0 )
389
- ) ;
390
- break ;
391
- }
373
+ if let Some ( mut payment_info) = self . payment_store . get ( & payment_hash) {
374
+ payment_info. preimage = Some ( payment_preimage) ;
375
+ payment_info. status = PaymentStatus :: Succeeded ;
376
+ self . payment_store
377
+ . insert ( payment_info. clone ( ) )
378
+ . expect ( "Failed to access payment store" ) ;
379
+ log_info ! (
380
+ self . logger,
381
+ "Successfully sent payment of {} msats{} from \
382
+ payment hash {:?} with preimage {:?}",
383
+ payment_info. amount_msat. unwrap( ) ,
384
+ if let Some ( fee) = fee_paid_msat {
385
+ format!( " (fee {} msat)" , fee)
386
+ } else {
387
+ "" . to_string( )
388
+ } ,
389
+ hex_utils:: to_string( & payment_hash. 0 ) ,
390
+ hex_utils:: to_string( & payment_preimage. 0 )
391
+ ) ;
392
392
}
393
393
self . event_queue
394
394
. add_event ( Event :: PaymentSuccessful { payment_hash } )
@@ -401,12 +401,9 @@ where
401
401
hex_utils:: to_string( & payment_hash. 0 )
402
402
) ;
403
403
404
- let mut payments = self . outbound_payments . lock ( ) . unwrap ( ) ;
405
- if payments. contains_key ( & payment_hash) {
406
- let payment = payments. get_mut ( & payment_hash) . unwrap ( ) ;
407
- assert_eq ! ( payment. status, PaymentStatus :: Pending ) ;
408
- payment. status = PaymentStatus :: Failed ;
409
- }
404
+ self . payment_store
405
+ . set_status ( & payment_hash, PaymentStatus :: Failed )
406
+ . expect ( "Failed to access payment store" ) ;
410
407
self . event_queue
411
408
. add_event ( Event :: PaymentFailed { payment_hash } )
412
409
. expect ( "Failed to push to event queue" ) ;
0 commit comments