Skip to content

Commit e94b2e0

Browse files
committed
Test for PaymentContext in offers_tests.rs
1 parent b75ad39 commit e94b2e0

File tree

1 file changed

+36
-20
lines changed

1 file changed

+36
-20
lines changed

lightning/src/ln/offers_tests.rs

+36-20
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
use bitcoin::network::constants::Network;
4444
use core::time::Duration;
4545
use crate::blinded_path::BlindedPath;
46+
use crate::blinded_path::payment::PaymentContext;
4647
use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
4748
use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry, self};
4849
use crate::ln::functional_test_utils::*;
@@ -151,14 +152,21 @@ 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];
156159
match get_event!(recipient, Event::PaymentClaimable) {
157160
Event::PaymentClaimable {
158161
purpose: PaymentPurpose::InvoicePayment {
159-
payment_preimage: Some(payment_preimage), ..
162+
payment_preimage: Some(payment_preimage),
163+
payment_context: Some(payment_context),
164+
..
160165
}, ..
161-
} => claim_payment(node, path, payment_preimage),
166+
} => {
167+
assert_eq!(payment_context, expected_payment_context);
168+
claim_payment(node, path, payment_preimage)
169+
},
162170
_ => panic!(),
163171
};
164172
}
@@ -367,10 +375,12 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
367375
disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
368376
disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
369377

370-
let offer = alice.node
371-
.create_offer_builder("coffee".to_string()).unwrap()
372-
.amount_msats(10_000_000)
373-
.build().unwrap();
378+
let builder = alice.node
379+
.create_offer_builder("coffee".to_string())
380+
.unwrap()
381+
.amount_msats(10_000_000);
382+
let payment_context = PaymentContext::Bolt12Offer { offer_id: builder.offer_id() };
383+
let offer = builder.build().unwrap();
374384
assert_ne!(offer.signing_pubkey(), alice_id);
375385
assert!(!offer.paths().is_empty());
376386
for path in offer.paths() {
@@ -414,7 +424,7 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
414424
route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
415425
expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
416426

417-
claim_bolt12_payment(david, &[charlie, bob, alice]);
427+
claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
418428
expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
419429
}
420430

@@ -473,6 +483,7 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
473483
}
474484
expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
475485

486+
let payment_context = PaymentContext::Bolt12Refund {};
476487
let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
477488

478489
connect_peers(alice, charlie);
@@ -496,7 +507,7 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
496507
route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
497508
expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
498509

499-
claim_bolt12_payment(david, &[charlie, bob, alice]);
510+
claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
500511
expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
501512
}
502513

@@ -517,10 +528,11 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
517528
let bob = &nodes[1];
518529
let bob_id = bob.node.get_our_node_id();
519530

520-
let offer = alice.node
531+
let builder = alice.node
521532
.create_offer_builder("coffee".to_string()).unwrap()
522-
.amount_msats(10_000_000)
523-
.build().unwrap();
533+
.amount_msats(10_000_000);
534+
let payment_context = PaymentContext::Bolt12Offer { offer_id: builder.offer_id() };
535+
let offer = builder.build().unwrap();
524536
assert_ne!(offer.signing_pubkey(), alice_id);
525537
assert!(!offer.paths().is_empty());
526538
for path in offer.paths() {
@@ -553,7 +565,7 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
553565
route_bolt12_payment(bob, &[alice], &invoice);
554566
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
555567

556-
claim_bolt12_payment(bob, &[alice]);
568+
claim_bolt12_payment(bob, &[alice], payment_context);
557569
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
558570
}
559571

@@ -591,6 +603,7 @@ fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
591603
}
592604
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
593605

606+
let payment_context = PaymentContext::Bolt12Refund {};
594607
let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
595608

596609
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
@@ -609,7 +622,7 @@ fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
609622
route_bolt12_payment(bob, &[alice], &invoice);
610623
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
611624

612-
claim_bolt12_payment(bob, &[alice]);
625+
claim_bolt12_payment(bob, &[alice], payment_context);
613626
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
614627
}
615628

@@ -630,11 +643,12 @@ fn pays_for_offer_without_blinded_paths() {
630643
let bob = &nodes[1];
631644
let bob_id = bob.node.get_our_node_id();
632645

633-
let offer = alice.node
646+
let builder = alice.node
634647
.create_offer_builder("coffee".to_string()).unwrap()
635648
.clear_paths()
636-
.amount_msats(10_000_000)
637-
.build().unwrap();
649+
.amount_msats(10_000_000);
650+
let payment_context = PaymentContext::Bolt12Offer { offer_id: builder.offer_id() };
651+
let offer = builder.build().unwrap();
638652
assert_eq!(offer.signing_pubkey(), alice_id);
639653
assert!(offer.paths().is_empty());
640654

@@ -652,7 +666,7 @@ fn pays_for_offer_without_blinded_paths() {
652666
route_bolt12_payment(bob, &[alice], &invoice);
653667
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
654668

655-
claim_bolt12_payment(bob, &[alice]);
669+
claim_bolt12_payment(bob, &[alice], payment_context);
656670
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
657671
}
658672

@@ -685,6 +699,7 @@ fn pays_for_refund_without_blinded_paths() {
685699
assert!(refund.paths().is_empty());
686700
expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
687701

702+
let payment_context = PaymentContext::Bolt12Refund {};
688703
let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
689704

690705
let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
@@ -696,7 +711,7 @@ fn pays_for_refund_without_blinded_paths() {
696711
route_bolt12_payment(bob, &[alice], &invoice);
697712
expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
698713

699-
claim_bolt12_payment(bob, &[alice]);
714+
claim_bolt12_payment(bob, &[alice], payment_context);
700715
expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
701716
}
702717

@@ -1069,12 +1084,13 @@ fn fails_paying_invoice_more_than_once() {
10691084
david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
10701085

10711086
// David pays the first invoice
1087+
let payment_context = PaymentContext::Bolt12Refund {};
10721088
let invoice1 = extract_invoice(david, &onion_message);
10731089

10741090
route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
10751091
expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
10761092

1077-
claim_bolt12_payment(david, &[charlie, bob, alice]);
1093+
claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
10781094
expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
10791095

10801096
disconnect_peers(alice, &[charlie]);

0 commit comments

Comments
 (0)