@@ -433,9 +433,19 @@ where
433
433
onion_fields : _,
434
434
counterparty_skimmed_fee_msat,
435
435
} => {
436
- let payment_id = PaymentId ( payment_hash. 0 ) ;
436
+ let payment_id = match purpose {
437
+ PaymentPurpose :: Bolt11InvoicePayment { .. } => PaymentId ( payment_hash. 0 ) ,
438
+ PaymentPurpose :: Bolt12OfferPayment { ref payment_context, .. } => {
439
+ PaymentId ( payment_context. offer_id . 0 )
440
+ } ,
441
+ PaymentPurpose :: Bolt12RefundPayment { .. } => PaymentId ( payment_hash. 0 ) ,
442
+ PaymentPurpose :: SpontaneousPayment ( ..) => PaymentId ( payment_hash. 0 ) ,
443
+ } ;
444
+
437
445
if let Some ( info) = self . payment_store . get ( & payment_id) {
438
- if info. status == PaymentStatus :: Succeeded {
446
+ if info. status == PaymentStatus :: Succeeded
447
+ || matches ! ( info. kind, PaymentKind :: Spontaneous { .. } )
448
+ {
439
449
log_info ! (
440
450
self . logger,
441
451
"Refused duplicate inbound payment from payment hash {} of {}msat" ,
@@ -445,6 +455,7 @@ where
445
455
self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
446
456
447
457
let update = PaymentDetailsUpdate {
458
+ hash : Some ( Some ( payment_hash) ) ,
448
459
status : Some ( PaymentStatus :: Failed ) ,
449
460
..PaymentDetailsUpdate :: new ( payment_id)
450
461
} ;
@@ -483,6 +494,7 @@ where
483
494
self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
484
495
485
496
let update = PaymentDetailsUpdate {
497
+ hash : Some ( Some ( payment_hash) ) ,
486
498
status : Some ( PaymentStatus :: Failed ) ,
487
499
..PaymentDetailsUpdate :: new ( payment_id)
488
500
} ;
@@ -502,48 +514,75 @@ where
502
514
) ;
503
515
let payment_preimage = match purpose {
504
516
PaymentPurpose :: Bolt11InvoicePayment { payment_preimage, payment_secret } => {
505
- if payment_preimage. is_some ( ) {
506
- payment_preimage
507
- } else {
508
- self . channel_manager
509
- . get_payment_preimage ( payment_hash, payment_secret)
510
- . ok ( )
511
- }
517
+ payment_preimage. or ( self
518
+ . channel_manager
519
+ . get_payment_preimage ( payment_hash, payment_secret)
520
+ . ok ( ) )
512
521
} ,
513
- PaymentPurpose :: Bolt12OfferPayment { .. } => {
514
- // TODO: support BOLT12.
515
- log_error ! (
516
- self . logger,
517
- "Failed to claim unsupported BOLT12 payment with hash: {}" ,
518
- payment_hash
519
- ) ;
520
- self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
521
- return ;
522
- } ,
523
- PaymentPurpose :: Bolt12RefundPayment { .. } => {
524
- // TODO: support BOLT12.
525
- log_error ! (
526
- self . logger,
527
- "Failed to claim unsupported BOLT12 payment with hash: {}" ,
528
- payment_hash
529
- ) ;
530
- self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
531
- return ;
522
+ PaymentPurpose :: Bolt12OfferPayment {
523
+ payment_preimage, payment_secret, ..
524
+ } => payment_preimage. or ( self
525
+ . channel_manager
526
+ . get_payment_preimage ( payment_hash, payment_secret)
527
+ . ok ( ) ) ,
528
+ PaymentPurpose :: Bolt12RefundPayment {
529
+ payment_preimage,
530
+ payment_secret,
531
+ ..
532
+ } => payment_preimage. or ( self
533
+ . channel_manager
534
+ . get_payment_preimage ( payment_hash, payment_secret)
535
+ . ok ( ) ) ,
536
+ PaymentPurpose :: SpontaneousPayment ( preimage) => {
537
+ // Since it's spontaneous, we insert it now into our store.
538
+ let payment = PaymentDetails {
539
+ id : payment_id,
540
+ kind : PaymentKind :: Spontaneous {
541
+ hash : payment_hash,
542
+ preimage : Some ( preimage) ,
543
+ } ,
544
+ amount_msat : Some ( amount_msat) ,
545
+ direction : PaymentDirection :: Inbound ,
546
+ status : PaymentStatus :: Pending ,
547
+ } ;
548
+
549
+ match self . payment_store . insert ( payment) {
550
+ Ok ( false ) => ( ) ,
551
+ Ok ( true ) => {
552
+ log_error ! (
553
+ self . logger,
554
+ "Spontaneous payment with ID {} was previously known" ,
555
+ payment_id,
556
+ ) ;
557
+ debug_assert ! ( false ) ;
558
+ } ,
559
+ Err ( e) => {
560
+ log_error ! (
561
+ self . logger,
562
+ "Failed to insert payment with ID {}: {}" ,
563
+ payment_id,
564
+ e
565
+ ) ;
566
+ debug_assert ! ( false ) ;
567
+ } ,
568
+ }
569
+
570
+ Some ( preimage)
532
571
} ,
533
- PaymentPurpose :: SpontaneousPayment ( preimage) => Some ( preimage) ,
534
572
} ;
535
573
536
574
if let Some ( preimage) = payment_preimage {
537
575
self . channel_manager . claim_funds ( preimage) ;
538
576
} else {
539
577
log_error ! (
540
578
self . logger,
541
- "Failed to claim payment with hash {}: preimage unknown." ,
542
- hex_utils :: to_string ( & payment_hash . 0 ) ,
579
+ "Failed to claim payment with ID {}: preimage unknown." ,
580
+ payment_id ,
543
581
) ;
544
582
self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
545
583
546
584
let update = PaymentDetailsUpdate {
585
+ hash : Some ( Some ( payment_hash) ) ,
547
586
status : Some ( PaymentStatus :: Failed ) ,
548
587
..PaymentDetailsUpdate :: new ( payment_id)
549
588
} ;
@@ -561,99 +600,87 @@ where
561
600
htlcs : _,
562
601
sender_intended_total_msat : _,
563
602
} => {
603
+ let payment_id = match purpose {
604
+ PaymentPurpose :: Bolt11InvoicePayment { .. } => PaymentId ( payment_hash. 0 ) ,
605
+ PaymentPurpose :: Bolt12OfferPayment { ref payment_context, .. } => {
606
+ PaymentId ( payment_context. offer_id . 0 )
607
+ } ,
608
+ PaymentPurpose :: Bolt12RefundPayment { .. } => PaymentId ( payment_hash. 0 ) ,
609
+ PaymentPurpose :: SpontaneousPayment ( ..) => PaymentId ( payment_hash. 0 ) ,
610
+ } ;
611
+
564
612
log_info ! (
565
613
self . logger,
566
- "Claimed payment from payment hash {} of {}msat." ,
614
+ "Claimed payment with ID {} from payment hash {} of {}msat." ,
615
+ payment_id,
567
616
hex_utils:: to_string( & payment_hash. 0 ) ,
568
617
amount_msat,
569
618
) ;
570
- let payment_id = PaymentId ( payment_hash . 0 ) ;
571
- match purpose {
619
+
620
+ let update = match purpose {
572
621
PaymentPurpose :: Bolt11InvoicePayment {
573
622
payment_preimage,
574
623
payment_secret,
575
624
..
576
- } => {
577
- let update = PaymentDetailsUpdate {
578
- preimage : Some ( payment_preimage) ,
579
- secret : Some ( Some ( payment_secret) ) ,
580
- amount_msat : Some ( Some ( amount_msat) ) ,
581
- status : Some ( PaymentStatus :: Succeeded ) ,
582
- ..PaymentDetailsUpdate :: new ( payment_id)
583
- } ;
584
- match self . payment_store . update ( & update) {
585
- Ok ( true ) => ( ) ,
586
- Ok ( false ) => {
587
- log_error ! (
588
- self . logger,
589
- "Payment with hash {} couldn't be found in store" ,
590
- hex_utils:: to_string( & payment_hash. 0 )
591
- ) ;
592
- debug_assert ! ( false ) ;
593
- } ,
594
- Err ( e) => {
595
- log_error ! (
596
- self . logger,
597
- "Failed to update payment with hash {}: {}" ,
598
- hex_utils:: to_string( & payment_hash. 0 ) ,
599
- e
600
- ) ;
601
- debug_assert ! ( false ) ;
602
- } ,
603
- }
625
+ } => PaymentDetailsUpdate {
626
+ hash : Some ( Some ( payment_hash) ) ,
627
+ preimage : Some ( payment_preimage) ,
628
+ secret : Some ( Some ( payment_secret) ) ,
629
+ amount_msat : Some ( Some ( amount_msat) ) ,
630
+ status : Some ( PaymentStatus :: Succeeded ) ,
631
+ ..PaymentDetailsUpdate :: new ( payment_id)
604
632
} ,
605
- PaymentPurpose :: Bolt12OfferPayment { .. } => {
606
- // TODO: support BOLT12.
633
+ PaymentPurpose :: Bolt12OfferPayment {
634
+ payment_preimage, payment_secret, ..
635
+ } => PaymentDetailsUpdate {
636
+ hash : Some ( Some ( payment_hash) ) ,
637
+ preimage : Some ( payment_preimage) ,
638
+ secret : Some ( Some ( payment_secret) ) ,
639
+ amount_msat : Some ( Some ( amount_msat) ) ,
640
+ status : Some ( PaymentStatus :: Succeeded ) ,
641
+ ..PaymentDetailsUpdate :: new ( payment_id)
642
+ } ,
643
+ PaymentPurpose :: Bolt12RefundPayment {
644
+ payment_preimage,
645
+ payment_secret,
646
+ ..
647
+ } => PaymentDetailsUpdate {
648
+ hash : Some ( Some ( payment_hash) ) ,
649
+ preimage : Some ( payment_preimage) ,
650
+ secret : Some ( Some ( payment_secret) ) ,
651
+ amount_msat : Some ( Some ( amount_msat) ) ,
652
+ status : Some ( PaymentStatus :: Succeeded ) ,
653
+ ..PaymentDetailsUpdate :: new ( payment_id)
654
+ } ,
655
+ PaymentPurpose :: SpontaneousPayment ( preimage) => PaymentDetailsUpdate {
656
+ hash : Some ( Some ( payment_hash) ) ,
657
+ preimage : Some ( Some ( preimage) ) ,
658
+ amount_msat : Some ( Some ( amount_msat) ) ,
659
+ status : Some ( PaymentStatus :: Succeeded ) ,
660
+ ..PaymentDetailsUpdate :: new ( payment_id)
661
+ } ,
662
+ } ;
663
+
664
+ match self . payment_store . update ( & update) {
665
+ Ok ( true ) => ( ) ,
666
+ Ok ( false ) => {
607
667
log_error ! (
608
668
self . logger,
609
- "Failed to claim unsupported BOLT12 payment with hash: {} " ,
610
- payment_hash
669
+ "Payment with ID {} couldn't be found in store " ,
670
+ payment_id ,
611
671
) ;
612
- return ;
672
+ debug_assert ! ( false ) ;
613
673
} ,
614
- PaymentPurpose :: Bolt12RefundPayment { .. } => {
615
- // TODO: support BOLT12.
674
+ Err ( e) => {
616
675
log_error ! (
617
676
self . logger,
618
- "Failed to claim unsupported BOLT12 payment with hash: {}" ,
619
- payment_hash
677
+ "Failed to update payment with ID {}: {}" ,
678
+ payment_id,
679
+ e
620
680
) ;
621
- return ;
622
- } ,
623
- PaymentPurpose :: SpontaneousPayment ( preimage) => {
624
- let payment = PaymentDetails {
625
- id : payment_id,
626
- kind : PaymentKind :: Spontaneous {
627
- hash : payment_hash,
628
- preimage : Some ( preimage) ,
629
- } ,
630
- amount_msat : Some ( amount_msat) ,
631
- direction : PaymentDirection :: Inbound ,
632
- status : PaymentStatus :: Succeeded ,
633
- } ;
634
-
635
- match self . payment_store . insert ( payment) {
636
- Ok ( false ) => ( ) ,
637
- Ok ( true ) => {
638
- log_error ! (
639
- self . logger,
640
- "Spontaneous payment with hash {} was previously known" ,
641
- hex_utils:: to_string( & payment_hash. 0 )
642
- ) ;
643
- debug_assert ! ( false ) ;
644
- } ,
645
- Err ( e) => {
646
- log_error ! (
647
- self . logger,
648
- "Failed to insert payment with hash {}: {}" ,
649
- hex_utils:: to_string( & payment_hash. 0 ) ,
650
- e
651
- ) ;
652
- debug_assert ! ( false ) ;
653
- } ,
654
- }
681
+ panic ! ( "Failed to access payment store" ) ;
655
682
} ,
656
- } ;
683
+ }
657
684
658
685
self . event_queue
659
686
. add_event ( Event :: PaymentReceived {
@@ -681,6 +708,7 @@ where
681
708
} ;
682
709
683
710
let update = PaymentDetailsUpdate {
711
+ hash : Some ( Some ( payment_hash) ) ,
684
712
preimage : Some ( Some ( payment_preimage) ) ,
685
713
status : Some ( PaymentStatus :: Succeeded ) ,
686
714
..PaymentDetailsUpdate :: new ( payment_id)
@@ -727,6 +755,7 @@ where
727
755
) ;
728
756
729
757
let update = PaymentDetailsUpdate {
758
+ hash : Some ( Some ( payment_hash) ) ,
730
759
status : Some ( PaymentStatus :: Failed ) ,
731
760
..PaymentDetailsUpdate :: new ( payment_id)
732
761
} ;
0 commit comments