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_given_level, log_info, log_internal, Logger } ;
@@ -17,7 +17,7 @@ use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
17
17
18
18
use bitcoin:: secp256k1:: Secp256k1 ;
19
19
use rand:: { thread_rng, Rng } ;
20
- use std:: collections:: { hash_map , VecDeque } ;
20
+ use std:: collections:: VecDeque ;
21
21
use std:: ops:: Deref ;
22
22
use std:: sync:: { Arc , Condvar , Mutex } ;
23
23
use std:: time:: Duration ;
@@ -238,8 +238,7 @@ where
238
238
channel_manager : Arc < ChannelManager > ,
239
239
network_graph : Arc < NetworkGraph > ,
240
240
keys_manager : Arc < KeysManager > ,
241
- inbound_payments : Arc < PaymentInfoStorage > ,
242
- outbound_payments : Arc < PaymentInfoStorage > ,
241
+ payment_store : Arc < PaymentInfoStorage < K > > ,
243
242
tokio_runtime : Arc < tokio:: runtime:: Runtime > ,
244
243
logger : L ,
245
244
_config : Arc < Config > ,
@@ -253,18 +252,16 @@ where
253
252
pub fn new (
254
253
wallet : Arc < Wallet < bdk:: sled:: Tree > > , event_queue : Arc < EventQueue < K > > ,
255
254
channel_manager : Arc < ChannelManager > , network_graph : Arc < NetworkGraph > ,
256
- keys_manager : Arc < KeysManager > , inbound_payments : Arc < PaymentInfoStorage > ,
257
- outbound_payments : Arc < PaymentInfoStorage > , tokio_runtime : Arc < tokio:: runtime:: Runtime > ,
258
- logger : L , _config : Arc < Config > ,
255
+ keys_manager : Arc < KeysManager > , payment_store : Arc < PaymentInfoStorage < K > > ,
256
+ tokio_runtime : Arc < tokio:: runtime:: Runtime > , logger : L , _config : Arc < Config > ,
259
257
) -> Self {
260
258
Self {
261
259
event_queue,
262
260
wallet,
263
261
channel_manager,
264
262
network_graph,
265
263
keys_manager,
266
- inbound_payments,
267
- outbound_payments,
264
+ payment_store,
268
265
logger,
269
266
tokio_runtime,
270
267
_config,
@@ -373,7 +370,9 @@ where
373
370
hex_utils:: to_string( & payment_hash. 0 ) ,
374
371
) ;
375
372
self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
376
- self . inbound_payments . lock ( ) . unwrap ( ) . remove ( & payment_hash) ;
373
+ self . payment_store
374
+ . set_payment_status ( & payment_hash, PaymentStatus :: Failed )
375
+ . expect ( "Failed to access payment store" ) ;
377
376
}
378
377
}
379
378
LdkEvent :: PaymentClaimed {
@@ -394,48 +393,51 @@ where
394
393
}
395
394
PaymentPurpose :: SpontaneousPayment ( preimage) => ( Some ( preimage) , None ) ,
396
395
} ;
397
- let mut payments = self . inbound_payments . lock ( ) . unwrap ( ) ;
398
- match payments. entry ( payment_hash) {
399
- hash_map:: Entry :: Occupied ( mut e) => {
400
- let payment = e. get_mut ( ) ;
401
- payment. status = PaymentStatus :: Succeeded ;
402
- payment. preimage = payment_preimage;
403
- payment. secret = payment_secret;
404
- }
405
- hash_map:: Entry :: Vacant ( e) => {
406
- e. insert ( PaymentInfo {
396
+
397
+ let payment_info =
398
+ if let Some ( mut payment_info) = self . payment_store . payment ( & payment_hash) {
399
+ payment_info. status = PaymentStatus :: Succeeded ;
400
+ payment_info. preimage = payment_preimage;
401
+ payment_info. secret = payment_secret;
402
+ payment_info
403
+ } else {
404
+ PaymentInfo {
407
405
preimage : payment_preimage,
406
+ hash : payment_hash,
408
407
secret : payment_secret,
409
- status : PaymentStatus :: Succeeded ,
410
408
amount_msat : Some ( amount_msat) ,
411
- } ) ;
412
- }
413
- }
409
+ direction : PaymentDirection :: Inbound ,
410
+ status : PaymentStatus :: Succeeded ,
411
+ }
412
+ } ;
413
+
414
+ self . payment_store
415
+ . insert_payment ( payment_info)
416
+ . expect ( "Failed to access payment store" ) ;
414
417
self . event_queue
415
418
. add_event ( Event :: PaymentReceived { payment_hash, amount_msat } )
416
419
. expect ( "Failed to push to event queue" ) ;
417
420
}
418
421
LdkEvent :: PaymentSent { payment_preimage, payment_hash, fee_paid_msat, .. } => {
419
- let mut payments = self . outbound_payments . lock ( ) . unwrap ( ) ;
420
- for ( hash, payment) in payments. iter_mut ( ) {
421
- if * hash == payment_hash {
422
- payment. preimage = Some ( payment_preimage) ;
423
- payment. status = PaymentStatus :: Succeeded ;
424
- log_info ! (
425
- self . logger,
426
- "Successfully sent payment of {} msats{} from \
427
- payment hash {:?} with preimage {:?}",
428
- payment. amount_msat. unwrap( ) ,
429
- if let Some ( fee) = fee_paid_msat {
430
- format!( " (fee {} msats)" , fee)
431
- } else {
432
- "" . to_string( )
433
- } ,
434
- hex_utils:: to_string( & payment_hash. 0 ) ,
435
- hex_utils:: to_string( & payment_preimage. 0 )
436
- ) ;
437
- break ;
438
- }
422
+ if let Some ( mut payment_info) = self . payment_store . payment ( & payment_hash) {
423
+ payment_info. preimage = Some ( payment_preimage) ;
424
+ payment_info. status = PaymentStatus :: Succeeded ;
425
+ self . payment_store
426
+ . insert_payment ( payment_info. clone ( ) )
427
+ . expect ( "Failed to access payment store" ) ;
428
+ log_info ! (
429
+ self . logger,
430
+ "Successfully sent payment of {} msats{} from \
431
+ payment hash {:?} with preimage {:?}",
432
+ payment_info. amount_msat. unwrap( ) ,
433
+ if let Some ( fee) = fee_paid_msat {
434
+ format!( " (fee {} msat)" , fee)
435
+ } else {
436
+ "" . to_string( )
437
+ } ,
438
+ hex_utils:: to_string( & payment_hash. 0 ) ,
439
+ hex_utils:: to_string( & payment_preimage. 0 )
440
+ ) ;
439
441
}
440
442
self . event_queue
441
443
. add_event ( Event :: PaymentSuccessful { payment_hash } )
@@ -448,12 +450,9 @@ where
448
450
hex_utils:: to_string( & payment_hash. 0 )
449
451
) ;
450
452
451
- let mut payments = self . outbound_payments . lock ( ) . unwrap ( ) ;
452
- if payments. contains_key ( & payment_hash) {
453
- let payment = payments. get_mut ( & payment_hash) . unwrap ( ) ;
454
- assert_eq ! ( payment. status, PaymentStatus :: Pending ) ;
455
- payment. status = PaymentStatus :: Failed ;
456
- }
453
+ self . payment_store
454
+ . set_payment_status ( & payment_hash, PaymentStatus :: Failed )
455
+ . expect ( "Failed to access payment store" ) ;
457
456
self . event_queue
458
457
. add_event ( Event :: PaymentFailed { payment_hash } )
459
458
. expect ( "Failed to push to event queue" ) ;
0 commit comments