4343use bitcoin:: network:: constants:: Network ;
4444use core:: time:: Duration ;
4545use crate :: blinded_path:: { BlindedPath , IntroductionNode } ;
46- use crate :: events:: { Event , MessageSendEventsProvider } ;
46+ use crate :: blinded_path:: payment:: { Bolt12OfferContext , Bolt12RefundContext , PaymentContext } ;
47+ use crate :: events:: { Event , MessageSendEventsProvider , PaymentPurpose } ;
4748use crate :: ln:: channelmanager:: { PaymentId , RecentPaymentDetails , Retry , self } ;
4849use crate :: ln:: functional_test_utils:: * ;
4950use crate :: ln:: msgs:: { ChannelMessageHandler , Init , NodeAnnouncement , OnionMessage , OnionMessageHandler , RoutingMessageHandler , SocketAddress , UnsignedGossipMessage , UnsignedNodeAnnouncement } ;
@@ -151,15 +152,28 @@ fn route_bolt12_payment<'a, 'b, 'c>(
151152 do_pass_along_path ( args) ;
152153}
153154
154- fn claim_bolt12_payment < ' a , ' b , ' c > ( node : & Node < ' a , ' b , ' c > , path : & [ & Node < ' a , ' b , ' c > ] ) {
155+ fn claim_bolt12_payment < ' a , ' b , ' c > (
156+ node : & Node < ' a , ' b , ' c > , path : & [ & Node < ' a , ' b , ' c > ] , expected_payment_context : PaymentContext
157+ ) {
155158 let recipient = & path[ path. len ( ) - 1 ] ;
156- match get_event ! ( recipient, Event :: PaymentClaimable ) {
157- Event :: PaymentClaimable { purpose, .. } => match purpose. preimage ( ) {
158- Some ( payment_preimage) => claim_payment ( node, path, payment_preimage) ,
159- None => panic ! ( "No preimage in Event::PaymentClaimable" ) ,
160- } ,
159+ let payment_purpose = match get_event ! ( recipient, Event :: PaymentClaimable ) {
160+ Event :: PaymentClaimable { purpose, .. } => purpose,
161161 _ => panic ! ( "No Event::PaymentClaimable" ) ,
162162 } ;
163+ let payment_preimage = match payment_purpose. preimage ( ) {
164+ Some ( preimage) => preimage,
165+ None => panic ! ( "No preimage in Event::PaymentClaimable" ) ,
166+ } ;
167+ match payment_purpose {
168+ PaymentPurpose :: Bolt12OfferPayment { payment_context, .. } => {
169+ assert_eq ! ( PaymentContext :: Bolt12Offer ( payment_context) , expected_payment_context) ;
170+ } ,
171+ PaymentPurpose :: Bolt12RefundPayment { payment_context, .. } => {
172+ assert_eq ! ( PaymentContext :: Bolt12Refund ( payment_context) , expected_payment_context) ;
173+ } ,
174+ _ => panic ! ( "Unexpected payment purpose: {:?}" , payment_purpose) ,
175+ }
176+ claim_payment ( node, path, payment_preimage) ;
163177}
164178
165179fn extract_invoice_request < ' a , ' b , ' c > (
@@ -367,9 +381,11 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
367381 disconnect_peers ( david, & [ bob, & nodes[ 4 ] , & nodes[ 5 ] ] ) ;
368382
369383 let offer = alice. node
370- . create_offer_builder ( "coffee" . to_string ( ) ) . unwrap ( )
384+ . create_offer_builder ( "coffee" . to_string ( ) )
385+ . unwrap ( )
371386 . amount_msats ( 10_000_000 )
372387 . build ( ) . unwrap ( ) ;
388+ let payment_context = PaymentContext :: Bolt12Offer ( Bolt12OfferContext { offer_id : offer. id ( ) } ) ;
373389 assert_ne ! ( offer. signing_pubkey( ) , alice_id) ;
374390 assert ! ( !offer. paths( ) . is_empty( ) ) ;
375391 for path in offer. paths ( ) {
@@ -413,7 +429,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
413429 route_bolt12_payment ( david, & [ charlie, bob, alice] , & invoice) ;
414430 expect_recent_payment ! ( david, RecentPaymentDetails :: Pending , payment_id) ;
415431
416- claim_bolt12_payment ( david, & [ charlie, bob, alice] ) ;
432+ claim_bolt12_payment ( david, & [ charlie, bob, alice] , payment_context ) ;
417433 expect_recent_payment ! ( david, RecentPaymentDetails :: Fulfilled , payment_id) ;
418434}
419435
@@ -472,6 +488,7 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
472488 }
473489 expect_recent_payment ! ( david, RecentPaymentDetails :: AwaitingInvoice , payment_id) ;
474490
491+ let payment_context = PaymentContext :: Bolt12Refund ( Bolt12RefundContext { } ) ;
475492 let expected_invoice = alice. node . request_refund_payment ( & refund) . unwrap ( ) ;
476493
477494 connect_peers ( alice, charlie) ;
@@ -495,7 +512,7 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
495512 route_bolt12_payment ( david, & [ charlie, bob, alice] , & invoice) ;
496513 expect_recent_payment ! ( david, RecentPaymentDetails :: Pending , payment_id) ;
497514
498- claim_bolt12_payment ( david, & [ charlie, bob, alice] ) ;
515+ claim_bolt12_payment ( david, & [ charlie, bob, alice] , payment_context ) ;
499516 expect_recent_payment ! ( david, RecentPaymentDetails :: Fulfilled , payment_id) ;
500517}
501518
@@ -520,6 +537,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
520537 . create_offer_builder ( "coffee" . to_string ( ) ) . unwrap ( )
521538 . amount_msats ( 10_000_000 )
522539 . build ( ) . unwrap ( ) ;
540+ let payment_context = PaymentContext :: Bolt12Offer ( Bolt12OfferContext { offer_id : offer. id ( ) } ) ;
523541 assert_ne ! ( offer. signing_pubkey( ) , alice_id) ;
524542 assert ! ( !offer. paths( ) . is_empty( ) ) ;
525543 for path in offer. paths ( ) {
@@ -552,7 +570,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
552570 route_bolt12_payment ( bob, & [ alice] , & invoice) ;
553571 expect_recent_payment ! ( bob, RecentPaymentDetails :: Pending , payment_id) ;
554572
555- claim_bolt12_payment ( bob, & [ alice] ) ;
573+ claim_bolt12_payment ( bob, & [ alice] , payment_context ) ;
556574 expect_recent_payment ! ( bob, RecentPaymentDetails :: Fulfilled , payment_id) ;
557575}
558576
@@ -590,6 +608,7 @@ fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
590608 }
591609 expect_recent_payment ! ( bob, RecentPaymentDetails :: AwaitingInvoice , payment_id) ;
592610
611+ let payment_context = PaymentContext :: Bolt12Refund ( Bolt12RefundContext { } ) ;
593612 let expected_invoice = alice. node . request_refund_payment ( & refund) . unwrap ( ) ;
594613
595614 let onion_message = alice. onion_messenger . next_onion_message_for_peer ( bob_id) . unwrap ( ) ;
@@ -608,7 +627,7 @@ fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
608627 route_bolt12_payment ( bob, & [ alice] , & invoice) ;
609628 expect_recent_payment ! ( bob, RecentPaymentDetails :: Pending , payment_id) ;
610629
611- claim_bolt12_payment ( bob, & [ alice] ) ;
630+ claim_bolt12_payment ( bob, & [ alice] , payment_context ) ;
612631 expect_recent_payment ! ( bob, RecentPaymentDetails :: Fulfilled , payment_id) ;
613632}
614633
@@ -634,6 +653,7 @@ fn pays_for_offer_without_blinded_paths() {
634653 . clear_paths ( )
635654 . amount_msats ( 10_000_000 )
636655 . build ( ) . unwrap ( ) ;
656+ let payment_context = PaymentContext :: Bolt12Offer ( Bolt12OfferContext { offer_id : offer. id ( ) } ) ;
637657 assert_eq ! ( offer. signing_pubkey( ) , alice_id) ;
638658 assert ! ( offer. paths( ) . is_empty( ) ) ;
639659
@@ -651,7 +671,7 @@ fn pays_for_offer_without_blinded_paths() {
651671 route_bolt12_payment ( bob, & [ alice] , & invoice) ;
652672 expect_recent_payment ! ( bob, RecentPaymentDetails :: Pending , payment_id) ;
653673
654- claim_bolt12_payment ( bob, & [ alice] ) ;
674+ claim_bolt12_payment ( bob, & [ alice] , payment_context ) ;
655675 expect_recent_payment ! ( bob, RecentPaymentDetails :: Fulfilled , payment_id) ;
656676}
657677
@@ -684,6 +704,7 @@ fn pays_for_refund_without_blinded_paths() {
684704 assert ! ( refund. paths( ) . is_empty( ) ) ;
685705 expect_recent_payment ! ( bob, RecentPaymentDetails :: AwaitingInvoice , payment_id) ;
686706
707+ let payment_context = PaymentContext :: Bolt12Refund ( Bolt12RefundContext { } ) ;
687708 let expected_invoice = alice. node . request_refund_payment ( & refund) . unwrap ( ) ;
688709
689710 let onion_message = alice. onion_messenger . next_onion_message_for_peer ( bob_id) . unwrap ( ) ;
@@ -695,7 +716,7 @@ fn pays_for_refund_without_blinded_paths() {
695716 route_bolt12_payment ( bob, & [ alice] , & invoice) ;
696717 expect_recent_payment ! ( bob, RecentPaymentDetails :: Pending , payment_id) ;
697718
698- claim_bolt12_payment ( bob, & [ alice] ) ;
719+ claim_bolt12_payment ( bob, & [ alice] , payment_context ) ;
699720 expect_recent_payment ! ( bob, RecentPaymentDetails :: Fulfilled , payment_id) ;
700721}
701722
@@ -1068,12 +1089,13 @@ fn fails_paying_invoice_more_than_once() {
10681089 david. onion_messenger . handle_onion_message ( & charlie_id, & onion_message) ;
10691090
10701091 // David pays the first invoice
1092+ let payment_context = PaymentContext :: Bolt12Refund ( Bolt12RefundContext { } ) ;
10711093 let invoice1 = extract_invoice ( david, & onion_message) ;
10721094
10731095 route_bolt12_payment ( david, & [ charlie, bob, alice] , & invoice1) ;
10741096 expect_recent_payment ! ( david, RecentPaymentDetails :: Pending , payment_id) ;
10751097
1076- claim_bolt12_payment ( david, & [ charlie, bob, alice] ) ;
1098+ claim_bolt12_payment ( david, & [ charlie, bob, alice] , payment_context ) ;
10771099 expect_recent_payment ! ( david, RecentPaymentDetails :: Fulfilled , payment_id) ;
10781100
10791101 disconnect_peers ( alice, & [ charlie] ) ;
0 commit comments