@@ -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,56 @@ 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 ;
507
+ PaymentPurpose :: Bolt11InvoicePayment { payment_preimage, .. } => {
508
+ payment_preimage
522
509
} ,
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 ;
532
- } ,
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 { payment_preimage, .. } => {
553
+ payment_preimage
622
554
} ,
623
555
PaymentPurpose :: SpontaneousPayment ( preimage) => {
556
+ // Since it's spontaneous, we insert it now into our store.
624
557
let payment = PaymentDetails {
625
558
id : payment_id,
626
559
kind : PaymentKind :: Spontaneous {
@@ -629,32 +562,133 @@ where
629
562
} ,
630
563
amount_msat : Some ( amount_msat) ,
631
564
direction : PaymentDirection :: Inbound ,
632
- status : PaymentStatus :: Succeeded ,
565
+ status : PaymentStatus :: Pending ,
633
566
} ;
634
567
635
568
match self . payment_store . insert ( payment) {
636
569
Ok ( false ) => ( ) ,
637
570
Ok ( true ) => {
638
571
log_error ! (
639
572
self . logger,
640
- "Spontaneous payment with hash {} was previously known" ,
641
- hex_utils :: to_string ( & payment_hash . 0 )
573
+ "Spontaneous payment with ID {} was previously known" ,
574
+ payment_id ,
642
575
) ;
643
576
debug_assert ! ( false ) ;
644
577
} ,
645
578
Err ( e) => {
646
579
log_error ! (
647
580
self . logger,
648
- "Failed to insert payment with hash {}: {}" ,
649
- hex_utils :: to_string ( & payment_hash . 0 ) ,
581
+ "Failed to insert payment with ID {}: {}" ,
582
+ payment_id ,
650
583
e
651
584
) ;
652
585
debug_assert ! ( false ) ;
653
586
} ,
654
587
}
588
+
589
+ Some ( preimage)
655
590
} ,
656
591
} ;
657
592
593
+ if let Some ( preimage) = payment_preimage {
594
+ self . channel_manager . claim_funds ( preimage) ;
595
+ } else {
596
+ log_error ! (
597
+ self . logger,
598
+ "Failed to claim payment with ID {}: preimage unknown." ,
599
+ payment_id,
600
+ ) ;
601
+ self . channel_manager . fail_htlc_backwards ( & payment_hash) ;
602
+
603
+ let update = PaymentDetailsUpdate {
604
+ hash : Some ( Some ( payment_hash) ) ,
605
+ status : Some ( PaymentStatus :: Failed ) ,
606
+ ..PaymentDetailsUpdate :: new ( payment_id)
607
+ } ;
608
+ self . payment_store . update ( & update) . unwrap_or_else ( |e| {
609
+ log_error ! ( self . logger, "Failed to access payment store: {}" , e) ;
610
+ panic ! ( "Failed to access payment store" ) ;
611
+ } ) ;
612
+ }
613
+ } ,
614
+ LdkEvent :: PaymentClaimed {
615
+ payment_hash,
616
+ purpose,
617
+ amount_msat,
618
+ receiver_node_id : _,
619
+ htlcs : _,
620
+ sender_intended_total_msat : _,
621
+ } => {
622
+ let payment_id = PaymentId ( payment_hash. 0 ) ;
623
+ log_info ! (
624
+ self . logger,
625
+ "Claimed payment with ID {} from payment hash {} of {}msat." ,
626
+ payment_id,
627
+ hex_utils:: to_string( & payment_hash. 0 ) ,
628
+ amount_msat,
629
+ ) ;
630
+
631
+ let update = match purpose {
632
+ PaymentPurpose :: Bolt11InvoicePayment {
633
+ payment_preimage,
634
+ payment_secret,
635
+ ..
636
+ } => PaymentDetailsUpdate {
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 :: Bolt12OfferPayment {
644
+ payment_preimage, payment_secret, ..
645
+ } => PaymentDetailsUpdate {
646
+ preimage : Some ( payment_preimage) ,
647
+ secret : Some ( Some ( payment_secret) ) ,
648
+ amount_msat : Some ( Some ( amount_msat) ) ,
649
+ status : Some ( PaymentStatus :: Succeeded ) ,
650
+ ..PaymentDetailsUpdate :: new ( payment_id)
651
+ } ,
652
+ PaymentPurpose :: Bolt12RefundPayment {
653
+ payment_preimage,
654
+ payment_secret,
655
+ ..
656
+ } => PaymentDetailsUpdate {
657
+ preimage : Some ( payment_preimage) ,
658
+ secret : Some ( Some ( payment_secret) ) ,
659
+ amount_msat : Some ( Some ( amount_msat) ) ,
660
+ status : Some ( PaymentStatus :: Succeeded ) ,
661
+ ..PaymentDetailsUpdate :: new ( payment_id)
662
+ } ,
663
+ PaymentPurpose :: SpontaneousPayment ( preimage) => PaymentDetailsUpdate {
664
+ preimage : Some ( Some ( preimage) ) ,
665
+ amount_msat : Some ( Some ( amount_msat) ) ,
666
+ status : Some ( PaymentStatus :: Succeeded ) ,
667
+ ..PaymentDetailsUpdate :: new ( payment_id)
668
+ } ,
669
+ } ;
670
+
671
+ match self . payment_store . update ( & update) {
672
+ Ok ( true ) => ( ) ,
673
+ Ok ( false ) => {
674
+ log_error ! (
675
+ self . logger,
676
+ "Payment with ID {} couldn't be found in store" ,
677
+ payment_id,
678
+ ) ;
679
+ debug_assert ! ( false ) ;
680
+ } ,
681
+ Err ( e) => {
682
+ log_error ! (
683
+ self . logger,
684
+ "Failed to update payment with ID {}: {}" ,
685
+ payment_id,
686
+ e
687
+ ) ;
688
+ panic ! ( "Failed to access payment store" ) ;
689
+ } ,
690
+ }
691
+
658
692
self . event_queue
659
693
. add_event ( Event :: PaymentReceived {
660
694
payment_id : Some ( payment_id) ,
@@ -681,6 +715,7 @@ where
681
715
} ;
682
716
683
717
let update = PaymentDetailsUpdate {
718
+ hash : Some ( Some ( payment_hash) ) ,
684
719
preimage : Some ( Some ( payment_preimage) ) ,
685
720
status : Some ( PaymentStatus :: Succeeded ) ,
686
721
..PaymentDetailsUpdate :: new ( payment_id)
@@ -727,6 +762,7 @@ where
727
762
) ;
728
763
729
764
let update = PaymentDetailsUpdate {
765
+ hash : Some ( Some ( payment_hash) ) ,
730
766
status : Some ( PaymentStatus :: Failed ) ,
731
767
..PaymentDetailsUpdate :: new ( payment_id)
732
768
} ;
0 commit comments