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 //! Two-node success tests use an announced channel:
21 //! While two-node failure tests use an unannounced channel:
25 //! Six-node tests use unannounced channels for the sender and recipient and announced channels for
26 //! the rest of the network.
32 //! Alice ... Bob -------- Charlie ... David
38 //! Unnamed nodes are needed to ensure unannounced nodes can create two-hop blinded paths.
40 //! Nodes without channels are disconnected and connected as needed to ensure that deterministic
41 //! blinded paths are used.
43 use core::time::Duration;
44 use crate::blinded_path::BlindedPath;
45 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
46 use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry, self};
47 use crate::ln::functional_test_utils::*;
48 use crate::ln::msgs::{ChannelMessageHandler, Init, OnionMessage, OnionMessageHandler};
49 use crate::offers::invoice::Bolt12Invoice;
50 use crate::offers::invoice_error::InvoiceError;
51 use crate::offers::invoice_request::InvoiceRequest;
52 use crate::offers::parse::Bolt12SemanticError;
53 use crate::onion_message::messenger::PeeledOnion;
54 use crate::onion_message::offers::OffersMessage;
55 use crate::onion_message::packet::ParsedOnionMessageContents;
57 use crate::prelude::*;
59 macro_rules! expect_recent_payment {
60 ($node: expr, $payment_state: path, $payment_id: expr) => {
61 match $node.node.list_recent_payments().first() {
62 Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
63 assert_eq!($payment_id, actual_payment_id);
65 Some(_) => panic!("Unexpected recent payment state"),
66 None => panic!("No recent payments"),
71 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
72 let node_id_a = node_a.node.get_our_node_id();
73 let node_id_b = node_b.node.get_our_node_id();
76 features: node_a.init_features(&node_id_b),
78 remote_network_address: None,
81 features: node_b.init_features(&node_id_a),
83 remote_network_address: None,
86 node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
87 node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
88 node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
89 node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
92 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
94 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
95 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
96 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
97 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
101 fn route_bolt12_payment<'a, 'b, 'c>(
102 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
104 // Monitor added when handling the invoice onion message.
105 check_added_monitors(node, 1);
107 let mut events = node.node.get_and_clear_pending_msg_events();
108 assert_eq!(events.len(), 1);
109 let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
111 // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
112 // invoice contains the payment_hash but it was encrypted inside an onion message.
113 let amount_msats = invoice.amount_msats();
114 let payment_hash = invoice.payment_hash();
116 node, path, amount_msats, payment_hash, None, ev, false, false, None, false
120 fn claim_bolt12_payment<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>]) {
121 let recipient = &path[path.len() - 1];
122 match get_event!(recipient, Event::PaymentClaimable) {
123 Event::PaymentClaimable {
124 purpose: PaymentPurpose::InvoicePayment {
125 payment_preimage: Some(payment_preimage), ..
127 } => claim_payment(node, path, payment_preimage),
132 fn extract_invoice_request<'a, 'b, 'c>(
133 node: &Node<'a, 'b, 'c>, message: &OnionMessage
134 ) -> (InvoiceRequest, Option<BlindedPath>) {
135 match node.onion_messenger.peel_onion_message(message) {
136 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
137 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
138 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
139 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
140 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
142 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
144 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
145 Err(e) => panic!("Failed to process onion message {:?}", e),
149 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
150 match node.onion_messenger.peel_onion_message(message) {
151 Ok(PeeledOnion::Receive(message, _, _)) => match message {
152 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
153 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
154 OffersMessage::Invoice(invoice) => invoice,
155 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
157 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
159 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
160 Err(e) => panic!("Failed to process onion message {:?}", e),
164 fn extract_invoice_error<'a, 'b, 'c>(
165 node: &Node<'a, 'b, 'c>, message: &OnionMessage
167 match node.onion_messenger.peel_onion_message(message) {
168 Ok(PeeledOnion::Receive(message, _, _)) => match message {
169 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
170 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
171 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
172 OffersMessage::InvoiceError(error) => error,
174 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
176 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
177 Err(e) => panic!("Failed to process onion message {:?}", e),
181 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
182 /// rather than exposing a node's pubkey.
184 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
185 let mut accept_forward_cfg = test_default_channel_config();
186 accept_forward_cfg.accept_forwards_to_priv_channels = true;
188 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
189 features.set_onion_messages_optional();
190 features.set_route_blinding_optional();
192 let chanmon_cfgs = create_chanmon_cfgs(6);
193 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
195 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
197 let node_chanmgrs = create_node_chanmgrs(
198 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
200 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
202 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
203 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
204 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
205 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
206 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
207 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
208 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
210 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
211 let alice_id = alice.node.get_our_node_id();
212 let bob_id = bob.node.get_our_node_id();
213 let charlie_id = charlie.node.get_our_node_id();
214 let david_id = david.node.get_our_node_id();
216 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
217 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
219 let offer = alice.node
220 .create_offer_builder("coffee".to_string()).unwrap()
221 .amount_msats(10_000_000)
223 assert_ne!(offer.signing_pubkey(), alice_id);
224 assert!(!offer.paths().is_empty());
225 for path in offer.paths() {
226 assert_eq!(path.introduction_node_id, bob_id);
229 let payment_id = PaymentId([1; 32]);
230 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
232 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
234 connect_peers(david, bob);
236 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
237 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
239 connect_peers(alice, charlie);
241 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
242 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
244 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
245 assert_eq!(invoice_request.amount_msats(), None);
246 assert_ne!(invoice_request.payer_id(), david_id);
247 assert_eq!(reply_path.unwrap().introduction_node_id, charlie_id);
249 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
250 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
252 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
253 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
255 let invoice = extract_invoice(david, &onion_message);
256 assert_eq!(invoice.amount_msats(), 10_000_000);
257 assert_ne!(invoice.signing_pubkey(), alice_id);
258 assert!(!invoice.payment_paths().is_empty());
259 for (_, path) in invoice.payment_paths() {
260 assert_eq!(path.introduction_node_id, bob_id);
263 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
264 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
266 claim_bolt12_payment(david, &[charlie, bob, alice]);
267 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
270 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
271 /// rather than exposing a node's pubkey.
273 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
274 let mut accept_forward_cfg = test_default_channel_config();
275 accept_forward_cfg.accept_forwards_to_priv_channels = true;
277 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
278 features.set_onion_messages_optional();
279 features.set_route_blinding_optional();
281 let chanmon_cfgs = create_chanmon_cfgs(6);
282 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
284 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
286 let node_chanmgrs = create_node_chanmgrs(
287 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
289 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
291 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
292 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
293 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
294 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
295 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
296 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
297 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
299 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
300 let alice_id = alice.node.get_our_node_id();
301 let bob_id = bob.node.get_our_node_id();
302 let charlie_id = charlie.node.get_our_node_id();
303 let david_id = david.node.get_our_node_id();
305 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
306 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
308 let absolute_expiry = Duration::from_secs(u64::MAX);
309 let payment_id = PaymentId([1; 32]);
310 let refund = david.node
311 .create_refund_builder(
312 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
316 assert_eq!(refund.amount_msats(), 10_000_000);
317 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
318 assert_ne!(refund.payer_id(), david_id);
319 assert!(!refund.paths().is_empty());
320 for path in refund.paths() {
321 assert_eq!(path.introduction_node_id, charlie_id);
323 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
325 alice.node.request_refund_payment(&refund).unwrap();
327 connect_peers(alice, charlie);
329 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
330 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
332 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
333 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
335 let invoice = extract_invoice(david, &onion_message);
336 assert_eq!(invoice.amount_msats(), 10_000_000);
337 assert_ne!(invoice.signing_pubkey(), alice_id);
338 assert!(!invoice.payment_paths().is_empty());
339 for (_, path) in invoice.payment_paths() {
340 assert_eq!(path.introduction_node_id, bob_id);
343 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
344 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
346 claim_bolt12_payment(david, &[charlie, bob, alice]);
347 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
350 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
351 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
352 /// introduction node of the blinded path.
354 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
355 let chanmon_cfgs = create_chanmon_cfgs(2);
356 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
357 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
358 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
360 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
362 let alice = &nodes[0];
363 let alice_id = alice.node.get_our_node_id();
365 let bob_id = bob.node.get_our_node_id();
367 let offer = alice.node
368 .create_offer_builder("coffee".to_string()).unwrap()
369 .amount_msats(10_000_000)
371 assert_ne!(offer.signing_pubkey(), alice_id);
372 assert!(!offer.paths().is_empty());
373 for path in offer.paths() {
374 assert_eq!(path.introduction_node_id, alice_id);
377 let payment_id = PaymentId([1; 32]);
378 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
379 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
381 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
382 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
384 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
385 assert_eq!(invoice_request.amount_msats(), None);
386 assert_ne!(invoice_request.payer_id(), bob_id);
387 assert_eq!(reply_path.unwrap().introduction_node_id, bob_id);
389 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
390 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
392 let invoice = extract_invoice(bob, &onion_message);
393 assert_eq!(invoice.amount_msats(), 10_000_000);
394 assert_ne!(invoice.signing_pubkey(), alice_id);
395 assert!(!invoice.payment_paths().is_empty());
396 for (_, path) in invoice.payment_paths() {
397 assert_eq!(path.introduction_node_id, alice_id);
400 route_bolt12_payment(bob, &[alice], &invoice);
401 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
403 claim_bolt12_payment(bob, &[alice]);
404 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
407 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
408 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
409 /// introduction node of the blinded path.
411 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
412 let chanmon_cfgs = create_chanmon_cfgs(2);
413 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
414 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
415 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
417 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
419 let alice = &nodes[0];
420 let alice_id = alice.node.get_our_node_id();
422 let bob_id = bob.node.get_our_node_id();
424 let absolute_expiry = Duration::from_secs(u64::MAX);
425 let payment_id = PaymentId([1; 32]);
426 let refund = bob.node
427 .create_refund_builder(
428 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
432 assert_eq!(refund.amount_msats(), 10_000_000);
433 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
434 assert_ne!(refund.payer_id(), bob_id);
435 assert!(!refund.paths().is_empty());
436 for path in refund.paths() {
437 assert_eq!(path.introduction_node_id, bob_id);
439 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
441 alice.node.request_refund_payment(&refund).unwrap();
443 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
444 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
446 let invoice = extract_invoice(bob, &onion_message);
447 assert_eq!(invoice.amount_msats(), 10_000_000);
448 assert_ne!(invoice.signing_pubkey(), alice_id);
449 assert!(!invoice.payment_paths().is_empty());
450 for (_, path) in invoice.payment_paths() {
451 assert_eq!(path.introduction_node_id, alice_id);
454 route_bolt12_payment(bob, &[alice], &invoice);
455 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
457 claim_bolt12_payment(bob, &[alice]);
458 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
461 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
462 /// the requested is sent directly using the node's pubkey, the response and the payment still use
463 /// blinded paths as required by the spec.
465 fn pays_for_offer_without_blinded_paths() {
466 let chanmon_cfgs = create_chanmon_cfgs(2);
467 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
468 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
469 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
471 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
473 let alice = &nodes[0];
474 let alice_id = alice.node.get_our_node_id();
476 let bob_id = bob.node.get_our_node_id();
478 let offer = alice.node
479 .create_offer_builder("coffee".to_string()).unwrap()
481 .amount_msats(10_000_000)
483 assert_eq!(offer.signing_pubkey(), alice_id);
484 assert!(offer.paths().is_empty());
486 let payment_id = PaymentId([1; 32]);
487 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
488 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
490 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
491 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
493 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
494 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
496 let invoice = extract_invoice(bob, &onion_message);
497 route_bolt12_payment(bob, &[alice], &invoice);
498 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
500 claim_bolt12_payment(bob, &[alice]);
501 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
504 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
505 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
507 fn pays_for_refund_without_blinded_paths() {
508 let chanmon_cfgs = create_chanmon_cfgs(2);
509 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
510 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
511 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
513 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
515 let alice = &nodes[0];
516 let alice_id = alice.node.get_our_node_id();
518 let bob_id = bob.node.get_our_node_id();
520 let absolute_expiry = Duration::from_secs(u64::MAX);
521 let payment_id = PaymentId([1; 32]);
522 let refund = bob.node
523 .create_refund_builder(
524 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
529 assert_eq!(refund.payer_id(), bob_id);
530 assert!(refund.paths().is_empty());
531 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
533 alice.node.request_refund_payment(&refund).unwrap();
535 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
536 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
538 let invoice = extract_invoice(bob, &onion_message);
539 route_bolt12_payment(bob, &[alice], &invoice);
540 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
542 claim_bolt12_payment(bob, &[alice]);
543 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
546 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
548 fn fails_creating_offer_without_blinded_paths() {
549 let chanmon_cfgs = create_chanmon_cfgs(2);
550 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
551 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
552 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
554 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
556 match nodes[0].node.create_offer_builder("coffee".to_string()) {
557 Ok(_) => panic!("Expected error"),
558 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
562 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
564 fn fails_creating_refund_without_blinded_paths() {
565 let chanmon_cfgs = create_chanmon_cfgs(2);
566 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
567 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
568 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
570 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
572 let absolute_expiry = Duration::from_secs(u64::MAX);
573 let payment_id = PaymentId([1; 32]);
575 match nodes[0].node.create_refund_builder(
576 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
578 Ok(_) => panic!("Expected error"),
579 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
582 assert!(nodes[0].node.list_recent_payments().is_empty());
585 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
588 fn fails_creating_invoice_request_without_blinded_reply_path() {
589 let chanmon_cfgs = create_chanmon_cfgs(6);
590 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
591 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
592 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
594 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
595 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
596 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
597 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
598 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
600 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
602 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
603 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
605 let offer = alice.node
606 .create_offer_builder("coffee".to_string()).unwrap()
607 .amount_msats(10_000_000)
610 let payment_id = PaymentId([1; 32]);
612 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
613 Ok(_) => panic!("Expected error"),
614 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
617 assert!(nodes[0].node.list_recent_payments().is_empty());
621 fn fails_creating_invoice_request_with_duplicate_payment_id() {
622 let chanmon_cfgs = create_chanmon_cfgs(6);
623 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
624 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
625 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
627 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
628 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
629 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
630 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
631 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
632 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
633 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
635 let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
637 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
639 let offer = alice.node
640 .create_offer_builder("coffee".to_string()).unwrap()
641 .amount_msats(10_000_000)
644 let payment_id = PaymentId([1; 32]);
646 david.node.pay_for_offer(
647 &offer, None, None, None, payment_id, Retry::Attempts(0), None
650 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
652 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
653 Ok(_) => panic!("Expected error"),
654 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
657 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
661 fn fails_creating_refund_with_duplicate_payment_id() {
662 let chanmon_cfgs = create_chanmon_cfgs(2);
663 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
664 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
665 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
667 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
669 let absolute_expiry = Duration::from_secs(u64::MAX);
670 let payment_id = PaymentId([1; 32]);
672 nodes[0].node.create_refund_builder(
673 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
676 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
678 match nodes[0].node.create_refund_builder(
679 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
681 Ok(_) => panic!("Expected error"),
682 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
685 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
689 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
690 let mut accept_forward_cfg = test_default_channel_config();
691 accept_forward_cfg.accept_forwards_to_priv_channels = true;
693 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
694 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
695 features.set_onion_messages_optional();
696 features.clear_route_blinding();
698 let chanmon_cfgs = create_chanmon_cfgs(6);
699 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
701 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
703 let node_chanmgrs = create_node_chanmgrs(
704 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
706 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
708 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
709 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
710 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
711 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
712 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
713 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
714 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
716 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
717 let alice_id = alice.node.get_our_node_id();
718 let bob_id = bob.node.get_our_node_id();
719 let charlie_id = charlie.node.get_our_node_id();
720 let david_id = david.node.get_our_node_id();
722 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
723 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
725 let offer = alice.node
726 .create_offer_builder("coffee".to_string()).unwrap()
727 .amount_msats(10_000_000)
730 let payment_id = PaymentId([1; 32]);
731 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
734 connect_peers(david, bob);
736 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
737 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
739 connect_peers(alice, charlie);
741 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
742 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
744 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
745 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
747 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
748 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
750 let invoice_error = extract_invoice_error(david, &onion_message);
751 assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
755 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
756 let mut accept_forward_cfg = test_default_channel_config();
757 accept_forward_cfg.accept_forwards_to_priv_channels = true;
759 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
760 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
761 features.set_onion_messages_optional();
762 features.clear_route_blinding();
764 let chanmon_cfgs = create_chanmon_cfgs(6);
765 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
767 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
769 let node_chanmgrs = create_node_chanmgrs(
770 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
772 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
774 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
775 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
776 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
777 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
778 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
779 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
780 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
782 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
784 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
785 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
787 let absolute_expiry = Duration::from_secs(u64::MAX);
788 let payment_id = PaymentId([1; 32]);
789 let refund = david.node
790 .create_refund_builder(
791 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
796 match alice.node.request_refund_payment(&refund) {
797 Ok(_) => panic!("Expected error"),
798 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
803 fn fails_paying_invoice_more_than_once() {
804 let mut accept_forward_cfg = test_default_channel_config();
805 accept_forward_cfg.accept_forwards_to_priv_channels = true;
807 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
808 features.set_onion_messages_optional();
809 features.set_route_blinding_optional();
811 let chanmon_cfgs = create_chanmon_cfgs(6);
812 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
814 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
816 let node_chanmgrs = create_node_chanmgrs(
817 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
819 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
821 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
822 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
823 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
824 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
825 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
826 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
827 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
829 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
830 let alice_id = alice.node.get_our_node_id();
831 let bob_id = bob.node.get_our_node_id();
832 let charlie_id = charlie.node.get_our_node_id();
833 let david_id = david.node.get_our_node_id();
835 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
836 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
838 let absolute_expiry = Duration::from_secs(u64::MAX);
839 let payment_id = PaymentId([1; 32]);
840 let refund = david.node
841 .create_refund_builder(
842 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
846 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
848 // Alice sends the first invoice
849 alice.node.request_refund_payment(&refund).unwrap();
851 connect_peers(alice, charlie);
853 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
854 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
856 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
857 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
859 // David pays the first invoice
860 let invoice1 = extract_invoice(david, &onion_message);
862 route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
863 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
865 claim_bolt12_payment(david, &[charlie, bob, alice]);
866 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
868 disconnect_peers(alice, &[charlie]);
870 // Alice sends the second invoice
871 alice.node.request_refund_payment(&refund).unwrap();
873 connect_peers(alice, charlie);
874 connect_peers(david, bob);
876 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
877 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
879 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
880 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
882 let invoice2 = extract_invoice(david, &onion_message);
883 assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
885 // David sends an error instead of paying the second invoice
886 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
887 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
889 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
890 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
892 let invoice_error = extract_invoice_error(alice, &onion_message);
893 assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));