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,
@@ -298,9 +295,25 @@ where
298
295
via_channel_id : _,
299
296
via_user_channel_id : _,
300
297
} => {
298
+ if let Some ( info) = self . payment_store . get ( & payment_hash) {
299
+ if info. status == PaymentStatus :: Succeeded {
300
+ log_info ! (
301
+ self . logger,
302
+ "Refused duplicate inbound payment from payment hash {} of {}msat" ,
303
+ hex_utils:: to_string( & payment_hash. 0 ) ,
304
+ amount_msat,
305
+ ) ;
306
+ self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
307
+ self . payment_store
308
+ . set_status ( & payment_hash, PaymentStatus :: Failed )
309
+ . expect ( "Failed to access payment store" ) ;
310
+ return ;
311
+ }
312
+ }
313
+
301
314
log_info ! (
302
315
self . logger,
303
- "Received payment from payment hash {} of {} msats " ,
316
+ "Received payment from payment hash {} of {}msat " ,
304
317
hex_utils:: to_string( & payment_hash. 0 ) ,
305
318
amount_msat,
306
319
) ;
@@ -326,7 +339,9 @@ where
326
339
hex_utils:: to_string( & payment_hash. 0 ) ,
327
340
) ;
328
341
self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
329
- self . inbound_payments . lock ( ) . unwrap ( ) . remove ( & payment_hash) ;
342
+ self . payment_store
343
+ . set_status ( & payment_hash, PaymentStatus :: Failed )
344
+ . expect ( "Failed to access payment store" ) ;
330
345
}
331
346
}
332
347
LdkEvent :: PaymentClaimed {
@@ -337,7 +352,7 @@ where
337
352
} => {
338
353
log_info ! (
339
354
self . logger,
340
- "Claimed payment from payment hash {} of {} msats ." ,
355
+ "Claimed payment from payment hash {} of {}msat ." ,
341
356
hex_utils:: to_string( & payment_hash. 0 ) ,
342
357
amount_msat,
343
358
) ;
@@ -347,49 +362,50 @@ where
347
362
}
348
363
PaymentPurpose :: SpontaneousPayment ( preimage) => ( Some ( preimage) , None ) ,
349
364
} ;
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 {
365
+
366
+ let payment_info =
367
+ if let Some ( mut payment_info) = self . payment_store . get ( & payment_hash) {
368
+ payment_info. status = PaymentStatus :: Succeeded ;
369
+ payment_info. preimage = payment_preimage;
370
+ payment_info. secret = payment_secret;
371
+ payment_info. amount_msat = Some ( amount_msat) ;
372
+ payment_info
373
+ } else {
374
+ PaymentInfo {
361
375
preimage : payment_preimage,
376
+ payment_hash,
362
377
secret : payment_secret,
363
- status : PaymentStatus :: Succeeded ,
364
378
amount_msat : Some ( amount_msat) ,
365
- } ) ;
366
- }
367
- }
379
+ direction : PaymentDirection :: Inbound ,
380
+ status : PaymentStatus :: Succeeded ,
381
+ }
382
+ } ;
383
+
384
+ self . payment_store . insert ( payment_info) . expect ( "Failed to access payment store" ) ;
368
385
self . event_queue
369
386
. add_event ( Event :: PaymentReceived { payment_hash, amount_msat } )
370
387
. expect ( "Failed to push to event queue" ) ;
371
388
}
372
389
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
- }
390
+ if let Some ( mut payment_info) = self . payment_store . get ( & payment_hash) {
391
+ payment_info. preimage = Some ( payment_preimage) ;
392
+ payment_info. status = PaymentStatus :: Succeeded ;
393
+ self . payment_store
394
+ . insert ( payment_info. clone ( ) )
395
+ . expect ( "Failed to access payment store" ) ;
396
+ log_info ! (
397
+ self . logger,
398
+ "Successfully sent payment of {}msat{} from \
399
+ payment hash {:?} with preimage {:?}",
400
+ payment_info. amount_msat. unwrap( ) ,
401
+ if let Some ( fee) = fee_paid_msat {
402
+ format!( " (fee {} msat)" , fee)
403
+ } else {
404
+ "" . to_string( )
405
+ } ,
406
+ hex_utils:: to_string( & payment_hash. 0 ) ,
407
+ hex_utils:: to_string( & payment_preimage. 0 )
408
+ ) ;
393
409
}
394
410
self . event_queue
395
411
. add_event ( Event :: PaymentSuccessful { payment_hash } )
@@ -402,12 +418,9 @@ where
402
418
hex_utils:: to_string( & payment_hash. 0 )
403
419
) ;
404
420
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
- }
421
+ self . payment_store
422
+ . set_status ( & payment_hash, PaymentStatus :: Failed )
423
+ . expect ( "Failed to access payment store" ) ;
411
424
self . event_queue
412
425
. add_event ( Event :: PaymentFailed { payment_hash } )
413
426
. expect ( "Failed to push to event queue" ) ;
@@ -493,15 +506,15 @@ where
493
506
if claim_from_onchain_tx {
494
507
log_info ! (
495
508
self . logger,
496
- "Forwarded payment{}{}, earning {} msats in fees from claiming onchain." ,
509
+ "Forwarded payment{}{}, earning {}msat in fees from claiming onchain." ,
497
510
from_prev_str,
498
511
to_next_str,
499
512
fee_earned,
500
513
) ;
501
514
} else {
502
515
log_info ! (
503
516
self . logger,
504
- "Forwarded payment{}{}, earning {} msats in fees." ,
517
+ "Forwarded payment{}{}, earning {}msat in fees." ,
505
518
from_prev_str,
506
519
to_next_str,
507
520
fee_earned,
0 commit comments