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, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
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;
56 use crate::routing::gossip::{NodeAlias, NodeId};
57 use crate::sign::{NodeSigner, Recipient};
59 use crate::prelude::*;
61 macro_rules! expect_recent_payment {
62 ($node: expr, $payment_state: path, $payment_id: expr) => {
63 match $node.node.list_recent_payments().first() {
64 Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
65 assert_eq!($payment_id, actual_payment_id);
67 Some(_) => panic!("Unexpected recent payment state"),
68 None => panic!("No recent payments"),
73 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
74 let node_id_a = node_a.node.get_our_node_id();
75 let node_id_b = node_b.node.get_our_node_id();
78 features: node_a.init_features(&node_id_b),
80 remote_network_address: None,
83 features: node_b.init_features(&node_id_a),
85 remote_network_address: None,
88 node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
89 node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
90 node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
91 node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
94 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
96 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
97 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
98 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
99 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
103 fn announce_node_address<'a, 'b, 'c>(
104 node: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>], address: SocketAddress,
106 let features = node.onion_messenger.provided_node_features()
107 | node.gossip_sync.provided_node_features();
109 let announcement = UnsignedNodeAnnouncement {
112 node_id: NodeId::from_pubkey(&node.keys_manager.get_node_id(Recipient::Node).unwrap()),
114 alias: NodeAlias([0u8; 32]),
115 addresses: vec![address],
116 excess_address_data: Vec::new(),
117 excess_data: Vec::new(),
119 let signature = node.keys_manager.sign_gossip_message(
120 UnsignedGossipMessage::NodeAnnouncement(&announcement)
123 let msg = NodeAnnouncement {
125 contents: announcement
128 node.gossip_sync.handle_node_announcement(&msg).unwrap();
130 peer.gossip_sync.handle_node_announcement(&msg).unwrap();
134 fn route_bolt12_payment<'a, 'b, 'c>(
135 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
137 // Monitor added when handling the invoice onion message.
138 check_added_monitors(node, 1);
140 let mut events = node.node.get_and_clear_pending_msg_events();
141 assert_eq!(events.len(), 1);
142 let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
144 // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
145 // invoice contains the payment_hash but it was encrypted inside an onion message.
146 let amount_msats = invoice.amount_msats();
147 let payment_hash = invoice.payment_hash();
149 node, path, amount_msats, payment_hash, None, ev, false, false, None, false
153 fn claim_bolt12_payment<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>]) {
154 let recipient = &path[path.len() - 1];
155 match get_event!(recipient, Event::PaymentClaimable) {
156 Event::PaymentClaimable {
157 purpose: PaymentPurpose::InvoicePayment {
158 payment_preimage: Some(payment_preimage), ..
160 } => claim_payment(node, path, payment_preimage),
165 fn extract_invoice_request<'a, 'b, 'c>(
166 node: &Node<'a, 'b, 'c>, message: &OnionMessage
167 ) -> (InvoiceRequest, Option<BlindedPath>) {
168 match node.onion_messenger.peel_onion_message(message) {
169 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
170 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
171 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
172 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
173 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
175 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
177 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
178 Err(e) => panic!("Failed to process onion message {:?}", e),
182 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
183 match node.onion_messenger.peel_onion_message(message) {
184 Ok(PeeledOnion::Receive(message, _, _)) => match message {
185 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
186 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
187 OffersMessage::Invoice(invoice) => invoice,
188 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
190 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
192 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
193 Err(e) => panic!("Failed to process onion message {:?}", e),
197 fn extract_invoice_error<'a, 'b, 'c>(
198 node: &Node<'a, 'b, 'c>, message: &OnionMessage
200 match node.onion_messenger.peel_onion_message(message) {
201 Ok(PeeledOnion::Receive(message, _, _)) => match message {
202 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
203 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
204 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
205 OffersMessage::InvoiceError(error) => error,
207 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
209 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
210 Err(e) => panic!("Failed to process onion message {:?}", e),
214 /// Checks that blinded paths without Tor-only nodes are preferred when constructing an offer.
216 fn prefers_non_tor_nodes_in_blinded_paths() {
217 let mut accept_forward_cfg = test_default_channel_config();
218 accept_forward_cfg.accept_forwards_to_priv_channels = true;
220 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
221 features.set_onion_messages_optional();
222 features.set_route_blinding_optional();
224 let chanmon_cfgs = create_chanmon_cfgs(6);
225 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
227 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
229 let node_chanmgrs = create_node_chanmgrs(
230 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
232 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
234 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
235 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
236 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
237 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
238 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
239 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
240 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
242 // Add an extra channel so that more than one of Bob's peers have MIN_PEER_CHANNELS.
243 create_announced_chan_between_nodes_with_value(&nodes, 4, 5, 10_000_000, 1_000_000_000);
245 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
246 let bob_id = bob.node.get_our_node_id();
247 let charlie_id = charlie.node.get_our_node_id();
249 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
250 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
252 let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
253 announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor);
256 .create_offer_builder("coffee".to_string()).unwrap()
257 .amount_msats(10_000_000)
259 assert_ne!(offer.signing_pubkey(), bob_id);
260 assert!(!offer.paths().is_empty());
261 for path in offer.paths() {
262 assert_ne!(path.introduction_node_id, charlie_id);
266 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
267 /// rather than exposing a node's pubkey.
269 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
270 let mut accept_forward_cfg = test_default_channel_config();
271 accept_forward_cfg.accept_forwards_to_priv_channels = true;
273 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
274 features.set_onion_messages_optional();
275 features.set_route_blinding_optional();
277 let chanmon_cfgs = create_chanmon_cfgs(6);
278 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
280 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
282 let node_chanmgrs = create_node_chanmgrs(
283 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
285 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
287 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
288 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
289 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
290 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
291 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
292 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
293 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
295 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
296 let alice_id = alice.node.get_our_node_id();
297 let bob_id = bob.node.get_our_node_id();
298 let charlie_id = charlie.node.get_our_node_id();
299 let david_id = david.node.get_our_node_id();
301 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
302 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
304 let offer = alice.node
305 .create_offer_builder("coffee".to_string()).unwrap()
306 .amount_msats(10_000_000)
308 assert_ne!(offer.signing_pubkey(), alice_id);
309 assert!(!offer.paths().is_empty());
310 for path in offer.paths() {
311 assert_eq!(path.introduction_node_id, bob_id);
314 let payment_id = PaymentId([1; 32]);
315 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
317 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
319 connect_peers(david, bob);
321 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
322 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
324 connect_peers(alice, charlie);
326 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
327 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
329 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
330 assert_eq!(invoice_request.amount_msats(), None);
331 assert_ne!(invoice_request.payer_id(), david_id);
332 assert_eq!(reply_path.unwrap().introduction_node_id, charlie_id);
334 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
335 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
337 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
338 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
340 let invoice = extract_invoice(david, &onion_message);
341 assert_eq!(invoice.amount_msats(), 10_000_000);
342 assert_ne!(invoice.signing_pubkey(), alice_id);
343 assert!(!invoice.payment_paths().is_empty());
344 for (_, path) in invoice.payment_paths() {
345 assert_eq!(path.introduction_node_id, bob_id);
348 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
349 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
351 claim_bolt12_payment(david, &[charlie, bob, alice]);
352 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
355 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
356 /// rather than exposing a node's pubkey.
358 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
359 let mut accept_forward_cfg = test_default_channel_config();
360 accept_forward_cfg.accept_forwards_to_priv_channels = true;
362 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
363 features.set_onion_messages_optional();
364 features.set_route_blinding_optional();
366 let chanmon_cfgs = create_chanmon_cfgs(6);
367 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
369 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
371 let node_chanmgrs = create_node_chanmgrs(
372 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
374 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
376 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
377 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
378 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
379 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
380 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
381 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
382 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
384 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
385 let alice_id = alice.node.get_our_node_id();
386 let bob_id = bob.node.get_our_node_id();
387 let charlie_id = charlie.node.get_our_node_id();
388 let david_id = david.node.get_our_node_id();
390 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
391 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
393 let absolute_expiry = Duration::from_secs(u64::MAX);
394 let payment_id = PaymentId([1; 32]);
395 let refund = david.node
396 .create_refund_builder(
397 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
401 assert_eq!(refund.amount_msats(), 10_000_000);
402 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
403 assert_ne!(refund.payer_id(), david_id);
404 assert!(!refund.paths().is_empty());
405 for path in refund.paths() {
406 assert_eq!(path.introduction_node_id, charlie_id);
408 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
410 alice.node.request_refund_payment(&refund).unwrap();
412 connect_peers(alice, charlie);
414 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
415 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
417 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
418 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
420 let invoice = extract_invoice(david, &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, bob_id);
428 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
429 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
431 claim_bolt12_payment(david, &[charlie, bob, alice]);
432 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
435 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
436 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
437 /// introduction node of the blinded path.
439 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
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()
454 .amount_msats(10_000_000)
456 assert_ne!(offer.signing_pubkey(), alice_id);
457 assert!(!offer.paths().is_empty());
458 for path in offer.paths() {
459 assert_eq!(path.introduction_node_id, alice_id);
462 let payment_id = PaymentId([1; 32]);
463 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
464 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
466 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
467 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
469 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
470 assert_eq!(invoice_request.amount_msats(), None);
471 assert_ne!(invoice_request.payer_id(), bob_id);
472 assert_eq!(reply_path.unwrap().introduction_node_id, bob_id);
474 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
475 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
477 let invoice = extract_invoice(bob, &onion_message);
478 assert_eq!(invoice.amount_msats(), 10_000_000);
479 assert_ne!(invoice.signing_pubkey(), alice_id);
480 assert!(!invoice.payment_paths().is_empty());
481 for (_, path) in invoice.payment_paths() {
482 assert_eq!(path.introduction_node_id, alice_id);
485 route_bolt12_payment(bob, &[alice], &invoice);
486 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
488 claim_bolt12_payment(bob, &[alice]);
489 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
492 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
493 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
494 /// introduction node of the blinded path.
496 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
497 let chanmon_cfgs = create_chanmon_cfgs(2);
498 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
499 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
500 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
502 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
504 let alice = &nodes[0];
505 let alice_id = alice.node.get_our_node_id();
507 let bob_id = bob.node.get_our_node_id();
509 let absolute_expiry = Duration::from_secs(u64::MAX);
510 let payment_id = PaymentId([1; 32]);
511 let refund = bob.node
512 .create_refund_builder(
513 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
517 assert_eq!(refund.amount_msats(), 10_000_000);
518 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
519 assert_ne!(refund.payer_id(), bob_id);
520 assert!(!refund.paths().is_empty());
521 for path in refund.paths() {
522 assert_eq!(path.introduction_node_id, bob_id);
524 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
526 alice.node.request_refund_payment(&refund).unwrap();
528 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
529 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
531 let invoice = extract_invoice(bob, &onion_message);
532 assert_eq!(invoice.amount_msats(), 10_000_000);
533 assert_ne!(invoice.signing_pubkey(), alice_id);
534 assert!(!invoice.payment_paths().is_empty());
535 for (_, path) in invoice.payment_paths() {
536 assert_eq!(path.introduction_node_id, alice_id);
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 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
547 /// the requested is sent directly using the node's pubkey, the response and the payment still use
548 /// blinded paths as required by the spec.
550 fn pays_for_offer_without_blinded_paths() {
551 let chanmon_cfgs = create_chanmon_cfgs(2);
552 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
553 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
554 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
556 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
558 let alice = &nodes[0];
559 let alice_id = alice.node.get_our_node_id();
561 let bob_id = bob.node.get_our_node_id();
563 let offer = alice.node
564 .create_offer_builder("coffee".to_string()).unwrap()
566 .amount_msats(10_000_000)
568 assert_eq!(offer.signing_pubkey(), alice_id);
569 assert!(offer.paths().is_empty());
571 let payment_id = PaymentId([1; 32]);
572 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
573 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
575 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
576 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
578 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
579 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
581 let invoice = extract_invoice(bob, &onion_message);
582 route_bolt12_payment(bob, &[alice], &invoice);
583 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
585 claim_bolt12_payment(bob, &[alice]);
586 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
589 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
590 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
592 fn pays_for_refund_without_blinded_paths() {
593 let chanmon_cfgs = create_chanmon_cfgs(2);
594 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
595 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
596 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
598 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
600 let alice = &nodes[0];
601 let alice_id = alice.node.get_our_node_id();
603 let bob_id = bob.node.get_our_node_id();
605 let absolute_expiry = Duration::from_secs(u64::MAX);
606 let payment_id = PaymentId([1; 32]);
607 let refund = bob.node
608 .create_refund_builder(
609 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
614 assert_eq!(refund.payer_id(), bob_id);
615 assert!(refund.paths().is_empty());
616 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
618 alice.node.request_refund_payment(&refund).unwrap();
620 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
621 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
623 let invoice = extract_invoice(bob, &onion_message);
624 route_bolt12_payment(bob, &[alice], &invoice);
625 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
627 claim_bolt12_payment(bob, &[alice]);
628 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
631 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
633 fn fails_creating_offer_without_blinded_paths() {
634 let chanmon_cfgs = create_chanmon_cfgs(2);
635 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
636 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
637 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
639 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
641 match nodes[0].node.create_offer_builder("coffee".to_string()) {
642 Ok(_) => panic!("Expected error"),
643 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
647 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
649 fn fails_creating_refund_without_blinded_paths() {
650 let chanmon_cfgs = create_chanmon_cfgs(2);
651 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
652 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
653 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
655 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
657 let absolute_expiry = Duration::from_secs(u64::MAX);
658 let payment_id = PaymentId([1; 32]);
660 match nodes[0].node.create_refund_builder(
661 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
663 Ok(_) => panic!("Expected error"),
664 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
667 assert!(nodes[0].node.list_recent_payments().is_empty());
670 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
673 fn fails_creating_invoice_request_without_blinded_reply_path() {
674 let chanmon_cfgs = create_chanmon_cfgs(6);
675 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
676 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
677 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
679 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
680 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
681 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
682 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
683 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
685 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
687 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
688 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
690 let offer = alice.node
691 .create_offer_builder("coffee".to_string()).unwrap()
692 .amount_msats(10_000_000)
695 let payment_id = PaymentId([1; 32]);
697 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
698 Ok(_) => panic!("Expected error"),
699 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
702 assert!(nodes[0].node.list_recent_payments().is_empty());
706 fn fails_creating_invoice_request_with_duplicate_payment_id() {
707 let chanmon_cfgs = create_chanmon_cfgs(6);
708 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
709 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
710 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
712 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
713 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
714 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
715 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
716 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
717 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
718 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
720 let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
722 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
724 let offer = alice.node
725 .create_offer_builder("coffee".to_string()).unwrap()
726 .amount_msats(10_000_000)
729 let payment_id = PaymentId([1; 32]);
731 david.node.pay_for_offer(
732 &offer, None, None, None, payment_id, Retry::Attempts(0), None
735 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
737 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
738 Ok(_) => panic!("Expected error"),
739 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
742 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
746 fn fails_creating_refund_with_duplicate_payment_id() {
747 let chanmon_cfgs = create_chanmon_cfgs(2);
748 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
749 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
750 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
752 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
754 let absolute_expiry = Duration::from_secs(u64::MAX);
755 let payment_id = PaymentId([1; 32]);
757 nodes[0].node.create_refund_builder(
758 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
761 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
763 match nodes[0].node.create_refund_builder(
764 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
766 Ok(_) => panic!("Expected error"),
767 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
770 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
774 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
775 let mut accept_forward_cfg = test_default_channel_config();
776 accept_forward_cfg.accept_forwards_to_priv_channels = true;
778 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
779 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
780 features.set_onion_messages_optional();
781 features.clear_route_blinding();
783 let chanmon_cfgs = create_chanmon_cfgs(6);
784 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
786 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
788 let node_chanmgrs = create_node_chanmgrs(
789 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
791 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
793 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
794 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
795 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
796 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
797 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
798 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
799 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
801 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
802 let alice_id = alice.node.get_our_node_id();
803 let bob_id = bob.node.get_our_node_id();
804 let charlie_id = charlie.node.get_our_node_id();
805 let david_id = david.node.get_our_node_id();
807 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
808 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
810 let offer = alice.node
811 .create_offer_builder("coffee".to_string()).unwrap()
812 .amount_msats(10_000_000)
815 let payment_id = PaymentId([1; 32]);
816 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
819 connect_peers(david, bob);
821 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
822 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
824 connect_peers(alice, charlie);
826 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
827 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
829 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
830 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
832 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
833 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
835 let invoice_error = extract_invoice_error(david, &onion_message);
836 assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
840 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
841 let mut accept_forward_cfg = test_default_channel_config();
842 accept_forward_cfg.accept_forwards_to_priv_channels = true;
844 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
845 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
846 features.set_onion_messages_optional();
847 features.clear_route_blinding();
849 let chanmon_cfgs = create_chanmon_cfgs(6);
850 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
852 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
854 let node_chanmgrs = create_node_chanmgrs(
855 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
857 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
859 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
860 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
861 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
862 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
863 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
864 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
865 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
867 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
869 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
870 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
872 let absolute_expiry = Duration::from_secs(u64::MAX);
873 let payment_id = PaymentId([1; 32]);
874 let refund = david.node
875 .create_refund_builder(
876 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
881 match alice.node.request_refund_payment(&refund) {
882 Ok(_) => panic!("Expected error"),
883 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
888 fn fails_paying_invoice_more_than_once() {
889 let mut accept_forward_cfg = test_default_channel_config();
890 accept_forward_cfg.accept_forwards_to_priv_channels = true;
892 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
893 features.set_onion_messages_optional();
894 features.set_route_blinding_optional();
896 let chanmon_cfgs = create_chanmon_cfgs(6);
897 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
899 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
901 let node_chanmgrs = create_node_chanmgrs(
902 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
904 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
906 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
907 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
908 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
909 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
910 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
911 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
912 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
914 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
915 let alice_id = alice.node.get_our_node_id();
916 let bob_id = bob.node.get_our_node_id();
917 let charlie_id = charlie.node.get_our_node_id();
918 let david_id = david.node.get_our_node_id();
920 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
921 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
923 let absolute_expiry = Duration::from_secs(u64::MAX);
924 let payment_id = PaymentId([1; 32]);
925 let refund = david.node
926 .create_refund_builder(
927 "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
931 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
933 // Alice sends the first invoice
934 alice.node.request_refund_payment(&refund).unwrap();
936 connect_peers(alice, charlie);
938 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
939 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
941 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
942 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
944 // David pays the first invoice
945 let invoice1 = extract_invoice(david, &onion_message);
947 route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
948 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
950 claim_bolt12_payment(david, &[charlie, bob, alice]);
951 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
953 disconnect_peers(alice, &[charlie]);
955 // Alice sends the second invoice
956 alice.node.request_refund_payment(&refund).unwrap();
958 connect_peers(alice, charlie);
959 connect_peers(david, bob);
961 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
962 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
964 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
965 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
967 let invoice2 = extract_invoice(david, &onion_message);
968 assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
970 // David sends an error instead of paying the second invoice
971 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
972 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
974 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
975 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
977 let invoice_error = extract_invoice_error(alice, &onion_message);
978 assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));