@@ -45,9 +45,10 @@ use core::time::Duration;
45
45
use crate :: blinded_path:: { BlindedPath , IntroductionNode } ;
46
46
use crate :: blinded_path:: payment:: { Bolt12OfferContext , Bolt12RefundContext , PaymentContext } ;
47
47
use crate :: events:: { Event , MessageSendEventsProvider , PaymentPurpose } ;
48
- use crate :: ln:: channelmanager:: { PaymentId , RecentPaymentDetails , Retry , self } ;
48
+ use crate :: ln:: channelmanager:: { Bolt12PaymentError , PaymentId , RecentPaymentDetails , Retry , self } ;
49
49
use crate :: ln:: functional_test_utils:: * ;
50
50
use crate :: ln:: msgs:: { ChannelMessageHandler , Init , NodeAnnouncement , OnionMessage , OnionMessageHandler , RoutingMessageHandler , SocketAddress , UnsignedGossipMessage , UnsignedNodeAnnouncement } ;
51
+ use crate :: ln:: outbound_payment:: IDEMPOTENCY_TIMEOUT_TICKS ;
51
52
use crate :: offers:: invoice:: Bolt12Invoice ;
52
53
use crate :: offers:: invoice_error:: InvoiceError ;
53
54
use crate :: offers:: invoice_request:: { InvoiceRequest , InvoiceRequestFields } ;
@@ -737,6 +738,90 @@ fn pays_for_refund_without_blinded_paths() {
737
738
expect_recent_payment ! ( bob, RecentPaymentDetails :: Fulfilled , payment_id) ;
738
739
}
739
740
741
+ /// Checks that a deferred invoice can be paid asynchronously from an Event::InvoiceReceived.
742
+ #[ test]
743
+ fn pays_bolt12_invoice_asynchronously ( ) {
744
+ let mut manually_pay_cfg = test_default_channel_config ( ) ;
745
+ manually_pay_cfg. manually_handle_bolt12_invoices = true ;
746
+
747
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
748
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
749
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , Some ( manually_pay_cfg) ] ) ;
750
+ let nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
751
+
752
+ create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 10_000_000 , 1_000_000_000 ) ;
753
+
754
+ let alice = & nodes[ 0 ] ;
755
+ let alice_id = alice. node . get_our_node_id ( ) ;
756
+ let bob = & nodes[ 1 ] ;
757
+ let bob_id = bob. node . get_our_node_id ( ) ;
758
+
759
+ let offer = alice. node
760
+ . create_offer_builder ( ) . unwrap ( )
761
+ . amount_msats ( 10_000_000 )
762
+ . build ( ) . unwrap ( ) ;
763
+
764
+ let payment_id = PaymentId ( [ 1 ; 32 ] ) ;
765
+ bob. node . pay_for_offer ( & offer, None , None , None , payment_id, Retry :: Attempts ( 0 ) , None ) . unwrap ( ) ;
766
+ expect_recent_payment ! ( bob, RecentPaymentDetails :: AwaitingInvoice , payment_id) ;
767
+
768
+ let onion_message = bob. onion_messenger . next_onion_message_for_peer ( alice_id) . unwrap ( ) ;
769
+ alice. onion_messenger . handle_onion_message ( & bob_id, & onion_message) ;
770
+
771
+ let ( invoice_request, _) = extract_invoice_request ( alice, & onion_message) ;
772
+ let payment_context = PaymentContext :: Bolt12Offer ( Bolt12OfferContext {
773
+ offer_id : offer. id ( ) ,
774
+ invoice_request : InvoiceRequestFields {
775
+ payer_id : invoice_request. payer_id ( ) ,
776
+ quantity : None ,
777
+ payer_note_truncated : None ,
778
+ } ,
779
+ } ) ;
780
+
781
+ let onion_message = alice. onion_messenger . next_onion_message_for_peer ( bob_id) . unwrap ( ) ;
782
+ bob. onion_messenger . handle_onion_message ( & alice_id, & onion_message) ;
783
+
784
+ let invoice = match get_event ! ( bob, Event :: InvoiceReceived ) {
785
+ Event :: InvoiceReceived { payment_id : actual_payment_id, invoice, .. } => {
786
+ assert_eq ! ( actual_payment_id, payment_id) ;
787
+ invoice
788
+ } ,
789
+ _ => panic ! ( "No Event::InvoiceReceived" ) ,
790
+ } ;
791
+ assert_eq ! ( invoice. amount_msats( ) , 10_000_000 ) ;
792
+ assert_ne ! ( invoice. signing_pubkey( ) , alice_id) ;
793
+ assert ! ( !invoice. payment_paths( ) . is_empty( ) ) ;
794
+ for ( _, path) in invoice. payment_paths ( ) {
795
+ assert_eq ! ( path. introduction_node, IntroductionNode :: NodeId ( alice_id) ) ;
796
+ }
797
+
798
+ assert ! ( bob. node. send_payment_for_bolt12_invoice( & invoice) . is_ok( ) ) ;
799
+ assert_eq ! (
800
+ bob. node. send_payment_for_bolt12_invoice( & invoice) ,
801
+ Err ( Bolt12PaymentError :: DuplicateInvoice ) ,
802
+ ) ;
803
+
804
+ route_bolt12_payment ( bob, & [ alice] , & invoice) ;
805
+ expect_recent_payment ! ( bob, RecentPaymentDetails :: Pending , payment_id) ;
806
+
807
+ claim_bolt12_payment ( bob, & [ alice] , payment_context) ;
808
+ expect_recent_payment ! ( bob, RecentPaymentDetails :: Fulfilled , payment_id) ;
809
+
810
+ assert_eq ! (
811
+ bob. node. send_payment_for_bolt12_invoice( & invoice) ,
812
+ Err ( Bolt12PaymentError :: DuplicateInvoice ) ,
813
+ ) ;
814
+
815
+ for _ in 0 ..=IDEMPOTENCY_TIMEOUT_TICKS {
816
+ bob. node . timer_tick_occurred ( ) ;
817
+ }
818
+
819
+ assert_eq ! (
820
+ bob. node. send_payment_for_bolt12_invoice( & invoice) ,
821
+ Err ( Bolt12PaymentError :: UnexpectedInvoice ) ,
822
+ ) ;
823
+ }
824
+
740
825
/// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
741
826
#[ test]
742
827
fn fails_creating_offer_without_blinded_paths ( ) {
0 commit comments