1 // This file is Copyright its original authors, visible in version control
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
10 //! Functional tests for the BOLT 12 Offers payment flow.
12 //! [`ChannelManager`] provides utilities to create [`Offer`]s and [`Refund`]s along with utilities
13 //! to initiate and request payment for them, respectively. It also manages the payment flow via
14 //! implementing [`OffersMessageHandler`]. This module tests that functionality, including the
15 //! resulting [`Event`] generation.
17 use core::time::Duration;
18 use crate::blinded_path::BlindedPath;
19 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
20 use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry};
21 use crate::ln::functional_test_utils::*;
22 use crate::ln::msgs::{OnionMessage, OnionMessageHandler};
23 use crate::offers::invoice::Bolt12Invoice;
24 use crate::offers::invoice_request::InvoiceRequest;
25 use crate::onion_message::messenger::PeeledOnion;
26 use crate::onion_message::offers::OffersMessage;
27 use crate::onion_message::packet::ParsedOnionMessageContents;
29 use crate::prelude::*;
31 macro_rules! expect_recent_payment {
32 ($node: expr, $payment_state: path, $payment_id: expr) => {
33 match $node.node.list_recent_payments().first() {
34 Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
35 assert_eq!($payment_id, actual_payment_id);
37 Some(_) => panic!("Unexpected recent payment state"),
38 None => panic!("No recent payments"),
43 fn route_bolt12_payment<'a, 'b, 'c>(
44 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
46 // Monitor added when handling the invoice onion message.
47 check_added_monitors(node, 1);
49 let mut events = node.node.get_and_clear_pending_msg_events();
50 assert_eq!(events.len(), 1);
51 let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
53 // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
54 // invoice contains the payment_hash but it was encrypted inside an onion message.
55 let amount_msats = invoice.amount_msats();
56 let payment_hash = invoice.payment_hash();
58 node, path, amount_msats, payment_hash, None, ev, false, false, None, false
62 fn claim_bolt12_payment<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>]) {
63 let recipient = &path[path.len() - 1];
64 match get_event!(recipient, Event::PaymentClaimable) {
65 Event::PaymentClaimable {
66 purpose: PaymentPurpose::InvoicePayment {
67 payment_preimage: Some(payment_preimage), ..
69 } => claim_payment(node, path, payment_preimage),
74 fn extract_invoice_request<'a, 'b, 'c>(
75 node: &Node<'a, 'b, 'c>, message: &OnionMessage
76 ) -> (InvoiceRequest, Option<BlindedPath>) {
77 match node.onion_messenger.peel_onion_message(message) {
78 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
79 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
80 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
81 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
82 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
84 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
86 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
87 Err(e) => panic!("Failed to process onion message {:?}", e),
91 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
92 match node.onion_messenger.peel_onion_message(message) {
93 Ok(PeeledOnion::Receive(message, _, _)) => match message {
94 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
95 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
96 OffersMessage::Invoice(invoice) => invoice,
97 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
99 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
101 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
102 Err(e) => panic!("Failed to process onion message {:?}", e),
106 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
107 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
108 /// introduction node of the blinded path.
110 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
111 let chanmon_cfgs = create_chanmon_cfgs(2);
112 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
113 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
114 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
116 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
118 let alice = &nodes[0];
119 let alice_id = alice.node.get_our_node_id();
121 let bob_id = bob.node.get_our_node_id();
123 let offer = alice.node
124 .create_offer_builder("coffee".to_string()).unwrap()
125 .amount_msats(10_000_000)
127 assert_ne!(offer.signing_pubkey(), alice_id);
128 assert!(!offer.paths().is_empty());
129 for path in offer.paths() {
130 assert_eq!(path.introduction_node_id, alice_id);
133 let payment_id = PaymentId([1; 32]);
134 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
135 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
137 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
138 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
140 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
141 assert_eq!(invoice_request.amount_msats(), None);
142 assert_ne!(invoice_request.payer_id(), bob_id);
143 assert_eq!(reply_path.unwrap().introduction_node_id, bob_id);
145 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
146 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
148 let invoice = extract_invoice(bob, &onion_message);
149 assert_eq!(invoice.amount_msats(), 10_000_000);
150 assert_ne!(invoice.signing_pubkey(), alice_id);
151 assert!(!invoice.payment_paths().is_empty());
152 for (_, path) in invoice.payment_paths() {
153 assert_eq!(path.introduction_node_id, alice_id);
156 route_bolt12_payment(bob, &[alice], &invoice);
157 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
159 claim_bolt12_payment(bob, &[alice]);
160 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
163 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
164 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
165 /// introduction node of the blinded path.
167 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
168 let chanmon_cfgs = create_chanmon_cfgs(2);
169 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
170 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
171 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
173 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
175 let alice = &nodes[0];
176 let alice_id = alice.node.get_our_node_id();
178 let bob_id = bob.node.get_our_node_id();
180 let absolute_expiry = Duration::from_secs(u64::MAX);
181 let payment_id = PaymentId([1; 32]);
182 let refund = bob.node
183 .create_refund_builder(
184 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
188 assert_eq!(refund.amount_msats(), 10_000_000);
189 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
190 assert_ne!(refund.payer_id(), bob_id);
191 assert!(!refund.paths().is_empty());
192 for path in refund.paths() {
193 assert_eq!(path.introduction_node_id, bob_id);
195 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
197 alice.node.request_refund_payment(&refund).unwrap();
199 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
200 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
202 let invoice = extract_invoice(bob, &onion_message);
203 assert_eq!(invoice.amount_msats(), 10_000_000);
204 assert_ne!(invoice.signing_pubkey(), alice_id);
205 assert!(!invoice.payment_paths().is_empty());
206 for (_, path) in invoice.payment_paths() {
207 assert_eq!(path.introduction_node_id, alice_id);
210 route_bolt12_payment(bob, &[alice], &invoice);
211 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
213 claim_bolt12_payment(bob, &[alice]);
214 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);