@@ -435,7 +435,9 @@ where
435
435
} => {
436
436
let payment_id = PaymentId ( payment_hash. 0 ) ;
437
437
if let Some ( info) = self . payment_store . get ( & payment_id) {
438
- if info. status == PaymentStatus :: Succeeded {
438
+ if info. status == PaymentStatus :: Succeeded
439
+ || matches ! ( info. kind, PaymentKind :: Spontaneous { .. } )
440
+ {
439
441
log_info ! (
440
442
self . logger,
441
443
"Refused duplicate inbound payment from payment hash {} of {}msat" ,
@@ -483,6 +485,7 @@ where
483
485
self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
484
486
485
487
let update = PaymentDetailsUpdate {
488
+ hash : Some ( Some ( payment_hash) ) ,
486
489
status : Some ( PaymentStatus :: Failed ) ,
487
490
..PaymentDetailsUpdate :: new ( payment_id)
488
491
} ;
@@ -501,126 +504,91 @@ where
501
504
amount_msat,
502
505
) ;
503
506
let payment_preimage = match purpose {
504
- 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
- }
512
- } ,
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 ;
507
+ PaymentPurpose :: Bolt11InvoicePayment { payment_preimage, .. } => {
508
+ payment_preimage
532
509
} ,
533
- PaymentPurpose :: SpontaneousPayment ( preimage) => Some ( preimage) ,
534
- } ;
535
-
536
- if let Some ( preimage) = payment_preimage {
537
- self . channel_manager . claim_funds ( preimage) ;
538
- } else {
539
- log_error ! (
540
- self . logger,
541
- "Failed to claim payment with hash {}: preimage unknown." ,
542
- hex_utils:: to_string( & payment_hash. 0 ) ,
543
- ) ;
544
- self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
545
-
546
- let update = PaymentDetailsUpdate {
547
- status : Some ( PaymentStatus :: Failed ) ,
548
- ..PaymentDetailsUpdate :: new ( payment_id)
549
- } ;
550
- self . payment_store . update ( & update) . unwrap_or_else ( |e| {
551
- log_error ! ( self . logger, "Failed to access payment store: {}" , e) ;
552
- panic ! ( "Failed to access payment store" ) ;
553
- } ) ;
554
- }
555
- } ,
556
- LdkEvent :: PaymentClaimed {
557
- payment_hash,
558
- purpose,
559
- amount_msat,
560
- receiver_node_id : _,
561
- htlcs : _,
562
- sender_intended_total_msat : _,
563
- } => {
564
- log_info ! (
565
- self . logger,
566
- "Claimed payment from payment hash {} of {}msat." ,
567
- hex_utils:: to_string( & payment_hash. 0 ) ,
568
- amount_msat,
569
- ) ;
570
- let payment_id = PaymentId ( payment_hash. 0 ) ;
571
- match purpose {
572
- PaymentPurpose :: Bolt11InvoicePayment {
510
+ PaymentPurpose :: Bolt12OfferPayment {
573
511
payment_preimage,
574
512
payment_secret,
513
+ payment_context,
575
514
..
576
515
} => {
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)
516
+ let offer_id = payment_context. offer_id ;
517
+ let payment = PaymentDetails {
518
+ id : payment_id,
519
+ kind : PaymentKind :: Bolt12Offer {
520
+ hash : Some ( payment_hash) ,
521
+ preimage : payment_preimage,
522
+ secret : Some ( payment_secret) ,
523
+ offer_id,
524
+ } ,
525
+ amount_msat : Some ( amount_msat) ,
526
+ direction : PaymentDirection :: Inbound ,
527
+ status : PaymentStatus :: Pending ,
583
528
} ;
584
- match self . payment_store . update ( & update) {
585
- Ok ( true ) => ( ) ,
586
- Ok ( false ) => {
529
+
530
+ match self . payment_store . insert ( payment) {
531
+ Ok ( false ) => ( ) ,
532
+ Ok ( true ) => {
587
533
log_error ! (
588
534
self . logger,
589
- "Payment with hash {} couldn't be found in store " ,
590
- hex_utils :: to_string ( & payment_hash . 0 )
535
+ "Bolt12OfferPayment with ID {} was previously known " ,
536
+ payment_id ,
591
537
) ;
592
538
debug_assert ! ( false ) ;
593
539
} ,
594
540
Err ( e) => {
595
541
log_error ! (
596
542
self . logger,
597
- "Failed to update payment with hash {}: {}" ,
598
- hex_utils :: to_string ( & payment_hash . 0 ) ,
543
+ "Failed to insert payment with ID {}: {}" ,
544
+ payment_id ,
599
545
e
600
546
) ;
601
547
debug_assert ! ( false ) ;
602
548
} ,
603
549
}
550
+ payment_preimage
604
551
} ,
605
- PaymentPurpose :: Bolt12OfferPayment { .. } => {
606
- // TODO: support BOLT12.
607
- log_error ! (
608
- self . logger,
609
- "Failed to claim unsupported BOLT12 payment with hash: {}" ,
610
- payment_hash
611
- ) ;
612
- return ;
613
- } ,
614
- PaymentPurpose :: Bolt12RefundPayment { .. } => {
615
- // TODO: support BOLT12.
616
- log_error ! (
617
- self . logger,
618
- "Failed to claim unsupported BOLT12 payment with hash: {}" ,
619
- payment_hash
620
- ) ;
621
- return ;
552
+ PaymentPurpose :: Bolt12RefundPayment {
553
+ payment_preimage,
554
+ payment_secret,
555
+ ..
556
+ } => {
557
+ let payment = PaymentDetails {
558
+ id : payment_id,
559
+ kind : PaymentKind :: Bolt12Refund {
560
+ hash : Some ( payment_hash) ,
561
+ preimage : payment_preimage,
562
+ secret : Some ( payment_secret) ,
563
+ } ,
564
+ amount_msat : Some ( amount_msat) ,
565
+ direction : PaymentDirection :: Inbound ,
566
+ status : PaymentStatus :: Pending ,
567
+ } ;
568
+ match self . payment_store . insert ( payment) {
569
+ Ok ( false ) => ( ) ,
570
+ Ok ( true ) => {
571
+ log_error ! (
572
+ self . logger,
573
+ "Bolt12RefundPayment with ID {} was previously known" ,
574
+ payment_id,
575
+ ) ;
576
+ debug_assert ! ( false ) ;
577
+ } ,
578
+ Err ( e) => {
579
+ log_error ! (
580
+ self . logger,
581
+ "Failed to insert payment with ID {}: {}" ,
582
+ payment_id,
583
+ e
584
+ ) ;
585
+ debug_assert ! ( false ) ;
586
+ } ,
587
+ }
588
+ payment_preimage
622
589
} ,
623
590
PaymentPurpose :: SpontaneousPayment ( preimage) => {
591
+ // Since it's spontaneous, we insert it now into our store.
624
592
let payment = PaymentDetails {
625
593
id : payment_id,
626
594
kind : PaymentKind :: Spontaneous {
@@ -629,32 +597,133 @@ where
629
597
} ,
630
598
amount_msat : Some ( amount_msat) ,
631
599
direction : PaymentDirection :: Inbound ,
632
- status : PaymentStatus :: Succeeded ,
600
+ status : PaymentStatus :: Pending ,
633
601
} ;
634
602
635
603
match self . payment_store . insert ( payment) {
636
604
Ok ( false ) => ( ) ,
637
605
Ok ( true ) => {
638
606
log_error ! (
639
607
self . logger,
640
- "Spontaneous payment with hash {} was previously known" ,
641
- hex_utils :: to_string ( & payment_hash . 0 )
608
+ "Spontaneous payment with ID {} was previously known" ,
609
+ payment_id ,
642
610
) ;
643
611
debug_assert ! ( false ) ;
644
612
} ,
645
613
Err ( e) => {
646
614
log_error ! (
647
615
self . logger,
648
- "Failed to insert payment with hash {}: {}" ,
649
- hex_utils :: to_string ( & payment_hash . 0 ) ,
616
+ "Failed to insert payment with ID {}: {}" ,
617
+ payment_id ,
650
618
e
651
619
) ;
652
620
debug_assert ! ( false ) ;
653
621
} ,
654
622
}
623
+
624
+ Some ( preimage)
625
+ } ,
626
+ } ;
627
+
628
+ if let Some ( preimage) = payment_preimage {
629
+ self . channel_manager . claim_funds ( preimage) ;
630
+ } else {
631
+ log_error ! (
632
+ self . logger,
633
+ "Failed to claim payment with ID {}: preimage unknown." ,
634
+ payment_id,
635
+ ) ;
636
+ self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
637
+
638
+ let update = PaymentDetailsUpdate {
639
+ hash : Some ( Some ( payment_hash) ) ,
640
+ status : Some ( PaymentStatus :: Failed ) ,
641
+ ..PaymentDetailsUpdate :: new ( payment_id)
642
+ } ;
643
+ self . payment_store . update ( & update) . unwrap_or_else ( |e| {
644
+ log_error ! ( self . logger, "Failed to access payment store: {}" , e) ;
645
+ panic ! ( "Failed to access payment store" ) ;
646
+ } ) ;
647
+ }
648
+ } ,
649
+ LdkEvent :: PaymentClaimed {
650
+ payment_hash,
651
+ purpose,
652
+ amount_msat,
653
+ receiver_node_id : _,
654
+ htlcs : _,
655
+ sender_intended_total_msat : _,
656
+ } => {
657
+ let payment_id = PaymentId ( payment_hash. 0 ) ;
658
+ log_info ! (
659
+ self . logger,
660
+ "Claimed payment with ID {} from payment hash {} of {}msat." ,
661
+ payment_id,
662
+ hex_utils:: to_string( & payment_hash. 0 ) ,
663
+ amount_msat,
664
+ ) ;
665
+
666
+ let update = match purpose {
667
+ PaymentPurpose :: Bolt11InvoicePayment {
668
+ payment_preimage,
669
+ payment_secret,
670
+ ..
671
+ } => PaymentDetailsUpdate {
672
+ preimage : Some ( payment_preimage) ,
673
+ secret : Some ( Some ( payment_secret) ) ,
674
+ amount_msat : Some ( Some ( amount_msat) ) ,
675
+ status : Some ( PaymentStatus :: Succeeded ) ,
676
+ ..PaymentDetailsUpdate :: new ( payment_id)
677
+ } ,
678
+ PaymentPurpose :: Bolt12OfferPayment {
679
+ payment_preimage, payment_secret, ..
680
+ } => PaymentDetailsUpdate {
681
+ preimage : Some ( payment_preimage) ,
682
+ secret : Some ( Some ( payment_secret) ) ,
683
+ amount_msat : Some ( Some ( amount_msat) ) ,
684
+ status : Some ( PaymentStatus :: Succeeded ) ,
685
+ ..PaymentDetailsUpdate :: new ( payment_id)
686
+ } ,
687
+ PaymentPurpose :: Bolt12RefundPayment {
688
+ payment_preimage,
689
+ payment_secret,
690
+ ..
691
+ } => PaymentDetailsUpdate {
692
+ preimage : Some ( payment_preimage) ,
693
+ secret : Some ( Some ( payment_secret) ) ,
694
+ amount_msat : Some ( Some ( amount_msat) ) ,
695
+ status : Some ( PaymentStatus :: Succeeded ) ,
696
+ ..PaymentDetailsUpdate :: new ( payment_id)
697
+ } ,
698
+ PaymentPurpose :: SpontaneousPayment ( preimage) => PaymentDetailsUpdate {
699
+ preimage : Some ( Some ( preimage) ) ,
700
+ amount_msat : Some ( Some ( amount_msat) ) ,
701
+ status : Some ( PaymentStatus :: Succeeded ) ,
702
+ ..PaymentDetailsUpdate :: new ( payment_id)
655
703
} ,
656
704
} ;
657
705
706
+ match self . payment_store . update ( & update) {
707
+ Ok ( true ) => ( ) ,
708
+ Ok ( false ) => {
709
+ log_error ! (
710
+ self . logger,
711
+ "Payment with ID {} couldn't be found in store" ,
712
+ payment_id,
713
+ ) ;
714
+ debug_assert ! ( false ) ;
715
+ } ,
716
+ Err ( e) => {
717
+ log_error ! (
718
+ self . logger,
719
+ "Failed to update payment with ID {}: {}" ,
720
+ payment_id,
721
+ e
722
+ ) ;
723
+ panic ! ( "Failed to access payment store" ) ;
724
+ } ,
725
+ }
726
+
658
727
self . event_queue
659
728
. add_event ( Event :: PaymentReceived {
660
729
payment_id : Some ( payment_id) ,
@@ -681,6 +750,7 @@ where
681
750
} ;
682
751
683
752
let update = PaymentDetailsUpdate {
753
+ hash : Some ( Some ( payment_hash) ) ,
684
754
preimage : Some ( Some ( payment_preimage) ) ,
685
755
status : Some ( PaymentStatus :: Succeeded ) ,
686
756
..PaymentDetailsUpdate :: new ( payment_id)
@@ -727,6 +797,7 @@ where
727
797
) ;
728
798
729
799
let update = PaymentDetailsUpdate {
800
+ hash : Some ( Some ( payment_hash) ) ,
730
801
status : Some ( PaymentStatus :: Failed ) ,
731
802
..PaymentDetailsUpdate :: new ( payment_id)
732
803
} ;
0 commit comments