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, self};
21 use crate::ln::functional_test_utils::*;
22 use crate::ln::msgs::{ChannelMessageHandler, Init, OnionMessage, OnionMessageHandler};
23 use crate::offers::invoice::Bolt12Invoice;
24 use crate::offers::invoice_error::InvoiceError;
25 use crate::offers::invoice_request::InvoiceRequest;
26 use crate::offers::parse::Bolt12SemanticError;
27 use crate::onion_message::messenger::PeeledOnion;
28 use crate::onion_message::offers::OffersMessage;
29 use crate::onion_message::packet::ParsedOnionMessageContents;
31 use crate::prelude::*;
33 macro_rules! expect_recent_payment {
34 ($node: expr, $payment_state: path, $payment_id: expr) => {
35 match $node.node.list_recent_payments().first() {
36 Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
37 assert_eq!($payment_id, actual_payment_id);
39 Some(_) => panic!("Unexpected recent payment state"),
40 None => panic!("No recent payments"),
45 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
46 let node_id_a = node_a.node.get_our_node_id();
47 let node_id_b = node_b.node.get_our_node_id();
50 features: node_a.init_features(&node_id_b),
52 remote_network_address: None,
55 features: node_b.init_features(&node_id_a),
57 remote_network_address: None,
60 node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
61 node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
62 node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
63 node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
66 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
68 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
69 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
70 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
71 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
75 fn route_bolt12_payment<'a, 'b, 'c>(
76 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
78 // Monitor added when handling the invoice onion message.
79 check_added_monitors(node, 1);
81 let mut events = node.node.get_and_clear_pending_msg_events();
82 assert_eq!(events.len(), 1);
83 let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
85 // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
86 // invoice contains the payment_hash but it was encrypted inside an onion message.
87 let amount_msats = invoice.amount_msats();
88 let payment_hash = invoice.payment_hash();
90 node, path, amount_msats, payment_hash, None, ev, false, false, None, false
94 fn claim_bolt12_payment<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>]) {
95 let recipient = &path[path.len() - 1];
96 match get_event!(recipient, Event::PaymentClaimable) {
97 Event::PaymentClaimable {
98 purpose: PaymentPurpose::InvoicePayment {
99 payment_preimage: Some(payment_preimage), ..
101 } => claim_payment(node, path, payment_preimage),
106 fn extract_invoice_request<'a, 'b, 'c>(
107 node: &Node<'a, 'b, 'c>, message: &OnionMessage
108 ) -> (InvoiceRequest, Option<BlindedPath>) {
109 match node.onion_messenger.peel_onion_message(message) {
110 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
111 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
112 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
113 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
114 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
116 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
118 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
119 Err(e) => panic!("Failed to process onion message {:?}", e),
123 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
124 match node.onion_messenger.peel_onion_message(message) {
125 Ok(PeeledOnion::Receive(message, _, _)) => match message {
126 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
127 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
128 OffersMessage::Invoice(invoice) => invoice,
129 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
131 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
133 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
134 Err(e) => panic!("Failed to process onion message {:?}", e),
138 fn extract_invoice_error<'a, 'b, 'c>(
139 node: &Node<'a, 'b, 'c>, message: &OnionMessage
141 match node.onion_messenger.peel_onion_message(message) {
142 Ok(PeeledOnion::Receive(message, _, _)) => match message {
143 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
144 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
145 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
146 OffersMessage::InvoiceError(error) => error,
148 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
150 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
151 Err(e) => panic!("Failed to process onion message {:?}", e),
155 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
156 /// rather than exposing a node's pubkey.
158 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
159 let mut accept_forward_cfg = test_default_channel_config();
160 accept_forward_cfg.accept_forwards_to_priv_channels = true;
162 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
163 features.set_onion_messages_optional();
164 features.set_route_blinding_optional();
166 let chanmon_cfgs = create_chanmon_cfgs(6);
167 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
169 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
171 let node_chanmgrs = create_node_chanmgrs(
172 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
174 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
176 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
177 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
178 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
179 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
180 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
181 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
182 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
184 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
185 let alice_id = alice.node.get_our_node_id();
186 let bob_id = bob.node.get_our_node_id();
187 let charlie_id = charlie.node.get_our_node_id();
188 let david_id = david.node.get_our_node_id();
190 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
191 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
193 let offer = alice.node
194 .create_offer_builder("coffee".to_string()).unwrap()
195 .amount_msats(10_000_000)
197 assert_ne!(offer.signing_pubkey(), alice_id);
198 assert!(!offer.paths().is_empty());
199 for path in offer.paths() {
200 assert_eq!(path.introduction_node_id, bob_id);
203 let payment_id = PaymentId([1; 32]);
204 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
206 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
208 connect_peers(david, bob);
210 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
211 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
213 connect_peers(alice, charlie);
215 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
216 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
218 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
219 assert_eq!(invoice_request.amount_msats(), None);
220 assert_ne!(invoice_request.payer_id(), david_id);
221 assert_eq!(reply_path.unwrap().introduction_node_id, charlie_id);
223 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
224 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
226 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
227 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
229 let invoice = extract_invoice(david, &onion_message);
230 assert_eq!(invoice.amount_msats(), 10_000_000);
231 assert_ne!(invoice.signing_pubkey(), alice_id);
232 assert!(!invoice.payment_paths().is_empty());
233 for (_, path) in invoice.payment_paths() {
234 assert_eq!(path.introduction_node_id, bob_id);
237 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
238 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
240 claim_bolt12_payment(david, &[charlie, bob, alice]);
241 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
244 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
245 /// rather than exposing a node's pubkey.
247 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
248 let mut accept_forward_cfg = test_default_channel_config();
249 accept_forward_cfg.accept_forwards_to_priv_channels = true;
251 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
252 features.set_onion_messages_optional();
253 features.set_route_blinding_optional();
255 let chanmon_cfgs = create_chanmon_cfgs(6);
256 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
258 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
260 let node_chanmgrs = create_node_chanmgrs(
261 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
263 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
265 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
266 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
267 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
268 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
269 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
270 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
271 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
273 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
274 let alice_id = alice.node.get_our_node_id();
275 let bob_id = bob.node.get_our_node_id();
276 let charlie_id = charlie.node.get_our_node_id();
277 let david_id = david.node.get_our_node_id();
279 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
280 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
282 let absolute_expiry = Duration::from_secs(u64::MAX);
283 let payment_id = PaymentId([1; 32]);
284 let refund = david.node
285 .create_refund_builder(
286 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
290 assert_eq!(refund.amount_msats(), 10_000_000);
291 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
292 assert_ne!(refund.payer_id(), david_id);
293 assert!(!refund.paths().is_empty());
294 for path in refund.paths() {
295 assert_eq!(path.introduction_node_id, charlie_id);
297 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
299 alice.node.request_refund_payment(&refund).unwrap();
301 connect_peers(alice, charlie);
303 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
304 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
306 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
307 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
309 let invoice = extract_invoice(david, &onion_message);
310 assert_eq!(invoice.amount_msats(), 10_000_000);
311 assert_ne!(invoice.signing_pubkey(), alice_id);
312 assert!(!invoice.payment_paths().is_empty());
313 for (_, path) in invoice.payment_paths() {
314 assert_eq!(path.introduction_node_id, bob_id);
317 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
318 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
320 claim_bolt12_payment(david, &[charlie, bob, alice]);
321 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
324 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
325 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
326 /// introduction node of the blinded path.
328 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
329 let chanmon_cfgs = create_chanmon_cfgs(2);
330 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
331 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
332 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
334 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
336 let alice = &nodes[0];
337 let alice_id = alice.node.get_our_node_id();
339 let bob_id = bob.node.get_our_node_id();
341 let offer = alice.node
342 .create_offer_builder("coffee".to_string()).unwrap()
343 .amount_msats(10_000_000)
345 assert_ne!(offer.signing_pubkey(), alice_id);
346 assert!(!offer.paths().is_empty());
347 for path in offer.paths() {
348 assert_eq!(path.introduction_node_id, alice_id);
351 let payment_id = PaymentId([1; 32]);
352 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
353 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
355 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
356 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
358 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
359 assert_eq!(invoice_request.amount_msats(), None);
360 assert_ne!(invoice_request.payer_id(), bob_id);
361 assert_eq!(reply_path.unwrap().introduction_node_id, bob_id);
363 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
364 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
366 let invoice = extract_invoice(bob, &onion_message);
367 assert_eq!(invoice.amount_msats(), 10_000_000);
368 assert_ne!(invoice.signing_pubkey(), alice_id);
369 assert!(!invoice.payment_paths().is_empty());
370 for (_, path) in invoice.payment_paths() {
371 assert_eq!(path.introduction_node_id, alice_id);
374 route_bolt12_payment(bob, &[alice], &invoice);
375 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
377 claim_bolt12_payment(bob, &[alice]);
378 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
381 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
382 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
383 /// introduction node of the blinded path.
385 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
386 let chanmon_cfgs = create_chanmon_cfgs(2);
387 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
388 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
389 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
391 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
393 let alice = &nodes[0];
394 let alice_id = alice.node.get_our_node_id();
396 let bob_id = bob.node.get_our_node_id();
398 let absolute_expiry = Duration::from_secs(u64::MAX);
399 let payment_id = PaymentId([1; 32]);
400 let refund = bob.node
401 .create_refund_builder(
402 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
406 assert_eq!(refund.amount_msats(), 10_000_000);
407 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
408 assert_ne!(refund.payer_id(), bob_id);
409 assert!(!refund.paths().is_empty());
410 for path in refund.paths() {
411 assert_eq!(path.introduction_node_id, bob_id);
413 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
415 alice.node.request_refund_payment(&refund).unwrap();
417 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
418 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
420 let invoice = extract_invoice(bob, &onion_message);
421 assert_eq!(invoice.amount_msats(), 10_000_000);
422 assert_ne!(invoice.signing_pubkey(), alice_id);
423 assert!(!invoice.payment_paths().is_empty());
424 for (_, path) in invoice.payment_paths() {
425 assert_eq!(path.introduction_node_id, alice_id);
428 route_bolt12_payment(bob, &[alice], &invoice);
429 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
431 claim_bolt12_payment(bob, &[alice]);
432 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
435 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
436 /// the requested is sent directly using the node's pubkey, the response and the payment still use
437 /// blinded paths as required by the spec.
439 fn pays_for_offer_without_blinded_paths() {
440 let chanmon_cfgs = create_chanmon_cfgs(2);
441 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
442 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
443 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
445 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
447 let alice = &nodes[0];
448 let alice_id = alice.node.get_our_node_id();
450 let bob_id = bob.node.get_our_node_id();
452 let offer = alice.node
453 .create_offer_builder("coffee".to_string()).unwrap()
455 .amount_msats(10_000_000)
457 assert_eq!(offer.signing_pubkey(), alice_id);
458 assert!(offer.paths().is_empty());
460 let payment_id = PaymentId([1; 32]);
461 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
462 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
464 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
465 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
467 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
468 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
470 let invoice = extract_invoice(bob, &onion_message);
471 route_bolt12_payment(bob, &[alice], &invoice);
472 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
474 claim_bolt12_payment(bob, &[alice]);
475 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
478 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
479 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
481 fn pays_for_refund_without_blinded_paths() {
482 let chanmon_cfgs = create_chanmon_cfgs(2);
483 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
484 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
485 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
487 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
489 let alice = &nodes[0];
490 let alice_id = alice.node.get_our_node_id();
492 let bob_id = bob.node.get_our_node_id();
494 let absolute_expiry = Duration::from_secs(u64::MAX);
495 let payment_id = PaymentId([1; 32]);
496 let refund = bob.node
497 .create_refund_builder(
498 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
503 assert_eq!(refund.payer_id(), bob_id);
504 assert!(refund.paths().is_empty());
505 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
507 alice.node.request_refund_payment(&refund).unwrap();
509 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
510 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
512 let invoice = extract_invoice(bob, &onion_message);
513 route_bolt12_payment(bob, &[alice], &invoice);
514 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
516 claim_bolt12_payment(bob, &[alice]);
517 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
520 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
522 fn fails_creating_offer_without_blinded_paths() {
523 let chanmon_cfgs = create_chanmon_cfgs(2);
524 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
525 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
526 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
528 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
530 match nodes[0].node.create_offer_builder("coffee".to_string()) {
531 Ok(_) => panic!("Expected error"),
532 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
536 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
538 fn fails_creating_refund_without_blinded_paths() {
539 let chanmon_cfgs = create_chanmon_cfgs(2);
540 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
541 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
542 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
544 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
546 let absolute_expiry = Duration::from_secs(u64::MAX);
547 let payment_id = PaymentId([1; 32]);
549 match nodes[0].node.create_refund_builder(
550 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
552 Ok(_) => panic!("Expected error"),
553 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
556 assert!(nodes[0].node.list_recent_payments().is_empty());
559 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
562 fn fails_creating_invoice_request_without_blinded_reply_path() {
563 let chanmon_cfgs = create_chanmon_cfgs(6);
564 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
565 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
566 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
568 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
569 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
570 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
571 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
572 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
574 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
576 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
577 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
579 let offer = alice.node
580 .create_offer_builder("coffee".to_string()).unwrap()
581 .amount_msats(10_000_000)
584 let payment_id = PaymentId([1; 32]);
586 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
587 Ok(_) => panic!("Expected error"),
588 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
591 assert!(nodes[0].node.list_recent_payments().is_empty());
595 fn fails_creating_invoice_request_with_duplicate_payment_id() {
596 let chanmon_cfgs = create_chanmon_cfgs(6);
597 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
598 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
599 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
601 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
602 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
603 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
604 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
605 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
606 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
607 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
609 let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
611 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
613 let offer = alice.node
614 .create_offer_builder("coffee".to_string()).unwrap()
615 .amount_msats(10_000_000)
618 let payment_id = PaymentId([1; 32]);
620 david.node.pay_for_offer(
621 &offer, None, None, None, payment_id, Retry::Attempts(0), None
624 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
626 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
627 Ok(_) => panic!("Expected error"),
628 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
631 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
635 fn fails_creating_refund_with_duplicate_payment_id() {
636 let chanmon_cfgs = create_chanmon_cfgs(2);
637 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
638 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
639 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
641 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
643 let absolute_expiry = Duration::from_secs(u64::MAX);
644 let payment_id = PaymentId([1; 32]);
646 nodes[0].node.create_refund_builder(
647 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
650 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
652 match nodes[0].node.create_refund_builder(
653 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
655 Ok(_) => panic!("Expected error"),
656 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
659 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
663 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
664 let mut accept_forward_cfg = test_default_channel_config();
665 accept_forward_cfg.accept_forwards_to_priv_channels = true;
667 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
668 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
669 features.set_onion_messages_optional();
670 features.clear_route_blinding();
672 let chanmon_cfgs = create_chanmon_cfgs(6);
673 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
675 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
677 let node_chanmgrs = create_node_chanmgrs(
678 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
680 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
682 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
683 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
684 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
685 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
686 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
687 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
688 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
690 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
691 let alice_id = alice.node.get_our_node_id();
692 let bob_id = bob.node.get_our_node_id();
693 let charlie_id = charlie.node.get_our_node_id();
694 let david_id = david.node.get_our_node_id();
696 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
697 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
699 let offer = alice.node
700 .create_offer_builder("coffee".to_string()).unwrap()
701 .amount_msats(10_000_000)
704 let payment_id = PaymentId([1; 32]);
705 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
708 connect_peers(david, bob);
710 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
711 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
713 connect_peers(alice, charlie);
715 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
716 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
718 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
719 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
721 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
722 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
724 let invoice_error = extract_invoice_error(david, &onion_message);
725 assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
729 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
730 let mut accept_forward_cfg = test_default_channel_config();
731 accept_forward_cfg.accept_forwards_to_priv_channels = true;
733 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
734 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
735 features.set_onion_messages_optional();
736 features.clear_route_blinding();
738 let chanmon_cfgs = create_chanmon_cfgs(6);
739 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
741 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
743 let node_chanmgrs = create_node_chanmgrs(
744 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
746 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
748 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
749 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
750 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
751 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
752 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
753 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
754 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
756 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
758 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
759 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
761 let absolute_expiry = Duration::from_secs(u64::MAX);
762 let payment_id = PaymentId([1; 32]);
763 let refund = david.node
764 .create_refund_builder(
765 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
770 match alice.node.request_refund_payment(&refund) {
771 Ok(_) => panic!("Expected error"),
772 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),