11use 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 ,
44} ;
55
66use crate :: logger:: { log_error, log_info, Logger } ;
@@ -18,7 +18,7 @@ use lightning::util::ser::{Readable, ReadableArgs, Writeable, Writer};
1818
1919use bitcoin:: secp256k1:: Secp256k1 ;
2020use rand:: { thread_rng, Rng } ;
21- use std:: collections:: { hash_map , VecDeque } ;
21+ use std:: collections:: VecDeque ;
2222use std:: ops:: Deref ;
2323use std:: sync:: { Arc , Condvar , Mutex } ;
2424use std:: time:: Duration ;
@@ -191,8 +191,7 @@ where
191191 channel_manager : Arc < ChannelManager > ,
192192 network_graph : Arc < NetworkGraph > ,
193193 keys_manager : Arc < KeysManager > ,
194- inbound_payments : Arc < PaymentInfoStorage > ,
195- outbound_payments : Arc < PaymentInfoStorage > ,
194+ payment_store : Arc < PaymentInfoStorage < K > > ,
196195 tokio_runtime : Arc < tokio:: runtime:: Runtime > ,
197196 logger : L ,
198197 _config : Arc < Config > ,
@@ -206,18 +205,16 @@ where
206205 pub fn new (
207206 wallet : Arc < Wallet < bdk:: database:: SqliteDatabase > > , event_queue : Arc < EventQueue < K > > ,
208207 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 > ,
212210 ) -> Self {
213211 Self {
214212 event_queue,
215213 wallet,
216214 channel_manager,
217215 network_graph,
218216 keys_manager,
219- inbound_payments,
220- outbound_payments,
217+ payment_store,
221218 logger,
222219 tokio_runtime,
223220 _config,
@@ -326,7 +323,9 @@ where
326323 hex_utils:: to_string( & payment_hash. 0 ) ,
327324 ) ;
328325 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" ) ;
330329 }
331330 }
332331 LdkEvent :: PaymentClaimed {
@@ -347,49 +346,50 @@ where
347346 }
348347 PaymentPurpose :: SpontaneousPayment ( preimage) => ( Some ( preimage) , None ) ,
349348 } ;
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- payment. amount_msat = Some ( amount_msat) ;
358- }
359- hash_map:: Entry :: Vacant ( e) => {
360- 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. amount_msat = Some ( amount_msat) ;
356+ payment_info
357+ } else {
358+ PaymentInfo {
361359 preimage : payment_preimage,
360+ payment_hash,
362361 secret : payment_secret,
363- status : PaymentStatus :: Succeeded ,
364362 amount_msat : Some ( amount_msat) ,
365- } ) ;
366- }
367- }
363+ direction : PaymentDirection :: Inbound ,
364+ status : PaymentStatus :: Succeeded ,
365+ }
366+ } ;
367+
368+ self . payment_store . insert ( payment_info) . expect ( "Failed to access payment store" ) ;
368369 self . event_queue
369370 . add_event ( Event :: PaymentReceived { payment_hash, amount_msat } )
370371 . expect ( "Failed to push to event queue" ) ;
371372 }
372373 LdkEvent :: PaymentSent { payment_preimage, payment_hash, fee_paid_msat, .. } => {
373- let mut payments = self . outbound_payments . lock ( ) . unwrap ( ) ;
374- for ( hash, payment) in payments. iter_mut ( ) {
375- if * hash == payment_hash {
376- payment. preimage = Some ( payment_preimage) ;
377- payment. status = PaymentStatus :: Succeeded ;
378- log_info ! (
379- self . logger,
380- "Successfully sent payment of {} msats{} from \
381- payment hash {:?} with preimage {:?}",
382- payment. amount_msat. unwrap( ) ,
383- if let Some ( fee) = fee_paid_msat {
384- format!( " (fee {} msats)" , fee)
385- } else {
386- "" . to_string( )
387- } ,
388- hex_utils:: to_string( & payment_hash. 0 ) ,
389- hex_utils:: to_string( & payment_preimage. 0 )
390- ) ;
391- break ;
392- }
374+ if let Some ( mut payment_info) = self . payment_store . get ( & payment_hash) {
375+ payment_info. preimage = Some ( payment_preimage) ;
376+ payment_info. status = PaymentStatus :: Succeeded ;
377+ self . payment_store
378+ . insert ( payment_info. clone ( ) )
379+ . expect ( "Failed to access payment store" ) ;
380+ log_info ! (
381+ self . logger,
382+ "Successfully sent payment of {} msats{} from \
383+ payment hash {:?} with preimage {:?}",
384+ payment_info. amount_msat. unwrap( ) ,
385+ if let Some ( fee) = fee_paid_msat {
386+ format!( " (fee {} msat)" , fee)
387+ } else {
388+ "" . to_string( )
389+ } ,
390+ hex_utils:: to_string( & payment_hash. 0 ) ,
391+ hex_utils:: to_string( & payment_preimage. 0 )
392+ ) ;
393393 }
394394 self . event_queue
395395 . add_event ( Event :: PaymentSuccessful { payment_hash } )
@@ -402,12 +402,9 @@ where
402402 hex_utils:: to_string( & payment_hash. 0 )
403403 ) ;
404404
405- let mut payments = self . outbound_payments . lock ( ) . unwrap ( ) ;
406- if payments. contains_key ( & payment_hash) {
407- let payment = payments. get_mut ( & payment_hash) . unwrap ( ) ;
408- assert_eq ! ( payment. status, PaymentStatus :: Pending ) ;
409- payment. status = PaymentStatus :: Failed ;
410- }
405+ self . payment_store
406+ . set_status ( & payment_hash, PaymentStatus :: Failed )
407+ . expect ( "Failed to access payment store" ) ;
411408 self . event_queue
412409 . add_event ( Event :: PaymentFailed { payment_hash } )
413410 . expect ( "Failed to push to event queue" ) ;
0 commit comments