@@ -25,8 +25,8 @@ use lightning::{
25
25
26
26
use lightning_types:: payment:: { PaymentHash , PaymentPreimage , PaymentSecret } ;
27
27
28
+ use std:: collections:: hash_map;
28
29
use std:: collections:: HashMap ;
29
- use std:: iter:: FromIterator ;
30
30
use std:: ops:: Deref ;
31
31
use std:: sync:: { Arc , Mutex } ;
32
32
use std:: time:: { Duration , SystemTime , UNIX_EPOCH } ;
@@ -448,6 +448,29 @@ impl PaymentDetailsUpdate {
448
448
}
449
449
}
450
450
451
+ impl From < & PaymentDetails > for PaymentDetailsUpdate {
452
+ fn from ( value : & PaymentDetails ) -> Self {
453
+ let ( hash, preimage, secret) = match value. kind {
454
+ PaymentKind :: Bolt11 { hash, preimage, secret, .. } => ( Some ( hash) , preimage, secret) ,
455
+ PaymentKind :: Bolt11Jit { hash, preimage, secret, .. } => ( Some ( hash) , preimage, secret) ,
456
+ PaymentKind :: Bolt12Offer { hash, preimage, secret, .. } => ( hash, preimage, secret) ,
457
+ PaymentKind :: Bolt12Refund { hash, preimage, secret, .. } => ( hash, preimage, secret) ,
458
+ PaymentKind :: Spontaneous { hash, preimage, .. } => ( Some ( hash) , preimage, None ) ,
459
+ _ => ( None , None , None ) ,
460
+ } ;
461
+
462
+ Self {
463
+ id : value. id ,
464
+ hash : Some ( hash) ,
465
+ preimage : Some ( preimage) ,
466
+ secret : Some ( secret) ,
467
+ amount_msat : Some ( value. amount_msat ) ,
468
+ direction : Some ( value. direction ) ,
469
+ status : Some ( value. status ) ,
470
+ }
471
+ }
472
+ }
473
+
451
474
pub ( crate ) struct PaymentStore < L : Deref >
452
475
where
453
476
L :: Target : LdkLogger ,
@@ -476,6 +499,28 @@ where
476
499
Ok ( updated)
477
500
}
478
501
502
+ pub ( crate ) fn insert_or_update ( & self , payment : & PaymentDetails ) -> Result < bool , Error > {
503
+ let mut locked_payments = self . payments . lock ( ) . unwrap ( ) ;
504
+
505
+ let updated;
506
+ match locked_payments. entry ( payment. id ) {
507
+ hash_map:: Entry :: Occupied ( mut e) => {
508
+ let update = payment. into ( ) ;
509
+ updated = e. get_mut ( ) . update ( & update) ;
510
+ if updated {
511
+ self . persist_info ( & payment. id , e. get ( ) ) ?;
512
+ }
513
+ } ,
514
+ hash_map:: Entry :: Vacant ( e) => {
515
+ e. insert ( payment. clone ( ) ) ;
516
+ self . persist_info ( & payment. id , payment) ?;
517
+ updated = true ;
518
+ } ,
519
+ }
520
+
521
+ Ok ( updated)
522
+ }
523
+
479
524
pub ( crate ) fn remove ( & self , id : & PaymentId ) -> Result < ( ) , Error > {
480
525
let store_key = hex_utils:: to_string ( & id. 0 ) ;
481
526
self . kv_store
0 commit comments